-
Notifications
You must be signed in to change notification settings - Fork 5
/
hashmap_test.go
160 lines (129 loc) · 2.74 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
package hashmap
import (
"testing"
"fmt"
)
const iterationCount = 1000000
// Set int: int
func testIntSet(t *testing.T, blockSize int) {
hashMap := NewHashMap(blockSize, hashFunc)
for i := 0; i < iterationCount; i++ {
err := hashMap.Set(i, i)
if err != nil {
t.Errorf("Setting error for key %d", i)
}
}
}
func TestIntSet16(t *testing.T) {
testIntSet(t, 16)
}
func TestIntSet64(t *testing.T) {
testIntSet(t, 64)
}
func TestIntSet128(t *testing.T) {
testIntSet(t, 128)
}
func TestIntSet1024(t *testing.T) {
testIntSet(t, 1024)
}
// Get int
func testIntGet(t *testing.T, blockSize int) {
hashMap := NewHashMap(blockSize)
for i := 0; i < iterationCount; i++ {
err := hashMap.Set(i, i)
if err != nil {
t.Errorf("Setting error for key %d", i)
}
}
for i := 0; i < iterationCount; i++ {
_, err := hashMap.Get(i)
if err != nil {
t.Errorf("Inserted key %d not found", i)
}
}
}
// Get int
func testStringGet(t *testing.T, blockSize int) {
hashMap := NewHashMap(blockSize)
for i := 0; i < iterationCount; i++ {
err := hashMap.Set(string(i), string(i))
if err != nil {
t.Errorf("Setting error for key %d", i)
}
}
for i := 0; i < iterationCount; i++ {
_, err := hashMap.Get(string(i))
if err != nil {
t.Errorf("Inserted key %d not found", i)
}
}
}
func TestIntGet16(t *testing.T) {
testIntGet(t, 16)
}
func TestIntGet64(t *testing.T) {
testIntGet(t, 64)
}
func TestIntGet128(t *testing.T) {
testIntGet(t, 128)
}
func TestIntGet1024(t *testing.T) {
testIntGet(t, 1024)
}
func TestStringGet1024(t *testing.T) {
testStringGet(t, 1024)
}
// Unset int
func testIntUnset(t *testing.T, blockSize int) {
hashMap := NewHashMap(blockSize)
for i := 0; i < iterationCount; i++ {
err := hashMap.Set(i, i)
if err != nil {
t.Errorf("Setting error for key %d", i)
}
}
for i := 0; i < iterationCount; i++ {
err := hashMap.Unset(i)
if err != nil {
t.Errorf("Unset key %d error", i)
}
}
}
func TestIntUnset16(t *testing.T) {
testIntUnset(t, 16)
}
func TestIntUnset64(t *testing.T) {
testIntUnset(t, 64)
}
func TestIntUnset128(t *testing.T) {
testIntUnset(t, 128)
}
func TestIntUnset1024(t *testing.T) {
testIntUnset(t, 1024)
}
// Set string
func testStringSet(t *testing.T, blockSize int) {
hashMap := NewHashMap(blockSize)
for i := 0; i < iterationCount; i++ {
err := hashMap.Set(string(i), string(i))
if err != nil {
t.Errorf("Setting error for key %d", i)
}
}
}
func TestStringSet1024(t *testing.T) {
testStringSet(t, 1024)
}
// Iterate
func TestIterate(t *testing.T) {
hashMap := NewHashMap(16)
for i := 0; i < 100; i++ {
err := hashMap.Set(i, i)
if err != nil {
t.Errorf("Setting error for key %d", i)
}
}
for r := range hashMap.Iter() {
fmt.Sprintf("%s: %s, ", r.key, r.value)
}
}