@@ -24,7 +24,7 @@ func Example() {
24
24
NewChoice ('🍉' , 0 ),
25
25
NewChoice ('🥑' , 42 ),
26
26
)
27
- fruit := chooser .Pick ().( rune )
27
+ fruit := chooser .Pick ()
28
28
fmt .Printf ("%c" , fruit )
29
29
//Output: 🥑
30
30
}
@@ -45,32 +45,37 @@ const (
45
45
func TestNewChooser (t * testing.T ) {
46
46
tests := []struct {
47
47
name string
48
- cs []Choice
48
+ cs []Choice [ rune , int ]
49
49
wantErr error
50
50
}{
51
51
{
52
52
name : "zero choices" ,
53
- cs : []Choice {},
53
+ cs : []Choice [ rune , int ] {},
54
54
wantErr : errNoValidChoices ,
55
55
},
56
56
{
57
57
name : "no choices with positive weight" ,
58
- cs : []Choice {{Item : 'a' , Weight : 0 }, {Item : 'b' , Weight : 0 }},
58
+ cs : []Choice [ rune , int ] {{Item : 'a' , Weight : 0 }, {Item : 'b' , Weight : 0 }},
59
59
wantErr : errNoValidChoices ,
60
60
},
61
61
{
62
62
name : "choice with weight equals 1" ,
63
- cs : []Choice {{Item : 'a' , Weight : 1 }},
63
+ cs : []Choice [ rune , int ] {{Item : 'a' , Weight : 1 }},
64
64
wantErr : nil ,
65
65
},
66
66
{
67
67
name : "weight overflow" ,
68
- cs : []Choice {{Item : 'a' , Weight : maxInt / 2 + 1 }, {Item : 'b' , Weight : maxInt / 2 + 1 }},
68
+ cs : []Choice [ rune , int ] {{Item : 'a' , Weight : maxInt / 2 + 1 }, {Item : 'b' , Weight : maxInt / 2 + 1 }},
69
69
wantErr : errWeightOverflow ,
70
70
},
71
71
{
72
72
name : "nominal case" ,
73
- cs : []Choice {{Item : 'a' , Weight : 1 }, {Item : 'b' , Weight : 2 }},
73
+ cs : []Choice [rune , int ]{{Item : 'a' , Weight : 1 }, {Item : 'b' , Weight : 2 }},
74
+ wantErr : nil ,
75
+ },
76
+ {
77
+ name : "negative weight case" ,
78
+ cs : []Choice [rune , int ]{{Item : 'a' , Weight : 3 }, {Item : 'b' , Weight : - 2 }},
74
79
wantErr : nil ,
75
80
},
76
81
}
@@ -100,7 +105,7 @@ func TestChooser_Pick(t *testing.T) {
100
105
counts := make (map [int ]int )
101
106
for i := 0 ; i < testIterations ; i ++ {
102
107
c := chooser .Pick ()
103
- counts [c .( int ) ]++
108
+ counts [c ]++
104
109
}
105
110
106
111
verifyFrequencyCounts (t , counts , choices )
@@ -127,7 +132,7 @@ func TestChooser_PickSource(t *testing.T) {
127
132
rs := rand .New (rand .NewSource (time .Now ().UTC ().UnixNano ()))
128
133
for i := 0 ; i < testIterations / 2 ; i ++ {
129
134
c := chooser .PickSource (rs )
130
- counts [c .( int ) ]++
135
+ counts [c ]++
131
136
}
132
137
}
133
138
go checker (counts1 )
@@ -140,19 +145,19 @@ func TestChooser_PickSource(t *testing.T) {
140
145
141
146
// Similar to what is used in randutil test, but in randomized order to avoid
142
147
// any issues with algorithms that are accidentally dependant on presorted data.
143
- func mockFrequencyChoices (t * testing.T , n int ) []Choice {
148
+ func mockFrequencyChoices (t * testing.T , n int ) []Choice [ int , int ] {
144
149
t .Helper ()
145
- choices := make ([]Choice , 0 , n )
150
+ choices := make ([]Choice [ int , int ] , 0 , n )
146
151
list := rand .Perm (n )
147
152
for _ , v := range list {
148
- c := NewChoice (v , uint ( v ) )
153
+ c := NewChoice (v , v )
149
154
choices = append (choices , c )
150
155
}
151
156
t .Log ("mocked choices of" , choices )
152
157
return choices
153
158
}
154
159
155
- func verifyFrequencyCounts (t * testing.T , counts map [int ]int , choices []Choice ) {
160
+ func verifyFrequencyCounts (t * testing.T , counts map [int ]int , choices []Choice [ int , int ] ) {
156
161
t .Helper ()
157
162
158
163
// Ensure weight 0 results in no results
@@ -202,7 +207,7 @@ func BenchmarkPick(b *testing.B) {
202
207
b .ResetTimer ()
203
208
204
209
for i := 0 ; i < b .N ; i ++ {
205
- _ = chooser .Pick ().( rune )
210
+ _ = chooser .Pick ()
206
211
}
207
212
})
208
213
}
@@ -220,19 +225,19 @@ func BenchmarkPickParallel(b *testing.B) {
220
225
b .RunParallel (func (pb * testing.PB ) {
221
226
rs := rand .New (rand .NewSource (time .Now ().UTC ().UnixNano ()))
222
227
for pb .Next () {
223
- _ = chooser .PickSource (rs ).( rune )
228
+ _ = chooser .PickSource (rs )
224
229
}
225
230
})
226
231
})
227
232
}
228
233
}
229
234
230
- func mockChoices (n int ) []Choice {
231
- choices := make ([]Choice , 0 , n )
235
+ func mockChoices (n int ) []Choice [ rune , int ] {
236
+ choices := make ([]Choice [ rune , int ] , 0 , n )
232
237
for i := 0 ; i < n ; i ++ {
233
238
s := '🥑'
234
239
w := rand .Intn (10 )
235
- c := NewChoice (s , uint ( w ) )
240
+ c := NewChoice (s , w )
236
241
choices = append (choices , c )
237
242
}
238
243
return choices
0 commit comments