forked from fanliao/go-concurrentMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
1102 lines (980 loc) · 26 KB
/
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
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 concurrent
import (
"fmt"
"math"
"math/rand"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
c "github.com/smartystreets/goconvey/convey"
)
func TestNil(t *testing.T) {
c.Convey("Nil cannot be as key", t, func() {
cm := NewConcurrentMap()
_, err := cm.Put(nil, 1)
c.So(err, c.ShouldNotBeNil)
var nilVal interface{} = nil
_, err = cm.Put(nilVal, 1)
c.So(err, c.ShouldNotBeNil)
var nilPtr *string = nil
nilVal = nilPtr
_, err = cm.Put(nilVal, 1)
c.So(err, c.ShouldNotBeNil)
_, err = cm.Put(1, nil)
c.So(err, c.ShouldNotBeNil)
_, err = cm.Put(1, nilVal)
c.So(err, c.ShouldNotBeNil)
nilVal = nilPtr
_, err = cm.Put(1, nilVal)
c.So(err, c.ShouldNotBeNil)
_, err = cm.Get(nil)
c.So(err, c.ShouldNotBeNil)
_, err = cm.Get(nilVal)
c.So(err, c.ShouldNotBeNil)
nilVal = nilPtr
_, err = cm.Get(nilVal)
c.So(err, c.ShouldNotBeNil)
})
}
/*-------------test different types as key------------------------*/
func testConcurrentMap(t *testing.T, datas map[interface{}]interface{}) {
var firstKey, firstVal interface{}
var secondaryKey, secondaryVal interface{}
i := 0
for k, v := range datas {
if i == 0 {
firstKey, firstVal = k, v
} else if i == 1 {
secondaryKey, secondaryVal = k, v
break
}
i++
}
m := NewConcurrentMap()
//test Put first key-value pair
previou, err := m.Put(firstKey, firstVal)
if previou != nil || err != nil {
t.Errorf("Put %v, %v firstly, return %v, %v, want nil, nil", firstKey, firstVal, previou, err)
}
//test Put again
previou, err = m.Put(firstKey, firstVal)
if previou != firstVal || err != nil {
t.Errorf("Put %v, %v second time, return %v, %v, want %v, nil", firstKey, firstVal, previou, err, firstVal)
}
//test PutIfAbsent, if value is incorrect, PutIfAbsent will be ignored
v := rand.Float32()
previou, err = m.PutIfAbsent(firstKey, v)
if previou != firstVal || err != nil {
t.Errorf("PutIfAbsent %v, %v three time, return %v, %v, want %v, nil", firstKey, v, previou, err, firstVal)
}
//test Get
val, err := m.Get(firstKey)
if val != firstVal || err != nil {
t.Errorf("Get %v, return %v, %v, want %v, nil", firstKey, val, err, firstVal)
}
//test Size
s := m.Size()
if s != 1 {
t.Errorf("Get size of m, return %v, want 1", s)
}
//test PutAll
m.PutAll(datas)
s = m.Size()
if s != int32(len(datas)) {
t.Errorf("Get size of m, return %v, want %v", s, len(datas))
}
//test remove a key-value pair, if value is incorrect, RemoveKV will return be ignored and return false
ok, err := m.RemoveEntry(secondaryKey, v)
if ok != false || err != nil {
t.Errorf("RemoveKV %v, %v, return %v, %v, want false, nil", secondaryKey, v, ok, err)
}
//test replace a value for a key
previou, err = m.Replace(secondaryKey, v)
if previou != secondaryVal || err != nil {
t.Errorf("Replace %v, %v, return %v, %v, want %v, nil", secondaryKey, v, previou, err, secondaryVal)
}
//test replace a value for a key-value pair, if value is incorrect, replace will ignored and return false
ok, err = m.CompareAndReplace(secondaryKey, secondaryVal, v)
if ok != false || err != nil {
t.Errorf("ReplaceWithOld %v, %v, %v, return %v, %v, want false, nil", secondaryKey, secondaryVal, v, ok, err)
}
//test replace a value for a key-value pair, if value is correct, replace will success
ok, err = m.CompareAndReplace(secondaryKey, v, secondaryVal)
if ok != true || err != nil {
t.Errorf("ReplaceWithOld %v, %v, %v, return %v, %v, want true, nil", secondaryKey, v, secondaryVal, ok, err)
}
//test remove a key
previou, err = m.Remove(secondaryKey)
if previou != secondaryVal || err != nil {
t.Errorf("Remove %v, return %v, %v, want %v, nil", secondaryKey, previou, err, secondaryVal)
}
//test clear
m.Clear()
if m.Size() != 0 {
t.Errorf("Get size of m after calling Clear(), return %v, want 0", val)
}
}
func TestIntKey(t *testing.T) {
testConcurrentMap(t, map[interface{}]interface{}{
1: 10,
2: 20,
3: 30,
4: 40,
})
}
func TestStringKey(t *testing.T) {
testConcurrentMap(t, map[interface{}]interface{}{
strconv.Itoa(1): 10,
strconv.Itoa(2): 20,
strconv.Itoa(3): 30,
strconv.Itoa(4): 40,
})
}
func Testfloat32Key(t *testing.T) {
testConcurrentMap(t, map[interface{}]interface{}{
float32(1): 10,
float32(2): 20,
float32(3): 30,
float32(4): 40,
})
}
func Testfloat64Key(t *testing.T) {
testConcurrentMap(t, map[interface{}]interface{}{
float64(1): 10,
float64(2): 20,
float64(3): 30,
float64(4): 40,
})
}
//note interface{} is empty interface
func TestEmptyInterface(t *testing.T) {
var a, b, c, d interface{} = 1, 2, 3, 4
testConcurrentMap(t, map[interface{}]interface{}{
a: 10,
b: 20,
c: 30,
d: 40,
})
cm := NewConcurrentMap()
cm.Put(a, 10)
e := a
if v, err := cm.Get(e); v != 10 || err != nil {
t.Errorf("Get %v, return %v, %v, want %v", &e, v, err, 10)
}
}
//user implements concurrent.Hasher interface
type user struct {
id string
Name string
}
func (u *user) HashBytes() []byte {
return []byte(u.id)
}
func (u *user) Equals(v2 interface{}) (equal bool) {
u2, ok := v2.(*user)
return ok && u.id == u2.id
}
//test Hasher interface
func TestHasherKey(t *testing.T) {
a, b, c, d := &user{"1", "n1"}, &user{"2", "n2"}, &user{"3", "n3"}, &user{"4", "n4"}
testConcurrentMap(t, map[interface{}]interface{}{
a: 10,
b: 20,
c: 30,
d: 40,
})
cm := NewConcurrentMap()
cm.Put(a, 10)
e := &user{"1", "n1"}
if v, err := cm.Get(e); v != 10 || err != nil {
t.Errorf("Get %v, return %v, %v, want %v", &e, v, err, 10)
}
}
//size of small is less than word size
//the memory layout is different with struct what size is greater than word size before golang 1.4
type small struct {
Id byte
name byte
}
//test small struct
func TestSmallStruct(t *testing.T) {
a, b, c, d := small{1, 1}, small{2, 2}, small{3, 3}, small{4, 4}
testConcurrentMap(t, map[interface{}]interface{}{
a: 10,
b: 20,
c: 30,
d: 40,
})
//test using the interface object and original value as key, two value should return the same hash code
cm := NewConcurrentMap()
cm.Put(a, 10)
e := small{1, 1}
if v, err := cm.Get(e); v != 10 || err != nil {
t.Errorf("Get %v, return %v, %v, want %v", &e, v, err, 10)
}
}
//compositeStruct include anothe struct
type compositeStruct struct {
F1 string
f2 int
small
}
//test composite Struct
func TestCompositeStruct(t *testing.T) {
a, b, c, d := compositeStruct{"1", 1, small{1, 1}}, compositeStruct{"2", 2, small{2, 2}}, compositeStruct{"3", 3, small{3, 3}}, compositeStruct{"4", 4, small{4, 4}}
testConcurrentMap(t, map[interface{}]interface{}{
a: 10,
b: 20,
c: 30,
d: 40,
})
//test using the interface object and original value as key, two value should return the same hash code
cm := NewConcurrentMap()
cm.Put(a, 10)
e := compositeStruct{"1", 1, small{1, 1}}
if v, err := cm.Get(e); v != 10 || err != nil {
t.Errorf("Get %v, return %v, %v, want %v", &e, v, err, 10)
}
}
/**
* test update method
* put three *user, e.g. &{"1", "jack"}, &{"2", "jack"}, &{"3", "stone"}
* last map will include:
* map{
* "jack": [&{"1", "jack"}, &{"2", "jack"}]
* "stone": [&{"3", "stone"}]
* }
**/
func TestUpdate(t *testing.T) {
//appendFunc returns a function that appends an user into *user slice
appendFunc := func(u *user) func(oldVal interface{}) (newVal interface{}) {
return func(oldVal interface{}) (newVal interface{}) {
if u == nil {
return nil
}
if oldVal == nil {
users := make([]*user, 0, 1)
return append(users, u)
} else {
return append(oldVal.([]*user), u)
}
}
}
cm := NewConcurrentMap()
//put user with name jack
u1 := &user{id: "1", Name: "jack"}
old, err := cm.Update(u1.Name, appendFunc(u1))
if old != nil || err != nil {
t.Errorf("Update %v, %v, return %v, %v, want nil, nil", u1.id, u1, old, err)
}
//Getting value by "jack" returns [u1]
v, err := cm.Get(u1.Name)
if users := v.([]*user); len(users) != 1 || users[0] != u1 {
t.Errorf("Get %v, return %v, %v, want [%v], nil", u1.id, users, err, old, u1)
}
//put another user with name jack
u2 := &user{id: "2", Name: "jack"}
old, err = cm.Update(u2.Name, appendFunc(u2))
if users := old.([]*user); old == nil || len(users) != 1 || users[0] != u1 || err != nil {
t.Errorf("Update %v, %v, return %#v, %v, want [%v], nil", u2.Name, u2, old, err, u1)
}
//Getting value by "jack" returns [u1, u2]
v, err = cm.Get(u2.Name)
if users := v.([]*user); len(users) != 2 || users[1] != u2 {
t.Errorf("Get %v, return %v, %v, want [%v, %v], nil", u2.Name, users, err, old, u1, u2)
}
//put an user with name stone
u3 := &user{id: "3", Name: "stone"}
old, err = cm.Update(u3.Name, appendFunc(u3))
if old != nil || err != nil {
t.Errorf("Update %v, %v, return %#v, %v, want nil, nil", u3.Name, u3, old, err)
}
//Getting value by "stone" returns [u3]
v, err = cm.Get(u3.Name)
if users := v.([]*user); len(users) != 1 || users[0] != u3 {
t.Errorf("Get %v, return %v, %v, want [%v], nil", u3.Name, users, err, old, u3)
}
//put nil with name stone
old, err = cm.Update(u3.Name, appendFunc(nil))
if old == nil || err != nil {
t.Errorf("Update %v, nil, return %#v, %v, want %v, nil", u3.Name, old, err, u3)
}
//Getting value by "stone" returns nil
v, err = cm.Get(u3.Name)
if v != nil || err != nil {
t.Errorf("Get %v, return %v, %v, want nil, nil", u3.Name, v, err)
}
}
//user1 implements Ider interface
type user1 struct {
id string
Name string
}
func (u *user1) Id() string {
return u.id
}
type Ider interface {
Id() string
}
//test slice, function, map, pointer and interface as key
func TestUnableHash(t *testing.T) {
testHash := func(k interface{}) (err error) {
cm := NewConcurrentMap()
_, err = cm.Put(k, 1)
return
}
//do not support slice as key
err := testHash([]int{1})
if err == nil {
t.Errorf("Put slice, return nil, should be not nil")
}
//do not support function as key
f := func() {}
err = testHash(f)
if err == nil {
t.Errorf("Put function, return nil, should be not nil")
}
//do not support map as key
err = testHash(map[int]int{1: 1})
if err == nil {
t.Errorf("Put map, return nil, should be not nil")
}
//do not support pointer as key
a := 1
err = testHash(&a)
if err == nil {
t.Errorf("Put function, return nil, should be not nil")
}
//do not support interface as key (note interface is different with interface{})
//The kind of interface is a realy pointer
var i Ider = &user1{"1", "n1"}
err = testHash(i)
if err == nil {
t.Errorf("Put map, return nil, should be not nil")
}
}
func TestToSlice(t *testing.T) {
cm := NewConcurrentMap()
kvs := cm.ToSlice()
if kvs == nil || len(kvs) != 0 {
t.Errorf("Call ToSlice for a empty map, return %v, should be empty slice", kvs)
}
cm.Put(1, 10)
kvs = cm.ToSlice()
if kvs == nil || len(kvs) != 1 || kvs[0].Key() != 1 || kvs[0].Value() != 10 {
t.Errorf("Call ToSlice after put one key-value pair, return %v, should include one entry", kvs)
}
cm.Remove(1)
kvs = cm.ToSlice()
if kvs == nil || len(kvs) != 0 {
t.Errorf("Call ToSlice for a empty map, return %v, should be empty slice", kvs)
}
}
/*--------test cases copied from go standard library's map_test.go--------------------*/
//TestNegativeZero fail
//// negative zero is a good test because:
//// 1) 0 and -0 are equal, yet have distinct representations.
//// 2) 0 is represented as all zeros, -0 isn't.
//// I'm not sure the language spec actually requires this behavior,
//// but it's what the current map implementation does.
//func TestNegativeZero(t *testing.T) {
// m := NewConcurrentMap(0)
// var zero float64 = +0.0
// var nzero float64 = math.Copysign(0.0, -1.0)
// m.Put(zero, true)
// m.Put(nzero, true) // should overwrite +0 entry
// if m.Size() != 1 {
// t.Error("length wrong", m.Size())
// }
// itr := NewHashIterator(m)
// for {
// if itr.HasNext() {
// e := itr.nextEntry()
// if math.Copysign(1.0, e.key.(float64)) > 0 {
// t.Error("wrong sign")
// }
// } else {
// break
// }
// }
// m = NewConcurrentMap(0)
// m.Put(nzero, true)
// m.Put(zero, true) // should overwrite -0.0 entry
// if m.Size() != 1 {
// t.Error("length wrong")
// }
// itr = NewHashIterator(m)
// for {
// if itr.HasNext() {
// e := itr.nextEntry()
// if math.Copysign(1.0, e.key.(float64)) < 0 {
// t.Error("wrong sign")
// }
// } else {
// break
// }
// }
//}
// nan is a good test because nan != nan, and nan has
// a randomized hash value.
func TestNan(t *testing.T) {
m := NewConcurrentMap(0) //make(map[float64]int, 0)
nan := math.NaN()
m.Put(nan, 1)
m.Put(nan, 2)
m.Put(nan, 4)
if m.Size() != 3 {
t.Error("length wrong")
}
s := 0
itr := m.Iterator()
for {
k, v, ok := itr.Next()
if !ok {
break
}
if k == k {
t.Error("nan disappeared")
}
vi := v.(int)
if (vi & (vi - 1)) != 0 {
t.Error("value wrong")
}
s |= vi
}
if s != 7 {
t.Error("values wrong")
}
}
func TestGrowWithNaN(t *testing.T) {
m := NewConcurrentMap(0) //make(map[float64]int, 0)
nan := math.NaN()
m.Put(nan, 1)
m.Put(nan, 2)
m.Put(nan, 4)
cnt := 0
s := 0
growflag := true
for itr := m.Iterator(); itr.HasNext(); {
ki, vi, _ := itr.Next()
k, v := ki.(float64), vi.(int)
if growflag {
// force a hashtable resize
for i := 0; i < 100; i++ {
m.Put(float64(i), i)
}
growflag = false
}
if k != k {
cnt++
s |= v
}
}
if cnt != 3 {
t.Error("NaN keys lost during grow")
}
if s != 7 {
t.Error("NaN values lost during grow")
}
}
type FloatInt struct {
x float64
y int
}
func TestGrowWithNegativeZero(t *testing.T) {
negzero := math.Copysign(0.0, -1.0)
m := make(map[FloatInt]int, 4)
m[FloatInt{0.0, 0}] = 1
m[FloatInt{0.0, 1}] = 2
m[FloatInt{0.0, 2}] = 4
m[FloatInt{0.0, 3}] = 8
growflag := true
s := 0
cnt := 0
negcnt := 0
// The first iteration should return the +0 key.
// The subsequent iterations should return the -0 key.
// I'm not really sure this is required by the spec,
// but it makes sense.
// TODO: are we allowed to get the first entry returned again???
for k, v := range m {
if v == 0 {
continue
} // ignore entries added to grow table
cnt++
if math.Copysign(1.0, k.x) < 0 {
if v&16 == 0 {
t.Error("key/value not updated together 1")
}
negcnt++
s |= v & 15
} else {
if v&16 == 16 {
t.Error("key/value not updated together 2", k, v)
}
s |= v
}
if growflag {
// force a hashtable resize
for i := 0; i < 100; i++ {
m[FloatInt{3.0, i}] = 0
}
// then change all the entries
// to negative zero
m[FloatInt{negzero, 0}] = 1 | 16
m[FloatInt{negzero, 1}] = 2 | 16
m[FloatInt{negzero, 2}] = 4 | 16
m[FloatInt{negzero, 3}] = 8 | 16
growflag = false
}
}
if s != 15 {
t.Error("entry missing", s)
}
if cnt != 4 {
t.Error("wrong number of entries returned by iterator", cnt)
}
if negcnt != 3 {
t.Error("update to negzero missed by iteration", negcnt)
}
}
func TestIterGrowAndDelete(t *testing.T) {
m := make(map[int]int, 4)
for i := 0; i < 100; i++ {
m[i] = i
}
growflag := true
for k := range m {
//t.Log("k ad growflag", k, growflag)
if growflag {
// grow the table
for i := 100; i < 1000; i++ {
m[i] = i
}
// delete all odd keys
for i := 1; i < 1000; i += 2 {
delete(m, i)
}
growflag = false
} else {
if k&1 == 1 {
t.Error("odd value returned")
}
}
}
}
func TestIterGrowAndDelete1(t *testing.T) {
m := NewConcurrentMap(4) // make(map[int]int, 4)
for i := 0; i < 100; i++ {
m.Put(i, i)
}
growflag := true
for itr := m.Iterator(); itr.HasNext(); {
k, _, _ := itr.Next()
//t.Log("k ad growflag111111", k, growflag)
if growflag {
// grow the table
for i := 100; i < 1000; i++ {
m.Put(i, i)
}
// delete all odd keys
for i := 1; i < 1000; i += 2 {
m.Remove(i)
}
growflag = false
} else {
if k.(int)&1 == 1 {
for itr := m.Iterator(); itr.HasNext(); {
k, _, _ := itr.Next()
if k.(int)&1 == 1 {
t.Error("odd value returned by itr")
}
}
//ConcurrentMap cannot iterate the values changed outside iterator after grow
//t.Error("odd value returned")
}
}
}
}
// make sure old bucket arrays don't get GCd while
// an iterator is still using them.
func TestIterGrowWithGC(t *testing.T) {
m := NewConcurrentMap(4) // make(map[int]int, 4)
for i := 0; i < 16; i++ {
m.Put(i, i)
}
growflag := true
bitmask := 0
for itr := m.Iterator(); itr.HasNext(); {
ki, _, _ := itr.Next()
k := ki.(int)
if k < 16 {
bitmask |= 1 << uint(k)
}
if growflag {
// grow the table
for i := 100; i < 1000; i++ {
m.Put(i, i)
}
// trigger a gc
runtime.GC()
growflag = false
}
}
if bitmask != 1<<16-1 {
t.Error("missing key", bitmask)
}
}
func testConcurrentReadsAfterGrowth(t *testing.T, useReflect bool) {
if runtime.GOMAXPROCS(-1) == 1 {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(16))
}
numLoop := 10
numGrowStep := 250
numReader := 16
if testing.Short() {
numLoop, numGrowStep = 2, 500
}
for i := 0; i < numLoop; i++ {
m := NewConcurrentMap() // make(map[int]int, 0)
for gs := 0; gs < numGrowStep; gs++ {
m.Put(gs, gs)
var wg sync.WaitGroup
wg.Add(numReader * 2)
for nr := 0; nr < numReader; nr++ {
go func() {
defer wg.Done()
for itr := m.Iterator(); itr.HasNext(); {
_, _, _ = itr.Next()
}
}()
go func() {
defer wg.Done()
for key := 0; key < gs; key++ {
_, _ = m.Get(key)
}
}()
}
wg.Wait()
}
}
}
func TestConcurrentReadsAfterGrowth(t *testing.T) {
testConcurrentReadsAfterGrowth(t, false)
}
func TestConcurrentReadsAfterGrowthReflect(t *testing.T) {
testConcurrentReadsAfterGrowth(t, true)
}
func TestBigItems(t *testing.T) {
var key [256]string
for i := 0; i < 256; i++ {
key[i] = "foo"
}
m := NewConcurrentMap(4) //make(map[[256]string][256]string, 4)
for i := 0; i < 100; i++ {
key[37] = fmt.Sprintf("string%02d", i)
m.Put(key, key) //m[key] = key
}
var keys [100]string
var values [100]string
i := 0
for itr := m.Iterator(); itr.HasNext(); {
ki, vi, _ := itr.Next()
k, v := ki.([256]string), vi.([256]string)
//for k, v := range m {
keys[i] = k[37]
values[i] = v[37]
i++
}
sort.Strings(keys[:])
sort.Strings(values[:])
for i := 0; i < 100; i++ {
if keys[i] != fmt.Sprintf("string%02d", i) {
t.Errorf("#%d: missing key: %v", i, keys[i])
}
if values[i] != fmt.Sprintf("string%02d", i) {
t.Errorf("#%d: missing value: %v", i, values[i])
}
}
}
type empty struct {
}
func TestEmptyKeyAndValue(t *testing.T) {
//a := make(map[int]empty, 4)
//b := make(map[empty]int, 4)
//c := make(map[empty]empty, 4)
a := NewConcurrentMap(4)
b := NewConcurrentMap(4)
c := NewConcurrentMap(4)
a.Put(0, empty{}) //a[0] = empty{}
b.Put(empty{}, 0) //b[empty{}] = 0
b.Put(empty{}, 1) //b[empty{}] = 1
c.Put(empty{}, empty{}) //c[empty{}] = empty{}
if a.Size() != 1 { // len(a) != 1 {
t.Errorf("empty value insert problem")
}
if v, err := b.Get(empty{}); v != 1 || err != nil { //} b[empty{}] != 1 {
t.Errorf("empty key returned wrong value")
}
}
// Tests a map with a single bucket, with same-lengthed short keys
// ("quick keys") as well as long keys.
func TestSingleBucketMapStringKeys_DupLen(t *testing.T) {
testMapLookups(t, NewConcurrentMapFromMap(map[interface{}]interface{}{
"x": "x1val",
"xx": "x2val",
"foo": "fooval",
"bar": "barval", // same key length as "foo"
"xxxx": "x4val",
strings.Repeat("x", 128): "longval1",
strings.Repeat("y", 128): "longval2",
}))
}
// Tests a map with a single bucket, with all keys having different lengths.
func TestSingleBucketMapStringKeys_NoDupLen(t *testing.T) {
testMapLookups(t, NewConcurrentMapFromMap(map[interface{}]interface{}{
"x": "x1val",
"xx": "x2val",
"foo": "fooval",
"xxxx": "x4val",
"xxxxx": "x5val",
"xxxxxx": "x6val",
strings.Repeat("x", 128): "longval",
}))
}
func testMapLookups(t *testing.T, m *ConcurrentMap) {
for itr := m.Iterator(); itr.HasNext(); {
k, v, _ := itr.Next()
if v1, err := m.Get(k); v1 != v || err != nil {
t.Fatalf("m[%q] = %q; want %q", k, v1, v)
}
}
}
//TestMapNanGrowIterator fail
//// Tests whether the iterator returns the right elements when
//// started in the middle of a grow, when the keys are NaNs.
//func TestMapNanGrowIterator(t *testing.T) {
// m := make(map[float64]int)
// nan := math.NaN()
// const nBuckets = 16
// // To fill nBuckets buckets takes LOAD * nBuckets keys.
// nKeys := int(nBuckets * *runtime.HashLoad)
// // Get map to full point with nan keys.
// for i := 0; i < nKeys; i++ {
// m[nan] = i
// }
// // Trigger grow
// m[1.0] = 1
// delete(m, 1.0)
// // Run iterator
// found := make(map[int]struct{})
// for _, v := range m {
// if v != -1 {
// if _, repeat := found[v]; repeat {
// t.Fatalf("repeat of value %d", v)
// }
// found[v] = struct{}{}
// }
// if len(found) == nKeys/2 {
// // Halfway through iteration, finish grow.
// for i := 0; i < nBuckets; i++ {
// delete(m, 1.0)
// }
// }
// }
// if len(found) != nKeys {
// t.Fatalf("missing value")
// }
//}
func TestMapIterOrder(t *testing.T) {
for _, n := range [...]int{3, 7, 9, 15} {
// Make m be {0: true, 1: true, ..., n-1: true}.
m := make(map[int]bool)
for i := 0; i < n; i++ {
m[i] = true
}
// Check that iterating over the map produces at least two different orderings.
ord := func() []int {
var s []int
for key := range m {
s = append(s, key)
}
return s
}
first := ord()
ok := false
for try := 0; try < 100; try++ {
if !reflect.DeepEqual(first, ord()) {
ok = true
break
}
}
if !ok {
t.Errorf("Map with n=%d elements had consistent iteration order: %v", n, first)
}
}
}
//TestMapStringBytesLookup fail
//func TestMapStringBytesLookup(t *testing.T) {
// // Use large string keys to avoid small-allocation coalescing,
// // which can cause AllocsPerRun to report lower counts than it should.
// m0 := map[string]int{
// "1000000000000000000000000000000000000000000000000": 1,
// "2000000000000000000000000000000000000000000000000": 2,
// }
// m1 := map[interface{}]interface{}{
// "1000000000000000000000000000000000000000000000000": 1,
// "2000000000000000000000000000000000000000000000000": 2,
// }
// _ = m1
// m := NewConcurrentMapFromMap(m1)
// buf := []byte("1000000000000000000000000000000000000000000000000")
// if x, err := m.Get(string(buf)); x != 1 || err != nil { // m[string(buf)]; x != 1 {
// t.Errorf(`m[string([]byte("1"))] = %d, want 1`, x)
// }
// buf[0] = '2'
// if x, err := m.Get(string(buf)); x != 2 || err != nil { //x := m[string(buf)]; x != 2 {
// t.Errorf(`m[string([]byte("2"))] = %d, want 2`, x)
// }
// var x int
// n := testing.AllocsPerRun(100, func() {
// _, _ = m.Get(string(buf))
// //_ = m0[string(buf)] //n will be 0
// //_ = m1[string(buf)] //n will be 2
// //x += v.(int) //m[string(buf)]
// })
// if n != 0 {
// t.Errorf("AllocsPerRun for m[string(buf)] = %v, want 0", n)
// }
// x = 0
// n = testing.AllocsPerRun(100, func() {
// y, err := m.Get(string(buf))
// //y, ok := m[string(buf)]
// if err != nil {
// panic("!ok")
// }
// x += y.(int)
// })
// if n != 0 {
// t.Errorf("AllocsPerRun for x,ok = m[string(buf)] = %v, want 0", n)
// }
//}
/*----------------test concurrent-------------------------------*/
func TestConcurrent(t *testing.T) {
numCpu := runtime.NumCPU()
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(numCpu))
writeN := 2*numCpu + 1
readN := 3*numCpu + 1
n := 1000000
var repeat int32 = 0
wWg := new(sync.WaitGroup)
wWg.Add(writeN)
cDone := make(chan struct{})
cm := NewConcurrentMap()
//start writeN goroutines to write to map with repeated keys, and count the total number of repeated key
for i := 0; i < writeN; i++ {
j := i
go func() {
for k := 0; k < n; k++ {
//0-99999, 50000-149999, 100000-19999, 150000-249999,200000-29999, 250000-349999
key := k + (j * n / 2)
if previous, err := cm.Put(key, strconv.Itoa(key)+strings.Repeat(" ", j)); err != nil {
t.Errorf("Get error %v when concurrent write map", err)
return
} else if previous != nil {
//count the total number of repeated key
atomic.AddInt32(&repeat, 1)
}
}
wWg.Done()