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
|
// go:build unit
package u
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestFirst(t *testing.T) {
testCases := []struct {
name string
slice []int
first int
exist bool
}{
{
name: "multiple items slice",
slice: []int{1, 2, 3},
first: 1,
exist: true,
},
{
name: "single item slice",
slice: []int{1},
first: 1,
exist: true,
},
{
name: "empty slice",
slice: []int{},
first: 0,
exist: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
first, empty := First(tc.slice)
if first != tc.first {
t.Errorf("Error first, want %d got %d", tc.first, first)
}
if empty != tc.exist {
t.Errorf("Error empty, want %t got %t", tc.exist, empty)
}
})
}
}
func TestSubList(t *testing.T) {
testCases := []struct {
name string
slice []int
size int
want [][]int
}{
{
name: "sigle size sub list",
slice: []int{1, 2, 3},
size: 1,
want: [][]int{{1}, {2}, {3}},
},
{
name: "multiple size sub list",
slice: []int{1, 2, 3, 4},
size: 2,
want: [][]int{{1, 2}, {3, 4}},
},
{
name: "uneven multiple size sub list",
slice: []int{1, 2, 3, 4, 5},
size: 2,
want: [][]int{{1, 2}, {3, 4}, {5}},
},
{
name: "empty sub list",
slice: []int{},
size: 2,
want: [][]int{{}},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
subList := ChunkBy(tc.slice, tc.size)
if diff := cmp.Diff(tc.want, subList); diff != "" {
t.Errorf("Wrong result given - wanted + got\n %s", diff)
}
})
}
}
|