-
Notifications
You must be signed in to change notification settings - Fork 0
/
slices_test.go
79 lines (76 loc) · 1.37 KB
/
slices_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
package slices_test
import (
"testing"
"github.com/avamsi/ergo/slices"
"github.com/google/go-cmp/cmp"
)
func TestShard(t *testing.T) {
tests := []struct {
name string
s []int
n int
want [][]int
}{
{
name: "nil",
s: nil,
n: 1,
want: [][]int{nil},
},
{
name: "nils",
s: nil,
n: 3,
want: [][]int{nil, nil, nil},
},
{
name: "empty",
s: []int{},
n: 5,
want: [][]int{{}, {}, {}, {}, {}},
},
{
name: "non-empty",
s: []int{1, 2, 3, 4, 5, 6},
n: 3,
want: [][]int{
{1, 2},
{3, 4},
{5, 6},
},
},
{
name: "non-empty-over-subscribed",
s: []int{1, 2, 3},
n: 5,
want: [][]int{{1}, {2}, {3}, {}, {}},
},
{
name: "non-empty-optimal",
s: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
n: 4,
// Should avoid both 2-2-2-4 and 3-3-3-1 shards.
want: [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8},
{9, 10},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var got [][]int
it := slices.Shard(test.s, test.n)
it(func(i int, shard []int) {
if l := len(got); i != l {
t.Errorf("(i=)%v != %v(=len(%#v(=got))\n", i, l, got)
}
got = append(got, shard)
})
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("Chunks(%#v) returned diff(-want +got):\n%v", test.s, diff)
}
})
}
}