@@ -24,29 +24,57 @@ import (
24
24
3. difference
25
25
26
26
5. 거의 모든타입 지원이 되어야한다.
27
- 1. 함수타입은 별도의 uuid와 포인터로 보관되어야한다.
28
- 2. 구조체타입도 uuid로 할까 마샬링으로 할까 했는데 uuid가 좋아보이넹
29
-
30
27
6. 같은 set에 대해 동시작업이 원자성을 보장받아야한다.
31
28
*/
32
29
33
- // func testGongurrency(numOfGoroutine, numOfEashWork int, work func(prefix string, num int)) {
34
- // var wg sync.WaitGroup
35
- // for nthGoroutine := 0; nthGoroutine < numOfGoroutine; nthGoroutine++ {
36
- // wg.Add(1)
37
- // go func(n int) {
38
- // defer wg.Done()
39
- // for nthWork := 0; nthWork < numOfEashWork; nthWork++ {
40
- // work("testing-", nthWork)
41
- // }
42
-
43
- // }(nthGoroutine)
44
- // }
45
- // wg.Wait()
46
- // }
30
+ // Basic operations 0. Initialize
31
+ func TestInitializeFromArguments (t * testing.T ) {
32
+ s := New (1 , 2 , "3" )
33
+ require .NotEmpty (t , s )
34
+ require .Equal (t , 3 , s .Len ())
35
+
36
+ s .Add ("3" ) // It lengths is 3, because Set already has "3"
37
+ require .NotEqual (t , 4 , s .Len ())
38
+ }
39
+
40
+ // Basic operations: 1. Add element
41
+ func TestAddElement (t * testing.T ) {
42
+ s := New ()
43
+ require .NotEmpty (t , s )
44
+ require .Equal (t , 0 , s .Len ())
45
+ s .Add ("Add" )
46
+ s .Add (" multiple" )
47
+ s .Add ("values of" )
48
+ s .Add (3 )
49
+ s .Add ("like this:" )
50
+ s .Add ([]int {1 , 2 , 3 })
51
+ require .Equal (t , 6 , s .Len ())
52
+ }
53
+
54
+ // Basic operations: 2. Remove element
55
+ func TestRemoveElement (t * testing.T ) {
56
+ s := New ("1" , "2" , 3 )
57
+ require .NotEmpty (t , s )
58
+ require .Equal (t , 3 , s .Len ())
59
+ s .Remove ("1" )
60
+ s .Remove ("multiple" )
61
+ s .Remove ("3" )
62
+ require .Equal (t , 2 , s .Len ())
63
+ }
64
+
65
+ // Basic operations: 3.
66
+ // func TestMembershipCheck(t *testing.T) {}
67
+
68
+ // func TestDuplicate(t *testing.T) {}
69
+ // func TestConvertToSet(t *testing.T) {}
70
+ // func TestConvertToSlice(t *testing.T) {}
71
+ // func TestUnion(t *testing.T) {}
72
+ // func TestIntersection(t *testing.T) {}
73
+ // func TestDifference(t *testing.T) {}
74
+ // func TestFunctionElement(t *testing.T) {}
75
+ // func TestStructElement(t *testing.T) {}
47
76
func TestConcurrentAddElement10Goroutine100000Loop (t * testing.T ) {
48
77
var wg sync.WaitGroup
49
-
50
78
s := New ()
51
79
numOfGoroutine := 10
52
80
numOfLoop := 100000
@@ -67,7 +95,6 @@ func TestConcurrentAddElement10Goroutine100000Loop(t *testing.T) {
67
95
}
68
96
func TestConcurrentAddElement100000Goroutine10Loop (t * testing.T ) {
69
97
var wg sync.WaitGroup
70
-
71
98
s := New ()
72
99
numOfGoroutine := 100000
73
100
numOfLoop := 10
@@ -116,13 +143,72 @@ func TestConcurrentExclusiveLock100000Loop(t *testing.T) {
116
143
require .Equal (t , 0 , s .Len ())
117
144
}
118
145
119
- // func TestConcurrentRemoveElement(t *testing.T) {}
120
- // func TestMembershipCheck(t *testing.T) {}
121
- // func TestDuplicate(t *testing.T) {}
122
- // func TestConvertToSet(t *testing.T) {}
123
- // func TestConvertToSlice(t *testing.T) {}
124
- // func TestUnion(t *testing.T) {}
125
- // func TestIntersection(t *testing.T) {}
126
- // func TestDifference(t *testing.T) {}
127
- // func TestFunctionElement(t *testing.T) {}
128
- // func TestStructElement(t *testing.T) {}
146
+ func TestConcurrentRemoveElement (t * testing.T ) {
147
+ var wg sync.WaitGroup
148
+
149
+ s := New ()
150
+ numOfGoroutine := 1000
151
+ numOfLoop := 10
152
+ totalExpectElement := numOfGoroutine * numOfLoop
153
+
154
+ for nthGoroutine := 0 ; nthGoroutine < numOfGoroutine ; nthGoroutine ++ {
155
+ wg .Add (1 )
156
+ go func (n int ) {
157
+ defer wg .Done ()
158
+ for nthWork := 0 ; nthWork < numOfLoop ; nthWork ++ {
159
+ s .Add (strconv .Itoa (n ) + ".testing-" + strconv .Itoa (nthWork ))
160
+ }
161
+
162
+ }(nthGoroutine )
163
+ }
164
+ wg .Wait ()
165
+ require .Equal (t , totalExpectElement , s .Len ())
166
+
167
+ for nthGoroutine := 0 ; nthGoroutine < numOfGoroutine ; nthGoroutine ++ {
168
+ wg .Add (1 )
169
+ go func (n int ) {
170
+ defer wg .Done ()
171
+ for nthWork := 0 ; nthWork < numOfLoop ; nthWork ++ {
172
+ s .Remove (strconv .Itoa (n ) + ".testing-" + strconv .Itoa (nthWork ))
173
+ }
174
+
175
+ }(nthGoroutine )
176
+ }
177
+ wg .Wait ()
178
+ require .Equal (t , 0 , s .Len ())
179
+ }
180
+
181
+ func TestMembershipCheck (t * testing.T ) {
182
+ var wg sync.WaitGroup
183
+
184
+ s := New ()
185
+ numOfGoroutine := 10000
186
+ numOfLoop := 10
187
+ totalExpectElement := numOfGoroutine * numOfLoop
188
+
189
+ for nthGoroutine := 0 ; nthGoroutine < numOfGoroutine ; nthGoroutine ++ {
190
+ wg .Add (1 )
191
+ go func (n int ) {
192
+ defer wg .Done ()
193
+ for nthWork := 0 ; nthWork < numOfLoop ; nthWork ++ {
194
+ s .Add (strconv .Itoa (n ) + ".testing-" + strconv .Itoa (nthWork ))
195
+ }
196
+
197
+ }(nthGoroutine )
198
+ }
199
+ wg .Wait ()
200
+
201
+ require .Equal (t , totalExpectElement , s .Len ())
202
+ for nthGoroutine := 0 ; nthGoroutine < numOfGoroutine ; nthGoroutine ++ {
203
+ wg .Add (1 )
204
+ go func (n int ) {
205
+ defer wg .Done ()
206
+ for nthWork := 0 ; nthWork < numOfLoop ; nthWork ++ {
207
+ s .Add (strconv .Itoa (n ) + ".testing-phase-two-" + strconv .Itoa (nthWork ))
208
+ s .Remove (strconv .Itoa (n ) + ".testing-" + strconv .Itoa (nthWork ))
209
+ }
210
+
211
+ }(nthGoroutine )
212
+ }
213
+ wg .Wait ()
214
+ }
0 commit comments