-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_test.go
155 lines (140 loc) · 3.03 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
package hashstr
import (
"bufio"
"flag"
"fmt"
"os"
"testing"
)
var keysFile = flag.String("keys", "", "load keys datafile")
var gkeys []string
func loadKeys() []string {
if *keysFile != "" {
return loadBigKeys(*keysFile)
}
return []string{
"foo",
"bar",
"baz",
"qux",
"zot",
"frob",
"zork",
"zeek",
}
}
func loadBigKeys(filename string) []string {
f, err := os.Open(filename)
if err != nil {
//tb.Fatalf("unable to keys file: %v", err)
panic(fmt.Sprintf("unable to keys file: %v", err))
}
defer f.Close()
scanner := bufio.NewScanner(f)
var k []string
for scanner.Scan() {
k = append(k, scanner.Text())
}
return k
}
type HashFunc func(string) uint
func testHash(t *testing.T, keys []string, bucket uint, hf HashFunc) {
count := make([]int, bucket)
for _, k := range keys {
count[hf(k)%bucket]++
}
t.Logf("%+v\n", count)
}
func TestBKDRHash1_16(t *testing.T) {
testHash(t, gkeys, 16, Hash1)
}
func TestBKDRHash1_32(t *testing.T) {
testHash(t, gkeys, 32, Hash1)
}
func TestBKDRHash2_16(t *testing.T) {
testHash(t, gkeys, 16, Hash2)
}
func TestBKDRHash2_32(t *testing.T) {
testHash(t, gkeys, 32, Hash2)
}
func TestBKDRHash3_16(t *testing.T) {
testHash(t, gkeys, 16, Hash3)
}
func TestBKDRHash3_32(t *testing.T) {
testHash(t, gkeys, 32, Hash3)
}
func TestSDBMHash_16(t *testing.T) {
testHash(t, gkeys, 16, SDBMHash)
}
func TestSDBMHash_32(t *testing.T) {
testHash(t, gkeys, 32, SDBMHash)
}
func TestAPHash_16(t *testing.T) {
testHash(t, gkeys, 16, APHash)
}
func TestAPHash_32(t *testing.T) {
testHash(t, gkeys, 32, APHash)
}
func TestMetroHash_16(t *testing.T) {
testHash(t, gkeys, 16, MetroHash)
}
func TestMetroHash_32(t *testing.T) {
testHash(t, gkeys, 32, MetroHash)
}
func TestJavaHash_16(t *testing.T) {
testHash(t, gkeys, 16, JavaHash)
}
func TestJavaHash_32(t *testing.T) {
testHash(t, gkeys, 32, JavaHash)
}
func TestDJBHash_16(t *testing.T) {
testHash(t, gkeys, 16, DJBHash)
}
func TestDJBHash_32(t *testing.T) {
testHash(t, gkeys, 32, DJBHash)
}
func TestSuperFastHash_16(t *testing.T) {
testHash(t, gkeys, 16, SuperFastHash)
}
func TestSuperFastHash_32(t *testing.T) {
testHash(t, gkeys, 32, SuperFastHash)
}
func benchmarkHash(b *testing.B, keys []string, hf HashFunc) {
for i := 0; i < b.N; i++ {
for _, k := range keys {
hf(k)
}
}
}
func BenchmarkBKDRHash1(b *testing.B) {
benchmarkHash(b, gkeys, Hash1)
}
func BenchmarkBKDRHash2(b *testing.B) {
benchmarkHash(b, gkeys, Hash2)
}
func BenchmarkBKDRHash3(b *testing.B) {
benchmarkHash(b, gkeys, Hash3)
}
func BenchmarkSDBMHash(b *testing.B) {
benchmarkHash(b, gkeys, SDBMHash)
}
func BenchmarkAPHash(b *testing.B) {
benchmarkHash(b, gkeys, APHash)
}
func BenchmarkMetroHash(b *testing.B) {
benchmarkHash(b, gkeys, MetroHash)
}
func BenchmarkJavaHash(b *testing.B) {
benchmarkHash(b, gkeys, JavaHash)
}
func BenchmarkDJBHash(b *testing.B) {
benchmarkHash(b, gkeys, DJBHash)
}
func BenchmarkSuperFastHash(b *testing.B) {
benchmarkHash(b, gkeys, SuperFastHash)
}
func TestMain(m *testing.M) {
flag.Parse()
gkeys = loadKeys()
os.Exit(m.Run())
}