Debian Codegraph @

trees/redblacktree/redblacktree_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 redblacktree
6
7 import (
8 "encoding/json"
9 "fmt"
10 "slices"
11 "strings"
12 "testing"
13 )
14
15 func TestRedBlackTreeGet(t *testing.T) {
16 tree := New[int, string]()
17
18 if actualValue := tree.Size(); actualValue != 0 {
19 t.Errorf("Got %v expected %v", actualValue, 0)
20 }
21
22 if actualValue := tree.GetNode(2).Size(); actualValue != 0 {
23 t.Errorf("Got %v expected %v", actualValue, 0)
24 }
25
26 tree.Put(1, "x") // 1->x
27 tree.Put(2, "b") // 1->x, 2->b (in order)
28 tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
29 tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
30 tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
31 tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
32 tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
33
34 fmt.Println(tree)
35 //
36 // RedBlackTree
37 // │ ┌── 6
38 // │ ┌── 5
39 // │ ┌── 4
40 // │ │ └── 3
41 // └── 2
42 // └── 1
43
44 if actualValue := tree.Size(); actualValue != 6 {
45 t.Errorf("Got %v expected %v", actualValue, 6)
46 }
47
48 if actualValue := tree.GetNode(4).Size(); actualValue != 4 {
49 t.Errorf("Got %v expected %v", actualValue, 4)
50 }
51
52 if actualValue := tree.GetNode(2).Size(); actualValue != 6 {
53 t.Errorf("Got %v expected %v", actualValue, 6)
54 }
55
56 if actualValue := tree.GetNode(8).Size(); actualValue != 0 {
57 t.Errorf("Got %v expected %v", actualValue, 0)
58 }
59 }
60
61 func TestRedBlackTreePut(t *testing.T) {
62 tree := New[int, string]()
63 tree.Put(5, "e")
64 tree.Put(6, "f")
65 tree.Put(7, "g")
66 tree.Put(3, "c")
67 tree.Put(4, "d")
68 tree.Put(1, "x")
69 tree.Put(2, "b")
70 tree.Put(1, "a") //overwrite
71
72 if actualValue := tree.Size(); actualValue != 7 {
73 t.Errorf("Got %v expected %v", actualValue, 7)
74 }
75 if actualValue, expectedValue := tree.Keys(), []int{1, 2, 3, 4, 5, 6, 7}; !slices.Equal(actualValue, expectedValue) {
76 t.Errorf("Got %v expected %v", actualValue, expectedValue)
77 }
78 if actualValue, expectedValue := tree.Values(), []string{"a", "b", "c", "d", "e", "f", "g"}; !slices.Equal(actualValue, expectedValue) {
79 t.Errorf("Got %v expected %v", actualValue, expectedValue)
80 }
81
82 tests1 := [][]interface{}{
83 {1, "a", true},
84 {2, "b", true},
85 {3, "c", true},
86 {4, "d", true},
87 {5, "e", true},
88 {6, "f", true},
89 {7, "g", true},
90 {8, "", false},
91 }
92
93 for _, test := range tests1 {
94 // retrievals
95 actualValue, actualFound := tree.Get(test[0].(int))
96 if actualValue != test[1] || actualFound != test[2] {
97 t.Errorf("Got %v expected %v", actualValue, test[1])
98 }
99 }
100 }
101
102 func TestRedBlackTreeRemove(t *testing.T) {
103 tree := New[int, string]()
104 tree.Put(5, "e")
105 tree.Put(6, "f")
106 tree.Put(7, "g")
107 tree.Put(3, "c")
108 tree.Put(4, "d")
109 tree.Put(1, "x")
110 tree.Put(2, "b")
111 tree.Put(1, "a") //overwrite
112
113 tree.Remove(5)
114 tree.Remove(6)
115 tree.Remove(7)
116 tree.Remove(8)
117 tree.Remove(5)
118
119 if actualValue, expectedValue := tree.Keys(), []int{1, 2, 3, 4}; !slices.Equal(actualValue, expectedValue) {
120 t.Errorf("Got %v expected %v", actualValue, expectedValue)
121 }
122 if actualValue, expectedValue := tree.Values(), []string{"a", "b", "c", "d"}; !slices.Equal(actualValue, expectedValue) {
123 t.Errorf("Got %v expected %v", actualValue, expectedValue)
124 }
125 if actualValue := tree.Size(); actualValue != 4 {
126 t.Errorf("Got %v expected %v", actualValue, 7)
127 }
128
129 tests2 := [][]interface{}{
130 {1, "a", true},
131 {2, "b", true},
132 {3, "c", true},
133 {4, "d", true},
134 {5, "", false},
135 {6, "", false},
136 {7, "", false},
137 {8, "", false},
138 }
139
140 for _, test := range tests2 {
141 actualValue, actualFound := tree.Get(test[0].(int))
142 if actualValue != test[1] || actualFound != test[2] {
143 t.Errorf("Got %v expected %v", actualValue, test[1])
144 }
145 }
146
147 tree.Remove(1)
148 tree.Remove(4)
149 tree.Remove(2)
150 tree.Remove(3)
151 tree.Remove(2)
152 tree.Remove(2)
153
154 if actualValue, expectedValue := tree.Keys(), []int{}; !slices.Equal(actualValue, expectedValue) {
155 t.Errorf("Got %v expected %v", actualValue, expectedValue)
156 }
157 if actualValue, expectedValue := tree.Values(), []string{}; !slices.Equal(actualValue, expectedValue) {
158 t.Errorf("Got %v expected %v", actualValue, expectedValue)
159 }
160 if empty, size := tree.Empty(), tree.Size(); empty != true || size != -0 {
161 t.Errorf("Got %v expected %v", empty, true)
162 }
163
164 }
165
166 func TestRedBlackTreeLeftAndRight(t *testing.T) {
167 tree := New[int, string]()
168
169 if actualValue := tree.Left(); actualValue != nil {
170 t.Errorf("Got %v expected %v", actualValue, nil)
171 }
172 if actualValue := tree.Right(); actualValue != nil {
173 t.Errorf("Got %v expected %v", actualValue, nil)
174 }
175
176 tree.Put(1, "a")
177 tree.Put(5, "e")
178 tree.Put(6, "f")
179 tree.Put(7, "g")
180 tree.Put(3, "c")
181 tree.Put(4, "d")
182 tree.Put(1, "x") // overwrite
183 tree.Put(2, "b")
184
185 if actualValue, expectedValue := tree.Left().Key, 1; actualValue != expectedValue {
186 t.Errorf("Got %v expected %v", actualValue, expectedValue)
187 }
188 if actualValue, expectedValue := tree.Left().Value, "x"; actualValue != expectedValue {
189 t.Errorf("Got %v expected %v", actualValue, expectedValue)
190 }
191
192 if actualValue, expectedValue := tree.Right().Key, 7; actualValue != expectedValue {
193 t.Errorf("Got %v expected %v", actualValue, expectedValue)
194 }
195 if actualValue, expectedValue := tree.Right().Value, "g"; actualValue != expectedValue {
196 t.Errorf("Got %v expected %v", actualValue, expectedValue)
197 }
198 }
199
200 func TestRedBlackTreeCeilingAndFloor(t *testing.T) {
201 tree := New[int, string]()
202
203 if node, found := tree.Floor(0); node != nil || found {
204 t.Errorf("Got %v expected %v", node, "<nil>")
205 }
206 if node, found := tree.Ceiling(0); node != nil || found {
207 t.Errorf("Got %v expected %v", node, "<nil>")
208 }
209
210 tree.Put(5, "e")
211 tree.Put(6, "f")
212 tree.Put(7, "g")
213 tree.Put(3, "c")
214 tree.Put(4, "d")
215 tree.Put(1, "x")
216 tree.Put(2, "b")
217
218 if node, found := tree.Floor(4); node.Key != 4 || !found {
219 t.Errorf("Got %v expected %v", node.Key, 4)
220 }
221 if node, found := tree.Floor(0); node != nil || found {
222 t.Errorf("Got %v expected %v", node, "<nil>")
223 }
224
225 if node, found := tree.Ceiling(4); node.Key != 4 || !found {
226 t.Errorf("Got %v expected %v", node.Key, 4)
227 }
228 if node, found := tree.Ceiling(8); node != nil || found {
229 t.Errorf("Got %v expected %v", node, "<nil>")
230 }
231 }
232
233 func TestRedBlackTreeIteratorNextOnEmpty(t *testing.T) {
234 tree := New[int, string]()
235 it := tree.Iterator()
236 for it.Next() {
237 t.Errorf("Shouldn't iterate on empty tree")
238 }
239 }
240
241 func TestRedBlackTreeIteratorPrevOnEmpty(t *testing.T) {
242 tree := New[int, string]()
243 it := tree.Iterator()
244 for it.Prev() {
245 t.Errorf("Shouldn't iterate on empty tree")
246 }
247 }
248
249 func TestRedBlackTreeIterator1Next(t *testing.T) {
250 tree := New[int, string]()
251 tree.Put(5, "e")
252 tree.Put(6, "f")
253 tree.Put(7, "g")
254 tree.Put(3, "c")
255 tree.Put(4, "d")
256 tree.Put(1, "x")
257 tree.Put(2, "b")
258 tree.Put(1, "a") //overwrite
259 // │ ┌── 7
260 // └── 6
261 // │ ┌── 5
262 // └── 4
263 // │ ┌── 3
264 // └── 2
265 // └── 1
266 it := tree.Iterator()
267 count := 0
268 for it.Next() {
269 count++
270 key := it.Key()
271 if actualValue, expectedValue := key, count; actualValue != expectedValue {
272 t.Errorf("Got %v expected %v", actualValue, expectedValue)
273 }
274 }
275 if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
276 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
277 }
278 }
279
280 func TestRedBlackTreeIterator1Prev(t *testing.T) {
281 tree := New[int, string]()
282 tree.Put(5, "e")
283 tree.Put(6, "f")
284 tree.Put(7, "g")
285 tree.Put(3, "c")
286 tree.Put(4, "d")
287 tree.Put(1, "x")
288 tree.Put(2, "b")
289 tree.Put(1, "a") //overwrite
290 // │ ┌── 7
291 // └── 6
292 // │ ┌── 5
293 // └── 4
294 // │ ┌── 3
295 // └── 2
296 // └── 1
297 it := tree.Iterator()
298 for it.Next() {
299 }
300 countDown := tree.size
301 for it.Prev() {
302 key := it.Key()
303 if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
304 t.Errorf("Got %v expected %v", actualValue, expectedValue)
305 }
306 countDown--
307 }
308 if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
309 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
310 }
311 }
312
313 func TestRedBlackTreeIterator2Next(t *testing.T) {
314 tree := New[int, string]()
315 tree.Put(3, "c")
316 tree.Put(1, "a")
317 tree.Put(2, "b")
318 it := tree.Iterator()
319 count := 0
320 for it.Next() {
321 count++
322 key := it.Key()
323 if actualValue, expectedValue := key, count; actualValue != expectedValue {
324 t.Errorf("Got %v expected %v", actualValue, expectedValue)
325 }
326 }
327 if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
328 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
329 }
330 }
331
332 func TestRedBlackTreeIterator2Prev(t *testing.T) {
333 tree := New[int, string]()
334 tree.Put(3, "c")
335 tree.Put(1, "a")
336 tree.Put(2, "b")
337 it := tree.Iterator()
338 for it.Next() {
339 }
340 countDown := tree.size
341 for it.Prev() {
342 key := it.Key()
343 if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
344 t.Errorf("Got %v expected %v", actualValue, expectedValue)
345 }
346 countDown--
347 }
348 if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
349 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
350 }
351 }
352
353 func TestRedBlackTreeIterator3Next(t *testing.T) {
354 tree := New[int, string]()
355 tree.Put(1, "a")
356 it := tree.Iterator()
357 count := 0
358 for it.Next() {
359 count++
360 key := it.Key()
361 if actualValue, expectedValue := key, count; actualValue != expectedValue {
362 t.Errorf("Got %v expected %v", actualValue, expectedValue)
363 }
364 }
365 if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
366 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
367 }
368 }
369
370 func TestRedBlackTreeIterator3Prev(t *testing.T) {
371 tree := New[int, string]()
372 tree.Put(1, "a")
373 it := tree.Iterator()
374 for it.Next() {
375 }
376 countDown := tree.size
377 for it.Prev() {
378 key := it.Key()
379 if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
380 t.Errorf("Got %v expected %v", actualValue, expectedValue)
381 }
382 countDown--
383 }
384 if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
385 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
386 }
387 }
388
389 func TestRedBlackTreeIterator4Next(t *testing.T) {
390 tree := New[int, int]()
391 tree.Put(13, 5)
392 tree.Put(8, 3)
393 tree.Put(17, 7)
394 tree.Put(1, 1)
395 tree.Put(11, 4)
396 tree.Put(15, 6)
397 tree.Put(25, 9)
398 tree.Put(6, 2)
399 tree.Put(22, 8)
400 tree.Put(27, 10)
401 // │ ┌── 27
402 // │ ┌── 25
403 // │ │ └── 22
404 // │ ┌── 17
405 // │ │ └── 15
406 // └── 13
407 // │ ┌── 11
408 // └── 8
409 // │ ┌── 6
410 // └── 1
411 it := tree.Iterator()
412 count := 0
413 for it.Next() {
414 count++
415 value := it.Value()
416 if actualValue, expectedValue := value, count; actualValue != expectedValue {
417 t.Errorf("Got %v expected %v", actualValue, expectedValue)
418 }
419 }
420 if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
421 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
422 }
423 }
424
425 func TestRedBlackTreeIterator4Prev(t *testing.T) {
426 tree := New[int, int]()
427 tree.Put(13, 5)
428 tree.Put(8, 3)
429 tree.Put(17, 7)
430 tree.Put(1, 1)
431 tree.Put(11, 4)
432 tree.Put(15, 6)
433 tree.Put(25, 9)
434 tree.Put(6, 2)
435 tree.Put(22, 8)
436 tree.Put(27, 10)
437 // │ ┌── 27
438 // │ ┌── 25
439 // │ │ └── 22
440 // │ ┌── 17
441 // │ │ └── 15
442 // └── 13
443 // │ ┌── 11
444 // └── 8
445 // │ ┌── 6
446 // └── 1
447 it := tree.Iterator()
448 count := tree.Size()
449 for it.Next() {
450 }
451 for it.Prev() {
452 value := it.Value()
453 if actualValue, expectedValue := value, count; actualValue != expectedValue {
454 t.Errorf("Got %v expected %v", actualValue, expectedValue)
455 }
456 count--
457 }
458 if actualValue, expectedValue := count, 0; actualValue != expectedValue {
459 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
460 }
461 }
462
463 func TestRedBlackTreeIteratorBegin(t *testing.T) {
464 tree := New[int, string]()
465 tree.Put(3, "c")
466 tree.Put(1, "a")
467 tree.Put(2, "b")
468 it := tree.Iterator()
469
470 if it.node != nil {
471 t.Errorf("Got %v expected %v", it.node, nil)
472 }
473
474 it.Begin()
475
476 if it.node != nil {
477 t.Errorf("Got %v expected %v", it.node, nil)
478 }
479
480 for it.Next() {
481 }
482
483 it.Begin()
484
485 if it.node != nil {
486 t.Errorf("Got %v expected %v", it.node, nil)
487 }
488
489 it.Next()
490 if key, value := it.Key(), it.Value(); key != 1 || value != "a" {
491 t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")
492 }
493 }
494
495 func TestRedBlackTreeIteratorEnd(t *testing.T) {
496 tree := New[int, string]()
497 it := tree.Iterator()
498
499 if it.node != nil {
500 t.Errorf("Got %v expected %v", it.node, nil)
501 }
502
503 it.End()
504 if it.node != nil {
505 t.Errorf("Got %v expected %v", it.node, nil)
506 }
507
508 tree.Put(3, "c")
509 tree.Put(1, "a")
510 tree.Put(2, "b")
511 it.End()
512 if it.node != nil {
513 t.Errorf("Got %v expected %v", it.node, nil)
514 }
515
516 it.Prev()
517 if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
518 t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
519 }
520 }
521
522 func TestRedBlackTreeIteratorFirst(t *testing.T) {
523 tree := New[int, string]()
524 tree.Put(3, "c")
525 tree.Put(1, "a")
526 tree.Put(2, "b")
527 it := tree.Iterator()
528 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
529 t.Errorf("Got %v expected %v", actualValue, expectedValue)
530 }
531 if key, value := it.Key(), it.Value(); key != 1 || value != "a" {
532 t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")
533 }
534 }
535
536 func TestRedBlackTreeIteratorLast(t *testing.T) {
537 tree := New[int, string]()
538 tree.Put(3, "c")
539 tree.Put(1, "a")
540 tree.Put(2, "b")
541 it := tree.Iterator()
542 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
543 t.Errorf("Got %v expected %v", actualValue, expectedValue)
544 }
545 if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
546 t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
547 }
548 }
549
550 func TestRedBlackTreeIteratorNextTo(t *testing.T) {
551 // Sample seek function, i.e. string starting with "b"
552 seek := func(index int, value string) bool {
553 return strings.HasSuffix(value, "b")
554 }
555
556 // NextTo (empty)
557 {
558 tree := New[int, string]()
559 it := tree.Iterator()
560 for it.NextTo(seek) {
561 t.Errorf("Shouldn't iterate on empty tree")
562 }
563 }
564
565 // NextTo (not found)
566 {
567 tree := New[int, string]()
568 tree.Put(0, "xx")
569 tree.Put(1, "yy")
570 it := tree.Iterator()
571 for it.NextTo(seek) {
572 t.Errorf("Shouldn't iterate on empty tree")
573 }
574 }
575
576 // NextTo (found)
577 {
578 tree := New[int, string]()
579 tree.Put(2, "cc")
580 tree.Put(0, "aa")
581 tree.Put(1, "bb")
582 it := tree.Iterator()
583 it.Begin()
584 if !it.NextTo(seek) {
585 t.Errorf("Shouldn't iterate on empty tree")
586 }
587 if index, value := it.Key(), it.Value(); index != 1 || value != "bb" {
588 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
589 }
590 if !it.Next() {
591 t.Errorf("Should go to first element")
592 }
593 if index, value := it.Key(), it.Value(); index != 2 || value != "cc" {
594 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
595 }
596 if it.Next() {
597 t.Errorf("Should not go past last element")
598 }
599 }
600 }
601
602 func TestRedBlackTreeIteratorPrevTo(t *testing.T) {
603 // Sample seek function, i.e. string starting with "b"
604 seek := func(index int, value string) bool {
605 return strings.HasSuffix(value, "b")
606 }
607
608 // PrevTo (empty)
609 {
610 tree := New[int, string]()
611 it := tree.Iterator()
612 it.End()
613 for it.PrevTo(seek) {
614 t.Errorf("Shouldn't iterate on empty tree")
615 }
616 }
617
618 // PrevTo (not found)
619 {
620 tree := New[int, string]()
621 tree.Put(0, "xx")
622 tree.Put(1, "yy")
623 it := tree.Iterator()
624 it.End()
625 for it.PrevTo(seek) {
626 t.Errorf("Shouldn't iterate on empty tree")
627 }
628 }
629
630 // PrevTo (found)
631 {
632 tree := New[int, string]()
633 tree.Put(2, "cc")
634 tree.Put(0, "aa")
635 tree.Put(1, "bb")
636 it := tree.Iterator()
637 it.End()
638 if !it.PrevTo(seek) {
639 t.Errorf("Shouldn't iterate on empty tree")
640 }
641 if index, value := it.Key(), it.Value(); index != 1 || value != "bb" {
642 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
643 }
644 if !it.Prev() {
645 t.Errorf("Should go to first element")
646 }
647 if index, value := it.Key(), it.Value(); index != 0 || value != "aa" {
648 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
649 }
650 if it.Prev() {
651 t.Errorf("Should not go before first element")
652 }
653 }
654 }
655
656 func TestRedBlackTreeSerialization(t *testing.T) {
657 tree := New[string, string]()
658 tree.Put("c", "3")
659 tree.Put("b", "2")
660 tree.Put("a", "1")
661
662 var err error
663 assert := func() {
664 if actualValue, expectedValue := tree.Size(), 3; actualValue != expectedValue {
665 t.Errorf("Got %v expected %v", actualValue, expectedValue)
666 }
667 if actualValue, expectedValue := tree.Keys(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) {
668 t.Errorf("Got %v expected %v", actualValue, expectedValue)
669 }
670 if actualValue, expectedValue := tree.Values(), []string{"1", "2", "3"}; !slices.Equal(actualValue, expectedValue) {
671 t.Errorf("Got %v expected %v", actualValue, expectedValue)
672 }
673 if err != nil {
674 t.Errorf("Got error %v", err)
675 }
676 }
677
678 assert()
679
680 bytes, err := tree.ToJSON()
681 assert()
682
683 err = tree.FromJSON(bytes)
684 assert()
685
686 bytes, err = json.Marshal([]interface{}{"a", "b", "c", tree})
687 if err != nil {
688 t.Errorf("Got error %v", err)
689 }
690
691 intTree := New[string, int]()
692 err = json.Unmarshal([]byte(`{"a":1,"b":2}`), intTree)
693 if err != nil {
694 t.Errorf("Got error %v", err)
695 }
696 if actualValue, expectedValue := intTree.Size(), 2; actualValue != expectedValue {
697 t.Errorf("Got %v expected %v", actualValue, expectedValue)
698 }
699 if actualValue, expectedValue := intTree.Keys(), []string{"a", "b"}; !slices.Equal(actualValue, expectedValue) {
700 t.Errorf("Got %v expected %v", actualValue, expectedValue)
701 }
702 if actualValue, expectedValue := intTree.Values(), []int{1, 2}; !slices.Equal(actualValue, expectedValue) {
703 t.Errorf("Got %v expected %v", actualValue, expectedValue)
704 }
705 }
706
707 func TestRedBlackTreeString(t *testing.T) {
708 c := New[string, int]()
709 c.Put("a", 1)
710 if !strings.HasPrefix(c.String(), "RedBlackTree") {
711 t.Errorf("String should start with container name")
712 }
713 }
714
715 func benchmarkGet(b *testing.B, tree *Tree[int, struct{}], size int) {
716 for i := 0; i < b.N; i++ {
717 for n := 0; n < size; n++ {
718 tree.Get(n)
719 }
720 }
721 }
722
723 func benchmarkPut(b *testing.B, tree *Tree[int, struct{}], size int) {
724 for i := 0; i < b.N; i++ {
725 for n := 0; n < size; n++ {
726 tree.Put(n, struct{}{})
727 }
728 }
729 }
730
731 func benchmarkRemove(b *testing.B, tree *Tree[int, struct{}], size int) {
732 for i := 0; i < b.N; i++ {
733 for n := 0; n < size; n++ {
734 tree.Remove(n)
735 }
736 }
737 }
738
739 func BenchmarkRedBlackTreeGet100(b *testing.B) {
740 b.StopTimer()
741 size := 100
742 tree := New[int, struct{}]()
743 for n := 0; n < size; n++ {
744 tree.Put(n, struct{}{})
745 }
746 b.StartTimer()
747 benchmarkGet(b, tree, size)
748 }
749
750 func BenchmarkRedBlackTreeGet1000(b *testing.B) {
751 b.StopTimer()
752 size := 1000
753 tree := New[int, struct{}]()
754 for n := 0; n < size; n++ {
755 tree.Put(n, struct{}{})
756 }
757 b.StartTimer()
758 benchmarkGet(b, tree, size)
759 }
760
761 func BenchmarkRedBlackTreeGet10000(b *testing.B) {
762 b.StopTimer()
763 size := 10000
764 tree := New[int, struct{}]()
765 for n := 0; n < size; n++ {
766 tree.Put(n, struct{}{})
767 }
768 b.StartTimer()
769 benchmarkGet(b, tree, size)
770 }
771
772 func BenchmarkRedBlackTreeGet100000(b *testing.B) {
773 b.StopTimer()
774 size := 100000
775 tree := New[int, struct{}]()
776 for n := 0; n < size; n++ {
777 tree.Put(n, struct{}{})
778 }
779 b.StartTimer()
780 benchmarkGet(b, tree, size)
781 }
782
783 func BenchmarkRedBlackTreePut100(b *testing.B) {
784 b.StopTimer()
785 size := 100
786 tree := New[int, struct{}]()
787 b.StartTimer()
788 benchmarkPut(b, tree, size)
789 }
790
791 func BenchmarkRedBlackTreePut1000(b *testing.B) {
792 b.StopTimer()
793 size := 1000
794 tree := New[int, struct{}]()
795 for n := 0; n < size; n++ {
796 tree.Put(n, struct{}{})
797 }
798 b.StartTimer()
799 benchmarkPut(b, tree, size)
800 }
801
802 func BenchmarkRedBlackTreePut10000(b *testing.B) {
803 b.StopTimer()
804 size := 10000
805 tree := New[int, struct{}]()
806 for n := 0; n < size; n++ {
807 tree.Put(n, struct{}{})
808 }
809 b.StartTimer()
810 benchmarkPut(b, tree, size)
811 }
812
813 func BenchmarkRedBlackTreePut100000(b *testing.B) {
814 b.StopTimer()
815 size := 100000
816 tree := New[int, struct{}]()
817 for n := 0; n < size; n++ {
818 tree.Put(n, struct{}{})
819 }
820 b.StartTimer()
821 benchmarkPut(b, tree, size)
822 }
823
824 func BenchmarkRedBlackTreeRemove100(b *testing.B) {
825 b.StopTimer()
826 size := 100
827 tree := New[int, struct{}]()
828 for n := 0; n < size; n++ {
829 tree.Put(n, struct{}{})
830 }
831 b.StartTimer()
832 benchmarkRemove(b, tree, size)
833 }
834
835 func BenchmarkRedBlackTreeRemove1000(b *testing.B) {
836 b.StopTimer()
837 size := 1000
838 tree := New[int, struct{}]()
839 for n := 0; n < size; n++ {
840 tree.Put(n, struct{}{})
841 }
842 b.StartTimer()
843 benchmarkRemove(b, tree, size)
844 }
845
846 func BenchmarkRedBlackTreeRemove10000(b *testing.B) {
847 b.StopTimer()
848 size := 10000
849 tree := New[int, struct{}]()
850 for n := 0; n < size; n++ {
851 tree.Put(n, struct{}{})
852 }
853 b.StartTimer()
854 benchmarkRemove(b, tree, size)
855 }
856
857 func BenchmarkRedBlackTreeRemove100000(b *testing.B) {
858 b.StopTimer()
859 size := 100000
860 tree := New[int, struct{}]()
861 for n := 0; n < size; n++ {
862 tree.Put(n, struct{}{})
863 }
864 b.StartTimer()
865 benchmarkRemove(b, tree, size)
866 }