forked from cornelk/hashmap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashmap_test.go
385 lines (325 loc) · 7.57 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
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
package hashmap
import (
"fmt"
"strconv"
"sync/atomic"
"testing"
"time"
)
type Animal struct {
name string
}
func TestMapCreation(t *testing.T) {
m := &HashMap{}
if m.Len() != 0 {
t.Errorf("new map should be empty but has %d items.", m.Len())
}
}
func TestOverwrite(t *testing.T) {
m := &HashMap{}
elephant := "elephant"
monkey := "monkey"
m.Set(1, elephant)
m.Set(1, monkey)
if m.Len() != 1 {
t.Errorf("map should contain exactly one element but has %v items.", m.Len())
}
item, ok := m.Get(1) // Retrieve inserted element.
if !ok {
t.Error("ok should be true for item stored within the map.")
}
if item != monkey {
t.Error("wrong item returned.")
}
}
func TestInsert(t *testing.T) {
m := &HashMap{}
_, ok := m.GetUintKey(0)
if ok {
t.Error("empty map should not return an item.")
}
c := uintptr(16)
ok = m.Insert(uintptr(0), c)
if !ok {
t.Error("insert did not succeed.")
}
ok = m.Insert(uintptr(128), c)
if !ok {
t.Error("insert did not succeed.")
}
ok = m.Insert(uintptr(128), c)
if ok {
t.Error("insert on existing item did succeed.")
}
_, ok = m.GetUintKey(128)
if !ok {
t.Error("ok should be true for item stored within the map.")
}
_, ok = m.GetUintKey(127)
if ok {
t.Error("item for key should not exist.")
}
if m.Len() != 2 {
t.Errorf("map should contain exactly 2 elements but has %v items.", m.Len())
}
}
func TestSet(t *testing.T) {
m := New(4)
elephant := "elephant"
monkey := "monkey"
m.Set(4, elephant)
m.Set(3, elephant)
m.Set(2, monkey)
m.Set(1, monkey)
if m.Len() != 4 {
t.Error("map should contain exactly 4 elements.")
}
}
func TestGet(t *testing.T) {
m := &HashMap{}
elephant := "elephant"
val, ok := m.Get("animal") // Get a missing element.
if ok {
t.Error("ok should be false when item is missing from map.")
}
if val != nil {
t.Error("Missing values should return as nil.")
}
m.Set("animal", elephant)
_, ok = m.Get("human") // Get a missing element.
if ok {
t.Error("ok should be false when item is missing from map.")
}
value, ok := m.Get("animal") // Retrieve inserted element.
if !ok {
t.Error("ok should be true for item stored within the map.")
}
if value != elephant {
t.Error("item was modified.")
}
}
func TestGrow(t *testing.T) {
m := &HashMap{}
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(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() != 34 {
t.Error("Expecting 34 percent fillrate.")
}
for i := 0; i < itemCount; i++ {
_, ok := m.Get(uintptr(i))
if !ok {
t.Error("Getting inserted item failed.")
}
}
}
func TestStringer(t *testing.T) {
m := &HashMap{}
elephant := &Animal{"elephant"}
monkey := &Animal{"monkey"}
s := m.String()
if s != "[]" {
t.Error("empty map as string does not match.")
}
m.Set(0, elephant)
s = m.String()
hashedKey0 := getKeyHash(0)
if s != fmt.Sprintf("[%v]", hashedKey0) {
t.Error("1 item map as string does not match:", s)
}
m.Set(1, monkey)
s = m.String()
hashedKey1 := getKeyHash(1)
if s != fmt.Sprintf("[%v,%v]", hashedKey1, hashedKey0) {
t.Error("2 item map as string does not match:", s)
}
}
func TestDelete(t *testing.T) {
m := &HashMap{}
m.Del(0)
elephant := &Animal{"elephant"}
monkey := &Animal{"monkey"}
m.Set(1, elephant)
m.Set(2, monkey)
m.Del(0)
m.Del(3)
if m.Len() != 2 {
t.Error("map should contain exactly two elements.")
}
m.Del(1)
m.Del(1)
m.Del(2)
if m.Len() != 0 {
t.Error("map should be empty.")
}
for item := range m.Iter() {
t.Errorf("map should be empty but got %v in the iterator.", item)
}
val, ok := m.Get(1) // Get a missing element.
if ok {
t.Error("ok should be false when item is missing from map.")
}
if val != nil {
t.Error("Missing values should return as nil.")
}
m.Set(1, elephant)
}
func TestIterator(t *testing.T) {
m := &HashMap{}
for item := range m.Iter() {
t.Errorf("Expected no object but got %v.", item)
}
itemCount := 16
for i := itemCount; i > 0; i-- {
m.Set(uintptr(i), &Animal{strconv.Itoa(i)})
}
counter := 0
for item := range m.Iter() {
val := item.Value
if val == nil {
t.Error("Expecting an object.")
}
counter++
}
if counter != itemCount {
t.Error("Returned item count did not match.")
}
}
func TestHashedKey(t *testing.T) {
m := &HashMap{}
_, ok := m.GetHashedKey(uintptr(0))
if ok {
t.Error("empty map should not return an item.")
}
m.DelHashedKey(uintptr(0))
m.allocate(uintptr(64))
m.DelHashedKey(uintptr(0))
itemCount := 16
log := log2(uintptr(itemCount))
for i := 0; i < itemCount; i++ {
m.SetHashedKey(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.GetHashedKey(uintptr(i) << (strconv.IntSize - log))
if !ok {
t.Error("Getting inserted item failed.")
}
}
for i := 0; i < itemCount; i++ {
m.DelHashedKey(uintptr(i) << (strconv.IntSize - log))
}
_, ok = m.GetHashedKey(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 := &HashMap{}
elephant := &Animal{"elephant"}
monkey := &Animal{"monkey"}
m.SetHashedKey(1<<(strconv.IntSize-2), elephant)
if m.Len() != 1 {
t.Error("map should contain exactly one element.")
}
if !m.CasHashedKey(1<<(strconv.IntSize-2), elephant, monkey) {
t.Error("Cas should success if expectation met")
}
if m.CasHashedKey(1<<(strconv.IntSize-2), elephant, monkey) {
t.Error("Cas should fail if expectation didn't meet")
}
item, ok := m.GetHashedKey(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 TestCompareAndSwap(t *testing.T) {
m := &HashMap{}
ok := m.CasHashedKey(uintptr(0), nil, nil)
if ok {
t.Error("empty map should not return an item.")
}
elephant := &Animal{"elephant"}
monkey := &Animal{"monkey"}
m.Set("animal", elephant)
if m.Len() != 1 {
t.Error("map should contain exactly one element.")
}
if !m.Cas("animal", elephant, monkey) {
t.Error("Cas should success if expectation met")
}
if m.Cas("animal", elephant, monkey) {
t.Error("Cas should fail if expectation didn't meet")
}
item, ok := m.Get("animal")
if !ok {
t.Error("ok should be true for item stored within the map.")
}
if item != monkey {
t.Error("wrong item returned.")
}
}
// TestAPICounter shows how to use the hashmap to count REST server API calls
func TestAPICounter(t *testing.T) {
m := &HashMap{}
for i := 0; i < 100; i++ {
s := fmt.Sprintf("/api%d/", i%4)
for {
counter := int64(0)
actual, _ := m.GetOrInsert(s, &counter)
c := actual.(*int64)
atomic.AddInt64(c, 1)
break
}
}
s := fmt.Sprintf("/api%d/", 0)
val, _ := m.GetStringKey(s)
c := val.(*int64)
if *c != 25 {
t.Error("wrong API call count.")
}
}
func TestExample(t *testing.T) {
m := &HashMap{}
i := 123
m.Set("amount", i)
j, ok := m.Get("amount")
if !ok {
t.Fail()
}
if i != j {
t.Fail()
}
}