-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvotes_test.go
223 lines (193 loc) · 5.15 KB
/
votes_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
package bitknn_test
import (
"math"
"testing"
"github.com/keilerkonzept/bitknn"
"pgregory.net/rapid"
)
const eps = 1e-9
func TestVoteDiscard(t *testing.T) {
discard := bitknn.DiscardVotes
{
pre := discard
discard.Add(0, 1)
post := discard
if pre != post {
t.Fail()
}
}
if discard.Get(0) != 0 {
t.Fatal()
}
if discard.ArgMax() != 0 {
t.Fatal()
}
if discard.Max() != 0 {
t.Fatal()
}
{
pre := discard
discard.Clear()
post := discard
if pre != post {
t.Fail()
}
}
}
func TestVoteSlice_Clear(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
length := rapid.IntRange(0, 100).Draw(t, "length")
voteSlice := make(bitknn.VoteSlice, length)
for i := 0; i < length; i++ {
voteSlice[i] = rapid.Float64().Draw(t, "vote")
}
voteSlice.Clear()
for i := 0; i < length; i++ {
if voteSlice[i] != 0 {
t.Fatalf("expected voteSlice[%d] to be 0, got %f", i, voteSlice[i])
}
}
})
}
func TestVoteSlice_ArgMax(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
length := rapid.IntRange(0, 100).Draw(t, "length")
voteSlice := make(bitknn.VoteSlice, length)
for i := 0; i < length; i++ {
voteSlice[i] = rapid.Float64Range(-1000, 1000).Draw(t, "vote")
}
maxIdx := voteSlice.ArgMax()
if length == 0 && maxIdx != 0 {
t.Fatal()
}
for i := range voteSlice {
if voteSlice[i] > voteSlice[maxIdx] {
t.Fatalf("expected max index to be %d, but found a larger value at index %d", maxIdx, i)
}
}
})
}
func TestVoteSlice_Max(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
length := rapid.IntRange(1, 100).Draw(t, "length")
voteSlice := make(bitknn.VoteSlice, length)
for i := 0; i < length; i++ {
voteSlice[i] = rapid.Float64Range(-1000, 1000).Draw(t, "vote")
}
maxValue := voteSlice.Max()
expectedMax := voteSlice[0]
for _, val := range voteSlice[1:] {
if val > expectedMax {
expectedMax = val
}
}
if math.Abs(expectedMax-maxValue) > eps {
t.Fatalf("expected max value %f, got %f", expectedMax, maxValue)
}
})
}
func TestVoteSlice_Add(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
length := rapid.IntRange(1, 100).Draw(t, "length")
voteSlice := make(bitknn.VoteSlice, length)
class := rapid.IntRange(0, length-1).Draw(t, "class")
delta := rapid.Float64().Draw(t, "delta")
oldValue := voteSlice[class]
voteSlice.Add(class, delta)
newValue := voteSlice[class]
if math.Abs((oldValue+delta)-newValue) > eps {
t.Fatalf("expected new value to be %f, got %f", oldValue+delta, newValue)
}
})
}
func TestVoteSlice_Get(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
length := rapid.IntRange(1, 100).Draw(t, "length")
voteSlice := make(bitknn.VoteSlice, length)
for i := 0; i < length; i++ {
voteSlice[i] = rapid.Float64().Draw(t, "vote")
}
class := rapid.IntRange(0, length-1).Draw(t, "class")
value := voteSlice.Get(class)
if math.Abs(voteSlice[class]-value) > eps {
t.Fatalf("expected value %f, got %f", voteSlice[class], value)
}
})
}
func TestVoteMap_Clear(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
voteMap := make(bitknn.VoteMap)
for i := 0; i < rapid.IntRange(0, 100).Draw(t, "length"); i++ {
voteMap[i] = rapid.Float64().Draw(t, "vote")
}
voteMap.Clear()
if len(voteMap) != 0 {
t.Fatalf("expected VoteMap to be empty after Clear")
}
})
}
func TestVoteMap_ArgMax(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
voteMap := make(bitknn.VoteMap)
length := rapid.IntRange(0, 100).Draw(t, "length")
for i := 0; i < length; i++ {
voteMap[i] = rapid.Float64Range(-1000, 1000).Draw(t, "vote")
}
maxIdx := voteMap.ArgMax()
if length == 0 && maxIdx != 0 {
t.Fatal()
}
for i, v := range voteMap {
if v > voteMap[maxIdx] {
t.Fatalf("expected max index to be %d, but found a larger value at index %d", maxIdx, i)
}
}
})
}
func TestVoteMap_Max(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
voteMap := make(bitknn.VoteMap)
length := rapid.IntRange(1, 100).Draw(t, "length")
for i := 0; i < length; i++ {
voteMap[i] = rapid.Float64Range(-1000, 1000).Draw(t, "vote")
}
maxValue := voteMap.Max()
var expectedMax float64
first := true
for _, v := range voteMap {
if first || v > expectedMax {
expectedMax = v
first = false
}
}
if math.Abs(expectedMax-maxValue) > eps {
t.Fatalf("expected max value %f, got %f", expectedMax, maxValue)
}
})
}
func TestVoteMap_Add(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
voteMap := make(bitknn.VoteMap)
class := rapid.IntRange(0, 100).Draw(t, "class")
delta := rapid.Float64().Draw(t, "delta")
oldValue := voteMap[class]
voteMap.Add(class, delta)
newValue := voteMap[class]
if math.Abs((oldValue+delta)-newValue) > eps {
t.Fatalf("expected new value to be %f, got %f", oldValue+delta, newValue)
}
})
}
func TestVoteMap_Get(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
voteMap := make(bitknn.VoteMap)
for i := 0; i < rapid.IntRange(0, 100).Draw(t, "length"); i++ {
voteMap[i] = rapid.Float64().Draw(t, "vote")
}
class := rapid.IntRange(0, 100).Draw(t, "class")
value := voteMap.Get(class)
if math.Abs(voteMap[class]-value) > eps {
t.Fatalf("expected value %f, got %f", voteMap[class], value)
}
})
}