forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranking_test.go
229 lines (203 loc) · 8.05 KB
/
ranking_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
package main
import (
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
)
func TestGetNonexistentRanking(t *testing.T) {
clearDb()
defer clearDb()
db, err := OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
ranking, err := db.GetRankingForTeam(1114)
assert.Nil(t, err)
assert.Nil(t, ranking)
}
func TestRankingCrud(t *testing.T) {
clearDb()
defer clearDb()
db, err := OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
ranking := Ranking{254, 1, 20, 625, 90, 554, 10, 0.254, 3, 2, 1, 0, 10}
db.CreateRanking(&ranking)
ranking2, err := db.GetRankingForTeam(254)
assert.Nil(t, err)
assert.Equal(t, ranking, *ranking2)
ranking.Random = 0.1114
db.SaveRanking(&ranking)
ranking2, err = db.GetRankingForTeam(254)
assert.Nil(t, err)
assert.Equal(t, ranking.Random, ranking2.Random)
db.DeleteRanking(&ranking)
ranking2, err = db.GetRankingForTeam(254)
assert.Nil(t, err)
assert.Nil(t, ranking2)
}
func TestTruncateRankings(t *testing.T) {
clearDb()
defer clearDb()
db, err := OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
ranking := Ranking{254, 1, 20, 625, 90, 554, 10, 0.254, 3, 2, 1, 0, 10}
db.CreateRanking(&ranking)
db.TruncateRankings()
ranking2, err := db.GetRankingForTeam(254)
assert.Nil(t, err)
assert.Nil(t, ranking2)
}
func TestGetAllRankings(t *testing.T) {
clearDb()
defer clearDb()
db, err := OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
rankings, err := db.GetAllRankings()
assert.Nil(t, err)
assert.Empty(t, rankings)
numRankings := 20
for i := 1; i <= numRankings; i++ {
db.CreateRanking(&Ranking{TeamId: i})
}
rankings, err = db.GetAllRankings()
assert.Nil(t, err)
assert.Equal(t, numRankings, len(rankings))
for i := 0; i < numRankings; i++ {
assert.Equal(t, i+1, rankings[i].TeamId)
}
}
func TestCalculateRankings(t *testing.T) {
clearDb()
defer clearDb()
db, err := OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
rand.Seed(0)
setupMatchResultsForRankings(db)
err = db.CalculateRankings()
assert.Nil(t, err)
rankings, err := db.GetAllRankings()
assert.Nil(t, err)
if assert.Equal(t, 6, len(rankings)) {
assert.Equal(t, Ranking{1, 1, 6, 110, 20, 172, 120, 0.2730468047134829, 1, 0, 2, 0, 3}, rankings[0])
assert.Equal(t, Ranking{3, 2, 4, 55, 10, 86, 60, 0.9026048462705047, 1, 0, 1, 0, 2}, rankings[1])
assert.Equal(t, Ranking{4, 3, 2, 77, 45, 122, 85, 0.897169713149801, 0, 1, 1, 0, 2}, rankings[2])
assert.Equal(t, Ranking{5, 4, 3, 77, 45, 122, 85, 0.2885856518054551, 0, 1, 2, 0, 3}, rankings[3])
assert.Equal(t, Ranking{2, 5, 3, 55, 10, 86, 60, 0.8497802817628735, 0, 0, 2, 1, 3}, rankings[4])
assert.Equal(t, Ranking{6, 6, 1, 22, 35, 36, 25, 0.16735444255905835, 0, 1, 1, 0, 2}, rankings[5])
}
// Test after changing a match result.
matchResult3 := buildTestMatchResult(3, 3)
matchResult3.RedScore, matchResult3.BlueScore = matchResult3.BlueScore, matchResult3.RedScore
err = db.CreateMatchResult(&matchResult3)
assert.Nil(t, err)
err = db.CalculateRankings()
assert.Nil(t, err)
rankings, err = db.GetAllRankings()
assert.Nil(t, err)
if assert.Equal(t, 6, len(rankings)) {
assert.Equal(t, Ranking{3, 1, 6, 110, 20, 172, 120, 0.6930700440076261, 2, 0, 0, 0, 2}, rankings[0])
assert.Equal(t, Ranking{1, 2, 8, 165, 30, 258, 180, 0.284824110942037, 2, 0, 1, 0, 3}, rankings[1])
assert.Equal(t, Ranking{2, 3, 5, 110, 20, 172, 120, 0.4018978925803393, 1, 0, 1, 1, 3}, rankings[2])
assert.Equal(t, Ranking{4, 4, 2, 77, 45, 122, 85, 0.5102423328818813, 0, 1, 1, 0, 2}, rankings[3])
assert.Equal(t, Ranking{5, 5, 2, 99, 80, 158, 110, 0.2092018731282357, 0, 2, 1, 0, 3}, rankings[4])
assert.Equal(t, Ranking{6, 6, 0, 44, 70, 72, 50, 0.24043190328608438, 0, 2, 0, 0, 2}, rankings[5])
}
}
func TestSortRankings(t *testing.T) {
clearDb()
defer clearDb()
db, err := OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
// Check tiebreakers.
rankings := make(map[int]*Ranking)
rankings[1] = &Ranking{1, 0, 50, 50, 50, 50, 50, 0.49, 3, 2, 1, 0, 10}
rankings[2] = &Ranking{2, 0, 50, 50, 50, 50, 50, 0.51, 3, 2, 1, 0, 10}
rankings[3] = &Ranking{3, 0, 50, 50, 50, 50, 49, 0.50, 3, 2, 1, 0, 10}
rankings[4] = &Ranking{4, 0, 50, 50, 50, 50, 51, 0.50, 3, 2, 1, 0, 10}
rankings[5] = &Ranking{5, 0, 50, 50, 50, 49, 50, 0.50, 3, 2, 1, 0, 10}
rankings[6] = &Ranking{6, 0, 50, 50, 50, 51, 50, 0.50, 3, 2, 1, 0, 10}
rankings[7] = &Ranking{7, 0, 50, 50, 49, 50, 50, 0.50, 3, 2, 1, 0, 10}
rankings[8] = &Ranking{8, 0, 50, 50, 51, 50, 50, 0.50, 3, 2, 1, 0, 10}
rankings[9] = &Ranking{9, 0, 50, 49, 50, 50, 50, 0.50, 3, 2, 1, 0, 10}
rankings[10] = &Ranking{10, 0, 50, 51, 50, 50, 50, 0.50, 3, 2, 1, 0, 10}
rankings[11] = &Ranking{11, 0, 49, 50, 50, 50, 50, 0.50, 3, 2, 1, 0, 10}
rankings[12] = &Ranking{12, 0, 51, 50, 50, 50, 50, 0.50, 3, 2, 1, 0, 10}
sortedRankings := sortRankings(rankings)
assert.Equal(t, 12, sortedRankings[0].TeamId)
assert.Equal(t, 10, sortedRankings[1].TeamId)
assert.Equal(t, 8, sortedRankings[2].TeamId)
assert.Equal(t, 6, sortedRankings[3].TeamId)
assert.Equal(t, 4, sortedRankings[4].TeamId)
assert.Equal(t, 2, sortedRankings[5].TeamId)
assert.Equal(t, 1, sortedRankings[6].TeamId)
assert.Equal(t, 3, sortedRankings[7].TeamId)
assert.Equal(t, 5, sortedRankings[8].TeamId)
assert.Equal(t, 7, sortedRankings[9].TeamId)
assert.Equal(t, 9, sortedRankings[10].TeamId)
assert.Equal(t, 11, sortedRankings[11].TeamId)
// Check with unequal number of matches played.
rankings = make(map[int]*Ranking)
rankings[1] = &Ranking{1, 0, 10, 25, 25, 25, 25, 0.49, 3, 2, 1, 0, 5}
rankings[2] = &Ranking{2, 0, 19, 50, 50, 50, 50, 0.51, 3, 2, 1, 0, 9}
rankings[3] = &Ranking{3, 0, 20, 50, 50, 50, 50, 0.51, 3, 2, 1, 0, 10}
sortedRankings = sortRankings(rankings)
assert.Equal(t, 2, sortedRankings[0].TeamId)
assert.Equal(t, 3, sortedRankings[1].TeamId)
assert.Equal(t, 1, sortedRankings[2].TeamId)
}
func BenchmarkCalculateRankings(b *testing.B) {
clearDb()
defer clearDb()
db, err := OpenDatabase(testDbPath)
assert.Nil(b, err)
defer db.Close()
setupMatchResultsForRankings(db)
b.ResetTimer()
for i := 0; i < b.N; i++ {
db.CalculateRankings()
}
}
// Sets up a schedule and results that touches on all possible variables.
func setupMatchResultsForRankings(db *Database) {
match1 := Match{Type: "qualification", DisplayName: "1", Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5,
Blue3: 6, Status: "complete"}
db.CreateMatch(&match1)
matchResult1 := buildTestMatchResult(match1.Id, 1)
matchResult1.RedCards = map[string]string{"2": "red"}
db.CreateMatchResult(&matchResult1)
match2 := Match{Type: "qualification", DisplayName: "2", Red1: 1, Red2: 3, Red3: 5, Blue1: 2, Blue2: 4,
Blue3: 6, Status: "complete", Red2IsSurrogate: true, Blue3IsSurrogate: true}
db.CreateMatch(&match2)
matchResult2 := buildTestMatchResult(match2.Id, 1)
matchResult2.BlueScore = matchResult2.RedScore
db.CreateMatchResult(&matchResult2)
match3 := Match{Type: "qualification", DisplayName: "3", Red1: 6, Red2: 5, Red3: 4, Blue1: 3, Blue2: 2,
Blue3: 1, Status: "complete", Red3IsSurrogate: true}
db.CreateMatch(&match3)
matchResult3 := buildTestMatchResult(match3.Id, 1)
db.CreateMatchResult(&matchResult3)
matchResult3 = MatchResult{MatchId: match3.Id, PlayNumber: 2}
db.CreateMatchResult(&matchResult3)
match4 := Match{Type: "practice", DisplayName: "1", Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5,
Blue3: 6, Status: "complete"}
db.CreateMatch(&match4)
matchResult4 := buildTestMatchResult(match4.Id, 1)
db.CreateMatchResult(&matchResult4)
match5 := Match{Type: "elimination", DisplayName: "F-1", Red1: 1, Red2: 2, Red3: 3, Blue1: 4, Blue2: 5,
Blue3: 6, Status: "complete"}
db.CreateMatch(&match5)
matchResult5 := buildTestMatchResult(match5.Id, 1)
db.CreateMatchResult(&matchResult5)
match6 := Match{Type: "qualification", DisplayName: "4", Red1: 7, Red2: 8, Red3: 9, Blue1: 10, Blue2: 11,
Blue3: 12, Status: ""}
db.CreateMatch(&match6)
matchResult6 := buildTestMatchResult(match6.Id, 1)
db.CreateMatchResult(&matchResult6)
}