Debian Codegraph @

lists/singlylinkedlist/singlylinkedlist_test.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package singlylinkedlist
6
7 import (
8 "cmp"
9 "encoding/json"
10 "slices"
11 "strings"
12 "testing"
13 )
14
15 func TestListNew(t *testing.T) {
16 list1 := New[int]()
17
18 if actualValue := list1.Empty(); actualValue != true {
19 t.Errorf("Got %v expected %v", actualValue, true)
20 }
21
22 list2 := New[int](1, 2)
23
24 if actualValue := list2.Size(); actualValue != 2 {
25 t.Errorf("Got %v expected %v", actualValue, 2)
26 }
27
28 if actualValue, ok := list2.Get(0); actualValue != 1 || !ok {
29 t.Errorf("Got %v expected %v", actualValue, 1)
30 }
31
32 if actualValue, ok := list2.Get(1); actualValue != 2 || !ok {
33 t.Errorf("Got %v expected %v", actualValue, 2)
34 }
35
36 if actualValue, ok := list2.Get(2); actualValue != 0 || ok {
37 t.Errorf("Got %v expected %v", actualValue, 0)
38 }
39 }
40
41 func TestListAdd(t *testing.T) {
42 list := New[string]()
43 list.Add("a")
44 list.Add("b", "c")
45 if actualValue := list.Empty(); actualValue != false {
46 t.Errorf("Got %v expected %v", actualValue, false)
47 }
48 if actualValue := list.Size(); actualValue != 3 {
49 t.Errorf("Got %v expected %v", actualValue, 3)
50 }
51 if actualValue, ok := list.Get(2); actualValue != "c" || !ok {
52 t.Errorf("Got %v expected %v", actualValue, "c")
53 }
54 }
55
56 func TestListAppendAndPrepend(t *testing.T) {
57 list := New[string]()
58 list.Add("b")
59 list.Prepend("a")
60 list.Append("c")
61 if actualValue := list.Empty(); actualValue != false {
62 t.Errorf("Got %v expected %v", actualValue, false)
63 }
64 if actualValue := list.Size(); actualValue != 3 {
65 t.Errorf("Got %v expected %v", actualValue, 3)
66 }
67 if actualValue, ok := list.Get(0); actualValue != "a" || !ok {
68 t.Errorf("Got %v expected %v", actualValue, "c")
69 }
70 if actualValue, ok := list.Get(1); actualValue != "b" || !ok {
71 t.Errorf("Got %v expected %v", actualValue, "c")
72 }
73 if actualValue, ok := list.Get(2); actualValue != "c" || !ok {
74 t.Errorf("Got %v expected %v", actualValue, "c")
75 }
76 }
77
78 func TestListRemove(t *testing.T) {
79 list := New[string]()
80 list.Add("a")
81 list.Add("b", "c")
82 list.Remove(2)
83 if actualValue, ok := list.Get(2); actualValue != "" || ok {
84 t.Errorf("Got %v expected %v", actualValue, "")
85 }
86 list.Remove(1)
87 list.Remove(0)
88 list.Remove(0) // no effect
89 if actualValue := list.Empty(); actualValue != true {
90 t.Errorf("Got %v expected %v", actualValue, true)
91 }
92 if actualValue := list.Size(); actualValue != 0 {
93 t.Errorf("Got %v expected %v", actualValue, 0)
94 }
95 }
96
97 func TestListGet(t *testing.T) {
98 list := New[string]()
99 list.Add("a")
100 list.Add("b", "c")
101 if actualValue, ok := list.Get(0); actualValue != "a" || !ok {
102 t.Errorf("Got %v expected %v", actualValue, "a")
103 }
104 if actualValue, ok := list.Get(1); actualValue != "b" || !ok {
105 t.Errorf("Got %v expected %v", actualValue, "b")
106 }
107 if actualValue, ok := list.Get(2); actualValue != "c" || !ok {
108 t.Errorf("Got %v expected %v", actualValue, "c")
109 }
110 if actualValue, ok := list.Get(3); actualValue != "" || ok {
111 t.Errorf("Got %v expected %v", actualValue, "")
112 }
113 list.Remove(0)
114 if actualValue, ok := list.Get(0); actualValue != "b" || !ok {
115 t.Errorf("Got %v expected %v", actualValue, "b")
116 }
117 }
118
119 func TestListSwap(t *testing.T) {
120 list := New[string]()
121 list.Add("a")
122 list.Add("b", "c")
123 list.Swap(0, 1)
124 if actualValue, ok := list.Get(0); actualValue != "b" || !ok {
125 t.Errorf("Got %v expected %v", actualValue, "b")
126 }
127 }
128
129 func TestListSort(t *testing.T) {
130 list := New[string]()
131 list.Sort(cmp.Compare[string])
132 list.Add("e", "f", "g", "a", "b", "c", "d")
133 list.Sort(cmp.Compare[string])
134 for i := 1; i < list.Size(); i++ {
135 a, _ := list.Get(i - 1)
136 b, _ := list.Get(i)
137 if a > b {
138 t.Errorf("Not sorted! %s > %s", a, b)
139 }
140 }
141 }
142
143 func TestListClear(t *testing.T) {
144 list := New[string]()
145 list.Add("e", "f", "g", "a", "b", "c", "d")
146 list.Clear()
147 if actualValue := list.Empty(); actualValue != true {
148 t.Errorf("Got %v expected %v", actualValue, true)
149 }
150 if actualValue := list.Size(); actualValue != 0 {
151 t.Errorf("Got %v expected %v", actualValue, 0)
152 }
153 }
154
155 func TestListContains(t *testing.T) {
156 list := New[string]()
157 list.Add("a")
158 list.Add("b", "c")
159 if actualValue := list.Contains("a"); actualValue != true {
160 t.Errorf("Got %v expected %v", actualValue, true)
161 }
162 if actualValue := list.Contains(""); actualValue != false {
163 t.Errorf("Got %v expected %v", actualValue, false)
164 }
165 if actualValue := list.Contains("a", "b", "c"); actualValue != true {
166 t.Errorf("Got %v expected %v", actualValue, true)
167 }
168 if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false {
169 t.Errorf("Got %v expected %v", actualValue, false)
170 }
171 list.Clear()
172 if actualValue := list.Contains("a"); actualValue != false {
173 t.Errorf("Got %v expected %v", actualValue, false)
174 }
175 if actualValue := list.Contains("a", "b", "c"); actualValue != false {
176 t.Errorf("Got %v expected %v", actualValue, false)
177 }
178 }
179
180 func TestListValues(t *testing.T) {
181 list := New[string]()
182 list.Add("a")
183 list.Add("b", "c")
184 if actualValue, expectedValue := list.Values(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) {
185 t.Errorf("Got %v expected %v", actualValue, expectedValue)
186 }
187 }
188
189 func TestListIndexOf(t *testing.T) {
190 list := New[string]()
191
192 expectedIndex := -1
193 if index := list.IndexOf("a"); index != expectedIndex {
194 t.Errorf("Got %v expected %v", index, expectedIndex)
195 }
196
197 list.Add("a")
198 list.Add("b", "c")
199
200 expectedIndex = 0
201 if index := list.IndexOf("a"); index != expectedIndex {
202 t.Errorf("Got %v expected %v", index, expectedIndex)
203 }
204
205 expectedIndex = 1
206 if index := list.IndexOf("b"); index != expectedIndex {
207 t.Errorf("Got %v expected %v", index, expectedIndex)
208 }
209
210 expectedIndex = 2
211 if index := list.IndexOf("c"); index != expectedIndex {
212 t.Errorf("Got %v expected %v", index, expectedIndex)
213 }
214 }
215
216 func TestListInsert(t *testing.T) {
217 list := New[string]()
218 list.Insert(0, "b", "c")
219 list.Insert(0, "a")
220 list.Insert(10, "x") // ignore
221 if actualValue := list.Size(); actualValue != 3 {
222 t.Errorf("Got %v expected %v", actualValue, 3)
223 }
224 list.Insert(3, "d") // append
225 if actualValue := list.Size(); actualValue != 4 {
226 t.Errorf("Got %v expected %v", actualValue, 4)
227 }
228 if actualValue, expectedValue := strings.Join(list.Values(), ""), "abcd"; actualValue != expectedValue {
229 t.Errorf("Got %v expected %v", actualValue, expectedValue)
230 }
231 }
232
233 func TestListSet(t *testing.T) {
234 list := New[string]()
235 list.Set(0, "a")
236 list.Set(1, "b")
237 if actualValue := list.Size(); actualValue != 2 {
238 t.Errorf("Got %v expected %v", actualValue, 2)
239 }
240 list.Set(2, "c") // append
241 if actualValue := list.Size(); actualValue != 3 {
242 t.Errorf("Got %v expected %v", actualValue, 3)
243 }
244 list.Set(4, "d") // ignore
245 list.Set(1, "bb") // update
246 if actualValue := list.Size(); actualValue != 3 {
247 t.Errorf("Got %v expected %v", actualValue, 3)
248 }
249 if actualValue, expectedValue := list.Values(), []string{"a", "bb", "c"}; !slices.Equal(actualValue, expectedValue) {
250 t.Errorf("Got %v expected %v", actualValue, expectedValue)
251 }
252 }
253
254 func TestListEach(t *testing.T) {
255 list := New[string]()
256 list.Add("a", "b", "c")
257 list.Each(func(index int, value string) {
258 switch index {
259 case 0:
260 if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
261 t.Errorf("Got %v expected %v", actualValue, expectedValue)
262 }
263 case 1:
264 if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
265 t.Errorf("Got %v expected %v", actualValue, expectedValue)
266 }
267 case 2:
268 if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
269 t.Errorf("Got %v expected %v", actualValue, expectedValue)
270 }
271 default:
272 t.Errorf("Too many")
273 }
274 })
275 }
276
277 func TestListMap(t *testing.T) {
278 list := New[string]()
279 list.Add("a", "b", "c")
280 mappedList := list.Map(func(index int, value string) string {
281 return "mapped: " + value
282 })
283 if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" {
284 t.Errorf("Got %v expected %v", actualValue, "mapped: a")
285 }
286 if actualValue, _ := mappedList.Get(1); actualValue != "mapped: b" {
287 t.Errorf("Got %v expected %v", actualValue, "mapped: b")
288 }
289 if actualValue, _ := mappedList.Get(2); actualValue != "mapped: c" {
290 t.Errorf("Got %v expected %v", actualValue, "mapped: c")
291 }
292 if mappedList.Size() != 3 {
293 t.Errorf("Got %v expected %v", mappedList.Size(), 3)
294 }
295 }
296
297 func TestListSelect(t *testing.T) {
298 list := New[string]()
299 list.Add("a", "b", "c")
300 selectedList := list.Select(func(index int, value string) bool {
301 return value >= "a" && value <= "b"
302 })
303 if actualValue, _ := selectedList.Get(0); actualValue != "a" {
304 t.Errorf("Got %v expected %v", actualValue, "value: a")
305 }
306 if actualValue, _ := selectedList.Get(1); actualValue != "b" {
307 t.Errorf("Got %v expected %v", actualValue, "value: b")
308 }
309 if selectedList.Size() != 2 {
310 t.Errorf("Got %v expected %v", selectedList.Size(), 3)
311 }
312 }
313
314 func TestListAny(t *testing.T) {
315 list := New[string]()
316 list.Add("a", "b", "c")
317 any := list.Any(func(index int, value string) bool {
318 return value == "c"
319 })
320 if any != true {
321 t.Errorf("Got %v expected %v", any, true)
322 }
323 any = list.Any(func(index int, value string) bool {
324 return value == "x"
325 })
326 if any != false {
327 t.Errorf("Got %v expected %v", any, false)
328 }
329 }
330 func TestListAll(t *testing.T) {
331 list := New[string]()
332 list.Add("a", "b", "c")
333 all := list.All(func(index int, value string) bool {
334 return value >= "a" && value <= "c"
335 })
336 if all != true {
337 t.Errorf("Got %v expected %v", all, true)
338 }
339 all = list.All(func(index int, value string) bool {
340 return value >= "a" && value <= "b"
341 })
342 if all != false {
343 t.Errorf("Got %v expected %v", all, false)
344 }
345 }
346 func TestListFind(t *testing.T) {
347 list := New[string]()
348 list.Add("a", "b", "c")
349 foundIndex, foundValue := list.Find(func(index int, value string) bool {
350 return value == "c"
351 })
352 if foundValue != "c" || foundIndex != 2 {
353 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
354 }
355 foundIndex, foundValue = list.Find(func(index int, value string) bool {
356 return value == "x"
357 })
358 if foundValue != "" || foundIndex != -1 {
359 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
360 }
361 }
362 func TestListChaining(t *testing.T) {
363 list := New[string]()
364 list.Add("a", "b", "c")
365 chainedList := list.Select(func(index int, value string) bool {
366 return value > "a"
367 }).Map(func(index int, value string) string {
368 return value + value
369 })
370 if chainedList.Size() != 2 {
371 t.Errorf("Got %v expected %v", chainedList.Size(), 2)
372 }
373 if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok {
374 t.Errorf("Got %v expected %v", actualValue, "b")
375 }
376 if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok {
377 t.Errorf("Got %v expected %v", actualValue, "c")
378 }
379 }
380
381 func TestListIteratorNextOnEmpty(t *testing.T) {
382 list := New[string]()
383 it := list.Iterator()
384 for it.Next() {
385 t.Errorf("Shouldn't iterate on empty list")
386 }
387 }
388
389 func TestListIteratorNext(t *testing.T) {
390 list := New[string]()
391 list.Add("a", "b", "c")
392 it := list.Iterator()
393 count := 0
394 for it.Next() {
395 count++
396 index := it.Index()
397 value := it.Value()
398 switch index {
399 case 0:
400 if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
401 t.Errorf("Got %v expected %v", actualValue, expectedValue)
402 }
403 case 1:
404 if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
405 t.Errorf("Got %v expected %v", actualValue, expectedValue)
406 }
407 case 2:
408 if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
409 t.Errorf("Got %v expected %v", actualValue, expectedValue)
410 }
411 default:
412 t.Errorf("Too many")
413 }
414 }
415 if actualValue, expectedValue := count, 3; actualValue != expectedValue {
416 t.Errorf("Got %v expected %v", actualValue, expectedValue)
417 }
418 }
419
420 func TestListIteratorBegin(t *testing.T) {
421 list := New[string]()
422 it := list.Iterator()
423 it.Begin()
424 list.Add("a", "b", "c")
425 for it.Next() {
426 }
427 it.Begin()
428 it.Next()
429 if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
430 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
431 }
432 }
433
434 func TestListIteratorFirst(t *testing.T) {
435 list := New[string]()
436 it := list.Iterator()
437 if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
438 t.Errorf("Got %v expected %v", actualValue, expectedValue)
439 }
440 list.Add("a", "b", "c")
441 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
442 t.Errorf("Got %v expected %v", actualValue, expectedValue)
443 }
444 if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
445 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
446 }
447 }
448
449 func TestListIteratorNextTo(t *testing.T) {
450 // Sample seek function, i.e. string starting with "b"
451 seek := func(index int, value string) bool {
452 return strings.HasSuffix(value, "b")
453 }
454
455 // NextTo (empty)
456 {
457 list := New[string]()
458 it := list.Iterator()
459 for it.NextTo(seek) {
460 t.Errorf("Shouldn't iterate on empty list")
461 }
462 }
463
464 // NextTo (not found)
465 {
466 list := New[string]()
467 list.Add("xx", "yy")
468 it := list.Iterator()
469 for it.NextTo(seek) {
470 t.Errorf("Shouldn't iterate on empty list")
471 }
472 }
473
474 // NextTo (found)
475 {
476 list := New[string]()
477 list.Add("aa", "bb", "cc")
478 it := list.Iterator()
479 it.Begin()
480 if !it.NextTo(seek) {
481 t.Errorf("Shouldn't iterate on empty list")
482 }
483 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
484 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
485 }
486 if !it.Next() {
487 t.Errorf("Should go to first element")
488 }
489 if index, value := it.Index(), it.Value(); index != 2 || value != "cc" {
490 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
491 }
492 if it.Next() {
493 t.Errorf("Should not go past last element")
494 }
495 }
496 }
497
498 func TestListSerialization(t *testing.T) {
499 list := New[string]()
500 list.Add("a", "b", "c")
501
502 var err error
503 assert := func() {
504 if actualValue, expectedValue := list.Values(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) {
505 t.Errorf("Got %v expected %v", actualValue, expectedValue)
506 }
507 if actualValue, expectedValue := list.Size(), 3; actualValue != expectedValue {
508 t.Errorf("Got %v expected %v", actualValue, expectedValue)
509 }
510 if err != nil {
511 t.Errorf("Got error %v", err)
512 }
513 }
514
515 assert()
516
517 bytes, err := list.ToJSON()
518 assert()
519
520 err = list.FromJSON(bytes)
521 assert()
522
523 bytes, err = json.Marshal([]any{"a", "b", "c", list})
524 if err != nil {
525 t.Errorf("Got error %v", err)
526 }
527
528 err = json.Unmarshal([]byte(`["a","b","c"]`), &list)
529 if err != nil {
530 t.Errorf("Got error %v", err)
531 }
532 assert()
533 }
534
535 func TestListString(t *testing.T) {
536 c := New[int]()
537 c.Add(1)
538 if !strings.HasPrefix(c.String(), "SinglyLinkedList") {
539 t.Errorf("String should start with container name")
540 }
541 }
542
543 func benchmarkGet(b *testing.B, list *List[int], size int) {
544 for i := 0; i < b.N; i++ {
545 for n := 0; n < size; n++ {
546 list.Get(n)
547 }
548 }
549 }
550
551 func benchmarkAdd(b *testing.B, list *List[int], size int) {
552 for i := 0; i < b.N; i++ {
553 for n := 0; n < size; n++ {
554 list.Add(n)
555 }
556 }
557 }
558
559 func benchmarkRemove(b *testing.B, list *List[int], size int) {
560 for i := 0; i < b.N; i++ {
561 for n := 0; n < size; n++ {
562 list.Remove(n)
563 }
564 }
565 }
566
567 func BenchmarkSinglyLinkedListGet100(b *testing.B) {
568 b.StopTimer()
569 size := 100
570 list := New[int]()
571 for n := 0; n < size; n++ {
572 list.Add(n)
573 }
574 b.StartTimer()
575 benchmarkGet(b, list, size)
576 }
577
578 func BenchmarkSinglyLinkedListGet1000(b *testing.B) {
579 b.StopTimer()
580 size := 1000
581 list := New[int]()
582 for n := 0; n < size; n++ {
583 list.Add(n)
584 }
585 b.StartTimer()
586 benchmarkGet(b, list, size)
587 }
588
589 func BenchmarkSinglyLinkedListGet10000(b *testing.B) {
590 b.StopTimer()
591 size := 10000
592 list := New[int]()
593 for n := 0; n < size; n++ {
594 list.Add(n)
595 }
596 b.StartTimer()
597 benchmarkGet(b, list, size)
598 }
599
600 func BenchmarkSinglyLinkedListGet100000(b *testing.B) {
601 b.StopTimer()
602 size := 100000
603 list := New[int]()
604 for n := 0; n < size; n++ {
605 list.Add(n)
606 }
607 b.StartTimer()
608 benchmarkGet(b, list, size)
609 }
610
611 func BenchmarkSinglyLinkedListAdd100(b *testing.B) {
612 b.StopTimer()
613 size := 100
614 list := New[int]()
615 b.StartTimer()
616 benchmarkAdd(b, list, size)
617 }
618
619 func BenchmarkSinglyLinkedListAdd1000(b *testing.B) {
620 b.StopTimer()
621 size := 1000
622 list := New[int]()
623 for n := 0; n < size; n++ {
624 list.Add(n)
625 }
626 b.StartTimer()
627 benchmarkAdd(b, list, size)
628 }
629
630 func BenchmarkSinglyLinkedListAdd10000(b *testing.B) {
631 b.StopTimer()
632 size := 10000
633 list := New[int]()
634 for n := 0; n < size; n++ {
635 list.Add(n)
636 }
637 b.StartTimer()
638 benchmarkAdd(b, list, size)
639 }
640
641 func BenchmarkSinglyLinkedListAdd100000(b *testing.B) {
642 b.StopTimer()
643 size := 100000
644 list := New[int]()
645 for n := 0; n < size; n++ {
646 list.Add(n)
647 }
648 b.StartTimer()
649 benchmarkAdd(b, list, size)
650 }
651
652 func BenchmarkSinglyLinkedListRemove100(b *testing.B) {
653 b.StopTimer()
654 size := 100
655 list := New[int]()
656 for n := 0; n < size; n++ {
657 list.Add(n)
658 }
659 b.StartTimer()
660 benchmarkRemove(b, list, size)
661 }
662
663 func BenchmarkSinglyLinkedListRemove1000(b *testing.B) {
664 b.StopTimer()
665 size := 1000
666 list := New[int]()
667 for n := 0; n < size; n++ {
668 list.Add(n)
669 }
670 b.StartTimer()
671 benchmarkRemove(b, list, size)
672 }
673
674 func BenchmarkSinglyLinkedListRemove10000(b *testing.B) {
675 b.StopTimer()
676 size := 10000
677 list := New[int]()
678 for n := 0; n < size; n++ {
679 list.Add(n)
680 }
681 b.StartTimer()
682 benchmarkRemove(b, list, size)
683 }
684
685 func BenchmarkSinglyLinkedListRemove100000(b *testing.B) {
686 b.StopTimer()
687 size := 100000
688 list := New[int]()
689 for n := 0; n < size; n++ {
690 list.Add(n)
691 }
692 b.StartTimer()
693 benchmarkRemove(b, list, size)
694 }