-
Notifications
You must be signed in to change notification settings - Fork 4
/
chunk_test.go
116 lines (104 loc) · 2.66 KB
/
chunk_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package hoff
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestChunkInts(t *testing.T) {
type TestCase struct {
Input []int
ChunkSize int
ExpectedOutput [][]int
Message string
}
testCases := []TestCase{
{
Input: []int{1, 2, 3, 4, 5},
ChunkSize: 2,
ExpectedOutput: [][]int{{1, 2}, {3, 4}, {5}},
Message: "5 elements, chunk size 2",
},
{
Input: []int{5, 2, 4, 1, 3},
ChunkSize: 2,
ExpectedOutput: [][]int{{5, 2}, {4, 1}, {3}},
Message: "5 elements, chunk size 2, order preserved",
},
{
Input: []int{1, 2, 3, 4, 5},
ChunkSize: 5,
ExpectedOutput: [][]int{{1, 2, 3, 4, 5}},
Message: "5 elements, chunk size 5",
},
{
Input: []int{1, 2, 3, 4, 5},
ChunkSize: 500,
ExpectedOutput: [][]int{{1, 2, 3, 4, 5}},
Message: "5 elements, chunk size 500",
},
{
Input: []int{1},
ChunkSize: 2,
ExpectedOutput: [][]int{{1}},
Message: "1 elements, chunk size 2",
},
{
Input: []int{1},
ChunkSize: 0,
ExpectedOutput: [][]int{},
Message: "1 element array, chunk size 0",
},
{
Input: []int{},
ChunkSize: 2,
ExpectedOutput: [][]int{},
Message: "empty array, chunk size 2",
},
}
for _, testCase := range testCases {
output := Chunk(testCase.Input, testCase.ChunkSize)
require.Equal(t, testCase.ExpectedOutput, output, testCase.Message)
}
}
func TestChunkStrings(t *testing.T) {
type TestCase struct {
Input []string
ChunkSize int
ExpectedOutput [][]string
Message string
}
testCases := []TestCase{
{
Input: []string{"a", "b", "c"},
ChunkSize: 2,
ExpectedOutput: [][]string{{"a", "b"}, {"c"}},
Message: "3 elements, chunk size 2",
},
}
for _, testCase := range testCases {
output := Chunk(testCase.Input, testCase.ChunkSize)
require.Equal(t, testCase.ExpectedOutput, output, testCase.Message)
}
}
func TestChunkStructs(t *testing.T) {
type ExampleStruct struct {
Foo string
}
type TestCase struct {
Input []ExampleStruct
ChunkSize int
ExpectedOutput [][]ExampleStruct
Message string
}
testCases := []TestCase{
{
Input: []ExampleStruct{{Foo: "a"}, {Foo: "b"}, {Foo: "c"}},
ChunkSize: 2,
ExpectedOutput: [][]ExampleStruct{{{Foo: "a"}, {Foo: "b"}}, {{Foo: "c"}}},
Message: "3 elements, chunk size 2",
},
}
for _, testCase := range testCases {
output := Chunk(testCase.Input, testCase.ChunkSize)
require.Equal(t, testCase.ExpectedOutput, output, testCase.Message)
}
}