-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_test.go
More file actions
618 lines (463 loc) · 11.8 KB
/
Copy pathmap_test.go
File metadata and controls
618 lines (463 loc) · 11.8 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package gsyncmap_test
import (
"sync"
"sync/atomic"
"testing"
"github.com/min0625/gsyncmap"
)
// --- Map tests ---
func TestMap_StoreAndLoad(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, int]
// Load non-existing key returns zero value and false.
v, ok := m.Load("missing")
if ok || v != 0 {
t.Fatalf("Load missing key: got (%v, %v), want (0, false)", v, ok)
}
m.Store("k", 42)
v, ok = m.Load("k")
if !ok || v != 42 {
t.Fatalf("Load after Store: got (%v, %v), want (42, true)", v, ok)
}
}
func TestMap_StoreOverwrite(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, int]
m.Store("k", 1)
m.Store("k", 2)
v, ok := m.Load("k")
if !ok || v != 2 {
t.Fatalf("Load after overwrite: got (%v, %v), want (2, true)", v, ok)
}
}
func TestMap_Delete(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, int]
// Delete non-existing key should not panic.
m.Delete("missing")
m.Store("k", 1)
m.Delete("k")
_, ok := m.Load("k")
if ok {
t.Fatal("expected key to be deleted")
}
}
func TestMap_LoadOrStore(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, int]
// Key absent: stores and returns the given value, loaded=false.
actual, loaded := m.LoadOrStore("k", 10)
if loaded || actual != 10 {
t.Fatalf("LoadOrStore new key: got (%v, %v), want (10, false)", actual, loaded)
}
// Key present: returns the existing value, loaded=true.
actual, loaded = m.LoadOrStore("k", 99)
if !loaded || actual != 10 {
t.Fatalf("LoadOrStore existing key: got (%v, %v), want (10, true)", actual, loaded)
}
}
func TestMap_LoadAndDelete(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, int]
// Key absent: returns zero value and false.
v, loaded := m.LoadAndDelete("missing")
if loaded || v != 0 {
t.Fatalf("LoadAndDelete missing: got (%v, %v), want (0, false)", v, loaded)
}
m.Store("k", 7)
v, loaded = m.LoadAndDelete("k")
if !loaded || v != 7 {
t.Fatalf("LoadAndDelete existing: got (%v, %v), want (7, true)", v, loaded)
}
_, ok := m.Load("k")
if ok {
t.Fatal("key should be absent after LoadAndDelete")
}
}
func TestMap_Swap(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, int]
// Key absent: previous is zero, loaded=false.
prev, loaded := m.Swap("k", 1)
if loaded || prev != 0 {
t.Fatalf("Swap missing key: got (%v, %v), want (0, false)", prev, loaded)
}
// Key present: previous is old value, loaded=true.
prev, loaded = m.Swap("k", 2)
if !loaded || prev != 1 {
t.Fatalf("Swap existing key: got (%v, %v), want (1, true)", prev, loaded)
}
v, _ := m.Load("k")
if v != 2 {
t.Fatalf("Load after Swap: got %v, want 2", v)
}
}
func TestMap_Clear(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, int]
m.Store("a", 1)
m.Store("b", 2)
m.Clear()
var count int
m.Range(func(_ string, _ int) bool {
count++
return true
})
if count != 0 {
t.Fatalf("expected empty map after Clear, got %d entries", count)
}
}
func TestMap_Range(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, int]
want := map[string]int{"a": 1, "b": 2, "c": 3}
for k, v := range want {
m.Store(k, v)
}
got := make(map[string]int)
m.Range(func(k string, v int) bool {
got[k] = v
return true
})
if len(got) != len(want) {
t.Fatalf("Range visited %d entries, want %d", len(got), len(want))
}
for k, v := range want {
if got[k] != v {
t.Errorf("Range: key %q got %v, want %v", k, got[k], v)
}
}
}
func TestMap_Range_EmptyMap(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, int]
var visited int
m.Range(func(_ string, _ int) bool {
visited++
return true
})
if visited != 0 {
t.Fatalf("Range on empty map should not call f, called %d times", visited)
}
}
func TestMap_Range_EarlyStop(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, int]
m.Store("a", 1)
m.Store("b", 2)
m.Store("c", 3)
var visited int
m.Range(func(_ string, _ int) bool {
visited++
return false
})
if visited != 1 {
t.Fatalf("Range with early stop should visit exactly 1 entry, got %d", visited)
}
}
func TestMap_CompareAndDelete_Deprecated(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, string]
m.Store("k", "v1")
// Wrong value: should not delete.
if m.CompareAndDelete("k", "wrong") {
t.Fatal("CompareAndDelete with wrong value should return false")
}
v, ok := m.Load("k")
if !ok || v != "v1" {
t.Fatalf("key should still exist: got (%v, %v)", v, ok)
}
// Correct value: should delete.
if !m.CompareAndDelete("k", "v1") {
t.Fatal("CompareAndDelete with correct value should return true")
}
_, ok = m.Load("k")
if ok {
t.Fatal("key should be deleted")
}
// Key absent: should return false.
if m.CompareAndDelete("k", "v1") {
t.Fatal("CompareAndDelete on missing key should return false")
}
}
func TestMap_CompareAndSwap_Deprecated(t *testing.T) {
t.Parallel()
var m gsyncmap.Map[string, string]
m.Store("k", "old")
// Wrong old value: should not swap.
if m.CompareAndSwap("k", "wrong", "new") {
t.Fatal("CompareAndSwap with wrong old value should return false")
}
// Correct old value: should swap.
if !m.CompareAndSwap("k", "old", "new") {
t.Fatal("CompareAndSwap with correct old value should return true")
}
v, _ := m.Load("k")
if v != "new" {
t.Fatalf("value after swap: got %v, want new", v)
}
}
func TestMap_NonComparableValue(t *testing.T) {
t.Parallel()
// Map accepts non-comparable value types (e.g. slices) for every method
// except the deprecated CompareAndDelete/CompareAndSwap.
var m gsyncmap.Map[string, []int]
// Load absent key returns the zero value (nil slice) and false.
v, ok := m.Load("missing")
if ok || v != nil {
t.Fatalf("Load missing key: got (%v, %v), want (nil, false)", v, ok)
}
m.Store("k", []int{1, 2, 3})
v, ok = m.Load("k")
if !ok || len(v) != 3 || v[0] != 1 || v[1] != 2 || v[2] != 3 {
t.Fatalf("Load after Store: got (%v, %v), want ([1 2 3], true)", v, ok)
}
// LoadOrStore on an existing key returns the stored slice.
actual, loaded := m.LoadOrStore("k", []int{9})
if !loaded || len(actual) != 3 {
t.Fatalf("LoadOrStore existing: got (%v, %v), want ([1 2 3], true)", actual, loaded)
}
// Range visits the stored entry.
var visited int
m.Range(func(key string, value []int) bool {
visited++
if key != "k" || len(value) != 3 {
t.Errorf("Range: got (%q, %v), want (\"k\", [1 2 3])", key, value)
}
return true
})
if visited != 1 {
t.Fatalf("Range visited %d entries, want 1", visited)
}
m.Delete("k")
if _, ok := m.Load("k"); ok {
t.Fatal("key should be deleted")
}
}
// --- ComparableMap tests ---
func TestComparableMap_BasicOperations(t *testing.T) {
t.Parallel()
var m gsyncmap.ComparableMap[int, int]
m.Store(1, 100)
v, ok := m.Load(1)
if !ok || v != 100 {
t.Fatalf("Load: got (%v, %v), want (100, true)", v, ok)
}
m.Delete(1)
_, ok = m.Load(1)
if ok {
t.Fatal("key should be deleted")
}
}
func TestComparableMap_CompareAndDelete(t *testing.T) {
t.Parallel()
tests := []struct {
name string
stored string
compareWith string
wantDeleted bool
}{
{"match", "v1", "v1", true},
{"mismatch", "v1", "v2", false},
{"key absent", "", "", false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var m gsyncmap.ComparableMap[string, string]
if tc.name != "key absent" {
m.Store("k", tc.stored)
}
deleted := m.CompareAndDelete("k", tc.compareWith)
if deleted != tc.wantDeleted {
t.Fatalf("CompareAndDelete: got %v, want %v", deleted, tc.wantDeleted)
}
})
}
}
func TestComparableMap_CompareAndSwap(t *testing.T) {
t.Parallel()
tests := []struct {
name string
stored string
oldVal string
newVal string
wantSwap bool
wantValue string
}{
{"match", "v1", "v1", "v2", true, "v2"},
{"mismatch", "v1", "wrong", "v2", false, "v1"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var m gsyncmap.ComparableMap[string, string]
m.Store("k", tc.stored)
swapped := m.CompareAndSwap("k", tc.oldVal, tc.newVal)
if swapped != tc.wantSwap {
t.Fatalf("CompareAndSwap: got %v, want %v", swapped, tc.wantSwap)
}
v, _ := m.Load("k")
if v != tc.wantValue {
t.Fatalf("value after swap: got %v, want %v", v, tc.wantValue)
}
})
}
}
func TestComparableMap_LoadOrStore(t *testing.T) {
t.Parallel()
var m gsyncmap.ComparableMap[int, int]
actual, loaded := m.LoadOrStore(1, 10)
if loaded || actual != 10 {
t.Fatalf("LoadOrStore new: got (%v, %v), want (10, false)", actual, loaded)
}
actual, loaded = m.LoadOrStore(1, 99)
if !loaded || actual != 10 {
t.Fatalf("LoadOrStore existing: got (%v, %v), want (10, true)", actual, loaded)
}
}
func TestComparableMap_Swap(t *testing.T) {
t.Parallel()
var m gsyncmap.ComparableMap[string, int]
// Key absent: previous is zero, loaded=false.
prev, loaded := m.Swap("k", 1)
if loaded || prev != 0 {
t.Fatalf("Swap missing key: got (%v, %v), want (0, false)", prev, loaded)
}
// Key present: previous is old value, loaded=true.
prev, loaded = m.Swap("k", 2)
if !loaded || prev != 1 {
t.Fatalf("Swap existing key: got (%v, %v), want (1, true)", prev, loaded)
}
v, _ := m.Load("k")
if v != 2 {
t.Fatalf("Load after Swap: got %v, want 2", v)
}
}
func TestComparableMap_LoadAndDelete(t *testing.T) {
t.Parallel()
var m gsyncmap.ComparableMap[string, int]
// Key absent: returns zero value and false.
v, loaded := m.LoadAndDelete("missing")
if loaded || v != 0 {
t.Fatalf("LoadAndDelete missing: got (%v, %v), want (0, false)", v, loaded)
}
m.Store("k", 7)
v, loaded = m.LoadAndDelete("k")
if !loaded || v != 7 {
t.Fatalf("LoadAndDelete existing: got (%v, %v), want (7, true)", v, loaded)
}
_, ok := m.Load("k")
if ok {
t.Fatal("key should be absent after LoadAndDelete")
}
}
func TestComparableMap_Clear(t *testing.T) {
t.Parallel()
var m gsyncmap.ComparableMap[int, int]
for i := range 5 {
m.Store(i, i)
}
m.Clear()
var count int
m.Range(func(_, _ int) bool {
count++
return true
})
if count != 0 {
t.Fatalf("expected empty map after Clear, got %d entries", count)
}
}
// --- Concurrent tests ---
func TestMap_Concurrent_StoreLoad(t *testing.T) {
t.Parallel()
const (
goroutines = 50
iterations = 100
)
var (
m gsyncmap.Map[int, int]
wg sync.WaitGroup
)
wg.Add(goroutines)
for g := range goroutines {
go func(id int) {
defer wg.Done()
for i := range iterations {
m.Store(id*iterations+i, i)
m.Load(id*iterations + i)
}
}(g)
}
wg.Wait()
}
func TestMap_Concurrent_LoadOrStore(t *testing.T) {
t.Parallel()
const goroutines = 50
var (
m gsyncmap.Map[string, int]
stored atomic.Int64
)
var wg sync.WaitGroup
wg.Add(goroutines)
for range goroutines {
go func() {
defer wg.Done()
_, loaded := m.LoadOrStore("shared", 1)
if !loaded {
stored.Add(1)
}
}()
}
wg.Wait()
// Only one goroutine should have stored the value.
if stored.Load() != 1 {
t.Fatalf("LoadOrStore: expected exactly 1 store, got %d", stored.Load())
}
}
func TestMap_Concurrent_Range(t *testing.T) {
t.Parallel()
const (
entries = 100
goroutines = 10
)
var m gsyncmap.Map[int, int]
for i := range entries {
m.Store(i, i)
}
var wg sync.WaitGroup
wg.Add(goroutines)
for range goroutines {
go func() {
defer wg.Done()
m.Range(func(_, _ int) bool {
return true
})
}()
}
wg.Wait()
}
func TestComparableMap_Concurrent_CompareAndSwap(t *testing.T) {
t.Parallel()
const goroutines = 50
var m gsyncmap.ComparableMap[string, int]
m.Store("counter", 0)
// Each goroutine races to be the first to swap 0 -> 1.
var (
swapped atomic.Int64
wg sync.WaitGroup
)
wg.Add(goroutines)
for range goroutines {
go func() {
defer wg.Done()
if m.CompareAndSwap("counter", 0, 1) {
swapped.Add(1)
}
}()
}
wg.Wait()
if swapped.Load() != 1 {
t.Fatalf("expected exactly one successful CompareAndSwap, got %d", swapped.Load())
}
}