-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashset_test.go
329 lines (287 loc) · 7.8 KB
/
hashset_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
package hashset
import (
"encoding/json"
"reflect"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
/*
2. slice가오면 set으로 변경되어야한다.
3. set을 slice로 변경할 수 있어야한다.
4. set(집합) 연산이 가능해야한다.
1. intersection
2. union
3. difference
5. 거의 모든타입 지원이 되어야한다.
6. 같은 set에 대해 동시작업이 원자성을 보장받아야한다.
*/
// Basic operations 0. Initialize
func TestInitializeFromArguments(t *testing.T) {
s := New(1, 2, "3")
require.NotEmpty(t, s)
require.Equal(t, 3, s.Len())
s.Add("3") // It lengths is 3, because Set already has "3"
require.NotEqual(t, 4, s.Len())
s1 := New([]int{1, 2, 3})
require.Equal(t, 3, s1.Len())
require.True(t, s1.Contains(2))
}
// Basic operations: 1. Add element
func TestAddElement(t *testing.T) {
s := New()
require.NotEmpty(t, s)
require.Equal(t, 0, s.Len())
s.Add("Add")
s.Add(" multiple")
s.Add("values of")
s.Add(3)
s.Add("like this:")
s.Add([]int{1, 2, 3})
require.Equal(t, 6, s.Len())
}
// Basic operations: 2. Remove element
func TestRemoveElement(t *testing.T) {
s := New("1", "2", 3)
require.NotEmpty(t, s)
require.Equal(t, 3, s.Len())
s.Remove("1")
s.Remove("multiple")
s.Remove("3")
require.Equal(t, 2, s.Len())
}
// Basic operations: 3. Check membership in set
func TestMembershipCheck(t *testing.T) {
caseFunction := func() bool { return true }
s := New("1", "2", "3", 1, caseFunction)
require.NotEmpty(t, s)
require.Equal(t, 5, s.Len())
require.True(t, s.Contains("1"))
require.True(t, s.Contains("2"))
require.True(t, s.Contains("3"))
require.False(t, s.Contains(2))
require.True(t, s.Contains(caseFunction))
}
func TestDuplicate(t *testing.T) {
// Same address testing
caseFunction := func() bool { return true }
caseMap := map[string]int{}
caseSlice := []int{1, 2, 3}
caseSliceTwo := []string{"1", "a", "b"}
s := New(caseFunction, caseMap, caseSlice, caseSliceTwo)
require.Equal(t, 8, s.Len())
s.Add(caseFunction)
s.Add(caseMap)
s.Add(caseSlice)
s.Add(caseSliceTwo)
require.Equal(t, 10, s.Len())
// However, if the same signature arguments address are different, it is not a duplicate
caseFunction2 := func() bool { return true }
caseMap2 := map[string]int{}
caseSlice2 := []int{1, 2, 3}
caseSliceTwo2 := []string{"1", "a", "b"}
s.Add(caseFunction2)
s.Add(caseMap2)
s.Add(caseSlice2)
s.Add(caseSliceTwo2)
// require.NotEqual(t, 4, s.Len())
require.Equal(t, 14, s.Len())
// Comparable types are duplicated checked.
s.Add(1)
s.Add(2)
s.Add("caseSlice2")
s.Add("caseSliceTwo2")
require.Equal(t, 16, s.Len())
s.Add(1)
s.Add(2)
s.Add("caseSlice2")
s.Add("caseSliceTwo2")
require.Equal(t, 16, s.Len())
}
func TestConvertToSet(t *testing.T) {
caseSlice := []int{1, 2, 3}
caseSliceTwo := []string{"1", "a", "b"}
s := New(caseSlice, caseSliceTwo)
require.Equal(t, 6, s.Len())
require.True(t, s.Contains(1))
require.True(t, s.Contains("1"))
require.True(t, s.Contains("b"))
}
func TestConvertToSlice(t *testing.T) {
caseSlice := []int{1, 2, 3}
caseSliceTwo := []string{"1", "a", "b"}
// Splitting slices
s := New(caseSlice, caseSliceTwo)
require.Equal(t, 6, s.Len())
// Converting set to slice
arr := s.ToSlice()
require.Equal(t, 6, len(arr))
require.True(t, reflect.ValueOf(arr).Kind() == reflect.Slice)
require.Contains(t, arr, 1)
require.Contains(t, arr, 3)
require.Contains(t, arr, "1")
require.Contains(t, arr, "b")
}
func TestUnion(t *testing.T) {
caseSlice := []int{1, 2, 3}
caseSliceTwo := []int{3, 4, 5}
s1 := New(caseSlice)
require.Equal(t, 3, s1.Len())
s2 := New(caseSliceTwo)
require.Equal(t, 3, s2.Len())
// Union numbers
s3 := s1.Union(s2)
require.Equal(t, 5, s3.Len())
require.True(t, s3.Contains(1))
require.True(t, s3.Contains(5))
require.True(t, s3.Contains(3))
// Union other types
caseSliceThree := []interface{}{1, 4, 5, "1", "5"}
s4 := New(caseSliceThree)
s5 := s3.Union(s4)
require.Equal(t, 7, s5.Len())
require.True(t, s5.Contains(1))
require.True(t, s5.Contains(5))
require.True(t, s5.Contains("5"))
}
func TestIntersection(t *testing.T) {
}
func TestDifference(t *testing.T) {}
func TestFunctionElement(t *testing.T) {}
func TestStructElement(t *testing.T) {}
func TestMarshalJSON(t *testing.T) {
caseSlice := []int{1, 2, 3}
caseSliceTwo := []string{"1", "a", "b"}
// Splitting slices
s := New(caseSlice, caseSliceTwo)
ms, err := json.Marshal(s)
require.NoError(t, err)
s2 := New()
err = json.Unmarshal(ms, s2)
require.NoError(t, err)
}
func TestConcurrentAddElement10Goroutine100000Loop(t *testing.T) {
var wg sync.WaitGroup
s := New()
numOfGoroutine := 10
numOfLoop := 100000
totalExpectElement := numOfGoroutine * numOfLoop
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
for nthWork := 0; nthWork < numOfLoop; nthWork++ {
s.Add(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
}
}(nthGoroutine)
}
wg.Wait()
require.Equal(t, totalExpectElement, s.Len())
}
func TestConcurrentAddElement100000Goroutine10Loop(t *testing.T) {
var wg sync.WaitGroup
s := New()
numOfGoroutine := 100000
numOfLoop := 10
totalExpectElement := numOfGoroutine * numOfLoop
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
for nthWork := 0; nthWork < numOfLoop; nthWork++ {
s.Add(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
}
}(nthGoroutine)
}
wg.Wait()
require.Equal(t, totalExpectElement, s.Len())
}
func TestConcurrentExclusiveLock4Goroutine100000Loop(t *testing.T) {
var wg sync.WaitGroup
s := New()
numOfGoroutine := 4
numOfLoop := 100000
for idx := 0; idx < numOfLoop; idx++ {
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
key := strconv.Itoa(idx) + ".testing-" + strconv.Itoa(nthGoroutine/2)
wg.Add(1)
go func(n int) {
defer wg.Done()
if n%2 == 0 {
for s.Contains(key) {
time.Sleep(time.Nanosecond)
}
s.Add(key)
} else {
for !s.Contains(key) {
time.Sleep(time.Nanosecond)
}
s.Remove(key)
}
}(nthGoroutine)
}
wg.Wait()
}
wg.Wait()
require.Equal(t, 0, s.Len())
}
func TestConcurrentRemoveElement(t *testing.T) {
var wg sync.WaitGroup
s := New()
numOfGoroutine := 1000
numOfLoop := 10
totalExpectElement := numOfGoroutine * numOfLoop
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
for nthWork := 0; nthWork < numOfLoop; nthWork++ {
s.Add(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
}
}(nthGoroutine)
}
wg.Wait()
require.Equal(t, totalExpectElement, s.Len())
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
for nthWork := 0; nthWork < numOfLoop; nthWork++ {
s.Remove(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
}
}(nthGoroutine)
}
wg.Wait()
require.Equal(t, 0, s.Len())
}
func TestConcurrentMembershipCheck(t *testing.T) {
var wg sync.WaitGroup
s := New()
numOfGoroutine := 10000
numOfLoop := 10
totalExpectElement := numOfGoroutine * numOfLoop
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
for nthWork := 0; nthWork < numOfLoop; nthWork++ {
s.Add(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
}
}(nthGoroutine)
}
wg.Wait()
require.Equal(t, totalExpectElement, s.Len())
for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
for nthWork := 0; nthWork < numOfLoop; nthWork++ {
s.Add(strconv.Itoa(n) + ".testing-phase-two-" + strconv.Itoa(nthWork))
s.Remove(strconv.Itoa(n) + ".testing-" + strconv.Itoa(nthWork))
}
}(nthGoroutine)
}
wg.Wait()
}