@@ -9,7 +9,14 @@ import (
99 "testing"
1010)
1111
12- type hashString string
12+ type (
13+ hashString string
14+ unknown byte
15+ slices struct {
16+ actual interface {}
17+ expect interface {}
18+ }
19+ )
1320
1421var testKey = []byte ("0xff51afd7ed558ccd" )
1522
@@ -44,6 +51,7 @@ func Example() {
4451 // trying GET six.example.com/examples/object-key
4552 // trying GET five.example.com/examples/object-key
4653}
54+
4755func (h hashString ) Hash () uint64 {
4856 return Hash ([]byte (h ))
4957}
@@ -118,8 +126,8 @@ func TestSortSliceByValueFail(t *testing.T) {
118126 })
119127
120128 t .Run ("must 'fail' for unknown type" , func (t * testing.T ) {
121- actual := []byte {1 , 2 , 3 , 4 , 5 }
122- expect := []byte {1 , 2 , 3 , 4 , 5 }
129+ actual := []unknown {1 , 2 , 3 , 4 , 5 }
130+ expect := []unknown {1 , 2 , 3 , 4 , 5 }
123131 hash := Hash (testKey )
124132 SortSliceByValue (actual , hash )
125133 if ! reflect .DeepEqual (actual , expect ) {
@@ -139,12 +147,64 @@ func TestSortSliceByValueHasher(t *testing.T) {
139147}
140148
141149func TestSortSliceByValueIntSlice (t * testing.T ) {
142- actual := []int {0 , 1 , 2 , 3 , 4 , 5 }
143- expect := []int {2 , 3 , 1 , 4 , 0 , 5 }
150+ cases := []slices {
151+ {
152+ actual : []int {0 , 1 , 2 , 3 , 4 , 5 },
153+ expect : []int {2 , 3 , 1 , 4 , 0 , 5 },
154+ },
155+
156+ {
157+ actual : []uint {0 , 1 , 2 , 3 , 4 , 5 },
158+ expect : []uint {2 , 3 , 1 , 4 , 0 , 5 },
159+ },
160+
161+ {
162+ actual : []int8 {0 , 1 , 2 , 3 , 4 , 5 },
163+ expect : []int8 {2 , 0 , 5 , 1 , 4 , 3 },
164+ },
165+
166+ {
167+ actual : []uint8 {0 , 1 , 2 , 3 , 4 , 5 },
168+ expect : []uint8 {2 , 0 , 5 , 1 , 4 , 3 },
169+ },
170+
171+ {
172+ actual : []int16 {0 , 1 , 2 , 3 , 4 , 5 },
173+ expect : []int16 {5 , 4 , 0 , 3 , 2 , 1 },
174+ },
175+
176+ {
177+ actual : []uint16 {0 , 1 , 2 , 3 , 4 , 5 },
178+ expect : []uint16 {5 , 4 , 0 , 3 , 2 , 1 },
179+ },
180+
181+ {
182+ actual : []int32 {0 , 1 , 2 , 3 , 4 , 5 },
183+ expect : []int32 {1 , 3 , 5 , 4 , 2 , 0 },
184+ },
185+
186+ {
187+ actual : []uint32 {0 , 1 , 2 , 3 , 4 , 5 },
188+ expect : []uint32 {1 , 3 , 5 , 4 , 2 , 0 },
189+ },
190+
191+ {
192+ actual : []int64 {0 , 1 , 2 , 3 , 4 , 5 },
193+ expect : []int64 {1 , 5 , 3 , 4 , 2 , 0 },
194+ },
195+
196+ {
197+ actual : []uint64 {0 , 1 , 2 , 3 , 4 , 5 },
198+ expect : []uint64 {1 , 5 , 3 , 4 , 2 , 0 },
199+ },
200+ }
144201 hash := Hash (testKey )
145- SortSliceByValue (actual , hash )
146- if ! reflect .DeepEqual (actual , expect ) {
147- t .Errorf ("Was %#v, but expected %#v" , actual , expect )
202+
203+ for _ , tc := range cases {
204+ SortSliceByValue (tc .actual , hash )
205+ if ! reflect .DeepEqual (tc .actual , tc .expect ) {
206+ t .Errorf ("Was %#v, but expected %#v" , tc .actual , tc .expect )
207+ }
148208 }
149209}
150210
@@ -326,7 +386,6 @@ func TestUniformDistribution(t *testing.T) {
326386 "Chi2 condition for .9 is not met (expected %.2f <= %.2f)" ,
327387 chi2 , chiTable [size - 1 ])
328388 }
329-
330389 })
331390
332391 t .Run ("sortByInt32Value" , func (t * testing.T ) {
@@ -367,7 +426,6 @@ func TestUniformDistribution(t *testing.T) {
367426 "Chi2 condition for .9 is not met (expected %.2f <= %.2f)" ,
368427 chi2 , chiTable [size - 1 ])
369428 }
370-
371429 })
372430
373431 t .Run ("hash collision" , func (t * testing.T ) {
0 commit comments