forked from brentp/intintmap
-
Notifications
You must be signed in to change notification settings - Fork 4
/
set_test.go
149 lines (126 loc) · 2.73 KB
/
set_test.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package intmap
import "testing"
func TestSet(t *testing.T) {
s := NewSet[int](0)
if sz := s.Len(); sz != 0 {
t.Fatalf("length of set must be 0: %d", sz)
}
if added := s.Add(1); !added {
t.Fatalf("1 must be added")
}
if sz := s.Len(); sz != 1 {
t.Fatalf("length of set must be 1: %d", sz)
}
if added := s.Add(1); added {
t.Fatalf("1 must not be added")
}
if sz := s.Len(); sz != 1 {
t.Fatalf("length of set must be 1: %d", sz)
}
if added := s.Add(2); !added {
t.Fatalf("2 must be added")
}
if sz := s.Len(); sz != 2 {
t.Fatalf("length of set must be 2: %d", sz)
}
if !(s.Has(1) && s.Has(2)) {
t.Fatalf("set must have both 1 and 2")
}
sum := 0
s.ForEach(func(k int) bool {
sum += k
return true
})
if sum != 3 {
t.Fatalf("total sum of elements must be 3")
}
}
func TestSetClear(t *testing.T) {
s := NewSet[int](0)
s.Add(1)
s.Add(2)
if sz := s.Len(); sz != 2 {
t.Fatalf("unexpected set len %d", sz)
}
if !s.Has(1) || !s.Has(2) {
t.Fatalf("set must contain 1 and 2 before Clear()")
}
s.Clear()
if sz := s.Len(); sz != 0 {
t.Fatalf("unexpected set len %d", sz)
}
if s.Has(1) || s.Has(2) {
t.Fatalf("set must not contain 1 or 2 after Clear()")
}
}
func TestSetDel(t *testing.T) {
s := NewSet[int](0)
s.Add(1)
s.Add(2)
if sz := s.Len(); sz != 2 {
t.Fatalf("unexpected set len %d", sz)
}
if !s.Has(1) || !s.Has(2) {
t.Fatalf("set must contain 1 and 2 before Clear()")
}
if found := s.Del(27); found {
t.Fatalf("set must not contain 27")
}
// Delete 2
if found := s.Del(2); !found {
t.Fatalf("set must contain 2 on delete")
}
if sz := s.Len(); sz != 1 {
t.Fatalf("unexpected set len %d", sz)
}
if s.Has(2) {
t.Fatalf("set must not contain 2 after Del(2)")
}
if found := s.Del(2); found {
t.Fatalf("set must not contain 2 on seconb deletion")
}
// Delete 1
if found := s.Del(1); !found {
t.Fatalf("set must contain 1 on delete")
}
if sz := s.Len(); sz != 0 {
t.Fatalf("unexpected set len %d", sz)
}
if s.Has(1) {
t.Fatalf("set must not contain 1 after Del(1)")
}
if found := s.Del(1); found {
t.Fatalf("set must not contain 1 on seconb deletion")
}
}
func TestNilSet(t *testing.T) {
var s *Set[int]
if sz := s.Len(); sz != 0 {
t.Fatalf("length of nil set must be 0: %d", sz)
}
if s.Has(0) || s.Has(1) {
t.Fatalf("nil set must not have 0 or 1")
}
count := 0
s.ForEach(func(k int) bool {
count++
return true
})
if count != 0 {
t.Fatalf("total count of elements in nil set must be 0")
}
}
func TestSetIter(t *testing.T) {
s := NewSet[int](10)
for i := 0; i < 100; i++ {
s.Add(i)
}
sum := 0
for k := range s.All() {
sum += k
}
const sumTo99 = 99 * (99 + 1) / 2
if sum != sumTo99 {
t.Fatalf("unexpected sum: %d, want %d", sum, sumTo99)
}
}