-
Notifications
You must be signed in to change notification settings - Fork 7
/
concurrent_swiss_map_test.go
332 lines (291 loc) · 6.31 KB
/
concurrent_swiss_map_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
330
331
332
package csmap_test
import (
"strconv"
"sync"
"testing"
csmap "github.com/mhmtszr/concurrent-swiss-map"
)
func TestHas(t *testing.T) {
myMap := csmap.Create[int, string]()
myMap.Store(1, "test")
if !myMap.Has(1) {
t.Fatal("1 should exists")
}
}
func TestLoad(t *testing.T) {
myMap := csmap.Create[int, string]()
myMap.Store(1, "test")
v, ok := myMap.Load(1)
v2, ok2 := myMap.Load(2)
if v != "test" || !ok {
t.Fatal("1 should test")
}
if v2 != "" || ok2 {
t.Fatal("2 should not exist")
}
}
func TestDelete(t *testing.T) {
myMap := csmap.Create[int, string]()
myMap.Store(1, "test")
ok1 := myMap.Delete(20)
ok2 := myMap.Delete(1)
if myMap.Has(1) {
t.Fatal("1 should be deleted")
}
if ok1 {
t.Fatal("ok1 should be false")
}
if !ok2 {
t.Fatal("ok2 should be true")
}
}
func TestSetIfAbsent(t *testing.T) {
myMap := csmap.Create[int, string]()
myMap.SetIfAbsent(1, "test")
if !myMap.Has(1) {
t.Fatal("1 should be exist")
}
}
func TestSetIfPresent(t *testing.T) {
myMap := csmap.Create[int, string]()
myMap.SetIfPresent(1, "test")
if myMap.Has(1) {
t.Fatal("1 should be not exist")
}
myMap.Store(1, "test")
myMap.SetIfPresent(1, "new-test")
val, _ := myMap.Load(1)
if val != "new-test" {
t.Fatal("val should be new-test")
}
}
func TestSetIf(t *testing.T) {
myMap := csmap.Create[int, string]()
valueA := "value a"
myMap.SetIf(1, func(previousVale string, previousFound bool) (value string, set bool) {
// operate like a SetIfAbsent...
if !previousFound {
return valueA, true
}
return "", false
})
value, _ := myMap.Load(1)
if value != valueA {
t.Fatal("value should value a")
}
myMap.SetIf(1, func(previousVale string, previousFound bool) (value string, set bool) {
// operate like a SetIfAbsent...
if !previousFound {
return "bad", true
}
return "", false
})
value, _ = myMap.Load(1)
if value != valueA {
t.Fatal("value should value a")
}
}
func TestDeleteIf(t *testing.T) {
myMap := csmap.Create[int, string]()
myMap.Store(1, "value b")
ok1 := myMap.DeleteIf(20, func(value string) bool {
t.Fatal("condition function should not have been called")
return false
})
if ok1 {
t.Fatal("ok1 should be false")
}
ok2 := myMap.DeleteIf(1, func(value string) bool {
if value != "value b" {
t.Fatal("condition function arg should be tests")
}
return false // don't delete
})
if ok2 {
t.Fatal("ok1 should be false")
}
ok3 := myMap.DeleteIf(1, func(value string) bool {
if value != "value b" {
t.Fatal("condition function arg should be tests")
}
return true // delete the entry
})
if !ok3 {
t.Fatal("ok2 should be true")
}
}
func TestCount(t *testing.T) {
myMap := csmap.Create[int, string]()
myMap.SetIfAbsent(1, "test")
myMap.SetIfAbsent(2, "test2")
if myMap.Count() != 2 {
t.Fatal("count should be 2")
}
}
func TestIsEmpty(t *testing.T) {
myMap := csmap.Create[int, string]()
if !myMap.IsEmpty() {
t.Fatal("map should be empty")
}
}
func TestRangeStop(t *testing.T) {
myMap := csmap.Create[int, string](
csmap.WithShardCount[int, string](1),
)
myMap.SetIfAbsent(1, "test")
myMap.SetIfAbsent(2, "test2")
myMap.SetIfAbsent(3, "test2")
total := 0
myMap.Range(func(key int, value string) (stop bool) {
total++
return true
})
if total != 1 {
t.Fatal("total should be 1")
}
}
func TestRange(t *testing.T) {
myMap := csmap.Create[int, string]()
myMap.SetIfAbsent(1, "test")
myMap.SetIfAbsent(2, "test2")
total := 0
myMap.Range(func(key int, value string) (stop bool) {
total++
return
})
if total != 2 {
t.Fatal("total should be 2")
}
}
func TestCustomHasherWithRange(t *testing.T) {
myMap := csmap.Create[int, string](
csmap.WithCustomHasher[int, string](func(key int) uint64 {
return 0
}),
)
myMap.SetIfAbsent(1, "test")
myMap.SetIfAbsent(2, "test2")
myMap.SetIfAbsent(3, "test2")
myMap.SetIfAbsent(4, "test2")
total := 0
myMap.Range(func(key int, value string) (stop bool) {
total++
return true
})
if total != 1 {
t.Fatal("total should be 1, because currently range stops current shard only.")
}
}
func TestDeleteFromRange(t *testing.T) {
myMap := csmap.Create[string, int](
csmap.WithSize[string, int](1024),
)
myMap.Store("aaa", 10)
myMap.Store("aab", 11)
myMap.Store("aac", 15)
myMap.Store("aad", 124)
myMap.Store("aaf", 987)
myMap.Range(func(key string, value int) (stop bool) {
if value > 20 {
myMap.Delete(key)
}
return false
})
if myMap.Count() != 3 {
t.Fatal("total should be 3, because currently range deletes values that bigger than 20.")
}
}
func TestMarshal(t *testing.T) {
myMap := csmap.Create[string, int](
csmap.WithSize[string, int](1024),
)
myMap.Store("aaa", 10)
myMap.Store("aab", 11)
b, _ := myMap.MarshalJSON()
newMap := csmap.Create[string, int](
csmap.WithSize[string, int](1024),
)
_ = newMap.UnmarshalJSON(b)
if myMap.Count() != 2 || !myMap.Has("aaa") || !myMap.Has("aab") {
t.Fatal("count should be 2 after unmarshal")
}
}
func TestBasicConcurrentWriteDeleteCount(t *testing.T) {
myMap := csmap.Create[int, string](
csmap.WithShardCount[int, string](32),
csmap.WithSize[int, string](1000),
)
var wg sync.WaitGroup
wg.Add(1000000)
for i := 0; i < 1000000; i++ {
i := i
go func() {
defer wg.Done()
myMap.Store(i, strconv.Itoa(i))
}()
}
wg.Wait()
wg.Add(1000000)
for i := 0; i < 1000000; i++ {
i := i
go func() {
defer wg.Done()
if !myMap.Has(i) {
t.Error(strconv.Itoa(i) + " should exist")
return
}
}()
}
wg.Wait()
wg.Add(1000000)
for i := 0; i < 1000000; i++ {
i := i
go func() {
defer wg.Done()
myMap.Delete(i)
}()
}
wg.Wait()
wg.Add(1000000)
for i := 0; i < 1000000; i++ {
i := i
go func() {
defer wg.Done()
if myMap.Has(i) {
t.Error(strconv.Itoa(i) + " should not exist")
return
}
}()
}
wg.Wait()
}
func TestClear(t *testing.T) {
myMap := csmap.Create[int, string]()
loop := 10000
for i := 0; i < loop; i++ {
myMap.Store(i, "test")
}
myMap.Clear()
if !myMap.IsEmpty() {
t.Fatal("count should be true")
}
// store again
for i := 0; i < loop; i++ {
myMap.Store(i, "test")
}
// get again
for i := 0; i < loop; i++ {
val, ok := myMap.Load(i)
if ok != true {
t.Fatal("ok should be true")
}
if val != "test" {
t.Fatal("val should be test")
}
}
// check again
count := myMap.Count()
if count != loop {
t.Fatal("count should be 1000")
}
}