-
Notifications
You must be signed in to change notification settings - Fork 3
/
reslice_test.go
66 lines (63 loc) · 1.34 KB
/
reslice_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
package schema
import (
"testing"
)
type testCase struct {
inSize int
subSize int
}
func TestReslice(t *testing.T) {
cases := []testCase{
{10, 1},
{10, 2},
{10, 3},
{10, 4},
{10, 5},
{10, 6},
{10, 7},
{10, 8},
{10, 9},
{10, 10},
{10, 11},
{100, 1},
{100, 13},
{100, 39},
{100, 74},
{100, 143},
{100, 5000},
}
for _, c := range cases {
in := make([]*MetricData, c.inSize)
for i := 0; i < c.inSize; i++ {
in[i] = &MetricData{OrgId: i}
}
out := Reslice(in, c.subSize)
expectedLen := len(in) / c.subSize
fullSubSlices := len(in) / c.subSize
if len(in)%c.subSize != 0 {
expectedLen += 1
}
if len(out) != expectedLen {
t.Fatalf("case %#v: out array len expected %d, got %d", c, expectedLen, len(out))
}
for i := 0; i < fullSubSlices; i++ {
if len(out[i]) != c.subSize {
t.Fatalf("out sub array %d len expected %d, got %d", i, c.subSize, len(out[i]))
}
}
lastSize := len(in) % c.subSize
if lastSize == 0 {
lastSize = c.subSize
}
if len(out[len(out)-1]) != lastSize {
t.Fatalf("out last sub array len expected %d, got %d", lastSize, len(out[len(out)-1]))
}
for i := 0; i < len(in); i++ {
subArray := i / c.subSize
subI := i % c.subSize
if in[i] != out[subArray][subI] {
t.Fatalf("element mismatch. in: %v, out: %v", in[i], out[subArray][subI])
}
}
}
}