This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
/
set_ts_test.go
331 lines (269 loc) · 6.27 KB
/
set_ts_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package set
import (
"reflect"
"strconv"
"strings"
"testing"
)
func TestSet_New(t *testing.T) {
s := newTS()
if s.Size() != 0 {
t.Error("New: calling without any parameters should create a set with zero size")
}
}
func TestSet_New_parameters(t *testing.T) {
s := newTS()
s.Add("string", "another_string", 1, 3.14)
if s.Size() != 4 {
t.Error("New: calling with parameters should create a set with size of four")
}
}
func TestSet_Add(t *testing.T) {
s := newTS()
s.Add(1)
s.Add(2)
s.Add(2) // duplicate
s.Add("fatih")
s.Add("zeynep")
s.Add("zeynep") // another duplicate
if s.Size() != 4 {
t.Error("Add: items are not unique. The set size should be four")
}
if !s.Has(1, 2, "fatih", "zeynep") {
t.Error("Add: added items are not availabile in the set.")
}
}
func TestSet_Add_multiple(t *testing.T) {
s := newTS()
s.Add("ankara", "san francisco", 3.14)
if s.Size() != 3 {
t.Error("Add: items are not unique. The set size should be three")
}
if !s.Has("ankara", "san francisco", 3.14) {
t.Error("Add: added items are not availabile in the set.")
}
}
func TestSet_Remove(t *testing.T) {
s := newTS()
s.Add(1)
s.Add(2)
s.Add("fatih")
s.Remove(1)
if s.Size() != 2 {
t.Error("Remove: set size should be two after removing")
}
s.Remove(1)
if s.Size() != 2 {
t.Error("Remove: set size should be not change after trying to remove a non-existing item")
}
s.Remove(2)
s.Remove("fatih")
if s.Size() != 0 {
t.Error("Remove: set size should be zero")
}
s.Remove("fatih") // try to remove something from a zero length set
}
func TestSet_Remove_multiple(t *testing.T) {
s := newTS()
s.Add("ankara", "san francisco", 3.14, "istanbul")
s.Remove("ankara", "san francisco", 3.14)
if s.Size() != 1 {
t.Error("Remove: items are not unique. The set size should be four")
}
if !s.Has("istanbul") {
t.Error("Add: added items are not availabile in the set.")
}
}
func TestSet_Pop(t *testing.T) {
s := newTS()
s.Add(1)
s.Add(2)
s.Add("fatih")
a := s.Pop()
if s.Size() != 2 {
t.Error("Pop: set size should be two after popping out")
}
if s.Has(a) {
t.Error("Pop: returned item should not exist")
}
s.Pop()
s.Pop()
b := s.Pop()
if b != nil {
t.Error("Pop: should return nil because set is empty")
}
s.Pop() // try to remove something from a zero length set
}
func TestSet_Has(t *testing.T) {
s := newTS()
s.Add("1", "2", "3", "4")
if !s.Has("1") {
t.Error("Has: the item 1 exist, but 'Has' is returning false")
}
if !s.Has("1", "2", "3", "4") {
t.Error("Has: the items all exist, but 'Has' is returning false")
}
}
func TestSet_Clear(t *testing.T) {
s := newTS()
s.Add(1)
s.Add("istanbul")
s.Add("san francisco")
s.Clear()
if s.Size() != 0 {
t.Error("Clear: set size should be zero")
}
}
func TestSet_IsEmpty(t *testing.T) {
s := newTS()
empty := s.IsEmpty()
if !empty {
t.Error("IsEmpty: set is empty, it should be true")
}
s.Add(2)
s.Add(3)
notEmpty := s.IsEmpty()
if notEmpty {
t.Error("IsEmpty: set is filled, it should be false")
}
}
func TestSet_IsEqual(t *testing.T) {
// same size, same content
s := newTS()
s.Add("1", "2", "3")
u := newTS()
u.Add("1", "2", "3")
ok := s.IsEqual(u)
if !ok {
t.Error("IsEqual: set s and t are equal. However it returns false")
}
// same size, different content
a := newTS()
a.Add("1", "2", "3")
b := newTS()
b.Add("4", "5", "6")
ok = a.IsEqual(b)
if ok {
t.Error("IsEqual: set a and b are now equal (1). However it returns true")
}
// different size, similar content
a = newTS()
a.Add("1", "2", "3")
b = newTS()
b.Add("1", "2", "3", "4")
ok = a.IsEqual(b)
if ok {
t.Error("IsEqual: set s and t are now equal (2). However it returns true")
}
}
func TestSet_IsSubset(t *testing.T) {
s := newTS()
s.Add("1", "2", "3", "4")
u := newTS()
u.Add("1", "2", "3")
ok := s.IsSubset(u)
if !ok {
t.Error("IsSubset: u is a subset of s. However it returns false")
}
ok = u.IsSubset(s)
if ok {
t.Error("IsSubset: s is not a subset of u. However it returns true")
}
}
func TestSet_IsSuperset(t *testing.T) {
s := newTS()
s.Add("1", "2", "3", "4")
u := newTS()
u.Add("1", "2", "3")
ok := u.IsSuperset(s)
if !ok {
t.Error("IsSuperset: s is a superset of u. However it returns false")
}
ok = s.IsSuperset(u)
if ok {
t.Error("IsSuperset: u is not a superset of u. However it returns true")
}
}
func TestSet_String(t *testing.T) {
s := newTS()
if s.String() != "[]" {
t.Errorf("String: output is not what is excepted '%s'", s.String())
}
if !strings.HasPrefix(s.String(), "[") {
t.Error("String: output should begin with a square bracket")
}
if !strings.HasSuffix(s.String(), "]") {
t.Error("String: output should end with a square bracket")
}
}
func TestSet_List(t *testing.T) {
s := newTS()
s.Add("1", "2", "3", "4")
// this returns a slice of interface{}
if len(s.List()) != 4 {
t.Error("List: slice size should be four.")
}
for _, item := range s.List() {
r := reflect.TypeOf(item)
if r.Kind().String() != "string" {
t.Error("List: slice item should be a string")
}
}
}
func TestSet_Copy(t *testing.T) {
s := newTS()
s.Add("1", "2", "3", "4")
r := s.Copy()
if !s.IsEqual(r) {
t.Error("Copy: set s and r are not equal")
}
}
func TestSet_Merge(t *testing.T) {
s := newTS()
s.Add("1", "2", "3")
r := newTS()
r.Add("3", "4", "5")
s.Merge(r)
if s.Size() != 5 {
t.Error("Merge: the set doesn't have all items in it.")
}
if !s.Has("1", "2", "3", "4", "5") {
t.Error("Merge: merged items are not availabile in the set.")
}
}
func TestSet_Separate(t *testing.T) {
s := newTS()
s.Add("1", "2", "3")
r := newTS()
r.Add("3", "5")
s.Separate(r)
if s.Size() != 2 {
t.Error("Separate: the set doesn't have all items in it.")
}
if !s.Has("1", "2") {
t.Error("Separate: items after separation are not availabile in the set.")
}
}
func TestSet_RaceAdd(t *testing.T) {
// Create two sets. Add concurrently items to each of them. Remove from the
// other one.
// "go test -race" should detect this if the library is not thread-safe.
s := newTS()
u := newTS()
go func() {
for i := 0; i < 1000; i++ {
item := "item" + strconv.Itoa(i)
go func(i int) {
s.Add(item)
u.Add(item)
}(i)
}
}()
for i := 0; i < 1000; i++ {
item := "item" + strconv.Itoa(i)
go func(i int) {
s.Add(item)
u.Add(item)
}(i)
}
}