-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncmap_test.go
More file actions
442 lines (384 loc) · 9.09 KB
/
Copy pathsyncmap_test.go
File metadata and controls
442 lines (384 loc) · 9.09 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package syncmap_test
import (
"fmt"
"math/rand"
"sort"
"sync"
"sync/atomic"
"testing"
"time"
syncmap "github.com/donomii/genericsyncmap"
)
func TestSyncMap_Basic(t *testing.T) {
m := syncmap.NewSyncMap[string, int]()
// Store
m.Store("one", 1)
m.Store("two", 2)
// Load
if v, ok := m.Load("one"); !ok || v != 1 {
t.Errorf("expected 1, true; got %v, %v", v, ok)
}
if _, ok := m.Load("three"); ok {
t.Error("expected false for missing key")
}
// LoadOrStore
v, loaded := m.LoadOrStore("three", 3)
if loaded || v != 3 {
t.Errorf("expected 3, false; got %v, %v", v, loaded)
}
v, loaded = m.LoadOrStore("one", 100)
if !loaded || v != 1 {
t.Errorf("expected 1, true; got %v, %v", v, loaded)
}
// Delete
m.Delete("two")
if _, ok := m.Load("two"); ok {
t.Error("expected false after delete")
}
}
func TestSyncMap_ZeroValue(t *testing.T) {
var m syncmap.SyncMap[string, int]
m.Store("key", 10)
if v, ok := m.Load("key"); !ok || v != 10 {
t.Errorf("zero value: expected 10, true; got %v, %v", v, ok)
}
}
func TestMap_CanonicalName(t *testing.T) {
var m syncmap.Map[string, int]
m.Store("key", 10)
if v, ok := m.Load("key"); !ok || v != 10 {
t.Errorf("Map: expected 10, true; got %v, %v", v, ok)
}
}
func TestSyncMap_Clear(t *testing.T) {
m := syncmap.NewSyncMap[string, int]()
m.Store("one", 1)
m.Store("two", 2)
m.Clear()
if l := m.Len(); l != 0 {
t.Errorf("Clear: expected length 0, got %d", l)
}
m.Store("three", 3)
if v, ok := m.Load("three"); !ok || v != 3 {
t.Errorf("Clear: expected reusable map with 3, true; got %v, %v", v, ok)
}
}
func TestSyncMap_All(t *testing.T) {
var m syncmap.Map[int, int]
expected := map[int]int{1: 10, 2: 20, 3: 30}
for key, value := range expected {
m.Store(key, value)
}
actual := make(map[int]int)
for key, value := range m.All() {
actual[key] = value
}
if len(actual) != len(expected) {
t.Errorf("All: expected %d items, got %d", len(expected), len(actual))
}
for key, expectedValue := range expected {
if actualValue := actual[key]; actualValue != expectedValue {
t.Errorf("All: key %d expected value %d, got %d", key, expectedValue, actualValue)
}
}
}
func TestSyncMap_NewFeatures(t *testing.T) {
m := syncmap.NewSyncMap[string, int]()
// LoadAndDelete
m.Store("key", 10)
v, loaded := m.LoadAndDelete("key")
if !loaded || v != 10 {
t.Errorf("LoadAndDelete: expected 10, true; got %v, %v", v, loaded)
}
_, loaded = m.LoadAndDelete("key")
if loaded {
t.Error("LoadAndDelete: expected false on second call")
}
// CompareAndDelete
m.Store("key", 20)
if m.CompareAndDelete("key", 999) {
t.Error("CompareAndDelete: expected false for wrong value")
}
if !m.CompareAndDelete("key", 20) {
t.Error("CompareAndDelete: expected true for correct value")
}
if _, ok := m.Load("key"); ok {
t.Error("CompareAndDelete: expected key to be deleted")
}
// Swap
m.Store("key", 30)
prev, loaded := m.Swap("key", 40)
if !loaded || prev != 30 {
t.Errorf("Swap: expected 30, true; got %v, %v", prev, loaded)
}
if v, _ := m.Load("key"); v != 40 {
t.Errorf("Swap: expected new value 40; got %v", v)
}
// CompareAndSwap
if m.CompareAndSwap("key", 999, 50) {
t.Error("CompareAndSwap: expected false for wrong old value")
}
if !m.CompareAndSwap("key", 40, 50) {
t.Error("CompareAndSwap: expected true for correct old value")
}
if v, _ := m.Load("key"); v != 50 {
t.Errorf("CompareAndSwap: expected new value 50; got %v", v)
}
}
func TestSyncMap_Range(t *testing.T) {
m := syncmap.NewSyncMap[int, int]()
expected := map[int]int{1: 10, 2: 20, 3: 30}
for k, v := range expected {
m.Store(k, v)
}
count := 0
m.Range(func(key int, value int) bool {
if expected[key] != value {
t.Errorf("Range: expected value %d for key %d, got %d", expected[key], key, value)
}
count++
return true
})
if count != len(expected) {
t.Errorf("Range: expected %d items, got %d", len(expected), count)
}
}
func TestSyncMap_KeysValuesLen(t *testing.T) {
m := syncmap.NewSyncMap[string, int]()
data := map[string]int{"a": 1, "b": 2, "c": 3}
for k, v := range data {
m.Store(k, v)
}
// Len
if l := m.Len(); l != 3 {
t.Errorf("Len: expected 3, got %d", l)
}
// Keys
keys := m.Keys()
if len(keys) != 3 {
t.Errorf("Keys: expected 3 keys, got %d", len(keys))
}
// Verify keys content (ignoring order)
keyMap := make(map[string]bool)
for _, k := range keys {
keyMap[k] = true
}
for k := range data {
if !keyMap[k] {
t.Errorf("Keys: missing key %s", k)
}
}
// Values
values := m.Values()
if len(values) != 3 {
t.Errorf("Values: expected 3 values, got %d", len(values))
}
// Verify values
valMap := make(map[int]bool)
for _, v := range values {
valMap[v] = true
}
for _, v := range data {
if !valMap[v] {
t.Errorf("Values: missing value %d", v)
}
}
}
func TestSyncMap_Concurrency(t *testing.T) {
m := syncmap.NewSyncMap[int, int]()
var wg sync.WaitGroup
workers := 100
ops := 1000
wg.Add(workers)
for i := 0; i < workers; i++ {
go func(id int) {
defer wg.Done()
rng := rand.New(rand.NewSource(time.Now().UnixNano() + int64(id)))
for j := 0; j < ops; j++ {
key := rng.Intn(100)
val := rng.Intn(1000)
switch j % 4 {
case 0:
m.Store(key, val)
case 1:
m.Load(key)
case 2:
m.LoadOrStore(key, val)
default:
m.Delete(key)
}
}
}(i)
}
wg.Wait()
}
// ExampleMap_usage demonstrates basic usage of Map.
func ExampleMap_usage() {
var m syncmap.Map[string, int]
m.Store("apple", 10)
m.Store("banana", 20)
if val, ok := m.Load("apple"); ok {
fmt.Printf("apple: %d\n", val)
}
m.Delete("banana")
// Output:
// apple: 10
}
// ExampleMap_all demonstrates how to iterate over the map.
func ExampleMap_all() {
var m syncmap.Map[string, string]
m.Store("LOC", "USA")
m.Store("LANG", "EN")
// Collect keys for consistent output order
var keys []string
for key := range m.All() {
keys = append(keys, key)
}
sort.Strings(keys)
for _, k := range keys {
v, _ := m.Load(k)
fmt.Printf("%s: %s\n", k, v)
}
// Output:
// LANG: EN
// LOC: USA
}
// Benchmarks
const benchmarkKeyCount = 1024
type benchmarkLockedMap struct {
mu sync.RWMutex
values map[int]int
}
func newBenchmarkLockedMap() *benchmarkLockedMap {
return &benchmarkLockedMap{values: make(map[int]int)}
}
func (m *benchmarkLockedMap) Load(key int) (int, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
value, loaded := m.values[key]
return value, loaded
}
func (m *benchmarkLockedMap) Store(key int, value int) {
m.mu.Lock()
defer m.mu.Unlock()
m.values[key] = value
}
type benchmarkOperations struct {
load func(int) (int, bool)
store func(int, int)
}
func benchmarkConcurrentMixed(b *testing.B, operations benchmarkOperations) {
var workerID atomic.Int64
b.RunParallel(func(pb *testing.PB) {
rng := rand.New(rand.NewSource(workerID.Add(1)))
for pb.Next() {
key := rng.Intn(benchmarkKeyCount)
if rng.Intn(2) == 0 {
operations.load(key)
} else {
operations.store(key, key)
}
}
})
}
func BenchmarkLoadHit(b *testing.B) {
b.Run("genericsyncmap", func(b *testing.B) {
var m syncmap.Map[int, int]
m.Store(1, 1)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Load(1)
}
})
b.Run("sync-map", func(b *testing.B) {
var m sync.Map
m.Store(1, 1)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
value, loaded := m.Load(1)
if loaded {
_ = value.(int)
}
}
})
b.Run("rwmutex-map", func(b *testing.B) {
m := newBenchmarkLockedMap()
m.Store(1, 1)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Load(1)
}
})
}
func BenchmarkStoreExisting(b *testing.B) {
b.Run("genericsyncmap", func(b *testing.B) {
var m syncmap.Map[int, int]
m.Store(1, 1)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store(1, i)
}
})
b.Run("sync-map", func(b *testing.B) {
var m sync.Map
m.Store(1, 1)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store(1, i)
}
})
b.Run("rwmutex-map", func(b *testing.B) {
m := newBenchmarkLockedMap()
m.Store(1, 1)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store(1, i)
}
})
}
func BenchmarkConcurrentMixed(b *testing.B) {
b.Run("genericsyncmap", func(b *testing.B) {
var m syncmap.Map[int, int]
for key := 0; key < benchmarkKeyCount; key++ {
m.Store(key, key)
}
b.ReportAllocs()
b.ResetTimer()
benchmarkConcurrentMixed(b, benchmarkOperations{load: m.Load, store: m.Store})
})
b.Run("sync-map", func(b *testing.B) {
var m sync.Map
for key := 0; key < benchmarkKeyCount; key++ {
m.Store(key, key)
}
b.ReportAllocs()
b.ResetTimer()
benchmarkConcurrentMixed(b, benchmarkOperations{
load: func(key int) (int, bool) {
value, loaded := m.Load(key)
if loaded {
return value.(int), true
}
return 0, false
},
store: func(key int, value int) {
m.Store(key, value)
},
})
})
b.Run("rwmutex-map", func(b *testing.B) {
m := newBenchmarkLockedMap()
for key := 0; key < benchmarkKeyCount; key++ {
m.Store(key, key)
}
b.ReportAllocs()
b.ResetTimer()
benchmarkConcurrentMixed(b, benchmarkOperations{load: m.Load, store: m.Store})
})
}