Debian Codegraph @

lists/doublylinkedlist/doublylinkedlist_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 doublylinkedlist
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 TestListInsert(t *testing.T) {
190 list := New[string]()
191 list.Insert(0, "b", "c", "d")
192 list.Insert(0, "a")
193 list.Insert(10, "x") // ignore
194 if actualValue := list.Size(); actualValue != 4 {
195 t.Errorf("Got %v expected %v", actualValue, 4)
196 }
197 list.Insert(4, "g") // append
198 if actualValue := list.Size(); actualValue != 5 {
199 t.Errorf("Got %v expected %v", actualValue, 5)
200 }
201 if actualValue, expectedValue := strings.Join(list.Values(), ""), "abcdg"; actualValue != expectedValue {
202 t.Errorf("Got %v expected %v", actualValue, expectedValue)
203 }
204 list.Insert(4, "e", "f") // last to first traversal
205 if actualValue := list.Size(); actualValue != 7 {
206 t.Errorf("Got %v expected %v", actualValue, 7)
207 }
208 if actualValue, expectedValue := strings.Join(list.Values(), ""), "abcdefg"; actualValue != expectedValue {
209 t.Errorf("Got %v expected %v", actualValue, expectedValue)
210 }
211 }
212
213 func TestListSet(t *testing.T) {
214 list := New[string]()
215 list.Set(0, "a")
216 list.Set(1, "b")
217 if actualValue := list.Size(); actualValue != 2 {
218 t.Errorf("Got %v expected %v", actualValue, 2)
219 }
220 list.Set(2, "c") // append
221 if actualValue := list.Size(); actualValue != 3 {
222 t.Errorf("Got %v expected %v", actualValue, 3)
223 }
224 list.Set(4, "d") // ignore
225 list.Set(1, "bb") // update
226 if actualValue := list.Size(); actualValue != 3 {
227 t.Errorf("Got %v expected %v", actualValue, 3)
228 }
229 if actualValue, expectedValue := list.Values(), []string{"a", "bb", "c"}; !slices.Equal(actualValue, expectedValue) {
230 t.Errorf("Got %v expected %v", actualValue, expectedValue)
231 }
232 }
233
234 func TestListEach(t *testing.T) {
235 list := New[string]()
236 list.Add("a", "b", "c")
237 list.Each(func(index int, value string) {
238 switch index {
239 case 0:
240 if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
241 t.Errorf("Got %v expected %v", actualValue, expectedValue)
242 }
243 case 1:
244 if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
245 t.Errorf("Got %v expected %v", actualValue, expectedValue)
246 }
247 case 2:
248 if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
249 t.Errorf("Got %v expected %v", actualValue, expectedValue)
250 }
251 default:
252 t.Errorf("Too many")
253 }
254 })
255 }
256
257 func TestListMap(t *testing.T) {
258 list := New[string]()
259 list.Add("a", "b", "c")
260 mappedList := list.Map(func(index int, value string) string {
261 return "mapped: " + value
262 })
263 if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" {
264 t.Errorf("Got %v expected %v", actualValue, "mapped: a")
265 }
266 if actualValue, _ := mappedList.Get(1); actualValue != "mapped: b" {
267 t.Errorf("Got %v expected %v", actualValue, "mapped: b")
268 }
269 if actualValue, _ := mappedList.Get(2); actualValue != "mapped: c" {
270 t.Errorf("Got %v expected %v", actualValue, "mapped: c")
271 }
272 if mappedList.Size() != 3 {
273 t.Errorf("Got %v expected %v", mappedList.Size(), 3)
274 }
275 }
276
277 func TestListSelect(t *testing.T) {
278 list := New[string]()
279 list.Add("a", "b", "c")
280 selectedList := list.Select(func(index int, value string) bool {
281 return value >= "a" && value <= "b"
282 })
283 if actualValue, _ := selectedList.Get(0); actualValue != "a" {
284 t.Errorf("Got %v expected %v", actualValue, "value: a")
285 }
286 if actualValue, _ := selectedList.Get(1); actualValue != "b" {
287 t.Errorf("Got %v expected %v", actualValue, "value: b")
288 }
289 if selectedList.Size() != 2 {
290 t.Errorf("Got %v expected %v", selectedList.Size(), 3)
291 }
292 }
293
294 func TestListAny(t *testing.T) {
295 list := New[string]()
296 list.Add("a", "b", "c")
297 any := list.Any(func(index int, value string) bool {
298 return value == "c"
299 })
300 if any != true {
301 t.Errorf("Got %v expected %v", any, true)
302 }
303 any = list.Any(func(index int, value string) bool {
304 return value == "x"
305 })
306 if any != false {
307 t.Errorf("Got %v expected %v", any, false)
308 }
309 }
310 func TestListAll(t *testing.T) {
311 list := New[string]()
312 list.Add("a", "b", "c")
313 all := list.All(func(index int, value string) bool {
314 return value >= "a" && value <= "c"
315 })
316 if all != true {
317 t.Errorf("Got %v expected %v", all, true)
318 }
319 all = list.All(func(index int, value string) bool {
320 return value >= "a" && value <= "b"
321 })
322 if all != false {
323 t.Errorf("Got %v expected %v", all, false)
324 }
325 }
326 func TestListFind(t *testing.T) {
327 list := New[string]()
328 list.Add("a", "b", "c")
329 foundIndex, foundValue := list.Find(func(index int, value string) bool {
330 return value == "c"
331 })
332 if foundValue != "c" || foundIndex != 2 {
333 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
334 }
335 foundIndex, foundValue = list.Find(func(index int, value string) bool {
336 return value == "x"
337 })
338 if foundValue != "" || foundIndex != -1 {
339 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
340 }
341 }
342 func TestListChaining(t *testing.T) {
343 list := New[string]()
344 list.Add("a", "b", "c")
345 chainedList := list.Select(func(index int, value string) bool {
346 return value > "a"
347 }).Map(func(index int, value string) string {
348 return value + value
349 })
350 if chainedList.Size() != 2 {
351 t.Errorf("Got %v expected %v", chainedList.Size(), 2)
352 }
353 if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok {
354 t.Errorf("Got %v expected %v", actualValue, "b")
355 }
356 if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok {
357 t.Errorf("Got %v expected %v", actualValue, "c")
358 }
359 }
360
361 func TestListIteratorNextOnEmpty(t *testing.T) {
362 list := New[string]()
363 it := list.Iterator()
364 for it.Next() {
365 t.Errorf("Shouldn't iterate on empty list")
366 }
367 }
368
369 func TestListIteratorNext(t *testing.T) {
370 list := New[string]()
371 list.Add("a", "b", "c")
372 it := list.Iterator()
373 count := 0
374 for it.Next() {
375 count++
376 index := it.Index()
377 value := it.Value()
378 switch index {
379 case 0:
380 if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
381 t.Errorf("Got %v expected %v", actualValue, expectedValue)
382 }
383 case 1:
384 if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
385 t.Errorf("Got %v expected %v", actualValue, expectedValue)
386 }
387 case 2:
388 if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
389 t.Errorf("Got %v expected %v", actualValue, expectedValue)
390 }
391 default:
392 t.Errorf("Too many")
393 }
394 }
395 if actualValue, expectedValue := count, 3; actualValue != expectedValue {
396 t.Errorf("Got %v expected %v", actualValue, expectedValue)
397 }
398 }
399
400 func TestListIteratorPrevOnEmpty(t *testing.T) {
401 list := New[string]()
402 it := list.Iterator()
403 for it.Prev() {
404 t.Errorf("Shouldn't iterate on empty list")
405 }
406 }
407
408 func TestListIteratorPrev(t *testing.T) {
409 list := New[string]()
410 list.Add("a", "b", "c")
411 it := list.Iterator()
412 for it.Next() {
413 }
414 count := 0
415 for it.Prev() {
416 count++
417 index := it.Index()
418 value := it.Value()
419 switch index {
420 case 0:
421 if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
422 t.Errorf("Got %v expected %v", actualValue, expectedValue)
423 }
424 case 1:
425 if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
426 t.Errorf("Got %v expected %v", actualValue, expectedValue)
427 }
428 case 2:
429 if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
430 t.Errorf("Got %v expected %v", actualValue, expectedValue)
431 }
432 default:
433 t.Errorf("Too many")
434 }
435 }
436 if actualValue, expectedValue := count, 3; actualValue != expectedValue {
437 t.Errorf("Got %v expected %v", actualValue, expectedValue)
438 }
439 }
440
441 func TestListIteratorBegin(t *testing.T) {
442 list := New[string]()
443 it := list.Iterator()
444 it.Begin()
445 list.Add("a", "b", "c")
446 for it.Next() {
447 }
448 it.Begin()
449 it.Next()
450 if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
451 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
452 }
453 }
454
455 func TestListIteratorEnd(t *testing.T) {
456 list := New[string]()
457 it := list.Iterator()
458
459 if index := it.Index(); index != -1 {
460 t.Errorf("Got %v expected %v", index, -1)
461 }
462
463 it.End()
464 if index := it.Index(); index != 0 {
465 t.Errorf("Got %v expected %v", index, 0)
466 }
467
468 list.Add("a", "b", "c")
469 it.End()
470 if index := it.Index(); index != list.Size() {
471 t.Errorf("Got %v expected %v", index, list.Size())
472 }
473
474 it.Prev()
475 if index, value := it.Index(), it.Value(); index != list.Size()-1 || value != "c" {
476 t.Errorf("Got %v,%v expected %v,%v", index, value, list.Size()-1, "c")
477 }
478 }
479
480 func TestListIteratorFirst(t *testing.T) {
481 list := New[string]()
482 it := list.Iterator()
483 if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
484 t.Errorf("Got %v expected %v", actualValue, expectedValue)
485 }
486 list.Add("a", "b", "c")
487 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
488 t.Errorf("Got %v expected %v", actualValue, expectedValue)
489 }
490 if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
491 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
492 }
493 }
494
495 func TestListIteratorLast(t *testing.T) {
496 list := New[string]()
497 it := list.Iterator()
498 if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue {
499 t.Errorf("Got %v expected %v", actualValue, expectedValue)
500 }
501 list.Add("a", "b", "c")
502 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
503 t.Errorf("Got %v expected %v", actualValue, expectedValue)
504 }
505 if index, value := it.Index(), it.Value(); index != 2 || value != "c" {
506 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "c")
507 }
508 }
509
510 func TestListIteratorNextTo(t *testing.T) {
511 // Sample seek function, i.e. string starting with "b"
512 seek := func(index int, value string) bool {
513 return strings.HasSuffix(value, "b")
514 }
515
516 // NextTo (empty)
517 {
518 list := New[string]()
519 it := list.Iterator()
520 for it.NextTo(seek) {
521 t.Errorf("Shouldn't iterate on empty list")
522 }
523 }
524
525 // NextTo (not found)
526 {
527 list := New[string]()
528 list.Add("xx", "yy")
529 it := list.Iterator()
530 for it.NextTo(seek) {
531 t.Errorf("Shouldn't iterate on empty list")
532 }
533 }
534
535 // NextTo (found)
536 {
537 list := New[string]()
538 list.Add("aa", "bb", "cc")
539 it := list.Iterator()
540 it.Begin()
541 if !it.NextTo(seek) {
542 t.Errorf("Shouldn't iterate on empty list")
543 }
544 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
545 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
546 }
547 if !it.Next() {
548 t.Errorf("Should go to first element")
549 }
550 if index, value := it.Index(), it.Value(); index != 2 || value != "cc" {
551 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
552 }
553 if it.Next() {
554 t.Errorf("Should not go past last element")
555 }
556 }
557 }
558
559 func TestListIteratorPrevTo(t *testing.T) {
560 // Sample seek function, i.e. string starting with "b"
561 seek := func(index int, value string) bool {
562 return strings.HasSuffix(value, "b")
563 }
564
565 // PrevTo (empty)
566 {
567 list := New[string]()
568 it := list.Iterator()
569 it.End()
570 for it.PrevTo(seek) {
571 t.Errorf("Shouldn't iterate on empty list")
572 }
573 }
574
575 // PrevTo (not found)
576 {
577 list := New[string]()
578 list.Add("xx", "yy")
579 it := list.Iterator()
580 it.End()
581 for it.PrevTo(seek) {
582 t.Errorf("Shouldn't iterate on empty list")
583 }
584 }
585
586 // PrevTo (found)
587 {
588 list := New[string]()
589 list.Add("aa", "bb", "cc")
590 it := list.Iterator()
591 it.End()
592 if !it.PrevTo(seek) {
593 t.Errorf("Shouldn't iterate on empty list")
594 }
595 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
596 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
597 }
598 if !it.Prev() {
599 t.Errorf("Should go to first element")
600 }
601 if index, value := it.Index(), it.Value(); index != 0 || value != "aa" {
602 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
603 }
604 if it.Prev() {
605 t.Errorf("Should not go before first element")
606 }
607 }
608 }
609
610 func TestListSerialization(t *testing.T) {
611 list := New[string]()
612 list.Add("a", "b", "c")
613
614 var err error
615 assert := func() {
616 if actualValue, expectedValue := list.Values(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) {
617 t.Errorf("Got %v expected %v", actualValue, expectedValue)
618 }
619 if actualValue, expectedValue := list.Size(), 3; actualValue != expectedValue {
620 t.Errorf("Got %v expected %v", actualValue, expectedValue)
621 }
622 if err != nil {
623 t.Errorf("Got error %v", err)
624 }
625 }
626
627 assert()
628
629 bytes, err := list.ToJSON()
630 assert()
631
632 err = list.FromJSON(bytes)
633 assert()
634
635 bytes, err = json.Marshal([]any{"a", "b", "c", list})
636 if err != nil {
637 t.Errorf("Got error %v", err)
638 }
639
640 err = json.Unmarshal([]byte(`["a","b","c"]`), &list)
641 if err != nil {
642 t.Errorf("Got error %v", err)
643 }
644 assert()
645 }
646
647 func TestListString(t *testing.T) {
648 c := New[int]()
649 c.Add(1)
650 if !strings.HasPrefix(c.String(), "DoublyLinkedList") {
651 t.Errorf("String should start with container name")
652 }
653 }
654
655 func benchmarkGet(b *testing.B, list *List[int], size int) {
656 for i := 0; i < b.N; i++ {
657 for n := 0; n < size; n++ {
658 list.Get(n)
659 }
660 }
661 }
662
663 func benchmarkAdd(b *testing.B, list *List[int], size int) {
664 for i := 0; i < b.N; i++ {
665 for n := 0; n < size; n++ {
666 list.Add(n)
667 }
668 }
669 }
670
671 func benchmarkRemove(b *testing.B, list *List[int], size int) {
672 for i := 0; i < b.N; i++ {
673 for n := 0; n < size; n++ {
674 list.Remove(n)
675 }
676 }
677 }
678
679 func BenchmarkDoublyLinkedListGet100(b *testing.B) {
680 b.StopTimer()
681 size := 100
682 list := New[int]()
683 for n := 0; n < size; n++ {
684 list.Add(n)
685 }
686 b.StartTimer()
687 benchmarkGet(b, list, size)
688 }
689
690 func BenchmarkDoublyLinkedListGet1000(b *testing.B) {
691 b.StopTimer()
692 size := 1000
693 list := New[int]()
694 for n := 0; n < size; n++ {
695 list.Add(n)
696 }
697 b.StartTimer()
698 benchmarkGet(b, list, size)
699 }
700
701 func BenchmarkDoublyLinkedListGet10000(b *testing.B) {
702 b.StopTimer()
703 size := 10000
704 list := New[int]()
705 for n := 0; n < size; n++ {
706 list.Add(n)
707 }
708 b.StartTimer()
709 benchmarkGet(b, list, size)
710 }
711
712 func BenchmarkDoublyLinkedListGet100000(b *testing.B) {
713 b.StopTimer()
714 size := 100000
715 list := New[int]()
716 for n := 0; n < size; n++ {
717 list.Add(n)
718 }
719 b.StartTimer()
720 benchmarkGet(b, list, size)
721 }
722
723 func BenchmarkDoublyLinkedListAdd100(b *testing.B) {
724 b.StopTimer()
725 size := 100
726 list := New[int]()
727 b.StartTimer()
728 benchmarkAdd(b, list, size)
729 }
730
731 func BenchmarkDoublyLinkedListAdd1000(b *testing.B) {
732 b.StopTimer()
733 size := 1000
734 list := New[int]()
735 for n := 0; n < size; n++ {
736 list.Add(n)
737 }
738 b.StartTimer()
739 benchmarkAdd(b, list, size)
740 }
741
742 func BenchmarkDoublyLinkedListAdd10000(b *testing.B) {
743 b.StopTimer()
744 size := 10000
745 list := New[int]()
746 for n := 0; n < size; n++ {
747 list.Add(n)
748 }
749 b.StartTimer()
750 benchmarkAdd(b, list, size)
751 }
752
753 func BenchmarkDoublyLinkedListAdd100000(b *testing.B) {
754 b.StopTimer()
755 size := 100000
756 list := New[int]()
757 for n := 0; n < size; n++ {
758 list.Add(n)
759 }
760 b.StartTimer()
761 benchmarkAdd(b, list, size)
762 }
763
764 func BenchmarkDoublyLinkedListRemove100(b *testing.B) {
765 b.StopTimer()
766 size := 100
767 list := New[int]()
768 for n := 0; n < size; n++ {
769 list.Add(n)
770 }
771 b.StartTimer()
772 benchmarkRemove(b, list, size)
773 }
774
775 func BenchmarkDoublyLinkedListRemove1000(b *testing.B) {
776 b.StopTimer()
777 size := 1000
778 list := New[int]()
779 for n := 0; n < size; n++ {
780 list.Add(n)
781 }
782 b.StartTimer()
783 benchmarkRemove(b, list, size)
784 }
785
786 func BenchmarkDoublyLinkedListRemove10000(b *testing.B) {
787 b.StopTimer()
788 size := 10000
789 list := New[int]()
790 for n := 0; n < size; n++ {
791 list.Add(n)
792 }
793 b.StartTimer()
794 benchmarkRemove(b, list, size)
795 }
796
797 func BenchmarkDoublyLinkedListRemove100000(b *testing.B) {
798 b.StopTimer()
799 size := 100000
800 list := New[int]()
801 for n := 0; n < size; n++ {
802 list.Add(n)
803 }
804 b.StartTimer()
805 benchmarkRemove(b, list, size)
806 }