1// go:build unit
2
3package u
4
5import (
6 "testing"
7
8 "github.com/google/go-cmp/cmp"
9)
10
11func TestFirst(t *testing.T) {
12 testCases := []struct {
13 name string
14 slice []int
15 first int
16 exist bool
17 }{
18 {
19 name: "multiple items slice",
20 slice: []int{1, 2, 3},
21 first: 1,
22 exist: true,
23 },
24 {
25 name: "single item slice",
26 slice: []int{1},
27 first: 1,
28 exist: true,
29 },
30 {
31 name: "empty slice",
32 slice: []int{},
33 first: 0,
34 exist: false,
35 },
36 }
37 for _, tc := range testCases {
38 t.Run(tc.name, func(t *testing.T) {
39
40 first, empty := First(tc.slice)
41
42 if first != tc.first {
43 t.Errorf("Error first, want %d got %d", tc.first, first)
44 }
45
46 if empty != tc.exist {
47 t.Errorf("Error empty, want %t got %t", tc.exist, empty)
48 }
49
50 })
51 }
52}
53
54func TestSubList(t *testing.T) {
55 testCases := []struct {
56 name string
57 slice []int
58 size int
59 want [][]int
60 }{
61 {
62 name: "sigle size sub list",
63 slice: []int{1, 2, 3},
64 size: 1,
65 want: [][]int{{1}, {2}, {3}},
66 },
67 {
68 name: "multiple size sub list",
69 slice: []int{1, 2, 3, 4},
70 size: 2,
71 want: [][]int{{1, 2}, {3, 4}},
72 },
73 {
74 name: "uneven multiple size sub list",
75 slice: []int{1, 2, 3, 4, 5},
76 size: 2,
77 want: [][]int{{1, 2}, {3, 4}, {5}},
78 },
79 {
80 name: "empty sub list",
81 slice: []int{},
82 size: 2,
83 want: [][]int{{}},
84 },
85 }
86 for _, tc := range testCases {
87 t.Run(tc.name, func(t *testing.T) {
88
89 subList := ChunkBy(tc.slice, tc.size)
90
91 if diff := cmp.Diff(tc.want, subList); diff != "" {
92 t.Errorf("Wrong result given - wanted + got\n %s", diff)
93 }
94 })
95 }
96}
97
98func TestFirstOrZero(t *testing.T) {
99 testCases := []struct {
100 name string
101 slice []int
102 first int
103 }{
104 {
105 name: "multiple items slice",
106 slice: []int{1, 2, 3},
107 first: 1,
108 },
109 {
110 name: "single item slice",
111 slice: []int{1},
112 first: 1,
113 },
114 {
115 name: "empty slice",
116 slice: []int{},
117 first: 0,
118 },
119 }
120 for _, tc := range testCases {
121 t.Run(tc.name, func(t *testing.T) {
122
123 first := FirstOrZero(tc.slice)
124
125 if first != tc.first {
126 t.Errorf("Error first, want %d got %d", tc.first, first)
127 }
128
129 })
130 }
131}