forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_test.go
200 lines (190 loc) · 5.49 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package set
import (
"testing"
)
func TestNew(t *testing.T) {
set := New(1, 2, 3)
if set.Len() != 3 {
t.Errorf("expecting 3 elements in the set but got %v: %v", set.Len(), set.GetItems())
}
for _, n := range []int{1, 2, 3} {
if !set.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, set.GetItems())
}
}
if set.In(5) {
t.Errorf("expecting 5 not to be present in the set but it was; set is %v", set.GetItems())
}
}
func TestAdd(t *testing.T) {
td := []struct {
name string
input int
expElems []int
}{
{"add new element", 5, []int{1, 2, 3, 5}},
{"add exiting element", 3, []int{1, 2, 3}},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := New(1, 2, 3)
s.Add(tc.input)
if s.Len() != len(tc.expElems) {
t.Errorf("expecting %d elements in the set but got %d: set is %v", len(tc.expElems), s.Len(), s.GetItems())
}
for _, n := range tc.expElems {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
func TestDelete(t *testing.T) {
td := []struct {
name string
input int
expElems []int
}{
{"delete exiting element", 3, []int{1, 2}},
{"delete not existing element", 5, []int{1, 2, 3}},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := New(1, 2, 3)
s.Delete(tc.input)
if s.Len() != len(tc.expElems) {
t.Errorf("expecting %d elements in the set but got %d: set is %v", len(tc.expElems), s.Len(), s.GetItems())
}
for _, n := range tc.expElems {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
func TestIsSubsetOf(t *testing.T) {
s1, s2 := New(1, 2, 3), New(1, 2, 3, 4)
if !s1.IsSubsetOf(s2) {
t.Errorf("expecting %v to be a subset of %v", s1, s2)
}
if s2.IsSubsetOf(s1) {
t.Errorf("expecting %v not to be a subset of %v", s2, s1)
}
if s3 := New(1, 2, 5, 6); s1.IsSubsetOf(s3) {
t.Errorf("expecting %v not to be a subset of %v", s1, s3)
}
}
func TestIsSupersetOf(t *testing.T) {
s1, s2 := New(1, 2, 3), New(1, 2, 3, 4)
if !s2.IsSupersetOf(s1) {
t.Errorf("expecting %v to be a superset of %v", s2, s1)
}
if s1.IsSupersetOf(s2) {
t.Errorf("expecting %v not to be a superset of %v", s1, s2)
}
if s3 := New(1, 2, 5); s2.IsSupersetOf(s3) {
t.Errorf("expecting %v not to be a superset of %v", s2, s3)
}
}
func TestUnion(t *testing.T) {
td := []struct {
name string
s1 Set
s2 Set
expSet Set
}{
{"union of different sets", New(1, 2, 3), New(4, 5, 6), New(1, 2, 3, 4, 5, 6)},
{"union of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(1, 2, 3, 4)},
{"union of same sets", New(1, 2, 3), New(1, 2, 3), New(1, 2, 3)},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := tc.s1.Union(tc.s2)
if s.Len() != tc.expSet.Len() {
t.Errorf("expecting %d elements in the set but got %d: set is %v", tc.expSet.Len(), s.Len(), s.GetItems())
}
for _, n := range tc.expSet.GetItems() {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
func TestIntersection(t *testing.T) {
td := []struct {
name string
s1 Set
s2 Set
expSet Set
}{
{"intersection of different sets", New(0, 1, 2, 3), New(4, 5, 6), New()},
{"intersection of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(1, 2)},
{"intersection of same sets", New(1, 2, 3), New(1, 2, 3), New(1, 2, 3)},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := tc.s1.Intersection(tc.s2)
if s.Len() != tc.expSet.Len() {
t.Errorf("expecting %d elements in the set but got %d: set is %v", tc.expSet.Len(), s.Len(), s.GetItems())
}
for _, n := range tc.expSet.GetItems() {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
func TestDifference(t *testing.T) {
td := []struct {
name string
s1 Set
s2 Set
expSet Set
}{
{"difference of different sets", New(1, 2, 3), New(4, 5, 6), New(1, 2, 3)},
{"difference of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(3)},
{"difference of same sets", New(1, 2, 3), New(1, 2, 3), New()},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := tc.s1.Difference(tc.s2)
if s.Len() != tc.expSet.Len() {
t.Errorf("expecting %d elements in the set but got %d: set is %v", tc.expSet.Len(), s.Len(), s.GetItems())
}
for _, n := range tc.expSet.GetItems() {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
func TestSymmetricDifference(t *testing.T) {
td := []struct {
name string
s1 Set
s2 Set
expSet Set
}{
{"symmetric difference of different sets", New(1, 2, 3), New(4, 5, 6), New(1, 2, 3, 4, 5, 6)},
{"symmetric difference of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(3, 4)},
{"symmetric difference of same sets", New(1, 2, 3), New(1, 2, 3), New()},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := tc.s1.SymmetricDifference(tc.s2)
if s.Len() != tc.expSet.Len() {
t.Errorf("expecting %d elements in the set but got %d: set is %v", tc.expSet.Len(), s.Len(), s.GetItems())
}
for _, n := range tc.expSet.GetItems() {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}