-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_test.go
483 lines (468 loc) · 11.2 KB
/
list_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package collection
import (
"fmt"
"gotest.tools/v3/assert"
"strconv"
"testing"
)
var (
_ List[int] = empty[int]{}
_ List[int] = cons[int]{
consHead: 10,
consTail: Empty[int](),
length: 1,
}
emptyList = Empty[int]()
singleElementList = Of[int](10)
multipleElementsList = OfSlice([]int{1, 2, 3, 4, 5})
evenPredicate = func(value int) bool {
return value%2 == 0
}
)
func TestHead(t *testing.T) {
testCases := []struct {
name string
value List[int]
expected int
}{
{
name: "Empty List",
value: emptyList,
expected: 0,
},
{
name: "Single Element List",
value: singleElementList,
expected: 10,
},
{
name: "Multiple Elements List",
value: multipleElementsList,
expected: 1,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.value.head(), testCase.expected, fmt.Sprintf("expected %d but value is %d", testCase.expected, testCase.value.head()))
})
}
}
func TestTail(t *testing.T) {
testCases := []struct {
name string
value List[int]
expected List[int]
}{
{
name: "Empty List",
value: emptyList,
expected: emptyList,
},
{
name: "Single Element List",
value: singleElementList,
expected: emptyList,
},
{
name: "Multiple Elements List",
value: multipleElementsList,
expected: OfSlice[int]([]int{2, 3, 4, 5}),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.value.tail(), testCase.expected, fmt.Sprintf("expected %d but value is %d", testCase.expected, testCase.value.tail()))
})
}
}
func TestIsEmpty(t *testing.T) {
testCases := []struct {
name string
value List[int]
isEmpty bool
}{
{
name: "Empty List",
value: emptyList,
isEmpty: true,
},
{
name: "Single Element List",
value: singleElementList,
isEmpty: false,
},
{
name: "Multiple Elements List",
value: multipleElementsList,
isEmpty: false,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.value.IsEmpty(), testCase.isEmpty, fmt.Sprintf("expected %t but value is %t", testCase.isEmpty, testCase.value.IsEmpty()))
})
}
}
func TestLength(t *testing.T) {
testCases := []struct {
name string
value List[int]
length int
}{
{
name: "Empty List",
value: emptyList,
length: 0,
},
{
name: "Single Element List",
value: singleElementList,
length: 1,
},
{
name: "Multiple Elements List",
value: multipleElementsList,
length: 5,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.value.Length(), testCase.length, fmt.Sprintf("expected %d but value is %d", testCase.length, testCase.value.Length()))
})
}
}
func TestAppend(t *testing.T) {
testCases := []struct {
name string
value List[int]
length int
}{
{
name: "Empty List",
value: emptyList,
length: 1,
},
{
name: "Single Element List",
value: singleElementList,
length: 2,
},
{
name: "Multiple Elements List",
value: multipleElementsList,
length: 6,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := testCase.value.Append(10)
assert.Equal(t, result.Length(), testCase.length, fmt.Sprintf("expected %d but value is %d", testCase.length, result.Length()))
})
}
}
func TestAppendAll(t *testing.T) {
testCases := []struct {
name string
value List[int]
length int
}{
{
name: "Empty List",
value: emptyList,
length: 3,
},
{
name: "Single Element List",
value: singleElementList,
length: 4,
},
{
name: "Multiple Elements List",
value: multipleElementsList,
length: 8,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := testCase.value.AppendAll([]int{10, 20, 30})
assert.Equal(t, result.Length(), testCase.length, fmt.Sprintf("expected %d but value is %d", testCase.length, result.Length()))
})
}
}
func TestMapList(t *testing.T) {
var mapper = func(value int) string {
return strconv.Itoa(value)
}
testCases := []struct {
name string
value List[int]
expected List[string]
}{
{
name: "Empty List",
value: emptyList,
expected: Empty[string](),
},
{
name: "Single Element List",
value: singleElementList,
expected: Of[string]("10"),
},
{
name: "Multiple Elements List",
value: multipleElementsList,
expected: OfSlice[string]([]string{"1", "2", "3", "4", "5"}),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := MapList[int, string](testCase.value, mapper)
assert.Equal(t, result, testCase.expected, fmt.Sprintf("expected %+v but value is %+v", testCase.expected, result))
})
}
}
func TestFilterList(t *testing.T) {
testCases := []struct {
name string
value List[int]
expected List[int]
}{
{
name: "Empty List",
value: emptyList,
expected: Empty[int](),
},
{
name: "Single Element List",
value: singleElementList,
expected: Of[int](10),
},
{
name: "Multiple Elements List",
value: multipleElementsList,
expected: OfSlice[int]([]int{2, 4}),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := testCase.value.Filter(evenPredicate)
assert.Equal(t, result, testCase.expected, fmt.Sprintf("expected %+v but value is %+v", testCase.expected, result))
})
}
}
func TestRemoveFromList(t *testing.T) {
testCases := []struct {
name string
original List[int]
valueToRemove int
expected List[int]
}{
{
name: "Empty List",
original: emptyList,
valueToRemove: 10,
expected: Empty[int](),
},
{
name: "Single Element List with element removed",
original: singleElementList,
valueToRemove: 10,
expected: Empty[int](),
},
{
name: "Single Element List without element removed",
original: singleElementList,
valueToRemove: 5,
expected: Of[int](10),
},
{
name: "Multiple Elements List",
original: multipleElementsList,
valueToRemove: 3,
expected: OfSlice[int]([]int{1, 2, 4, 5}),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := testCase.original.Remove(testCase.valueToRemove)
assert.Equal(t, result, testCase.expected, fmt.Sprintf("expected %+v but value is %+v", testCase.expected, result))
})
}
}
func TestRemoveFromListWithPredicate(t *testing.T) {
testCases := []struct {
name string
original List[int]
predicate func(value int) bool
expected List[int]
}{
{
name: "Empty List",
original: emptyList,
predicate: evenPredicate,
expected: Empty[int](),
},
{
name: "Single Element List with element removed",
original: singleElementList,
predicate: evenPredicate,
expected: Empty[int](),
},
{
name: "Single Element List without element removed",
original: singleElementList,
predicate: func(value int) bool { return value == 5 },
expected: Of[int](10),
},
{
name: "Multiple Elements List",
original: multipleElementsList,
predicate: evenPredicate,
expected: OfSlice[int]([]int{1, 3, 5}),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := testCase.original.RemovePredicate(testCase.predicate)
assert.Equal(t, result, testCase.expected, fmt.Sprintf("expected %+v but value is %+v", testCase.expected, result))
})
}
}
func TestInsertInList(t *testing.T) {
testCases := []struct {
name string
original List[int]
index int
expected List[int]
checkError bool
errorMessage string
}{
{
name: "Empty List index 0",
original: emptyList,
index: 0,
expected: Of[int](7),
checkError: false,
},
{
name: "Empty List negative index",
original: emptyList,
index: -1,
expected: Empty[int](),
checkError: true,
errorMessage: "index out of range -1 on empty List",
},
{
name: "Empty List index > 0",
original: emptyList,
index: 1,
expected: Empty[int](),
checkError: true,
errorMessage: "index out of range 1 on empty List",
},
{
name: "Single Element List index 0",
original: singleElementList,
index: 0,
expected: OfSlice[int]([]int{7, 10}),
checkError: false,
},
{
name: "Single Element List index 1",
original: singleElementList,
index: 1,
expected: OfSlice[int]([]int{10, 7}),
checkError: false,
},
{
name: "Single Element List index > 1",
original: singleElementList,
index: 2,
expected: Empty[int](),
checkError: true,
errorMessage: "index out of range 1 on empty List",
},
{
name: "Single Element List index < 0",
original: singleElementList,
index: -1,
expected: Empty[int](),
checkError: true,
errorMessage: "index out of range -1 on List",
},
{
name: "Multiple Elements List index 0",
original: multipleElementsList,
index: 0,
expected: OfSlice[int]([]int{7, 1, 2, 3, 4, 5}),
checkError: false,
},
{
name: "Multiple Elements List index 3",
original: multipleElementsList,
index: 3,
expected: OfSlice[int]([]int{1, 2, 3, 7, 4, 5}),
checkError: false,
},
{
name: "Multiple Elements List index out of bounds",
original: multipleElementsList,
index: 7,
expected: Empty[int](),
checkError: true,
errorMessage: "index out of range 2 on empty List",
},
{
name: "Multiple Elements List negative index",
original: multipleElementsList,
index: -1,
expected: Empty[int](),
checkError: true,
errorMessage: "index out of range -1 on List",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result, err := testCase.original.Insert(testCase.index, 7)
if testCase.checkError {
assert.Error(t, err, testCase.errorMessage, "index of range error was expected")
if err == nil {
t.Errorf("index of range error was expected")
}
} else {
if result != testCase.expected {
t.Errorf("expected %+v but value is %+v", testCase.expected, result)
}
}
})
}
}
func TestReverseList(t *testing.T) {
testCases := []struct {
name string
original List[int]
expected List[int]
}{
{
name: "Empty List",
original: emptyList,
expected: Empty[int](),
},
{
name: "Single Element List",
original: singleElementList,
expected: Of[int](10),
},
{
name: "Multiple Elements List",
original: multipleElementsList,
expected: OfSlice[int]([]int{5, 4, 3, 2, 1}),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := testCase.original.Reverse()
assert.Equal(t, result, testCase.expected, fmt.Sprintf("expected %+v but value is %+v", testCase.expected, result))
})
}
}