-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_test.go
More file actions
279 lines (252 loc) · 5.35 KB
/
set_test.go
File metadata and controls
279 lines (252 loc) · 5.35 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
package main
import (
"math/rand"
"reflect"
"testing"
)
func TestGetBucket(t *testing.T) {
tests := []struct {
name string
input int
expected int
}{
{"Bucket for 63", 63, 0},
{"Bucket for 64", 64, 1},
{"Bucket for 100", 100, 1},
{"Bucket for 640", 640, 10},
{"Bucket for 0", 0, 0},
{"Bucket for 128", 128, 2},
{"Bucket for 512", 512, 8},
{"Bucket for 1023", 1023, 15},
}
s := NewSet()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := s.getBucket(tt.input)
if result != tt.expected {
t.Errorf("getBucket(%d) = %d; want %d", tt.input, result, tt.expected)
}
})
}
}
func TestSet(t *testing.T) {
tests := []struct {
name string
actions func(*Set)
expected map[int]bool // Key is the value to check, value is whether it should be contained in the set
}{
{
name: "Add 0 and 10, check contains",
actions: func(s *Set) {
s.Add(0)
s.Add(10)
},
expected: map[int]bool{10: true, 0: true, 11: false},
},
{
name: "Add 100, check contains",
actions: func(s *Set) {
s.Add(100)
},
expected: map[int]bool{100: true, 11: false},
},
{
name: "Add 100, check contains",
actions: func(s *Set) {
s.Add(1000000)
},
expected: map[int]bool{1000000: true, 11: false},
},
{
name: "Add 100, check contains",
actions: func(s *Set) {
s.Add(1)
s.Remove(1)
},
expected: map[int]bool{1: false},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewSet()
tt.actions(s)
for value, want := range tt.expected {
if got := s.Contains(int(value)); got != want {
if want {
t.Errorf("expected set to contain %d, but it did not", value)
} else {
t.Errorf("expected set to not contain %d, but it did", value)
}
}
}
})
}
}
func TestIntersection(t *testing.T) {
type test struct {
name string
set1 []int
set2 []int
expected []int
}
tests := []test{
{
name: "Basic intersection",
set1: []int{1, 2, 100},
set2: []int{1, 100},
expected: []int{1, 100},
},
{
name: "No intersection",
set1: []int{1, 2, 3},
set2: []int{4, 5, 6},
expected: []int{},
},
{
name: "Identical sets",
set1: []int{10, 20, 30},
set2: []int{10, 20, 30},
expected: []int{10, 20, 30},
},
{
name: "One empty set",
set1: []int{1, 2, 3},
set2: []int{},
expected: []int{},
},
{
name: "Both empty sets",
set1: []int{},
set2: []int{},
expected: []int{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
s1 := NewSet()
s2 := NewSet()
for _, val1 := range tc.set1 {
s1.Add(val1)
}
for _, val2 := range tc.set2 {
s2.Add(val2)
}
result := s1.Intersection(s2)
if !reflect.DeepEqual(result, tc.expected) {
t.Errorf("expected: %v, got %v", tc.expected, result)
}
})
}
}
func BenchmarkSet_Add(b *testing.B) {
s := NewSet()
for i := 0; i < b.N; i++ {
s.Add(rand.Intn(1000000))
}
}
func BenchmarkMap_Add(b *testing.B) {
m := make(map[int]struct{})
for i := 0; i < b.N; i++ {
m[rand.Intn(1000000)] = struct{}{}
}
}
func BenchmarkSet_Contains(b *testing.B) {
s := NewSet()
for i := 0; i < 1000000; i++ {
s.Add(int(i))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = s.Contains(rand.Intn(1000000))
}
}
func BenchmarkMap_Contains(b *testing.B) {
m := make(map[int]struct{})
for i := 0; i < 1000000; i++ {
m[int(i)] = struct{}{}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = m[rand.Intn(1000000)]
}
}
func BenchmarkSet_Remove(b *testing.B) {
s := NewSet()
for i := 0; i < 1000000; i++ {
s.Add(int(i))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Remove(rand.Intn(1000000))
}
}
func BenchmarkMap_Remove(b *testing.B) {
m := make(map[int]struct{})
for i := 0; i < 1000000; i++ {
m[int(i)] = struct{}{}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
delete(m, rand.Intn(1000000))
}
}
func generateRandomInts(size, maxValue int) []int {
nums := make([]int, size)
for i := range nums {
nums[i] = rand.Intn(maxValue)
}
return nums
}
func populateSet(values []int) *Set {
set := NewSet()
for _, v := range values {
set.Add(v)
}
return set
}
func populateMap(values []int) map[int]struct{} {
m := make(map[int]struct{})
for _, v := range values {
m[v] = struct{}{}
}
return m
}
func mapIntersection(a, b map[int]struct{}) []int {
intersection := []int{}
for k := range a {
if _, exists := b[k]; exists {
intersection = append(intersection, k)
}
}
return intersection
}
func BenchmarkSetIntersection(b *testing.B) {
setSize := 10000000
otherSetSize := 10000000
maxValue := 100000000
// Generate test data
setData := generateRandomInts(setSize, maxValue)
otherSetData := generateRandomInts(otherSetSize, maxValue)
// Populate sets
setA := populateSet(setData)
setB := populateSet(otherSetData)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = setA.Intersection(setB)
}
}
func BenchmarkMapIntersection(b *testing.B) {
setSize := 10000000
otherSetSize := 10000000
maxValue := 100000000
// Generate test data
setData := generateRandomInts(setSize, maxValue)
otherSetData := generateRandomInts(otherSetSize, maxValue)
// Populate maps
mapA := populateMap(setData)
mapB := populateMap(otherSetData)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = mapIntersection(mapA, mapB)
}
}