-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmapof_test.go
1419 lines (1330 loc) · 35.4 KB
/
mapof_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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package xsync_test
import (
"math"
"math/rand"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"unsafe"
. "github.com/puzpuzpuz/xsync/v3"
)
type point struct {
x int32
y int32
}
func TestMap_BucketOfStructSize(t *testing.T) {
size := unsafe.Sizeof(BucketOfPadded{})
if size != 64 {
t.Fatalf("size of 64B (one cache line) is expected, got: %d", size)
}
}
func TestMapOf_MissingEntry(t *testing.T) {
m := NewMapOf[string, string]()
v, ok := m.Load("foo")
if ok {
t.Fatalf("value was not expected: %v", v)
}
if deleted, loaded := m.LoadAndDelete("foo"); loaded {
t.Fatalf("value was not expected %v", deleted)
}
if actual, loaded := m.LoadOrStore("foo", "bar"); loaded {
t.Fatalf("value was not expected %v", actual)
}
}
func TestMapOf_EmptyStringKey(t *testing.T) {
m := NewMapOf[string, string]()
m.Store("", "foobar")
v, ok := m.Load("")
if !ok {
t.Fatal("value was expected")
}
if v != "foobar" {
t.Fatalf("value does not match: %v", v)
}
}
func TestMapOfStore_NilValue(t *testing.T) {
m := NewMapOf[string, *struct{}]()
m.Store("foo", nil)
v, ok := m.Load("foo")
if !ok {
t.Fatal("nil value was expected")
}
if v != nil {
t.Fatalf("value was not nil: %v", v)
}
}
func TestMapOfLoadOrStore_NilValue(t *testing.T) {
m := NewMapOf[string, *struct{}]()
m.LoadOrStore("foo", nil)
v, loaded := m.LoadOrStore("foo", nil)
if !loaded {
t.Fatal("nil value was expected")
}
if v != nil {
t.Fatalf("value was not nil: %v", v)
}
}
func TestMapOfLoadOrStore_NonNilValue(t *testing.T) {
type foo struct{}
m := NewMapOf[string, *foo]()
newv := &foo{}
v, loaded := m.LoadOrStore("foo", newv)
if loaded {
t.Fatal("no value was expected")
}
if v != newv {
t.Fatalf("value does not match: %v", v)
}
newv2 := &foo{}
v, loaded = m.LoadOrStore("foo", newv2)
if !loaded {
t.Fatal("value was expected")
}
if v != newv {
t.Fatalf("value does not match: %v", v)
}
}
func TestMapOfLoadAndStore_NilValue(t *testing.T) {
m := NewMapOf[string, *struct{}]()
m.LoadAndStore("foo", nil)
v, loaded := m.LoadAndStore("foo", nil)
if !loaded {
t.Fatal("nil value was expected")
}
if v != nil {
t.Fatalf("value was not nil: %v", v)
}
v, loaded = m.Load("foo")
if !loaded {
t.Fatal("nil value was expected")
}
if v != nil {
t.Fatalf("value was not nil: %v", v)
}
}
func TestMapOfLoadAndStore_NonNilValue(t *testing.T) {
m := NewMapOf[string, int]()
v1 := 1
v, loaded := m.LoadAndStore("foo", v1)
if loaded {
t.Fatal("no value was expected")
}
if v != v1 {
t.Fatalf("value does not match: %v", v)
}
v2 := 2
v, loaded = m.LoadAndStore("foo", v2)
if !loaded {
t.Fatal("value was expected")
}
if v != v1 {
t.Fatalf("value does not match: %v", v)
}
v, loaded = m.Load("foo")
if !loaded {
t.Fatal("value was expected")
}
if v != v2 {
t.Fatalf("value does not match: %v", v)
}
}
func TestMapOfRange(t *testing.T) {
const numEntries = 1000
m := NewMapOf[string, int]()
for i := 0; i < numEntries; i++ {
m.Store(strconv.Itoa(i), i)
}
iters := 0
met := make(map[string]int)
m.Range(func(key string, value int) bool {
if key != strconv.Itoa(value) {
t.Fatalf("got unexpected key/value for iteration %d: %v/%v", iters, key, value)
return false
}
met[key] += 1
iters++
return true
})
if iters != numEntries {
t.Fatalf("got unexpected number of iterations: %d", iters)
}
for i := 0; i < numEntries; i++ {
if c := met[strconv.Itoa(i)]; c != 1 {
t.Fatalf("range did not iterate correctly over %d: %d", i, c)
}
}
}
func TestMapOfRange_FalseReturned(t *testing.T) {
m := NewMapOf[string, int]()
for i := 0; i < 100; i++ {
m.Store(strconv.Itoa(i), i)
}
iters := 0
m.Range(func(key string, value int) bool {
iters++
return iters != 13
})
if iters != 13 {
t.Fatalf("got unexpected number of iterations: %d", iters)
}
}
func TestMapOfRange_NestedDelete(t *testing.T) {
const numEntries = 256
m := NewMapOf[string, int]()
for i := 0; i < numEntries; i++ {
m.Store(strconv.Itoa(i), i)
}
m.Range(func(key string, value int) bool {
m.Delete(key)
return true
})
for i := 0; i < numEntries; i++ {
if _, ok := m.Load(strconv.Itoa(i)); ok {
t.Fatalf("value found for %d", i)
}
}
}
func TestMapOfStringStore(t *testing.T) {
const numEntries = 128
m := NewMapOf[string, int]()
for i := 0; i < numEntries; i++ {
m.Store(strconv.Itoa(i), i)
}
for i := 0; i < numEntries; i++ {
v, ok := m.Load(strconv.Itoa(i))
if !ok {
t.Fatalf("value not found for %d", i)
}
if v != i {
t.Fatalf("values do not match for %d: %v", i, v)
}
}
}
func TestMapOfIntStore(t *testing.T) {
const numEntries = 128
m := NewMapOf[int, int]()
for i := 0; i < numEntries; i++ {
m.Store(i, i)
}
for i := 0; i < numEntries; i++ {
v, ok := m.Load(i)
if !ok {
t.Fatalf("value not found for %d", i)
}
if v != i {
t.Fatalf("values do not match for %d: %v", i, v)
}
}
}
func TestMapOfStore_StructKeys_IntValues(t *testing.T) {
const numEntries = 128
m := NewMapOf[point, int]()
for i := 0; i < numEntries; i++ {
m.Store(point{int32(i), -int32(i)}, i)
}
for i := 0; i < numEntries; i++ {
v, ok := m.Load(point{int32(i), -int32(i)})
if !ok {
t.Fatalf("value not found for %d", i)
}
if v != i {
t.Fatalf("values do not match for %d: %v", i, v)
}
}
}
func TestMapOfStore_StructKeys_StructValues(t *testing.T) {
const numEntries = 128
m := NewMapOf[point, point]()
for i := 0; i < numEntries; i++ {
m.Store(point{int32(i), -int32(i)}, point{-int32(i), int32(i)})
}
for i := 0; i < numEntries; i++ {
v, ok := m.Load(point{int32(i), -int32(i)})
if !ok {
t.Fatalf("value not found for %d", i)
}
if v.x != -int32(i) {
t.Fatalf("x value does not match for %d: %v", i, v)
}
if v.y != int32(i) {
t.Fatalf("y value does not match for %d: %v", i, v)
}
}
}
func TestMapOfWithHasher(t *testing.T) {
const numEntries = 10000
m := NewMapOfWithHasher[int, int](murmur3Finalizer)
for i := 0; i < numEntries; i++ {
m.Store(i, i)
}
for i := 0; i < numEntries; i++ {
v, ok := m.Load(i)
if !ok {
t.Fatalf("value not found for %d", i)
}
if v != i {
t.Fatalf("values do not match for %d: %v", i, v)
}
}
}
func murmur3Finalizer(i int, _ uint64) uint64 {
h := uint64(i)
h = (h ^ (h >> 33)) * 0xff51afd7ed558ccd
h = (h ^ (h >> 33)) * 0xc4ceb9fe1a85ec53
return h ^ (h >> 33)
}
func TestMapOfWithHasher_HashCodeCollisions(t *testing.T) {
const numEntries = 1000
m := NewMapOfWithHasher[int, int](func(i int, _ uint64) uint64 {
// We intentionally use an awful hash function here to make sure
// that the map copes with key collisions.
return 42
}, WithPresize(numEntries))
for i := 0; i < numEntries; i++ {
m.Store(i, i)
}
for i := 0; i < numEntries; i++ {
v, ok := m.Load(i)
if !ok {
t.Fatalf("value not found for %d", i)
}
if v != i {
t.Fatalf("values do not match for %d: %v", i, v)
}
}
}
func TestMapOfLoadOrStore(t *testing.T) {
const numEntries = 1000
m := NewMapOf[string, int]()
for i := 0; i < numEntries; i++ {
m.Store(strconv.Itoa(i), i)
}
for i := 0; i < numEntries; i++ {
if _, loaded := m.LoadOrStore(strconv.Itoa(i), i); !loaded {
t.Fatalf("value not found for %d", i)
}
}
}
func TestMapOfLoadOrCompute(t *testing.T) {
const numEntries = 1000
m := NewMapOf[string, int]()
for i := 0; i < numEntries; i++ {
v, loaded := m.LoadOrCompute(strconv.Itoa(i), func() int {
return i
})
if loaded {
t.Fatalf("value not computed for %d", i)
}
if v != i {
t.Fatalf("values do not match for %d: %v", i, v)
}
}
for i := 0; i < numEntries; i++ {
v, loaded := m.LoadOrCompute(strconv.Itoa(i), func() int {
return i
})
if !loaded {
t.Fatalf("value not loaded for %d", i)
}
if v != i {
t.Fatalf("values do not match for %d: %v", i, v)
}
}
}
func TestMapOfLoadOrCompute_FunctionCalledOnce(t *testing.T) {
m := NewMapOf[int, int]()
for i := 0; i < 100; {
m.LoadOrCompute(i, func() (v int) {
v, i = i, i+1
return v
})
}
m.Range(func(k, v int) bool {
if k != v {
t.Fatalf("%dth key is not equal to value %d", k, v)
}
return true
})
}
func TestMapOfCompute(t *testing.T) {
m := NewMapOf[string, int]()
// Store a new value.
v, ok := m.Compute("foobar", func(oldValue int, loaded bool) (newValue int, delete bool) {
if oldValue != 0 {
t.Fatalf("oldValue should be 0 when computing a new value: %d", oldValue)
}
if loaded {
t.Fatal("loaded should be false when computing a new value")
}
newValue = 42
delete = false
return
})
if v != 42 {
t.Fatalf("v should be 42 when computing a new value: %d", v)
}
if !ok {
t.Fatal("ok should be true when computing a new value")
}
// Update an existing value.
v, ok = m.Compute("foobar", func(oldValue int, loaded bool) (newValue int, delete bool) {
if oldValue != 42 {
t.Fatalf("oldValue should be 42 when updating the value: %d", oldValue)
}
if !loaded {
t.Fatal("loaded should be true when updating the value")
}
newValue = oldValue + 42
delete = false
return
})
if v != 84 {
t.Fatalf("v should be 84 when updating the value: %d", v)
}
if !ok {
t.Fatal("ok should be true when updating the value")
}
// Delete an existing value.
v, ok = m.Compute("foobar", func(oldValue int, loaded bool) (newValue int, delete bool) {
if oldValue != 84 {
t.Fatalf("oldValue should be 84 when deleting the value: %d", oldValue)
}
if !loaded {
t.Fatal("loaded should be true when deleting the value")
}
delete = true
return
})
if v != 84 {
t.Fatalf("v should be 84 when deleting the value: %d", v)
}
if ok {
t.Fatal("ok should be false when deleting the value")
}
// Try to delete a non-existing value. Notice different key.
v, ok = m.Compute("barbaz", func(oldValue int, loaded bool) (newValue int, delete bool) {
if oldValue != 0 {
t.Fatalf("oldValue should be 0 when trying to delete a non-existing value: %d", oldValue)
}
if loaded {
t.Fatal("loaded should be false when trying to delete a non-existing value")
}
// We're returning a non-zero value, but the map should ignore it.
newValue = 42
delete = true
return
})
if v != 0 {
t.Fatalf("v should be 0 when trying to delete a non-existing value: %d", v)
}
if ok {
t.Fatal("ok should be false when trying to delete a non-existing value")
}
}
func TestMapOfStringStoreThenDelete(t *testing.T) {
const numEntries = 1000
m := NewMapOf[string, int]()
for i := 0; i < numEntries; i++ {
m.Store(strconv.Itoa(i), i)
}
for i := 0; i < numEntries; i++ {
m.Delete(strconv.Itoa(i))
if _, ok := m.Load(strconv.Itoa(i)); ok {
t.Fatalf("value was not expected for %d", i)
}
}
}
func TestMapOfIntStoreThenDelete(t *testing.T) {
const numEntries = 1000
m := NewMapOf[int32, int32]()
for i := 0; i < numEntries; i++ {
m.Store(int32(i), int32(i))
}
for i := 0; i < numEntries; i++ {
m.Delete(int32(i))
if _, ok := m.Load(int32(i)); ok {
t.Fatalf("value was not expected for %d", i)
}
}
}
func TestMapOfStructStoreThenDelete(t *testing.T) {
const numEntries = 1000
m := NewMapOf[point, string]()
for i := 0; i < numEntries; i++ {
m.Store(point{int32(i), 42}, strconv.Itoa(i))
}
for i := 0; i < numEntries; i++ {
m.Delete(point{int32(i), 42})
if _, ok := m.Load(point{int32(i), 42}); ok {
t.Fatalf("value was not expected for %d", i)
}
}
}
func TestMapOfStringStoreThenLoadAndDelete(t *testing.T) {
const numEntries = 1000
m := NewMapOf[string, int]()
for i := 0; i < numEntries; i++ {
m.Store(strconv.Itoa(i), i)
}
for i := 0; i < numEntries; i++ {
if v, loaded := m.LoadAndDelete(strconv.Itoa(i)); !loaded || v != i {
t.Fatalf("value was not found or different for %d: %v", i, v)
}
if _, ok := m.Load(strconv.Itoa(i)); ok {
t.Fatalf("value was not expected for %d", i)
}
}
}
func TestMapOfIntStoreThenLoadAndDelete(t *testing.T) {
const numEntries = 1000
m := NewMapOf[int, int]()
for i := 0; i < numEntries; i++ {
m.Store(i, i)
}
for i := 0; i < numEntries; i++ {
if _, loaded := m.LoadAndDelete(i); !loaded {
t.Fatalf("value was not found for %d", i)
}
if _, ok := m.Load(i); ok {
t.Fatalf("value was not expected for %d", i)
}
}
}
func TestMapOfStructStoreThenLoadAndDelete(t *testing.T) {
const numEntries = 1000
m := NewMapOf[point, int]()
for i := 0; i < numEntries; i++ {
m.Store(point{42, int32(i)}, i)
}
for i := 0; i < numEntries; i++ {
if _, loaded := m.LoadAndDelete(point{42, int32(i)}); !loaded {
t.Fatalf("value was not found for %d", i)
}
if _, ok := m.Load(point{42, int32(i)}); ok {
t.Fatalf("value was not expected for %d", i)
}
}
}
func TestMapOfStoreThenParallelDelete_DoesNotShrinkBelowMinTableLen(t *testing.T) {
const numEntries = 1000
m := NewMapOf[int, int]()
for i := 0; i < numEntries; i++ {
m.Store(i, i)
}
cdone := make(chan bool)
go func() {
for i := 0; i < numEntries; i++ {
m.Delete(i)
}
cdone <- true
}()
go func() {
for i := 0; i < numEntries; i++ {
m.Delete(i)
}
cdone <- true
}()
// Wait for the goroutines to finish.
<-cdone
<-cdone
stats := m.Stats()
if stats.RootBuckets != DefaultMinMapTableLen {
t.Fatalf("table length was different from the minimum: %d", stats.RootBuckets)
}
}
func sizeBasedOnTypedRange(m *MapOf[string, int]) int {
size := 0
m.Range(func(key string, value int) bool {
size++
return true
})
return size
}
func TestMapOfSize(t *testing.T) {
const numEntries = 1000
m := NewMapOf[string, int]()
size := m.Size()
if size != 0 {
t.Fatalf("zero size expected: %d", size)
}
expectedSize := 0
for i := 0; i < numEntries; i++ {
m.Store(strconv.Itoa(i), i)
expectedSize++
size := m.Size()
if size != expectedSize {
t.Fatalf("size of %d was expected, got: %d", expectedSize, size)
}
rsize := sizeBasedOnTypedRange(m)
if size != rsize {
t.Fatalf("size does not match number of entries in Range: %v, %v", size, rsize)
}
}
for i := 0; i < numEntries; i++ {
m.Delete(strconv.Itoa(i))
expectedSize--
size := m.Size()
if size != expectedSize {
t.Fatalf("size of %d was expected, got: %d", expectedSize, size)
}
rsize := sizeBasedOnTypedRange(m)
if size != rsize {
t.Fatalf("size does not match number of entries in Range: %v, %v", size, rsize)
}
}
}
func TestMapOfClear(t *testing.T) {
const numEntries = 1000
m := NewMapOf[string, int]()
for i := 0; i < numEntries; i++ {
m.Store(strconv.Itoa(i), i)
}
size := m.Size()
if size != numEntries {
t.Fatalf("size of %d was expected, got: %d", numEntries, size)
}
m.Clear()
size = m.Size()
if size != 0 {
t.Fatalf("zero size was expected, got: %d", size)
}
rsize := sizeBasedOnTypedRange(m)
if rsize != 0 {
t.Fatalf("zero number of entries in Range was expected, got: %d", rsize)
}
}
func assertMapOfCapacity[K comparable, V any](t *testing.T, m *MapOf[K, V], expectedCap int) {
stats := m.Stats()
if stats.Capacity != expectedCap {
t.Fatalf("capacity was different from %d: %d", expectedCap, stats.Capacity)
}
}
func TestNewMapOfPresized(t *testing.T) {
assertMapOfCapacity(t, NewMapOf[string, string](), DefaultMinMapOfTableCap)
assertMapOfCapacity(t, NewMapOfPresized[string, string](0), DefaultMinMapOfTableCap)
assertMapOfCapacity(t, NewMapOf[string, string](WithPresize(0)), DefaultMinMapOfTableCap)
assertMapOfCapacity(t, NewMapOfPresized[string, string](-100), DefaultMinMapOfTableCap)
assertMapOfCapacity(t, NewMapOf[string, string](WithPresize(-100)), DefaultMinMapOfTableCap)
assertMapOfCapacity(t, NewMapOfPresized[string, string](500), 1280)
assertMapOfCapacity(t, NewMapOf[string, string](WithPresize(500)), 1280)
assertMapOfCapacity(t, NewMapOfPresized[int, int](1_000_000), 2621440)
assertMapOfCapacity(t, NewMapOf[int, int](WithPresize(1_000_000)), 2621440)
assertMapOfCapacity(t, NewMapOfPresized[point, point](100), 160)
assertMapOfCapacity(t, NewMapOf[point, point](WithPresize(100)), 160)
}
func TestNewMapOfPresized_DoesNotShrinkBelowMinTableLen(t *testing.T) {
const minTableLen = 1024
const numEntries = int(minTableLen * EntriesPerMapOfBucket * MapLoadFactor)
m := NewMapOf[int, int](WithPresize(numEntries))
for i := 0; i < 2*numEntries; i++ {
m.Store(i, i)
}
stats := m.Stats()
if stats.RootBuckets <= minTableLen {
t.Fatalf("table did not grow: %d", stats.RootBuckets)
}
for i := 0; i < 2*numEntries; i++ {
m.Delete(i)
}
stats = m.Stats()
if stats.RootBuckets != minTableLen {
t.Fatalf("table length was different from the minimum: %d", stats.RootBuckets)
}
}
func TestNewMapOfGrowOnly_OnlyShrinksOnClear(t *testing.T) {
const minTableLen = 128
const numEntries = minTableLen * EntriesPerMapOfBucket
m := NewMapOf[int, int](WithPresize(numEntries), WithGrowOnly())
stats := m.Stats()
initialTableLen := stats.RootBuckets
for i := 0; i < 2*numEntries; i++ {
m.Store(i, i)
}
stats = m.Stats()
maxTableLen := stats.RootBuckets
if maxTableLen <= minTableLen {
t.Fatalf("table did not grow: %d", maxTableLen)
}
for i := 0; i < numEntries; i++ {
m.Delete(i)
}
stats = m.Stats()
if stats.RootBuckets != maxTableLen {
t.Fatalf("table length was different from the expected: %d", stats.RootBuckets)
}
m.Clear()
stats = m.Stats()
if stats.RootBuckets != initialTableLen {
t.Fatalf("table length was different from the initial: %d", stats.RootBuckets)
}
}
func TestMapOfResize(t *testing.T) {
const numEntries = 100_000
m := NewMapOf[string, int]()
for i := 0; i < numEntries; i++ {
m.Store(strconv.Itoa(i), i)
}
stats := m.Stats()
if stats.Size != numEntries {
t.Fatalf("size was too small: %d", stats.Size)
}
expectedCapacity := int(math.RoundToEven(MapLoadFactor+1)) * stats.RootBuckets * EntriesPerMapOfBucket
if stats.Capacity > expectedCapacity {
t.Fatalf("capacity was too large: %d, expected: %d", stats.Capacity, expectedCapacity)
}
if stats.RootBuckets <= DefaultMinMapTableLen {
t.Fatalf("table was too small: %d", stats.RootBuckets)
}
if stats.TotalGrowths == 0 {
t.Fatalf("non-zero total growths expected: %d", stats.TotalGrowths)
}
if stats.TotalShrinks > 0 {
t.Fatalf("zero total shrinks expected: %d", stats.TotalShrinks)
}
// This is useful when debugging table resize and occupancy.
// Use -v flag to see the output.
t.Log(stats.ToString())
for i := 0; i < numEntries; i++ {
m.Delete(strconv.Itoa(i))
}
stats = m.Stats()
if stats.Size > 0 {
t.Fatalf("zero size was expected: %d", stats.Size)
}
expectedCapacity = stats.RootBuckets * EntriesPerMapOfBucket
if stats.Capacity != expectedCapacity {
t.Fatalf("capacity was too large: %d, expected: %d", stats.Capacity, expectedCapacity)
}
if stats.RootBuckets != DefaultMinMapTableLen {
t.Fatalf("table was too large: %d", stats.RootBuckets)
}
if stats.TotalShrinks == 0 {
t.Fatalf("non-zero total shrinks expected: %d", stats.TotalShrinks)
}
t.Log(stats.ToString())
}
func TestMapOfResize_CounterLenLimit(t *testing.T) {
const numEntries = 1_000_000
m := NewMapOf[string, string]()
for i := 0; i < numEntries; i++ {
m.Store("foo"+strconv.Itoa(i), "bar"+strconv.Itoa(i))
}
stats := m.Stats()
if stats.Size != numEntries {
t.Fatalf("size was too small: %d", stats.Size)
}
if stats.CounterLen != MaxMapCounterLen {
t.Fatalf("number of counter stripes was too large: %d, expected: %d",
stats.CounterLen, MaxMapCounterLen)
}
}
func parallelSeqTypedResizer(m *MapOf[int, int], numEntries int, positive bool, cdone chan bool) {
for i := 0; i < numEntries; i++ {
if positive {
m.Store(i, i)
} else {
m.Store(-i, -i)
}
}
cdone <- true
}
func TestMapOfParallelResize_GrowOnly(t *testing.T) {
const numEntries = 100_000
m := NewMapOf[int, int]()
cdone := make(chan bool)
go parallelSeqTypedResizer(m, numEntries, true, cdone)
go parallelSeqTypedResizer(m, numEntries, false, cdone)
// Wait for the goroutines to finish.
<-cdone
<-cdone
// Verify map contents.
for i := -numEntries + 1; i < numEntries; i++ {
v, ok := m.Load(i)
if !ok {
t.Fatalf("value not found for %d", i)
}
if v != i {
t.Fatalf("values do not match for %d: %v", i, v)
}
}
if s := m.Size(); s != 2*numEntries-1 {
t.Fatalf("unexpected size: %v", s)
}
}
func parallelRandTypedResizer(t *testing.T, m *MapOf[string, int], numIters, numEntries int, cdone chan bool) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < numIters; i++ {
coin := r.Int63n(2)
for j := 0; j < numEntries; j++ {
if coin == 1 {
m.Store(strconv.Itoa(j), j)
} else {
m.Delete(strconv.Itoa(j))
}
}
}
cdone <- true
}
func TestMapOfParallelResize(t *testing.T) {
const numIters = 1_000
const numEntries = 2 * EntriesPerMapOfBucket * DefaultMinMapTableLen
m := NewMapOf[string, int]()
cdone := make(chan bool)
go parallelRandTypedResizer(t, m, numIters, numEntries, cdone)
go parallelRandTypedResizer(t, m, numIters, numEntries, cdone)
// Wait for the goroutines to finish.
<-cdone
<-cdone
// Verify map contents.
for i := 0; i < numEntries; i++ {
v, ok := m.Load(strconv.Itoa(i))
if !ok {
// The entry may be deleted and that's ok.
continue
}
if v != i {
t.Fatalf("values do not match for %d: %v", i, v)
}
}
s := m.Size()
if s > numEntries {
t.Fatalf("unexpected size: %v", s)
}
rs := sizeBasedOnTypedRange(m)
if s != rs {
t.Fatalf("size does not match number of entries in Range: %v, %v", s, rs)
}
}
func parallelRandTypedClearer(t *testing.T, m *MapOf[string, int], numIters, numEntries int, cdone chan bool) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < numIters; i++ {
coin := r.Int63n(2)
for j := 0; j < numEntries; j++ {
if coin == 1 {
m.Store(strconv.Itoa(j), j)
} else {
m.Clear()
}
}
}
cdone <- true
}
func TestMapOfParallelClear(t *testing.T) {
const numIters = 100
const numEntries = 1_000
m := NewMapOf[string, int]()
cdone := make(chan bool)
go parallelRandTypedClearer(t, m, numIters, numEntries, cdone)
go parallelRandTypedClearer(t, m, numIters, numEntries, cdone)
// Wait for the goroutines to finish.
<-cdone
<-cdone
// Verify map size.
s := m.Size()
if s > numEntries {
t.Fatalf("unexpected size: %v", s)
}
rs := sizeBasedOnTypedRange(m)
if s != rs {
t.Fatalf("size does not match number of entries in Range: %v, %v", s, rs)
}
}
func parallelSeqTypedStorer(t *testing.T, m *MapOf[string, int], storeEach, numIters, numEntries int, cdone chan bool) {
for i := 0; i < numIters; i++ {
for j := 0; j < numEntries; j++ {
if storeEach == 0 || j%storeEach == 0 {
m.Store(strconv.Itoa(j), j)
// Due to atomic snapshots we must see a "<j>"/j pair.
v, ok := m.Load(strconv.Itoa(j))
if !ok {
t.Errorf("value was not found for %d", j)
break
}
if v != j {
t.Errorf("value was not expected for %d: %d", j, v)
break
}
}
}
}
cdone <- true
}
func TestMapOfParallelStores(t *testing.T) {
const numStorers = 4
const numIters = 10_000
const numEntries = 100
m := NewMapOf[string, int]()
cdone := make(chan bool)
for i := 0; i < numStorers; i++ {
go parallelSeqTypedStorer(t, m, i, numIters, numEntries, cdone)
}
// Wait for the goroutines to finish.
for i := 0; i < numStorers; i++ {
<-cdone
}
// Verify map contents.
for i := 0; i < numEntries; i++ {
v, ok := m.Load(strconv.Itoa(i))
if !ok {
t.Fatalf("value not found for %d", i)
}
if v != i {
t.Fatalf("values do not match for %d: %v", i, v)
}
}
}
func parallelRandTypedStorer(t *testing.T, m *MapOf[string, int], numIters, numEntries int, cdone chan bool) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < numIters; i++ {
j := r.Intn(numEntries)
if v, loaded := m.LoadOrStore(strconv.Itoa(j), j); loaded {
if v != j {
t.Errorf("value was not expected for %d: %d", j, v)
}
}
}
cdone <- true
}
func parallelRandTypedDeleter(t *testing.T, m *MapOf[string, int], numIters, numEntries int, cdone chan bool) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < numIters; i++ {
j := r.Intn(numEntries)
if v, loaded := m.LoadAndDelete(strconv.Itoa(j)); loaded {
if v != j {
t.Errorf("value was not expected for %d: %d", j, v)
}
}
}
cdone <- true
}
func parallelTypedLoader(t *testing.T, m *MapOf[string, int], numIters, numEntries int, cdone chan bool) {
for i := 0; i < numIters; i++ {
for j := 0; j < numEntries; j++ {
// Due to atomic snapshots we must either see no entry, or a "<j>"/j pair.
if v, ok := m.Load(strconv.Itoa(j)); ok {
if v != j {
t.Errorf("value was not expected for %d: %d", j, v)
}
}
}
}
cdone <- true
}
func TestMapOfAtomicSnapshot(t *testing.T) {
const numIters = 100_000
const numEntries = 100
m := NewMapOf[string, int]()
cdone := make(chan bool)
// Update or delete random entry in parallel with loads.
go parallelRandTypedStorer(t, m, numIters, numEntries, cdone)
go parallelRandTypedDeleter(t, m, numIters, numEntries, cdone)
go parallelTypedLoader(t, m, numIters, numEntries, cdone)
// Wait for the goroutines to finish.
for i := 0; i < 3; i++ {
<-cdone
}
}
func TestMapOfParallelStoresAndDeletes(t *testing.T) {
const numWorkers = 2
const numIters = 100_000
const numEntries = 1000
m := NewMapOf[string, int]()