@@ -24,7 +24,7 @@ func Example() {
2424 NewChoice ('🍉' , 0 ),
2525 NewChoice ('🥑' , 42 ),
2626 )
27- fruit := chooser .Pick ().( rune )
27+ fruit := chooser .Pick ()
2828 fmt .Printf ("%c" , fruit )
2929 //Output: 🥑
3030}
@@ -45,32 +45,37 @@ const (
4545func TestNewChooser (t * testing.T ) {
4646 tests := []struct {
4747 name string
48- cs []Choice
48+ cs []Choice [ rune , int ]
4949 wantErr error
5050 }{
5151 {
5252 name : "zero choices" ,
53- cs : []Choice {},
53+ cs : []Choice [ rune , int ] {},
5454 wantErr : errNoValidChoices ,
5555 },
5656 {
5757 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 }},
5959 wantErr : errNoValidChoices ,
6060 },
6161 {
6262 name : "choice with weight equals 1" ,
63- cs : []Choice {{Item : 'a' , Weight : 1 }},
63+ cs : []Choice [ rune , int ] {{Item : 'a' , Weight : 1 }},
6464 wantErr : nil ,
6565 },
6666 {
6767 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 }},
6969 wantErr : errWeightOverflow ,
7070 },
7171 {
7272 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 }},
7479 wantErr : nil ,
7580 },
7681 }
@@ -100,7 +105,7 @@ func TestChooser_Pick(t *testing.T) {
100105 counts := make (map [int ]int )
101106 for i := 0 ; i < testIterations ; i ++ {
102107 c := chooser .Pick ()
103- counts [c .( int ) ]++
108+ counts [c ]++
104109 }
105110
106111 verifyFrequencyCounts (t , counts , choices )
@@ -127,7 +132,7 @@ func TestChooser_PickSource(t *testing.T) {
127132 rs := rand .New (rand .NewSource (time .Now ().UTC ().UnixNano ()))
128133 for i := 0 ; i < testIterations / 2 ; i ++ {
129134 c := chooser .PickSource (rs )
130- counts [c .( int ) ]++
135+ counts [c ]++
131136 }
132137 }
133138 go checker (counts1 )
@@ -140,19 +145,19 @@ func TestChooser_PickSource(t *testing.T) {
140145
141146// Similar to what is used in randutil test, but in randomized order to avoid
142147// 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 ] {
144149 t .Helper ()
145- choices := make ([]Choice , 0 , n )
150+ choices := make ([]Choice [ int , int ] , 0 , n )
146151 list := rand .Perm (n )
147152 for _ , v := range list {
148- c := NewChoice (v , uint ( v ) )
153+ c := NewChoice (v , v )
149154 choices = append (choices , c )
150155 }
151156 t .Log ("mocked choices of" , choices )
152157 return choices
153158}
154159
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 ] ) {
156161 t .Helper ()
157162
158163 // Ensure weight 0 results in no results
@@ -202,7 +207,7 @@ func BenchmarkPick(b *testing.B) {
202207 b .ResetTimer ()
203208
204209 for i := 0 ; i < b .N ; i ++ {
205- _ = chooser .Pick ().( rune )
210+ _ = chooser .Pick ()
206211 }
207212 })
208213 }
@@ -220,19 +225,19 @@ func BenchmarkPickParallel(b *testing.B) {
220225 b .RunParallel (func (pb * testing.PB ) {
221226 rs := rand .New (rand .NewSource (time .Now ().UTC ().UnixNano ()))
222227 for pb .Next () {
223- _ = chooser .PickSource (rs ).( rune )
228+ _ = chooser .PickSource (rs )
224229 }
225230 })
226231 })
227232 }
228233}
229234
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 )
232237 for i := 0 ; i < n ; i ++ {
233238 s := '🥑'
234239 w := rand .Intn (10 )
235- c := NewChoice (s , uint ( w ) )
240+ c := NewChoice (s , w )
236241 choices = append (choices , c )
237242 }
238243 return choices
0 commit comments