-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_test.go
188 lines (148 loc) · 3.69 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
package set
import (
"reflect"
"testing"
)
func Test_Union(t *testing.T) {
s := New("1", "2", "3")
r := New("3", "4", "5")
x := NewNonTS("5", "6", "7")
u := Union(s, r, x)
if settype := reflect.TypeOf(u).String(); settype != "*set.Set" {
t.Error("Union should derive its set type from the first passed set, got", settype)
}
if u.Size() != 7 {
t.Error("Union: the merged set doesn't have all items in it.")
}
if !u.Has("1", "2", "3", "4", "5", "6", "7") {
t.Error("Union: merged items are not availabile in the set.")
}
z := Union(x, r)
if z.Size() != 5 {
t.Error("Union: Union of 2 sets doesn't have the proper number of items.")
}
if settype := reflect.TypeOf(z).String(); settype != "*set.SetNonTS" {
t.Error("Union should derive its set type from the first passed set, got", settype)
}
}
func Test_Difference(t *testing.T) {
s := New("1", "2", "3")
r := New("3", "4", "5")
x := New("5", "6", "7")
u := Difference(s, r, x)
if u.Size() != 2 {
t.Error("Difference: the set doesn't have all items in it.")
}
if !u.Has("1", "2") {
t.Error("Difference: items are not availabile in the set.")
}
y := Difference(r, r)
if y.Size() != 0 {
t.Error("Difference: size should be zero")
}
}
func Test_Intersection(t *testing.T) {
s1 := New("1", "3", "4", "5")
s2 := New("2", "3", "5", "6")
s3 := New("4", "5", "6", "7")
u := Intersection(s1, s2, s3)
if u.Size() != 1 {
t.Error("Intersection: the set doesn't have all items in it.")
}
if !u.Has("5") {
t.Error("Intersection: items after intersection are not availabile in the set.")
}
}
func Test_SymmetricDifference(t *testing.T) {
s := New("1", "2", "3")
r := New("3", "4", "5")
u := SymmetricDifference(s, r)
if u.Size() != 4 {
t.Error("SymmetricDifference: the set doesn't have all items in it.")
}
if !u.Has("1", "2", "4", "5") {
t.Error("SymmetricDifference: items are not availabile in the set.")
}
}
func Test_StringSlice(t *testing.T) {
s := New("san francisco", "istanbul", 3.14, 1321, "ankara")
u := StringSlice(s)
if len(u) != 3 {
t.Error("StringSlice: slice should only have three items")
}
for _, item := range u {
r := reflect.TypeOf(item)
if r.Kind().String() != "string" {
t.Error("StringSlice: slice item should be a string")
}
}
}
func Test_IntSlice(t *testing.T) {
s := New("san francisco", "istanbul", 3.14, 1321, "ankara", 8876)
u := IntSlice(s)
if len(u) != 2 {
t.Error("IntSlice: slice should only have two items")
}
for _, item := range u {
r := reflect.TypeOf(item)
if r.Kind().String() != "int" {
t.Error("Intslice: slice item should be a int")
}
}
}
func BenchmarkSetEquality(b *testing.B) {
s := New()
u := New()
for i := 0; i < b.N; i++ {
s.Add(i)
u.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.IsEqual(u)
}
}
func BenchmarkSubset(b *testing.B) {
s := New()
u := New()
for i := 0; i < b.N; i++ {
s.Add(i)
u.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.IsSubset(u)
}
}
func benchmarkIntersection(b *testing.B, numberOfItems int) {
s1 := New()
s2 := New()
for i := 0; i < numberOfItems/2; i++ {
s1.Add(i)
}
for i := 0; i < numberOfItems; i++ {
s2.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Intersection(s1, s2)
}
}
func BenchmarkIntersection10(b *testing.B) {
benchmarkIntersection(b, 10)
}
func BenchmarkIntersection100(b *testing.B) {
benchmarkIntersection(b, 100)
}
func BenchmarkIntersection1000(b *testing.B) {
benchmarkIntersection(b, 1000)
}
func BenchmarkIntersection10000(b *testing.B) {
benchmarkIntersection(b, 10000)
}
func BenchmarkIntersection100000(b *testing.B) {
benchmarkIntersection(b, 100000)
}
func BenchmarkIntersection1000000(b *testing.B) {
benchmarkIntersection(b, 1000000)
}