-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_test.go
More file actions
113 lines (101 loc) · 2.88 KB
/
map_test.go
File metadata and controls
113 lines (101 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package mrpkg
import (
"reflect"
"sort"
"strconv"
"testing"
)
var (
list1 = []int{1, 2, 3, 4}
list2 = []int{2, 3, 4, 5}
)
func TestConcurrentSet_Union(t *testing.T) {
var set1, set2 ConcurrentSet[int]
set1.BatchAdd(Iter(list1))
set2.BatchAdd(Iter(list2))
target := ToGoSlice(set1.Union(&set2).ListIterator())
sort.Ints(target)
if !reflect.DeepEqual(target, []int{1, 2, 3, 4, 5}) {
t.Errorf("ConcurrentSet.Union: \n\tset1=%v; \n\tset2=%v; \n\ttarget=%v",
ToGoSlice(set1.ListIterator()),
ToGoSlice(set2.ListIterator()),
target)
}
}
func TestConcurrentSet_Intersection(t *testing.T) {
var set1, set2 ConcurrentSet[int]
set1.BatchAdd(Iter(list1))
set2.BatchAdd(Iter(list2))
target := ToGoSlice(set1.Intersection(&set2).ListIterator())
sort.Ints(target)
if !reflect.DeepEqual(target, []int{2, 3, 4}) {
t.Errorf("ConcurrentSet.Intersection: \n\tset1=%v; \n\tset2=%v; \n\ttarget=%v",
ToGoSlice(set1.ListIterator()),
ToGoSlice(set2.ListIterator()),
target)
}
}
func TestConcurrentSet_Difference(t *testing.T) {
var set1, set2 ConcurrentSet[int]
set1.BatchAdd(Iter(list1))
set2.BatchAdd(Iter(list2))
target1 := ToGoSlice(set1.Difference(&set2).ListIterator())
sort.Ints(target1)
if !reflect.DeepEqual(target1, []int{1}) {
t.Errorf("ConcurrentSet.Difference: \n\tset1=%v; \n\tset2=%v; \n\ttarget1=%v",
ToGoSlice(set1.ListIterator()),
ToGoSlice(set2.ListIterator()),
target1)
}
target2 := ToGoSlice(set2.Difference(&set1).ListIterator())
sort.Ints(target2)
if !reflect.DeepEqual(target2, []int{5}) {
t.Errorf("ConcurrentSet.Difference: \n\tset2=%v; \n\tset1=%v; \n\ttarget2=%v",
ToGoSlice(set2.ListIterator()),
ToGoSlice(set1.ListIterator()),
target2)
}
}
func TestConcurrentSet_SymmetricDifference(t *testing.T) {
var set1, set2 ConcurrentSet[int]
set1.BatchAdd(Iter(list1))
set2.BatchAdd(Iter(list2))
target := ToGoSlice(set1.SymmetricDifference(&set2).ListIterator())
sort.Ints(target)
if !reflect.DeepEqual(target, []int{1, 5}) {
t.Errorf("ConcurrentSet.SymmetricDifference: \n\tset1=%v; \n\tset2=%v; \n\ttarget=%v",
ToGoSlice(set1.ListIterator()),
ToGoSlice(set2.ListIterator()),
target)
}
}
type Int int
var tinyInts = []int{
1, 2, 5, 6, 7, 9,
10, 100, 1000, 10000,
99, 999, 9999, 99880,
}
func TestTinyMap_Get(t *testing.T) {
var tiny TinyMap[Int, int]
for _, i := range tinyInts {
tiny.Set(Int(i), i)
}
for _, i := range tinyInts {
v, ok := tiny.Get(Int(i))
if !ok {
t.Errorf("TinyMap.Get: key %s not exists\n", strconv.Quote(strconv.Itoa(i)))
}
if v != i {
t.Errorf("TinyMap.Get: value of key %s is not expected; expect=%d; got=%d\n",
strconv.Quote(strconv.Itoa(i)), i, v)
}
}
}
func TestTinySet_Contains(t *testing.T) {
tiny := NewTinySetFromSlice(tinyInts)
for _, i := range tinyInts {
if !tiny.Contains(i) {
t.Errorf("TinySet.Contains: element %s not exists\n", strconv.Quote(strconv.Itoa(i)))
}
}
}