-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhasher_xxhash_test.go
115 lines (100 loc) · 2.48 KB
/
hasher_xxhash_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
//go:build go1.18
// +build go1.18
package xhash
import (
"strconv"
"strings"
"testing"
"github.com/fufuok/utils/assert"
)
func TestGenHasher64(t *testing.T) {
hasher0 := GenHasher64[string]()
var h0 string
h0 = "ff"
assert.Equal(t, hasher0(h0), hasher0(h0))
hasher1 := GenHasher64[int]()
var h1 int
h1 = 123
assert.Equal(t, hasher1(h1), hasher1(h1))
type L1 int
type L2 L1
hasher2 := GenHasher64[L2]()
var h2 L2
h2 = 123
assert.Equal(t, hasher2(h2), hasher2(h2))
assert.Equal(t, hasher1(h1), hasher2(h2))
type foo struct {
x int
y int
}
hasher3 := GenHasher64[*foo]()
h3 := new(foo)
h31 := h3
assert.Equal(t, hasher3(h3), hasher3(h31))
hasher4 := GenHasher[float64]()
assert.Equal(t, hasher4(3.1415926), hasher4(3.1415926))
assert.NotEqual(t, hasher4(3.1415926), hasher4(3.1415927))
hasher5 := GenHasher[complex128]()
assert.Equal(t, hasher5(complex(3, 5)), hasher5(complex(3, 5)))
assert.NotEqual(t, hasher5(complex(4, 5)), hasher5(complex(3, 5)))
hasher6 := GenHasher[byte]()
assert.Equal(t, hasher6('\n'), hasher6(10))
assert.NotEqual(t, hasher6('\r'), hasher6('\n'))
hasher7 := GenHasher[uintptr]()
assert.Equal(t, hasher7(8), hasher7(8))
assert.NotEqual(t, hasher7(7), hasher7(8))
}
func TestCollision_GenHasher64(t *testing.T) {
n := 20_000_000
sHasher := GenHasher64[string]()
iHasher := GenHasher64[int]()
ms := make(map[uint64]string)
mi := make(map[uint64]int)
for i := 0; i < n; i++ {
s := strconv.Itoa(i)
hs := sHasher(s)
hi := iHasher(i)
if v, ok := ms[hs]; ok {
t.Fatalf("Collision: %s(%d) == %s(%d)", v, sHasher(v), s, sHasher(s))
}
if v, ok := mi[hi]; ok {
t.Fatalf("Collision: %d(%d) == %d(%d)", v, iHasher(v), i, iHasher(i))
}
ms[hs] = s
mi[hi] = i
}
hi := iHasher(7)
if _, ok := mi[hi]; !ok {
t.Fatalf("The number 7 should exist")
}
if len(ms) != len(mi) || len(ms) != n {
t.Fatalf("Hash count: %d, %d, %d", len(ms), len(mi), n)
}
}
func BenchmarkHasher_GenHasher64(b *testing.B) {
sHasher := GenHasher64[string]()
iHasher := GenHasher64[int]()
b.ReportAllocs()
b.ResetTimer()
b.Run("string", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sHasher(strconv.Itoa(i))
}
})
b.Run("int", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = iHasher(i)
}
})
}
func BenchmarkHasher_Parallel_GenHasher64(b *testing.B) {
sHasher := GenHasher64[string]()
s := strings.Repeat(testString, 10)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = sHasher(s)
}
})
}