Debian Codegraph @

list/list_test.go

raw symbols SCIP
1 package list
2
3 import (
4 "testing"
5
6 "github.com/jesseduffield/generics/internal/testutils"
7 "github.com/jesseduffield/generics/slices"
8 )
9
10 func TestPush(t *testing.T) {
11 list := New[int]()
12 list.Push(1)
13 list.Push(2)
14 slice := list.ToSlice()
15 testutils.ExpectSlice(t, []int{1, 2}, slice)
16 }
17
18 func TestInsert(t *testing.T) {
19 tests := []struct {
20 startSlice []int
21 index int
22 values []int
23 endSlice []int
24 }{
25 {[]int{}, 0, []int{1}, []int{1}},
26 {[]int{1}, 0, []int{2}, []int{2, 1}},
27 {[]int{1, 2}, 1, []int{3}, []int{1, 3, 2}},
28 {[]int{1, 2}, 2, []int{3}, []int{1, 2, 3}},
29 {[]int{1, 2}, 2, []int{3, 4}, []int{1, 2, 3, 4}},
30 {[]int{1, 2}, 1, []int{3, 4}, []int{1, 3, 4, 2}},
31 }
32 for _, test := range tests {
33 list := NewFromSlice(test.startSlice)
34 list.Insert(test.index, test.values...)
35 testutils.ExpectSlice(t, test.endSlice, list.ToSlice())
36 }
37
38 panicTests := []struct {
39 startSlice []int
40 index int
41 value int
42 }{
43 {[]int{}, 1, 1},
44 {[]int{}, 2, 1},
45 {[]int{1}, 2, 1},
46 {[]int{1}, -1, 1},
47 }
48 for _, test := range panicTests {
49 func() {
50 defer testutils.ExpectPanic(t)
51 list := NewFromSlice(test.startSlice)
52 list.Insert(test.index, test.value)
53 }()
54 }
55 }
56
57 func TestRemove(t *testing.T) {
58 tests := []struct {
59 startSlice []int
60 index int
61 endSlice []int
62 }{
63 {[]int{1}, 0, []int{}},
64 {[]int{1, 2}, 0, []int{2}},
65 {[]int{1, 2}, 1, []int{1}},
66 }
67 for _, test := range tests {
68 list := NewFromSlice(test.startSlice)
69 list.Remove(test.index)
70 testutils.ExpectSlice(t, test.endSlice, list.ToSlice())
71 }
72
73 panicTests := []struct {
74 startSlice []int
75 index int
76 }{
77 {[]int{}, 0},
78 {[]int{}, 1},
79 {[]int{1}, 1},
80 {[]int{1}, -1},
81 }
82 for _, test := range panicTests {
83 func() {
84 defer testutils.ExpectPanic(t)
85 list := NewFromSlice(test.startSlice)
86 list.Remove(test.index)
87 }()
88 }
89 }
90
91 func TestPop(t *testing.T) {
92 tests := []struct {
93 startSlice []int
94 endSlice []int
95 }{
96 {[]int{1}, []int{}},
97 {[]int{1, 2}, []int{1}},
98 {[]int{1, 2, 3}, []int{1, 2}},
99 }
100 for _, test := range tests {
101 list := NewFromSlice(test.startSlice)
102 list.Pop()
103 testutils.ExpectSlice(t, test.endSlice, list.ToSlice())
104 }
105
106 panicTests := []struct {
107 startSlice []int
108 }{
109 {[]int{}},
110 }
111 for _, test := range panicTests {
112 func() {
113 defer testutils.ExpectPanic(t)
114 list := NewFromSlice(test.startSlice)
115 list.Pop()
116 }()
117 }
118 }
119
120 func TestFilter(t *testing.T) {
121 even := func(value int) bool { return value%2 == 0 }
122 tests := []struct {
123 startSlice []int
124 testFunc func(value int) bool
125 endSlice []int
126 }{
127 {[]int{1}, even, []int{}},
128 {[]int{1, 2}, even, []int{2}},
129 {[]int{1, 2, 3}, even, []int{2}},
130 {[]int{1, 2, 3, 4}, even, []int{2, 4}},
131 }
132 for _, test := range tests {
133 testSlice := slices.Clone(test.startSlice)
134 list := NewFromSlice(test.startSlice)
135 testutils.ExpectSlice(t, test.endSlice, list.Filter(test.testFunc).ToSlice())
136 testutils.ExpectSlice(t, testSlice, list.ToSlice())
137 }
138 }
139
140 func TestFilterInPlace(t *testing.T) {
141 even := func(value int) bool { return value%2 == 0 }
142 tests := []struct {
143 startSlice []int
144 testFunc func(value int) bool
145 endSlice []int
146 }{
147 {[]int{1}, even, []int{}},
148 {[]int{1, 2}, even, []int{2}},
149 {[]int{1, 2, 3}, even, []int{2}},
150 {[]int{1, 2, 3, 4}, even, []int{2, 4}},
151 }
152
153 for _, test := range tests {
154 list := NewFromSlice(test.startSlice)
155 list.FilterInPlace(test.testFunc)
156 testutils.ExpectSlice(t, test.endSlice, list.ToSlice())
157 }
158 }
159
160 func TestMap(t *testing.T) {
161 double := func(value int) int { return value * 2 }
162 tests := []struct {
163 startSlice []int
164 mapFunc func(value int) int
165 endSlice []int
166 }{
167 {[]int{}, double, []int{}},
168 {[]int{1}, double, []int{2}},
169 {[]int{1, 2}, double, []int{2, 4}},
170 {[]int{1, 2, 3}, double, []int{2, 4, 6}},
171 {[]int{1, 2, 3, 4}, double, []int{2, 4, 6, 8}},
172 }
173
174 for _, test := range tests {
175 testSlice := slices.Clone(test.startSlice)
176 list := NewFromSlice(test.startSlice)
177 testutils.ExpectSlice(t, test.endSlice, list.Map(test.mapFunc).ToSlice())
178 testutils.ExpectSlice(t, testSlice, list.ToSlice())
179 }
180 }
181
182 func TestMapInPlace(t *testing.T) {
183 double := func(value int) int { return value * 2 }
184 tests := []struct {
185 startSlice []int
186 mapFunc func(value int) int
187 endSlice []int
188 }{
189 {[]int{}, double, []int{}},
190 {[]int{1}, double, []int{2}},
191 {[]int{1, 2}, double, []int{2, 4}},
192 {[]int{1, 2, 3}, double, []int{2, 4, 6}},
193 {[]int{1, 2, 3, 4}, double, []int{2, 4, 6, 8}},
194 }
195 for _, test := range tests {
196 list := NewFromSlice(test.startSlice)
197 list.MapInPlace(test.mapFunc)
198 testutils.ExpectSlice(t, test.endSlice, list.ToSlice())
199 }
200 }
201
202 func TestAppend(t *testing.T) {
203 tests := []struct {
204 startSlice []int
205 other []int
206 endSlice []int
207 }{
208 {[]int{}, []int{}, []int{}},
209 {[]int{1}, []int{2}, []int{1, 2}},
210 {[]int{1, 2}, []int{3, 4}, []int{1, 2, 3, 4}},
211 }
212 for _, test := range tests {
213 list := NewFromSlice(test.startSlice)
214 list.Append(test.other...)
215 testutils.ExpectSlice(t, test.endSlice, list.ToSlice())
216 }
217 }
218
219 func TestPrepend(t *testing.T) {
220 tests := []struct {
221 startSlice []int
222 values []int
223 endSlice []int
224 }{
225 {[]int{}, []int{}, []int{}},
226 {[]int{}, []int{1}, []int{1}},
227 {[]int{1}, []int{2}, []int{2, 1}},
228 {[]int{1, 2}, []int{3}, []int{3, 1, 2}},
229 {[]int{1, 2}, []int{3, 4}, []int{3, 4, 1, 2}},
230 }
231 for _, test := range tests {
232 list := NewFromSlice(test.startSlice)
233 list.Prepend(test.values...)
234 testutils.ExpectSlice(t, test.endSlice, list.ToSlice())
235 }
236 }
237
238 func TestConcat(t *testing.T) {
239 tests := []struct {
240 startSlice []int
241 other []int
242 endSlice []int
243 }{
244 {[]int{}, []int{}, []int{}},
245 {[]int{1}, []int{2}, []int{1, 2}},
246 {[]int{1, 2}, []int{3, 4}, []int{1, 2, 3, 4}},
247 }
248 for _, test := range tests {
249 testSlice := slices.Clone(test.startSlice)
250 list := NewFromSlice(test.startSlice)
251 result := list.Concat(test.other...)
252 testutils.ExpectSlice(t, test.endSlice, result.ToSlice())
253 testutils.ExpectSlice(t, testSlice, list.ToSlice())
254 }
255 }
256
257 func TestSome(t *testing.T) {
258 even := func(value int) bool { return value%2 == 0 }
259 tests := []struct {
260 startSlice []int
261 testFunc func(value int) bool
262 expected bool
263 }{
264 {[]int{}, even, false},
265 {[]int{1}, even, false},
266 {[]int{2}, even, true},
267 {[]int{1, 2}, even, true},
268 }
269 for _, test := range tests {
270 list := NewFromSlice(test.startSlice)
271 if list.Some(test.testFunc) != test.expected {
272 t.Errorf("Some(%v) = %v, expected %v",
273 test.startSlice, list.Some(test.testFunc), test.expected,
274 )
275 }
276 }
277 }
278
279 func TestEvery(t *testing.T) {
280 even := func(value int) bool { return value%2 == 0 }
281 tests := []struct {
282 startSlice []int
283 testFunc func(value int) bool
284 expected bool
285 }{
286 {[]int{}, even, true},
287 {[]int{1}, even, false},
288 {[]int{2}, even, true},
289 {[]int{1, 2}, even, false},
290 {[]int{2, 2}, even, true},
291 }
292 for _, test := range tests {
293 list := NewFromSlice(test.startSlice)
294 if list.Every(test.testFunc) != test.expected {
295 t.Errorf("Every(%v) = %v, expected %v",
296 test.startSlice, list.Every(test.testFunc), test.expected,
297 )
298 }
299 }
300 }
301
302 func TestReverse(t *testing.T) {
303 tests := []struct {
304 startSlice []int
305 expected []int
306 }{
307 {[]int{}, []int{}},
308 {[]int{1}, []int{1}},
309 {[]int{1, 2}, []int{2, 1}},
310 {[]int{1, 2, 3}, []int{3, 2, 1}},
311 }
312 for _, test := range tests {
313 list := NewFromSlice(test.startSlice)
314 testutils.ExpectSlice(t, test.expected, list.Reverse().ToSlice())
315 }
316 }
317
318 func TestReverseInPlace(t *testing.T) {
319 tests := []struct {
320 startSlice []int
321 expected []int
322 }{
323 {[]int{}, []int{}},
324 {[]int{1}, []int{1}},
325 {[]int{1, 2}, []int{2, 1}},
326 {[]int{1, 2, 3}, []int{3, 2, 1}},
327 }
328 for _, test := range tests {
329 list := NewFromSlice(test.startSlice)
330 list.ReverseInPlace()
331 testutils.ExpectSlice(t, test.expected, list.ToSlice())
332 }
333 }
334
335 func TestClone(t *testing.T) {
336 list := NewFromSlice([]int{1, 2, 3})
337 clone := list.Clone()
338 testutils.ExpectSlice(t, []int{1, 2, 3}, clone.ToSlice())
339
340 // ensure that the clone has its own slice
341 clone.Insert(0, 1)
342 testutils.ExpectSlice(t, []int{1, 1, 2, 3}, clone.ToSlice())
343 testutils.ExpectSlice(t, []int{1, 2, 3}, list.ToSlice())
344 }
345
346 func TestFilterAndMap(t *testing.T) {
347 list := NewFromSlice([]int{1, 2, 3, 4})
348 result := list.
349 Filter(func(value int) bool { return value%2 == 0 }).
350 Map(func(value int) int { return value * 2 })
351
352 testutils.ExpectSlice(t, []int{4, 8}, result.ToSlice())
353 }
354
355 func TestIsEmpty(t *testing.T) {
356 list := NewFromSlice([]int{})
357 if !list.IsEmpty() {
358 t.Errorf("IsEmpty() = %v, expected %v", list.IsEmpty(), true)
359 }
360
361 list = NewFromSlice([]int{1})
362 if list.IsEmpty() {
363 t.Errorf("IsEmpty() = %v, expected %v", list.IsEmpty(), false)
364 }
365 }
366
367 func TestLen(t *testing.T) {
368 list := NewFromSlice([]int{})
369 if list.Len() != 0 {
370 t.Errorf("Len() = %v, expected %v", list.Len(), 0)
371 }
372
373 list = NewFromSlice([]int{1})
374 if list.Len() != 1 {
375 t.Errorf("Len() = %v, expected %v", list.Len(), 1)
376 }
377 }
378
379 func TestGet(t *testing.T) {
380 list := NewFromSlice([]int{1, 2, 3})
381 if list.Get(0) != 1 {
382 t.Errorf("Get(0) = %v, expected %v", list.Get(0), 1)
383 }
384 if list.Get(1) != 2 {
385 t.Errorf("Get(1) = %v, expected %v", list.Get(1), 2)
386 }
387 if list.Get(2) != 3 {
388 t.Errorf("Get(2) = %v, expected %v", list.Get(2), 3)
389 }
390 }