-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathmap_test.go
149 lines (138 loc) · 2.77 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
package goutil
import (
"sync"
"testing"
)
func TestRwMap(t *testing.T) {
m := RwMap(1000)
m.Store(1, "a")
m.Store(2, "b")
m.Store(3, "c")
m.Store(4, "d")
m.Store(5, "e")
m.Store(6, "f")
t.Logf("Len: %d", m.Len())
var s = make(map[interface{}]int)
for i := 10000; i > 0; i-- {
k, _, _ := m.Random()
s[k]++
}
t.Logf("%#v", s)
m.Clear()
if m.Len() != 0 {
t.Fatalf("after clear len: %d", m.Len())
}
}
func TestLoadOrStore(t *testing.T) {
m := RwMap()
if v, loaded := m.LoadOrStore(1, "rw-a"); v != "rw-a" || loaded {
t.Fatalf("v: %v, loaded: %v", v, loaded)
}
if v, loaded := m.LoadOrStore(1, "rw-b"); v != "rw-a" || !loaded {
t.Fatalf("v: %v, loaded: %v", v, loaded)
}
if v, loaded := m.LoadOrStore(1, "rw-c"); v != "rw-a" || !loaded {
t.Fatalf("v: %v, loaded: %v", v, loaded)
}
m.Clear()
if m.Len() != 0 {
t.Fatalf("after clear len: %d", m.Len())
}
m = AtomicMap()
if v, loaded := m.LoadOrStore(1, "atomic-a"); v != "atomic-a" || loaded {
t.Fatalf("v: %v, loaded: %v", v, loaded)
}
if v, loaded := m.LoadOrStore(1, "atomic-b"); v != "atomic-a" || !loaded {
t.Fatalf("v: %v, loaded: %v", v, loaded)
}
if v, loaded := m.LoadOrStore(1, "atomic-c"); v != "atomic-a" || !loaded {
t.Fatalf("v: %v, loaded: %v", v, loaded)
}
m.Clear()
if m.Len() != 0 {
t.Fatalf("after clear len: %d", m.Len())
}
}
func TestAtomicMap(t *testing.T) {
m := AtomicMap()
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func(a int) {
m.Store(a, "a")
m.LoadOrStore(a, "b")
m.Len()
m.Delete(a)
m.Len()
m.LoadOrStore(a, "b")
m.Store(a, "a")
m.Len()
wg.Done()
}(i)
}
wg.Wait()
if a := m.Len(); a != 10 {
b := m.Len()
t.Fatalf("Len: expect: 10, but have: %d %d", a, b)
}
var s = make(map[interface{}]int)
for i := 100000; i > 0; i-- {
k, _, _ := m.Random()
s[k]++
}
t.Logf("%#v", s)
i := 10 - 1
for ; i >= 0; i-- {
m.Delete(i)
}
if m.Len() != 0 {
t.Fatalf("Len: expect: 0, but have: %d", m.Len())
}
m.Clear()
if m.Len() != 0 {
t.Fatalf("after clear len: %d", m.Len())
}
}
func TestLen(t *testing.T) {
m := AtomicMap()
var wg sync.WaitGroup
for i := 1; i <= 100000; i++ {
wg.Add(1)
go func(a int) {
m.Store(a, "a")
wg.Done()
}(i)
}
m.Range(func(k, v interface{}) bool {
m.Delete(k)
return true
})
wg.Wait()
m.Range(func(k, v interface{}) bool {
m.Delete(k)
return true
})
if a := m.Len(); a != 0 {
t.Fatalf("Len: expect: 0, but have: %d", a)
}
m.Clear()
if m.Len() != 0 {
t.Fatalf("after clear len: %d", m.Len())
}
}
func TestOther(t *testing.T) {
m := RwMap()
t.Log(m.Load("key"))
m.Range(func(_, _ interface{}) bool {
return true
})
m.Len()
m.Random()
m = AtomicMap()
t.Log(m.Load("key"))
m.Range(func(_, _ interface{}) bool {
return true
})
m.Len()
m.Random()
}