-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_test.go
More file actions
598 lines (509 loc) · 14.5 KB
/
set_test.go
File metadata and controls
598 lines (509 loc) · 14.5 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package set
import (
"fmt"
"log"
"testing"
)
// TESTS
func TestNew(t *testing.T) {
testSet := New()
if testSet.Size() != 0 {
t.Errorf("A new set should have a size of 0")
}
testSet = New("one", "two", "three", 1, 2, 3)
if testSet.Size() != 6 {
t.Errorf("A new set initialized with 6 items should have a size of 6")
}
items := []interface{}{"english", "spanish", "italian", "french"}
testSet = New(items...)
if testSet.Size() != len(items) {
t.Errorf("A new set initialized with an array of items should have a size equal to the len of the array")
}
}
func TestAdd(t *testing.T) {
testSet := New()
testSet.Add("one")
if testSet.Size() != 1 {
t.Errorf("Adding one string item to a new set should have a size of 1")
}
testSet.Add(4)
if testSet.Size() != 2 {
t.Errorf("Adding an int item should increase size to 2")
}
testSet.Add("three")
if testSet.Size() != 3 {
t.Errorf("Adding another unique string item should increase size to 3")
}
testSet.Add("one")
if testSet.Size() != 3 {
t.Errorf("Adding a duplicate item should not increase the size")
}
testSet = New()
for i := 0; i < 100; i++ {
testSet.Add(i)
}
if testSet.Size() != 100 {
t.Errorf("Adding 100 int items to a new set should have a size of 100")
}
testSet = New()
for i := 0; i < 100; i++ {
testSet.Add(fmt.Sprintf("item_num_%d", i))
}
if testSet.Size() != 100 {
t.Errorf("Adding 100 string items to a new set should have a size of 100")
}
}
func TestRemove(t *testing.T) {
testSet := New()
testSet.Add("one")
testSet.Add("two")
sampleSize := testSet.Size()
testSet.Remove("one")
if testSet.Size() != sampleSize-1 {
t.Errorf("Removing an item from a set should decrease the size by 1")
}
sampleSize = testSet.Size()
testSet.Remove("unknown")
if testSet.Size() != sampleSize {
t.Errorf("Removing an item that is not in a set should not change the set size")
}
testSet = New()
for i := 0; i < 100; i++ {
testSet.Add(i)
}
for i := 0; i < 100; i++ {
testSet.Remove(i)
}
if testSet.Size() != 0 {
t.Errorf("Adding 100 items and them removing them should have a set size 0")
}
}
func TestContains(t *testing.T) {
testSet := New()
if testSet.Contains("nothing") {
t.Errorf("A new set should not contain an item")
}
testSet.Add("English")
if testSet.Contains("English") == false {
t.Errorf("After adding an item, the set should contain that item")
}
if testSet.Contains("english") {
t.Errorf("String items in the set should be case sensitive")
}
testSet.Add(6453)
if testSet.Contains(6453) == false {
t.Errorf("After adding an int item, the set should contain that item")
}
if testSet.Contains("arbitrary item") {
t.Errorf("After adding items, a set should not contain an arbitrary string item")
}
if testSet.Contains(29543) {
t.Errorf("After adding items, a set should not contain an arbitrary int item")
}
}
func TestEnumerate(t *testing.T) {
items := []interface{}{"english", "spanish", 5, 8, "french"}
testSet := New()
for i := range items {
testSet.Add(items[i])
}
enum := testSet.Enumerate()
for i := range items {
found := false
for j := range enum {
if items[i] == enum[j] {
found = true
break
}
}
if found == false {
t.Errorf("Enumerate should contains the elements that were added")
}
}
for i := range enum {
found := false
for j := range items {
if enum[i] == items[j] {
found = true
break
}
}
if found == false {
t.Errorf("Enumerate shouldn't contain elements that were not added")
}
}
}
func TestIsEmpty(t *testing.T) {
testSet := New()
if testSet.IsEmpty() == false {
t.Errorf("A new set should be empty")
}
testSet.Add("one")
if testSet.IsEmpty() {
t.Errorf("A set after adding an item should not be empty")
}
testSet.Remove("one")
if testSet.IsEmpty() == false {
t.Errorf("A set after removing the only item should be empty")
}
testSet = New()
items := []interface{}{"english", "spanish", 5, 8, "french"}
for i := range items {
testSet.Add(items[i])
}
if testSet.IsEmpty() {
t.Errorf("A set after adding several items should not be empty")
}
for i := range items {
testSet.Remove(items[i])
}
if testSet.IsEmpty() == false {
t.Errorf("A set after removing all of the items that were added should be empty")
}
}
func TestClear(t *testing.T) {
testSet := New()
items := []interface{}{"english", "spanish", 5, 8, "french"}
for i := range items {
testSet.Add(items[i])
}
testSet.Clear()
if testSet.Contains("spanish") {
t.Errorf("After clearing a set, it should not contain an item that was added")
}
if testSet.IsEmpty() == false {
t.Errorf("After clearing a set, the set should be empty")
}
}
func TestMap(t *testing.T) {
testSet := New()
items := []int{1, 2, 3, 4}
for i := range items {
testSet.Add(items[i])
}
mappedSet := testSet.Map(func(item interface{}) interface{} {
return item.(int) * 2
})
for i := range items {
if mappedSet.Contains(items[i]*2) == false {
t.Errorf("The result of the int * 2 mapping function should contain the value %d", items[i]*2)
}
}
}
func TestFilter(t *testing.T) {
testSet := New("ken", "brad", "jeff", "ryan", "tim", "greg", "scott")
filterFunc := func(i interface{}) bool {
if len(i.(string)) == 4 {
return true
}
return false
}
includedItems := []interface{}{"brad", "jeff", "ryan", "greg"}
excludedItems := []interface{}{"ken", "tim", "scott"}
filterSet := testSet.Filter(filterFunc)
if filterSet.Size() != len(includedItems) {
t.Errorf("There should be %d elements in the filtered set (%d found)", len(includedItems), filterSet.Size())
}
for i := range includedItems {
if filterSet.Contains(includedItems[i]) == false {
t.Errorf("All four letter items should be inluded in the filtered set (%v missing)", includedItems[i])
}
}
for i := range excludedItems {
if filterSet.Contains(excludedItems[i]) {
t.Errorf("Only four letter items should be included in the filtered set (%v unexpected)", excludedItems[i])
}
}
}
func TestPop(t *testing.T) {
items := []interface{}{"english", "spanish", "french", "italian"}
testSet := New(items...)
setSize := testSet.Size()
item := testSet.Pop()
if testSet.Size() != setSize-1 {
t.Errorf("Pop on a non-zero size set should reduce the set size by one")
}
if testSet.Contains(item) {
t.Errorf("The item returned by Pop should no longer be contained in the set")
}
found := false
for i := range items {
if item == items[i] {
found = true
break
}
}
if found == false {
t.Errorf("The result of Pop should have been one of the items that was added to the set")
}
testSet = New()
item = testSet.Pop()
if item != nil {
t.Errorf("The expected result of Pop on an empty set is nil, the actual result was %v", item)
}
for i := 0; i < 100; i++ {
testSet.Add(i)
}
for i := 0; i < 100; i++ {
_ = testSet.Pop()
}
if testSet.IsEmpty() == false {
t.Errorf("Popping the same number of unique items that were added should result in an empty set")
}
}
func TestSubset(t *testing.T) {
testSet := New("one", "two", "three", 4, 5, 6)
superSet := New("one", "two", "three", "four", "five", "six", 1, 2, 3, 4, 5, 6)
altSet := New(1, 2, 3, 4, 5, 6)
if testSet.Subset(superSet) == false {
t.Errorf("testSet should be a subset of superSet")
}
if testSet.Subset(altSet) {
t.Errorf("testSet is not a subset of altSet")
}
}
func TestUnion(t *testing.T) {
groupOne := []interface{}{"red", "blue", "green"}
groupTwo := []interface{}{"red", "yellow", "purple", "black"}
setOne := New(groupOne...)
setTwo := New(groupTwo...)
setOneSize := setOne.Size()
setTwoSize := setTwo.Size()
unionSet := Union(setOne, setOne)
if unionSet.Size() != setOne.Size() {
t.Errorf("The size of the union of two identical sets should be the size of the original set")
}
unionSet = Union(setOne, setTwo)
if setOne.Size() != setOneSize {
t.Errorf("Union should not affect the size of the first set")
}
if setTwo.Size() != setTwoSize {
t.Errorf("Union should not affect the size of the second set")
}
if unionSet.Size() != (setOne.Size() + setTwo.Size() - 1) {
t.Errorf("The size of the union of two sets with one common item should be the sum of the sizes of the two sets minus 1")
}
for i := range groupOne {
if unionSet.Contains(groupOne[i]) == false {
t.Errorf("The union of two sets should contain all of the elements of the first set, missing %v", groupOne[i])
}
}
for i := range groupTwo {
if unionSet.Contains(groupTwo[i]) == false {
t.Errorf("The union of two sets should contain all of the elements of the second set, missing %v", groupTwo[i])
}
}
}
func TestIntersection(t *testing.T) {
groupOne := []interface{}{"red", "blue", "green"}
groupTwo := []interface{}{"red", "yellow", "purple", "black", "blue"}
groupIntersection := []interface{}{"red", "blue"}
groupDifference := []interface{}{"green", "yellow", "purple", "black"}
setOne := New(groupOne...)
setTwo := New(groupTwo...)
setOneSize := setOne.Size()
setTwoSize := setTwo.Size()
intersectionSet := Intersection(setOne, setOne)
if intersectionSet.Size() != setOne.Size() {
t.Errorf("The size of the intersection of two identical sets should be the size of the original set")
}
intersectionSet = Intersection(setOne, setTwo)
if setOne.Size() != setOneSize {
t.Errorf("Intersection should not affect the size of the first set")
}
if setTwo.Size() != setTwoSize {
t.Errorf("Intersection should not affect the size of the second set")
}
if intersectionSet.Size() != len(groupIntersection) {
t.Errorf("The size of the intersection of two sets with %d common items should be %d", len(groupIntersection), len(groupIntersection))
}
for i := range groupIntersection {
if intersectionSet.Contains(groupIntersection[i] == false) {
t.Errorf("The intersection should contain all of the common items: missing %v", groupIntersection[i])
}
}
for i := range groupDifference {
if intersectionSet.Contains(groupDifference[i]) {
t.Errorf("The intersection should not contain any of the items that are not common to both sets: not expecting %v", groupDifference[i])
}
}
}
func TestDifference(t *testing.T) {
groupOne := []interface{}{"red", "blue", "green"}
groupTwo := []interface{}{"red", "yellow", "purple", "black", "blue"}
groupIntersection := []interface{}{"red", "blue"}
groupDifference := []interface{}{"green", "yellow", "purple", "black"}
setOne := New(groupOne...)
setTwo := New(groupTwo...)
setOneSize := setOne.Size()
setTwoSize := setTwo.Size()
differenceSet := Difference(setOne, setOne)
if differenceSet.Size() != 0 {
t.Errorf("The size of the difference of two identical sets should be 0")
}
differenceSet = Difference(setOne, setTwo)
if setOne.Size() != setOneSize {
t.Errorf("Difference should not affect the size of the first set")
}
if setTwo.Size() != setTwoSize {
t.Errorf("Difference should not affect the size of the second set")
}
if differenceSet.Size() != len(groupDifference) {
t.Errorf("The size of the difference of two sets with %d diffent items should be %d", len(groupDifference), len(groupDifference))
}
for i := range groupDifference {
if differenceSet.Contains(groupDifference[i] == false) {
t.Errorf("The difference should contain all of the different items: missing %v", groupDifference[i])
}
}
for i := range groupIntersection {
if differenceSet.Contains(groupIntersection[i]) {
t.Errorf("The difference should not contain any of the items that are common to both sets: not expecting %v", groupIntersection[i])
}
}
}
// BENCHMARKS
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = New()
}
}
func BenchmarkNewWithItems(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = New("red", "blue", "green", "yellow")
}
}
func BenchmarkAddInts(b *testing.B) {
set := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Add(i)
}
}
func BenchmarkAddStrings(b *testing.B) {
set := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Add(fmt.Sprintf("item_num_%d", i))
}
}
func BenchmarkRemoveInts(b *testing.B) {
set := New()
for i := 0; i < 40000000; i++ {
set.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Remove(i)
}
}
func BenchmarkRemoveStrings(b *testing.B) {
set := New()
for i := 0; i < 10000000; i++ {
set.Add(fmt.Sprintf("item_num_%d", i))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Remove(fmt.Sprintf("item_num_%d", i))
}
}
func BenchmarkContainsInts(b *testing.B) {
set := New()
for i := 0; i < 1000000; i++ {
set.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Contains(i)
}
}
func BenchmarkContainsStrings(b *testing.B) {
set := New()
for i := 0; i < 1000000; i++ {
set.Add(fmt.Sprintf("item_num_%d", i))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Contains(fmt.Sprintf("item_num_%d", i))
}
}
func BenchmarkSize(b *testing.B) {
set := New()
for i := 0; i < 1000000; i++ {
set.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Size()
}
}
func BenchmarkEnumerate(b *testing.B) {
set := New()
for i := 0; i < 1000; i++ {
set.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Enumerate()
}
}
func BenchmarkPop(b *testing.B) {
set := New()
for i := 0; i < 1000; i++ {
set.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Pop()
}
}
func BenchmarkUnion(b *testing.B) {
set1 := New()
set2 := New()
for i := 0; i < 1000; i++ {
set1.Add(i)
set2.Add(fmt.Sprintf("item_num_%d", i))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Union(set1, set2)
}
}
// EXAMPLES
func ExampleSet() {
// Creating a new empty set
set := New()
// Add items to the set
set.Add("blue")
set.Add("red")
set.Add("green")
// How many items does the set contain?
log.Println(set.Size()) // 3
// Adding a duplicate item doesn't change the set size
set.Add("blue")
log.Println(set.Size()) // 3
// Test to see if a set contains a specific item
log.Println(set.Contains("blue")) // true
// Remove an item from the set
set.Remove("blue")
log.Println(set.Contains("blue")) // false
// Create a set with initial values
set = New(1, 2, 3, 5, 7, 11)
// Find the intersection of two sets
multiplesOf2 := New(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
multiplesOf3 := New(3, 6, 9, 12, 15, 18, 21)
multiplesOf2And3 := Union(multiplesOf2, multiplesOf3)
log.Println(multiplesOf2And3.Enumerate()) // {6, 12, 18}
}
func ExampleNew() {
// Create an empty set
emptySet := New()
log.Println(emptySet.IsEmpty()) // true
// Create a set with initial values
colors := New("red", "blue", "green")
log.Println(colors.Size()) // 3
// Create a set with initial values from a slice of interfaces
slice := []interface{}{1, 2, 3, 4, "one", "two", "three", "four"}
numbers := New(slice...)
log.Println(numbers.Size()) // 8
}