Debian Codegraph @

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