forked from cornelk/hashmap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashmap_test.go
238 lines (209 loc) · 4.86 KB
/
hashmap_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
package fastintmap
import (
"fmt"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
)
type Animal struct {
name string
}
func TestMapCreation(t *testing.T) {
m := &Map[uintptr]{}
if m.Len() != 0 {
t.Errorf("new map should be empty but has %d items.", m.Len())
}
}
func TestGrow(t *testing.T) {
m := &Map[uintptr]{}
m.Grow(uintptr(63))
for { // make sure to wait for resize operation to finish
if atomic.LoadUintptr(&m.resizing) == 0 {
break
}
time.Sleep(time.Microsecond * 50)
}
d := m.mapData()
if d.keyShifts != 58 {
t.Error("Grow operation did not result in correct internal map data structure.")
}
}
func TestResize(t *testing.T) {
m := New[*Animal](2)
itemCount := 50
for i := 0; i < itemCount; i++ {
m.Set(uintptr(i), &Animal{strconv.Itoa(i)})
}
if m.Len() != itemCount {
t.Error("Expected element count did not match.")
}
for { // make sure to wait for resize operation to finish
if atomic.LoadUintptr(&m.resizing) == 0 {
break
}
time.Sleep(time.Microsecond * 50)
}
if m.FillRate() != 0.5 {
t.Errorf("Expecting 0.5 fill-rate got %f.", m.FillRate())
}
for i := 0; i < itemCount; i++ {
_, ok := m.Get(uintptr(i))
if !ok {
t.Error("Getting inserted item failed.")
}
}
}
func TestHashedKey(t *testing.T) {
m := &Map[*Animal]{}
_, ok := m.Get(uintptr(0))
if ok {
t.Error("empty map should not return an item.")
}
m.Delete(uintptr(0))
m.allocate(uintptr(64))
m.Delete(uintptr(0))
itemCount := 16
log := log2(uintptr(itemCount))
for i := 0; i < itemCount; i++ {
m.Set(uintptr(i)<<(strconv.IntSize-log), &Animal{strconv.Itoa(i)})
}
if m.Len() != itemCount {
t.Error("Expected element count did not match.")
}
for i := 0; i < itemCount; i++ {
_, ok = m.Get(uintptr(i) << (strconv.IntSize - log))
if !ok {
t.Error("Getting inserted item failed.")
}
}
for i := 0; i < itemCount; i++ {
m.Delete(uintptr(i) << (strconv.IntSize - log))
}
_, ok = m.Get(uintptr(0))
if ok {
t.Error("item for key should not exist.")
}
if m.Len() != 0 {
t.Error("Map is not empty.")
}
}
func TestCompareAndSwapHashedKey(t *testing.T) {
m := &Map[*Animal]{}
elephant := &Animal{"elephant"}
monkey := &Animal{"monkey"}
m.Set(1<<(strconv.IntSize-2), elephant)
if m.Len() != 1 {
t.Error("map should contain exactly one element.")
}
if !m.CAS(1<<(strconv.IntSize-2), elephant, monkey) {
t.Error("Cas should success if expectation met")
}
if m.Len() != 1 {
t.Error("map should contain exactly one element.")
}
if m.CAS(1<<(strconv.IntSize-2), elephant, monkey) {
t.Error("Cas should fail if expectation didn't meet")
}
if m.Len() != 1 {
t.Error("map should contain exactly one element.")
}
item, ok := m.Get(1 << (strconv.IntSize - 2))
if !ok {
t.Error("ok should be true for item stored within the map.")
}
if item != monkey {
t.Error("wrong item returned.")
}
}
func TestHashMap_parallel(t *testing.T) {
max := 10
dur := 2 * time.Second
m := &Map[int]{}
do := func(t *testing.T, max int, d time.Duration, fn func(*testing.T, int)) <-chan error {
t.Helper()
done := make(chan error)
var times int64
// This goroutines will terminate test in case if closure hangs.
go func() {
for {
select {
case <-time.After(d + 500*time.Millisecond):
if atomic.LoadInt64(×) == 0 {
done <- fmt.Errorf("closure was not executed even once, something blocks it")
}
close(done)
case <-done:
}
}
}()
go func() {
timer := time.NewTimer(d)
defer timer.Stop()
InfLoop:
for {
for i := 0; i < max; i++ {
select {
case <-timer.C:
break InfLoop
default:
}
fn(t, i)
atomic.AddInt64(×, 1)
}
}
close(done)
}()
return done
}
wait := func(t *testing.T, done <-chan error) {
t.Helper()
if err := <-done; err != nil {
t.Error(err)
}
}
// Initial fill.
for i := 0; i < max; i++ {
m.Set(uintptr(i), i)
}
t.Run("set_get", func(t *testing.T) {
doneSet := do(t, max, dur, func(t *testing.T, i int) {
m.Set(uintptr(i), i)
})
doneGetHashedKey := do(t, max, dur, func(t *testing.T, i int) {
if _, ok := m.Get(uintptr(i)); !ok {
t.Errorf("missing value for key: %d", i)
}
})
wait(t, doneSet)
wait(t, doneGetHashedKey)
})
t.Run("get-or-insert-and-delete", func(t *testing.T) {
doneGetOrInsert := do(t, max, dur, func(t *testing.T, i int) {
m.GetOrAdd(uintptr(i), i)
})
doneDel := do(t, max, dur, func(t *testing.T, i int) {
m.Delete(uintptr(i))
})
wait(t, doneGetOrInsert)
wait(t, doneDel)
})
}
func TestHashMap_SetConcurrent(t *testing.T) {
blocks := &Map[struct{}]{}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(blocks *Map[struct{}], i int) {
defer wg.Done()
blocks.Set(uintptr(i), struct{}{})
wg.Add(1)
go func(blocks *Map[struct{}], i int) {
defer wg.Done()
blocks.Get(uintptr(i))
}(blocks, i)
}(blocks, i)
}
wg.Wait()
}