Debian Codegraph @

sets/treeset/treeset_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 treeset
6
7 import (
8 "encoding/json"
9 "fmt"
10 "strings"
11 "testing"
12
13 "github.com/emirpasic/gods/v2/testutils"
14 )
15
16 func TestSetNew(t *testing.T) {
17 set := New[int](2, 1)
18 if actualValue := set.Size(); actualValue != 2 {
19 t.Errorf("Got %v expected %v", actualValue, 2)
20 }
21 values := set.Values()
22 if actualValue := values[0]; actualValue != 1 {
23 t.Errorf("Got %v expected %v", actualValue, 1)
24 }
25 if actualValue := values[1]; actualValue != 2 {
26 t.Errorf("Got %v expected %v", actualValue, 2)
27 }
28 }
29
30 func TestSetAdd(t *testing.T) {
31 set := New[int]()
32 set.Add()
33 set.Add(1)
34 set.Add(2)
35 set.Add(2, 3)
36 set.Add()
37 if actualValue := set.Empty(); actualValue != false {
38 t.Errorf("Got %v expected %v", actualValue, false)
39 }
40 if actualValue := set.Size(); actualValue != 3 {
41 t.Errorf("Got %v expected %v", actualValue, 3)
42 }
43 testutils.SameElements(t, set.Values(), []int{1, 2, 3})
44 }
45
46 func TestSetContains(t *testing.T) {
47 set := New[int]()
48 set.Add(3, 1, 2)
49 if actualValue := set.Contains(); actualValue != true {
50 t.Errorf("Got %v expected %v", actualValue, true)
51 }
52 if actualValue := set.Contains(1); actualValue != true {
53 t.Errorf("Got %v expected %v", actualValue, true)
54 }
55 if actualValue := set.Contains(1, 2, 3); actualValue != true {
56 t.Errorf("Got %v expected %v", actualValue, true)
57 }
58 if actualValue := set.Contains(1, 2, 3, 4); actualValue != false {
59 t.Errorf("Got %v expected %v", actualValue, false)
60 }
61 }
62
63 func TestSetRemove(t *testing.T) {
64 set := New[int]()
65 set.Add(3, 1, 2)
66 set.Remove()
67 if actualValue := set.Size(); actualValue != 3 {
68 t.Errorf("Got %v expected %v", actualValue, 3)
69 }
70 set.Remove(1)
71 if actualValue := set.Size(); actualValue != 2 {
72 t.Errorf("Got %v expected %v", actualValue, 2)
73 }
74 set.Remove(3)
75 set.Remove(3)
76 set.Remove()
77 set.Remove(2)
78 if actualValue := set.Size(); actualValue != 0 {
79 t.Errorf("Got %v expected %v", actualValue, 0)
80 }
81 }
82
83 func TestSetEach(t *testing.T) {
84 set := New[string]()
85 set.Add("c", "a", "b")
86 set.Each(func(index int, value string) {
87 switch index {
88 case 0:
89 if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
90 t.Errorf("Got %v expected %v", actualValue, expectedValue)
91 }
92 case 1:
93 if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
94 t.Errorf("Got %v expected %v", actualValue, expectedValue)
95 }
96 case 2:
97 if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
98 t.Errorf("Got %v expected %v", actualValue, expectedValue)
99 }
100 default:
101 t.Errorf("Too many")
102 }
103 })
104 }
105
106 func TestSetMap(t *testing.T) {
107 set := New[string]()
108 set.Add("c", "a", "b")
109 mappedSet := set.Map(func(index int, value string) string {
110 return "mapped: " + value
111 })
112 if actualValue, expectedValue := mappedSet.Contains("mapped: a", "mapped: b", "mapped: c"), true; actualValue != expectedValue {
113 t.Errorf("Got %v expected %v", actualValue, expectedValue)
114 }
115 if actualValue, expectedValue := mappedSet.Contains("mapped: a", "mapped: b", "mapped: x"), false; actualValue != expectedValue {
116 t.Errorf("Got %v expected %v", actualValue, expectedValue)
117 }
118 if mappedSet.Size() != 3 {
119 t.Errorf("Got %v expected %v", mappedSet.Size(), 3)
120 }
121 }
122
123 func TestSetSelect(t *testing.T) {
124 set := New[string]()
125 set.Add("c", "a", "b")
126 selectedSet := set.Select(func(index int, value string) bool {
127 return value >= "a" && value <= "b"
128 })
129 if actualValue, expectedValue := selectedSet.Contains("a", "b"), true; actualValue != expectedValue {
130 fmt.Println("A: ", selectedSet.Contains("b"))
131 t.Errorf("Got %v (%v) expected %v (%v)", actualValue, selectedSet.Values(), expectedValue, "[a b]")
132 }
133 if actualValue, expectedValue := selectedSet.Contains("a", "b", "c"), false; actualValue != expectedValue {
134 t.Errorf("Got %v (%v) expected %v (%v)", actualValue, selectedSet.Values(), expectedValue, "[a b]")
135 }
136 if selectedSet.Size() != 2 {
137 t.Errorf("Got %v expected %v", selectedSet.Size(), 3)
138 }
139 }
140
141 func TestSetAny(t *testing.T) {
142 set := New[string]()
143 set.Add("c", "a", "b")
144 any := set.Any(func(index int, value string) bool {
145 return value == "c"
146 })
147 if any != true {
148 t.Errorf("Got %v expected %v", any, true)
149 }
150 any = set.Any(func(index int, value string) bool {
151 return value == "x"
152 })
153 if any != false {
154 t.Errorf("Got %v expected %v", any, false)
155 }
156 }
157
158 func TestSetAll(t *testing.T) {
159 set := New[string]()
160 set.Add("c", "a", "b")
161 all := set.All(func(index int, value string) bool {
162 return value >= "a" && value <= "c"
163 })
164 if all != true {
165 t.Errorf("Got %v expected %v", all, true)
166 }
167 all = set.All(func(index int, value string) bool {
168 return value >= "a" && value <= "b"
169 })
170 if all != false {
171 t.Errorf("Got %v expected %v", all, false)
172 }
173 }
174
175 func TestSetFind(t *testing.T) {
176 set := New[string]()
177 set.Add("c", "a", "b")
178 foundIndex, foundValue := set.Find(func(index int, value string) bool {
179 return value == "c"
180 })
181 if foundValue != "c" || foundIndex != 2 {
182 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2)
183 }
184 foundIndex, foundValue = set.Find(func(index int, value string) bool {
185 return value == "x"
186 })
187 if foundValue != "" || foundIndex != -1 {
188 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil)
189 }
190 }
191
192 func TestSetChaining(t *testing.T) {
193 set := New[string]()
194 set.Add("c", "a", "b")
195 }
196
197 func TestSetIteratorNextOnEmpty(t *testing.T) {
198 set := New[string]()
199 it := set.Iterator()
200 for it.Next() {
201 t.Errorf("Shouldn't iterate on empty set")
202 }
203 }
204
205 func TestSetIteratorPrevOnEmpty(t *testing.T) {
206 set := New[string]()
207 it := set.Iterator()
208 for it.Prev() {
209 t.Errorf("Shouldn't iterate on empty set")
210 }
211 }
212
213 func TestSetIteratorNext(t *testing.T) {
214 set := New[string]()
215 set.Add("c", "a", "b")
216 it := set.Iterator()
217 count := 0
218 for it.Next() {
219 count++
220 index := it.Index()
221 value := it.Value()
222 switch index {
223 case 0:
224 if actualValue, expectedValue := value, "a"; actualValue != expectedValue {
225 t.Errorf("Got %v expected %v", actualValue, expectedValue)
226 }
227 case 1:
228 if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
229 t.Errorf("Got %v expected %v", actualValue, expectedValue)
230 }
231 case 2:
232 if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
233 t.Errorf("Got %v expected %v", actualValue, expectedValue)
234 }
235 default:
236 t.Errorf("Too many")
237 }
238 if actualValue, expectedValue := index, count-1; actualValue != expectedValue {
239 t.Errorf("Got %v expected %v", actualValue, expectedValue)
240 }
241 }
242 if actualValue, expectedValue := count, 3; actualValue != expectedValue {
243 t.Errorf("Got %v expected %v", actualValue, expectedValue)
244 }
245 }
246
247 func TestSetIteratorPrev(t *testing.T) {
248 set := New[string]()
249 set.Add("c", "a", "b")
250 it := set.Iterator()
251 for it.Prev() {
252 }
253 count := 0
254 for it.Next() {
255 count++
256 index := it.Index()
257 value := it.Value()
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 if actualValue, expectedValue := index, count-1; actualValue != expectedValue {
275 t.Errorf("Got %v expected %v", actualValue, expectedValue)
276 }
277 }
278 if actualValue, expectedValue := count, 3; actualValue != expectedValue {
279 t.Errorf("Got %v expected %v", actualValue, expectedValue)
280 }
281 }
282
283 func TestSetIteratorBegin(t *testing.T) {
284 set := New[string]()
285 it := set.Iterator()
286 it.Begin()
287 set.Add("a", "b", "c")
288 for it.Next() {
289 }
290 it.Begin()
291 it.Next()
292 if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
293 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
294 }
295 }
296
297 func TestSetIteratorEnd(t *testing.T) {
298 set := New[string]()
299 it := set.Iterator()
300
301 if index := it.Index(); index != -1 {
302 t.Errorf("Got %v expected %v", index, -1)
303 }
304
305 it.End()
306 if index := it.Index(); index != 0 {
307 t.Errorf("Got %v expected %v", index, 0)
308 }
309
310 set.Add("a", "b", "c")
311 it.End()
312 if index := it.Index(); index != set.Size() {
313 t.Errorf("Got %v expected %v", index, set.Size())
314 }
315
316 it.Prev()
317 if index, value := it.Index(), it.Value(); index != set.Size()-1 || value != "c" {
318 t.Errorf("Got %v,%v expected %v,%v", index, value, set.Size()-1, "c")
319 }
320 }
321
322 func TestSetIteratorFirst(t *testing.T) {
323 set := New[string]()
324 set.Add("a", "b", "c")
325 it := set.Iterator()
326 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
327 t.Errorf("Got %v expected %v", actualValue, expectedValue)
328 }
329 if index, value := it.Index(), it.Value(); index != 0 || value != "a" {
330 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a")
331 }
332 }
333
334 func TestSetIteratorLast(t *testing.T) {
335 set := New[string]()
336 set.Add("a", "b", "c")
337 it := set.Iterator()
338 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
339 t.Errorf("Got %v expected %v", actualValue, expectedValue)
340 }
341 if index, value := it.Index(), it.Value(); index != 2 || value != "c" {
342 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "c")
343 }
344 }
345
346 func TestSetIteratorNextTo(t *testing.T) {
347 // Sample seek function, i.e. string starting with "b"
348 seek := func(index int, value string) bool {
349 return strings.HasSuffix(value, "b")
350 }
351
352 // NextTo (empty)
353 {
354 set := New[string]()
355 it := set.Iterator()
356 for it.NextTo(seek) {
357 t.Errorf("Shouldn't iterate on empty set")
358 }
359 }
360
361 // NextTo (not found)
362 {
363 set := New[string]()
364 set.Add("xx", "yy")
365 it := set.Iterator()
366 for it.NextTo(seek) {
367 t.Errorf("Shouldn't iterate on empty set")
368 }
369 }
370
371 // NextTo (found)
372 {
373 set := New[string]()
374 set.Add("aa", "bb", "cc")
375 it := set.Iterator()
376 it.Begin()
377 if !it.NextTo(seek) {
378 t.Errorf("Shouldn't iterate on empty set")
379 }
380 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
381 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
382 }
383 if !it.Next() {
384 t.Errorf("Should go to first element")
385 }
386 if index, value := it.Index(), it.Value(); index != 2 || value != "cc" {
387 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
388 }
389 if it.Next() {
390 t.Errorf("Should not go past last element")
391 }
392 }
393 }
394
395 func TestSetIteratorPrevTo(t *testing.T) {
396 // Sample seek function, i.e. string starting with "b"
397 seek := func(index int, value string) bool {
398 return strings.HasSuffix(value, "b")
399 }
400
401 // PrevTo (empty)
402 {
403 set := New[string]()
404 it := set.Iterator()
405 it.End()
406 for it.PrevTo(seek) {
407 t.Errorf("Shouldn't iterate on empty set")
408 }
409 }
410
411 // PrevTo (not found)
412 {
413 set := New[string]()
414 set.Add("xx", "yy")
415 it := set.Iterator()
416 it.End()
417 for it.PrevTo(seek) {
418 t.Errorf("Shouldn't iterate on empty set")
419 }
420 }
421
422 // PrevTo (found)
423 {
424 set := New[string]()
425 set.Add("aa", "bb", "cc")
426 it := set.Iterator()
427 it.End()
428 if !it.PrevTo(seek) {
429 t.Errorf("Shouldn't iterate on empty set")
430 }
431 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
432 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
433 }
434 if !it.Prev() {
435 t.Errorf("Should go to first element")
436 }
437 if index, value := it.Index(), it.Value(); index != 0 || value != "aa" {
438 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
439 }
440 if it.Prev() {
441 t.Errorf("Should not go before first element")
442 }
443 }
444 }
445
446 func TestSetSerialization(t *testing.T) {
447 set := New[string]()
448 set.Add("a", "b", "c")
449
450 var err error
451 assert := func() {
452 if actualValue, expectedValue := set.Size(), 3; actualValue != expectedValue {
453 t.Errorf("Got %v expected %v", actualValue, expectedValue)
454 }
455 if actualValue := set.Contains("a", "b", "c"); actualValue != true {
456 t.Errorf("Got %v expected %v", actualValue, true)
457 }
458 if err != nil {
459 t.Errorf("Got error %v", err)
460 }
461 }
462
463 assert()
464
465 bytes, err := set.ToJSON()
466 assert()
467
468 err = set.FromJSON(bytes)
469 assert()
470
471 bytes, err = json.Marshal([]interface{}{"a", "b", "c", set})
472 if err != nil {
473 t.Errorf("Got error %v", err)
474 }
475
476 err = json.Unmarshal([]byte(`["1","2","3"]`), &set)
477 if err != nil {
478 t.Errorf("Got error %v", err)
479 }
480 }
481
482 func TestSetString(t *testing.T) {
483 c := New[int]()
484 c.Add(1)
485 if !strings.HasPrefix(c.String(), "TreeSet") {
486 t.Errorf("String should start with container name")
487 }
488 }
489
490 func TestSetIntersection(t *testing.T) {
491 set := New[string]()
492 another := New[string]()
493
494 intersection := set.Intersection(another)
495 if actualValue, expectedValue := intersection.Size(), 0; actualValue != expectedValue {
496 t.Errorf("Got %v expected %v", actualValue, expectedValue)
497 }
498
499 set.Add("a", "b", "c", "d")
500 another.Add("c", "d", "e", "f")
501
502 intersection = set.Intersection(another)
503
504 if actualValue, expectedValue := intersection.Size(), 2; actualValue != expectedValue {
505 t.Errorf("Got %v expected %v", actualValue, expectedValue)
506 }
507 if actualValue := intersection.Contains("c", "d"); actualValue != true {
508 t.Errorf("Got %v expected %v", actualValue, true)
509 }
510 }
511
512 func TestSetUnion(t *testing.T) {
513 set := New[string]()
514 another := New[string]()
515
516 union := set.Union(another)
517 if actualValue, expectedValue := union.Size(), 0; actualValue != expectedValue {
518 t.Errorf("Got %v expected %v", actualValue, expectedValue)
519 }
520
521 set.Add("a", "b", "c", "d")
522 another.Add("c", "d", "e", "f")
523
524 union = set.Union(another)
525
526 if actualValue, expectedValue := union.Size(), 6; actualValue != expectedValue {
527 t.Errorf("Got %v expected %v", actualValue, expectedValue)
528 }
529 if actualValue := union.Contains("a", "b", "c", "d", "e", "f"); actualValue != true {
530 t.Errorf("Got %v expected %v", actualValue, true)
531 }
532 }
533
534 func TestSetDifference(t *testing.T) {
535 set := New[string]()
536 another := New[string]()
537
538 difference := set.Difference(another)
539 if actualValue, expectedValue := difference.Size(), 0; actualValue != expectedValue {
540 t.Errorf("Got %v expected %v", actualValue, expectedValue)
541 }
542
543 set.Add("a", "b", "c", "d")
544 another.Add("c", "d", "e", "f")
545
546 difference = set.Difference(another)
547
548 if actualValue, expectedValue := difference.Size(), 2; actualValue != expectedValue {
549 t.Errorf("Got %v expected %v", actualValue, expectedValue)
550 }
551 if actualValue := difference.Contains("a", "b"); actualValue != true {
552 t.Errorf("Got %v expected %v", actualValue, true)
553 }
554 }
555
556 func benchmarkContains(b *testing.B, set *Set[int], size int) {
557 for i := 0; i < b.N; i++ {
558 for n := 0; n < size; n++ {
559 set.Contains(n)
560 }
561 }
562 }
563
564 func benchmarkAdd(b *testing.B, set *Set[int], size int) {
565 for i := 0; i < b.N; i++ {
566 for n := 0; n < size; n++ {
567 set.Add(n)
568 }
569 }
570 }
571
572 func benchmarkRemove(b *testing.B, set *Set[int], size int) {
573 for i := 0; i < b.N; i++ {
574 for n := 0; n < size; n++ {
575 set.Remove(n)
576 }
577 }
578 }
579
580 func BenchmarkTreeSetContains100(b *testing.B) {
581 b.StopTimer()
582 size := 100
583 set := New[int]()
584 for n := 0; n < size; n++ {
585 set.Add(n)
586 }
587 b.StartTimer()
588 benchmarkContains(b, set, size)
589 }
590
591 func BenchmarkTreeSetContains1000(b *testing.B) {
592 b.StopTimer()
593 size := 1000
594 set := New[int]()
595 for n := 0; n < size; n++ {
596 set.Add(n)
597 }
598 b.StartTimer()
599 benchmarkContains(b, set, size)
600 }
601
602 func BenchmarkTreeSetContains10000(b *testing.B) {
603 b.StopTimer()
604 size := 10000
605 set := New[int]()
606 for n := 0; n < size; n++ {
607 set.Add(n)
608 }
609 b.StartTimer()
610 benchmarkContains(b, set, size)
611 }
612
613 func BenchmarkTreeSetContains100000(b *testing.B) {
614 b.StopTimer()
615 size := 100000
616 set := New[int]()
617 for n := 0; n < size; n++ {
618 set.Add(n)
619 }
620 b.StartTimer()
621 benchmarkContains(b, set, size)
622 }
623
624 func BenchmarkTreeSetAdd100(b *testing.B) {
625 b.StopTimer()
626 size := 100
627 set := New[int]()
628 b.StartTimer()
629 benchmarkAdd(b, set, size)
630 }
631
632 func BenchmarkTreeSetAdd1000(b *testing.B) {
633 b.StopTimer()
634 size := 1000
635 set := New[int]()
636 for n := 0; n < size; n++ {
637 set.Add(n)
638 }
639 b.StartTimer()
640 benchmarkAdd(b, set, size)
641 }
642
643 func BenchmarkTreeSetAdd10000(b *testing.B) {
644 b.StopTimer()
645 size := 10000
646 set := New[int]()
647 for n := 0; n < size; n++ {
648 set.Add(n)
649 }
650 b.StartTimer()
651 benchmarkAdd(b, set, size)
652 }
653
654 func BenchmarkTreeSetAdd100000(b *testing.B) {
655 b.StopTimer()
656 size := 100000
657 set := New[int]()
658 for n := 0; n < size; n++ {
659 set.Add(n)
660 }
661 b.StartTimer()
662 benchmarkAdd(b, set, size)
663 }
664
665 func BenchmarkTreeSetRemove100(b *testing.B) {
666 b.StopTimer()
667 size := 100
668 set := New[int]()
669 for n := 0; n < size; n++ {
670 set.Add(n)
671 }
672 b.StartTimer()
673 benchmarkRemove(b, set, size)
674 }
675
676 func BenchmarkTreeSetRemove1000(b *testing.B) {
677 b.StopTimer()
678 size := 1000
679 set := New[int]()
680 for n := 0; n < size; n++ {
681 set.Add(n)
682 }
683 b.StartTimer()
684 benchmarkRemove(b, set, size)
685 }
686
687 func BenchmarkTreeSetRemove10000(b *testing.B) {
688 b.StopTimer()
689 size := 10000
690 set := New[int]()
691 for n := 0; n < size; n++ {
692 set.Add(n)
693 }
694 b.StartTimer()
695 benchmarkRemove(b, set, size)
696 }
697
698 func BenchmarkTreeSetRemove100000(b *testing.B) {
699 b.StopTimer()
700 size := 100000
701 set := New[int]()
702 for n := 0; n < size; n++ {
703 set.Add(n)
704 }
705 b.StartTimer()
706 benchmarkRemove(b, set, size)
707 }