-
Notifications
You must be signed in to change notification settings - Fork 10
/
set_test.go
148 lines (116 loc) · 2.41 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
package graphs
import (
"testing"
)
func TestSetAdd(t *testing.T) {
set := NewSet[string]()
foo := "foo"
if added := set.Add(foo); !added {
t.Error("foo should not exist")
}
if added := set.Add(foo); added {
t.Error("foo should exist")
}
}
func TestSetLen(t *testing.T) {
set := NewSet[string]()
if set.Len() != 0 {
t.Error("set length should be 0")
}
set.Add("foo")
if set.Len() != 1 {
t.Error("set length should be 1")
}
set.Add("bar")
if set.Len() != 2 {
t.Error("set length should be 2")
}
set.Add("bar")
if set.Len() != 2 {
t.Error("set length should be 2")
}
}
func TestSetEquals(t *testing.T) {
s1 := NewSet[string]()
s2 := NewSet[string]()
if s1.Equals(nil) {
t.Error("no set is equal to a nil set")
}
if !s1.Equals(s2) {
t.Error("two empty sets should be equal")
}
s1.Add("foo")
s2.Add("foo")
if !s1.Equals(s2) {
t.Error("two sets with both one element should be equal")
}
s1.Add("moo")
if s1.Equals(s2) {
t.Error("two sets with different length should not be equal")
}
s2.Add("cow")
if s1.Equals(s2) {
t.Error("two sets with different elements should not be equal")
}
}
func TestSetContains(t *testing.T) {
set := NewSet[string]()
set.Add("foo")
if !set.Contains("foo") {
t.Error("set should contain foo")
}
if set.Contains("bar") {
t.Error("set should not contain bar")
}
}
func TestSetMerge(t *testing.T) {
s1 := NewSet[string]()
s1.Add("foo")
s2 := NewSet[string]()
s2.Add("bar")
s2.Merge(s1)
if s2.Len() != 2 {
t.Error("merged set should have two elements")
}
}
func TestSetRemove(t *testing.T) {
set := NewSet[string]()
set.Add("foo")
set.Add("bar")
set.Remove("foo")
if set.Len() != 1 {
t.Error("set should contain one element")
}
if !set.Contains("bar") {
t.Error("set should contain bar")
}
}
func TestSetAny(t *testing.T) {
set := NewSetWithElements[string]("foo", "bar")
if e := set.Any(); e != "bar" && e != "foo" {
t.Error("any should return bar or foo")
}
}
func TestEach(t *testing.T) {
set := NewSetWithElements[string]("foo", "bar", "baz")
count := 0
set.Each(func(element string, stop func()) {
count += 1
})
if count != 3 {
t.Error("count should be 3")
}
}
func TestEachStop(t *testing.T) {
set := NewSetWithElements[string]("foo", "bar", "baz")
count := 0
set.Each(func(element string, stop func()) {
count += 1
if count == 2 {
stop()
}
})
if count != 2 {
t.Error("count should be 2")
}
}