-
Notifications
You must be signed in to change notification settings - Fork 17
/
hash_test.go
307 lines (282 loc) · 7.95 KB
/
hash_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
package gotomic
import (
"fmt"
"math/rand"
"reflect"
"runtime"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func assertMappy(t *testing.T, h *Hash, cmp map[Hashable]Thing) {
if e := h.Verify(); e != nil {
fmt.Println(h.Describe())
t.Errorf("%v should be valid, got %v", h, e)
}
if h.Size() != len(cmp) {
t.Errorf("%v should have size %v, but had size %v", h, len(cmp), h.Size())
}
if tm := h.ToMap(); !reflect.DeepEqual(tm, cmp) {
t.Errorf("%v should be %#v but is %#v", h, cmp, tm)
}
for k, v := range cmp {
if mv, _ := h.Get(k); !reflect.DeepEqual(mv, v) {
t.Errorf("%v.get(%v) should produce %v but produced %v", h, k, v, mv)
}
}
}
func fiddleHash(t *testing.T, h *Hash, s string, do, done chan bool) {
<-do
cmp := make(map[Hashable]Thing)
n := 100000
for i := 0; i < n; i++ {
k := StringKey(fmt.Sprint(s, i))
v := fmt.Sprint(k, "value")
if hv, _ := h.Put(k, v); hv != nil {
t.Errorf("1 Put(%v, %v) should produce nil but produced %v", k, v, hv)
}
cmp[k] = v
}
for k, v := range cmp {
if hv, _ := h.Get(k); !reflect.DeepEqual(hv, v) {
t.Errorf("1 Get(%v) should produce %v but produced %v", k, v, hv)
}
}
for k, v := range cmp {
v2 := fmt.Sprint(v, ".2")
cmp[k] = v2
if hv, _ := h.Put(k, v2); !reflect.DeepEqual(hv, v) {
t.Errorf("2 Put(%v, %v) should produce %v but produced %v", k, v2, v, hv)
}
}
for k, v := range cmp {
if hv, _ := h.Get(k); !reflect.DeepEqual(hv, v) {
t.Errorf("2 Get(%v) should produce %v but produced %v", k, v, hv)
}
}
for k, v := range cmp {
if hv, _ := h.Delete(k); !reflect.DeepEqual(hv, v) {
t.Errorf("1 Delete(%v) should produce %v but produced %v", k, v, hv)
}
}
for k, _ := range cmp {
if hv, _ := h.Delete(k); hv != nil {
t.Errorf("2 Delete(%v) should produce nil but produced %v", k, hv)
}
}
for k, _ := range cmp {
if hv, _ := h.Get(k); hv != nil {
t.Errorf("3 Get(%v) should produce nil but produced %v", k, hv)
}
}
done <- true
}
type hashInt int
func (self hashInt) HashCode() uint32 {
return uint32(self)
}
func (self hashInt) Equals(t Thing) bool {
if i, ok := t.(hashInt); ok {
return i == self
}
return false
}
func BenchmarkHash(b *testing.B) {
m := NewHash()
for i := 0; i < b.N; i++ {
k := hashInt(i)
m.Put(k, i)
j, _ := m.Get(k)
if j != i {
b.Error("should be same value")
}
}
}
func action(b *testing.B, m *Hash, i int, do, done chan bool) {
<-do
for j := 0; j < i; j++ {
k := hashInt(j)
m.Put(k, rand.Int())
m.Get(k)
}
done <- true
}
func BenchmarkHashConc(b *testing.B) {
b.StopTimer()
runtime.GOMAXPROCS(runtime.NumCPU())
do := make(chan bool)
done := make(chan bool)
m := NewHash()
for i := 0; i < runtime.NumCPU(); i++ {
go action(b, m, b.N, do, done)
}
close(do)
b.StartTimer()
for i := 0; i < runtime.NumCPU(); i++ {
<-done
}
runtime.GOMAXPROCS(1)
}
func TestHashPutIfPresent(t *testing.T) {
h := NewHash()
assertMappy(t, h, map[Hashable]Thing{})
if h.PutIfPresent(StringKey("k"), StringKey("v"), StringKey("blabla")) {
t.Error(h, "should not contain 'k': 'v'")
}
assertMappy(t, h, map[Hashable]Thing{})
if old, _ := h.Put(StringKey("k"), StringKey("v")); old != nil {
t.Error(h, "should not contain 'k': 'v'")
}
assertMappy(t, h, map[Hashable]Thing{StringKey("k"): StringKey("v")})
if h.PutIfPresent(StringKey("k"), StringKey("v3"), StringKey("v2")) {
t.Error(h, "should not contain 'k': 'v2'")
}
assertMappy(t, h, map[Hashable]Thing{StringKey("k"): StringKey("v")})
if !h.PutIfPresent(StringKey("k"), StringKey("v2"), StringKey("v")) {
t.Error(h, "should contain 'k': 'v'")
}
assertMappy(t, h, map[Hashable]Thing{StringKey("k"): StringKey("v2")})
if h.PutIfPresent(StringKey("k"), StringKey("v2"), StringKey("v")) {
t.Error(h, "should not contain 'k': 'v'")
}
assertMappy(t, h, map[Hashable]Thing{StringKey("k"): StringKey("v2")})
if !h.PutIfPresent(StringKey("k"), StringKey("v3"), StringKey("v2")) {
t.Error(h, "should contain 'k': 'v2'")
}
assertMappy(t, h, map[Hashable]Thing{StringKey("k"): StringKey("v3")})
}
func TestHashNilValues(t *testing.T) {
h := NewHash()
assertMappy(t, h, map[Hashable]Thing{})
h.Put(StringKey("k"), nil)
assertMappy(t, h, map[Hashable]Thing{StringKey("k"): nil})
v, ok := h.Get(StringKey("k"))
if !ok {
t.Error(h, "should contain 'k'")
}
if v != nil {
t.Error(h, "should contain 'k' => nil")
}
}
func TestHashPutIfMissing(t *testing.T) {
h := NewHash()
assertMappy(t, h, map[Hashable]Thing{})
if !h.PutIfMissing(StringKey("k"), "v") {
t.Error(h, "should not contain 'k'")
}
assertMappy(t, h, map[Hashable]Thing{StringKey("k"): "v"})
if h.PutIfMissing(StringKey("k"), "v") {
t.Error(h, "should contain 'k'")
}
assertMappy(t, h, map[Hashable]Thing{StringKey("k"): "v"})
}
func TestHashConcurrency(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
h := NewHash()
cmp := make(map[Hashable]Thing)
for i := 0; i < 1000; i++ {
k := StringKey(fmt.Sprint("StringKey", i))
v := fmt.Sprint("value", i)
h.Put(k, v)
cmp[k] = v
}
assertMappy(t, h, cmp)
do := make(chan bool)
done := make(chan bool)
for i := 0; i < runtime.NumCPU(); i++ {
go fiddleHash(t, h, fmt.Sprint("fiddler-", i, "-"), do, done)
}
close(do)
for i := 0; i < runtime.NumCPU(); i++ {
<-done
}
assertMappy(t, h, cmp)
}
func TestHashEach(t *testing.T) {
h := NewHash()
h.Put(StringKey("a"), "1")
h.Put(StringKey("b"), "2")
h.Put(StringKey("c"), "3")
h.Put(StringKey("d"), "4")
cmp := make(map[Hashable]Thing)
cmp[StringKey("a")] = "1"
cmp[StringKey("b")] = "2"
cmp[StringKey("c")] = "3"
cmp[StringKey("d")] = "4"
m := make(map[Hashable]Thing)
h.Each(func(k Hashable, v Thing) bool {
m[k] = v
return false
})
if !reflect.DeepEqual(cmp, m) {
t.Error(m, "should be", cmp)
}
}
func TestHashEachInterrupt(t *testing.T) {
h := NewHash()
h.Put(StringKey("a"), "1")
h.Put(StringKey("b"), "2")
h.Put(StringKey("c"), "3")
h.Put(StringKey("d"), "4")
m := make(map[Hashable]Thing)
interrupted := h.Each(func(k Hashable, v Thing) bool {
m[k] = v
// Break the iteration when we reach 2 elements
return len(m) == 2
})
if !interrupted {
t.Error("Iteration should have been interrupted.")
}
if len(m) != 2 {
t.Error(m, "should have 2 elements. Have", len(m))
}
}
func TestPutDelete(t *testing.T) {
h := NewHash()
if v, _ := h.Delete(StringKey("e")); v != nil {
t.Error(h, "should not be able to delete 'e' but got ", v)
}
assertMappy(t, h, map[Hashable]Thing{})
h.Put(StringKey("a"), "b")
if v, _ := h.Delete(StringKey("e")); v != nil {
t.Error(h, "should not be able to delete 'e' but got ", v)
}
assertMappy(t, h, map[Hashable]Thing{StringKey("a"): "b"})
h.Put(StringKey("a"), "b")
if v, _ := h.Delete(StringKey("e")); v != nil {
t.Error(h, "should not be able to delete 'e' but got ", v)
}
assertMappy(t, h, map[Hashable]Thing{StringKey("a"): "b"})
h.Put(StringKey("c"), "d")
if v, _ := h.Delete(StringKey("e")); v != nil {
t.Error(h, "should not be able to delete 'e' but got ", v)
}
assertMappy(t, h, map[Hashable]Thing{StringKey("a"): "b", StringKey("c"): "d"})
if v, _ := h.Delete(StringKey("a")); v != "b" {
t.Error(h, "should be able to delete 'a' but got ", v)
}
if v, _ := h.Delete(StringKey("e")); v != nil {
t.Error(h, "should not be able to delete 'e' but got ", v)
}
assertMappy(t, h, map[Hashable]Thing{StringKey("c"): "d"})
if v, _ := h.Delete(StringKey("a")); v != nil {
t.Error(h, "should not be able to delete 'a' but got ", v)
}
if v, _ := h.Delete(StringKey("e")); v != nil {
t.Error(h, "should not be able to delete 'e' but got ", v)
}
assertMappy(t, h, map[Hashable]Thing{StringKey("c"): "d"})
if v, _ := h.Delete(StringKey("c")); v != "d" {
t.Error(h, "should be able to delete 'c' but got ", v)
}
assertMappy(t, h, map[Hashable]Thing{})
if v, _ := h.Delete(StringKey("c")); v != nil {
t.Error(h, "should not be able to delete 'c' but got ", v)
}
if v, _ := h.Delete(StringKey("e")); v != nil {
t.Error(h, "should not be able to delete 'e' but got ", v)
}
assertMappy(t, h, map[Hashable]Thing{})
}