@@ -17,18 +17,20 @@ limitations under the License.
17
17
package shufflesharding
18
18
19
19
import (
20
+ "errors"
21
+ "math/rand"
20
22
"testing"
21
23
)
22
24
23
25
func TestValidateParameters (t * testing.T ) {
24
26
tests := []struct {
25
27
name string
26
- queueSize int32
28
+ numQueues int32
27
29
handSize int32
28
30
validated bool
29
31
}{
30
32
{
31
- "queueSize is < 0" ,
33
+ "numQueues is < 0" ,
32
34
- 100 ,
33
35
8 ,
34
36
false ,
@@ -40,7 +42,7 @@ func TestValidateParameters(t *testing.T) {
40
42
false ,
41
43
},
42
44
{
43
- "queueSize is 0" ,
45
+ "numQueues is 0" ,
44
46
0 ,
45
47
8 ,
46
48
false ,
@@ -52,33 +54,33 @@ func TestValidateParameters(t *testing.T) {
52
54
false ,
53
55
},
54
56
{
55
- "handSize is greater than queueSize " ,
57
+ "handSize is greater than numQueues " ,
56
58
128 ,
57
59
129 ,
58
60
false ,
59
61
},
60
62
{
61
- "queueSize : 128 handSize: 6" ,
63
+ "numQueues : 128 handSize: 6" ,
62
64
128 ,
63
65
6 ,
64
66
true ,
65
67
},
66
68
{
67
- "queueSize : 1024 handSize: 6" ,
69
+ "numQueues : 1024 handSize: 6" ,
68
70
1024 ,
69
71
6 ,
70
72
true ,
71
73
},
72
74
{
73
- "queueSize : 512 handSize: 8" ,
75
+ "numQueues : 512 handSize: 8" ,
74
76
512 ,
75
77
8 ,
76
78
false ,
77
79
},
78
80
}
79
81
for _ , test := range tests {
80
82
t .Run (test .name , func (t * testing.T ) {
81
- if ValidateParameters (test .queueSize , test .handSize ) != test .validated {
83
+ if ValidateParameters (test .numQueues , test .handSize ) != test .validated {
82
84
t .Errorf ("test case %s fails" , test .name )
83
85
return
84
86
}
@@ -87,9 +89,189 @@ func TestValidateParameters(t *testing.T) {
87
89
}
88
90
89
91
func BenchmarkValidateParameters (b * testing.B ) {
92
+ queueSize , handSize := int32 (512 ), int32 (8 )
90
93
for i := 0 ; i < b .N ; i ++ {
91
- //queueSize, handSize := uint32(rand.Intn(513)), uint32(rand.Intn(17))
92
- queueSize , handSize := int32 (512 ), int32 (8 )
93
- ValidateParameters (queueSize , handSize )
94
+ _ = ValidateParameters (queueSize , handSize )
95
+ }
96
+ }
97
+
98
+ func TestDealWithValidation (t * testing.T ) {
99
+ tests := []struct {
100
+ name string
101
+ numQueues int32
102
+ handSize int32
103
+ pick func (int32 ) error
104
+ validated bool
105
+ }{
106
+ {
107
+ "numQueues is < 0" ,
108
+ - 100 ,
109
+ 8 ,
110
+ func (i int32 ) error {
111
+ return nil
112
+ },
113
+ false ,
114
+ },
115
+ {
116
+ "handSize is < 0" ,
117
+ 128 ,
118
+ - 100 ,
119
+ func (i int32 ) error {
120
+ return nil
121
+ },
122
+ false ,
123
+ },
124
+ {
125
+ "numQueues is 0" ,
126
+ 0 ,
127
+ 8 ,
128
+ func (i int32 ) error {
129
+ return nil
130
+ },
131
+ false ,
132
+ },
133
+ {
134
+ "handSize is 0" ,
135
+ 128 ,
136
+ 0 ,
137
+ func (i int32 ) error {
138
+ return nil
139
+ },
140
+ false ,
141
+ },
142
+ {
143
+ "handSize is greater than numQueues" ,
144
+ 128 ,
145
+ 129 ,
146
+ func (i int32 ) error {
147
+ return nil
148
+ },
149
+ false ,
150
+ },
151
+ {
152
+ "numQueues: 128 handSize: 6" ,
153
+ 128 ,
154
+ 6 ,
155
+ func (i int32 ) error {
156
+ return nil
157
+ },
158
+ true ,
159
+ },
160
+ {
161
+ "numQueues: 1024 handSize: 6" ,
162
+ 1024 ,
163
+ 6 ,
164
+ func (i int32 ) error {
165
+ return nil
166
+ },
167
+ true ,
168
+ },
169
+ {
170
+ "numQueues: 128 handSize: 6 with bad pick" ,
171
+ 128 ,
172
+ 6 ,
173
+ func (i int32 ) error {
174
+ return errors .New ("for test" )
175
+ },
176
+ false ,
177
+ },
178
+ {
179
+ "numQueues: 512 handSize: 8" ,
180
+ 512 ,
181
+ 8 ,
182
+ func (i int32 ) error {
183
+ return nil
184
+ },
185
+ false ,
186
+ },
187
+ }
188
+ for _ , test := range tests {
189
+ t .Run (test .name , func (t * testing.T ) {
190
+ if (DealWithValidation (rand .Uint64 (), test .numQueues , test .handSize , test .pick ) == nil ) != test .validated {
191
+ t .Errorf ("test case %s fails" , test .name )
192
+ return
193
+ }
194
+ })
195
+ }
196
+ }
197
+
198
+ func BenchmarkDeal (b * testing.B ) {
199
+ hashValue := rand .Uint64 ()
200
+ queueSize , handSize := int32 (512 ), int32 (8 )
201
+ pick := func (int32 ) error {
202
+ return nil
203
+ }
204
+ for i := 0 ; i < b .N ; i ++ {
205
+ _ = Deal (hashValue , queueSize , handSize , pick )
206
+ }
207
+ }
208
+
209
+ func TestDealToSlices (t * testing.T ) {
210
+ tests := []struct {
211
+ name string
212
+ numQueues int32
213
+ handSize int32
214
+ validated bool
215
+ }{
216
+ {
217
+ "validation fails" ,
218
+ - 100 ,
219
+ - 100 ,
220
+ false ,
221
+ },
222
+ {
223
+ "numQueues == handSize == 4" ,
224
+ 4 ,
225
+ 4 ,
226
+ true ,
227
+ },
228
+ {
229
+ "numQueues == handSize == 8" ,
230
+ 8 ,
231
+ 8 ,
232
+ true ,
233
+ },
234
+ {
235
+ "numQueues == handSize == 10" ,
236
+ 10 ,
237
+ 10 ,
238
+ true ,
239
+ },
240
+ {
241
+ "numQueues == handSize == 12" ,
242
+ 12 ,
243
+ 12 ,
244
+ true ,
245
+ },
246
+ }
247
+ for _ , test := range tests {
248
+ hashValue := rand .Uint64 ()
249
+ t .Run (test .name , func (t * testing.T ) {
250
+ cards , err := DealToSlice (hashValue , test .numQueues , test .handSize )
251
+ if (err == nil ) != test .validated {
252
+ t .Errorf ("test case %s fails in validation check" , test .name )
253
+ return
254
+ }
255
+
256
+ if test .validated {
257
+ // check cards number
258
+ if len (cards ) != int (test .handSize ) {
259
+ t .Errorf ("test case %s fails in cards number" , test .name )
260
+ return
261
+ }
262
+
263
+ // check cards duplication
264
+ cardMap := make (map [int32 ]struct {}, test .handSize )
265
+ for _ , cardIdx := range cards {
266
+ cardMap [cardIdx ] = struct {}{}
267
+ }
268
+ for i := int32 (0 ); i < test .handSize ; i ++ {
269
+ if _ , ok := cardMap [i ]; ! ok {
270
+ t .Errorf ("test case %s fails in duplication check" , test .name )
271
+ return
272
+ }
273
+ }
274
+ }
275
+ })
94
276
}
95
277
}
0 commit comments