-
Notifications
You must be signed in to change notification settings - Fork 111
/
util_test.go
59 lines (45 loc) · 982 Bytes
/
util_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
package hashmap
import (
"testing"
"github.com/cornelk/hashmap/assert"
)
func TestLog2(t *testing.T) {
var fixtures = map[uintptr]uintptr{
0: 0,
1: 0,
2: 1,
3: 2,
4: 2,
5: 3,
}
for input, result := range fixtures {
output := log2(input)
assert.Equal(t, output, result)
}
}
func TestHashCollision(t *testing.T) {
m := New[string, int]()
staticHasher := func(key string) uintptr {
return 4 // chosen by fair dice roll. guaranteed to be random.
}
m.SetHasher(staticHasher)
inserted := m.Insert("1", 1)
assert.True(t, inserted)
inserted = m.Insert("2", 2)
assert.True(t, inserted)
value, ok := m.Get("1")
assert.True(t, ok)
assert.Equal(t, 1, value)
value, ok = m.Get("2")
assert.True(t, ok)
assert.Equal(t, 2, value)
}
func TestAliasTypeSupport(t *testing.T) {
type alias uintptr
m := New[alias, alias]()
inserted := m.Insert(1, 1)
assert.True(t, inserted)
value, ok := m.Get(1)
assert.True(t, ok)
assert.Equal(t, 1, value)
}