1 package xxh3
2
3 import (
4 "fmt"
5 "math/rand"
6 "testing"
7 )
8
9 func TestHasherCompat(t *testing.T) {
10 buf := make([]byte, 40970)
11 for i := range buf {
12 buf[i] = byte(uint64(i+1) * 2654435761)
13 }
14
15 for n := range buf {
16 check := func() {
17 h := New()
18 h.Write(buf[:n/2])
19 h.Reset()
20 h.Write(buf[:n])
21 if exp, got := Hash(buf[:n]), h.Sum64(); exp != got {
22 t.Fatalf("% -4d: %016x != %016x", n, exp, got)
23 }
24 if exp, got := Hash128(buf[:n]), h.Sum128(); exp != got {
25 t.Fatalf("% -4d: %016x != %016x", n, exp, got)
26 }
27 }
28
29 withAVX512(check)
30 withAVX2(check)
31 withSSE2(check)
32 withGeneric(check)
33 }
34 }
35
36 func TestHasherZeroValue(t *testing.T) {
37 buf := make([]byte, 40970)
38 for i := range buf {
39 buf[i] = byte(uint64(i+1) * 2654435761)
40 }
41
42 for n := range buf {
43 check := func() {
44 var h Hasher
45 h.Write(buf[:n])
46 if exp, got := Hash(buf[:n]), h.Sum64(); exp != got {
47 t.Fatalf("% -4d: %016x != %016x", n, exp, got)
48 }
49 if exp, got := Hash128(buf[:n]), h.Sum128(); exp != got {
50 t.Fatalf("% -4d: %016x != %016x", n, exp, got)
51 }
52 }
53
54 withAVX512(check)
55 withAVX2(check)
56 withSSE2(check)
57 withGeneric(check)
58 }
59 }
60
61 func TestHasherCompatSeed(t *testing.T) {
62 buf := make([]byte, 40970)
63 for i := range buf {
64 buf[i] = byte(uint64(i+1) * 2654435761)
65 }
66 rng := rand.New(rand.NewSource(42))
67
68 for n := range buf {
69 seed := rng.Uint64()
70
71 check := func() {
72 h := NewSeed(seed)
73
74 h.Write(buf[:n/2])
75 h.Reset()
76 h.Write(buf[:n])
77
78 if exp, got := HashSeed(buf[:n], seed), h.Sum64(); exp != got {
79 t.Fatalf("Sum64: % -4d: %016x != %016x, seed:%x", n, exp, got, seed)
80 return
81 }
82 if exp, got := Hash128Seed(buf[:n], seed), h.Sum128(); exp != got {
83 t.Errorf("Sum128: % -4d: %016x != %016x", n, exp, got)
84 }
85 }
86
87 withGeneric(check)
88 withAVX512(check)
89 withAVX2(check)
90 withSSE2(check)
91 }
92 }
93
94 func BenchmarkHasher64(b *testing.B) {
95 rng := rand.New(rand.NewSource(42))
96 for n := uint(4); n <= 28; n += 2 {
97 size := 1 << n
98 buf := make([]byte, size)
99 for i := range buf {
100 buf[i] = byte(uint64(i+int(n)+1) * 2654435761)
101 }
102 seed := rng.Uint64() | 1
103 b.Run(fmt.Sprint(size), func(b *testing.B) {
104 var bn *testing.B
105 check := func() {
106 bn.Run("plain", func(b *testing.B) {
107 h := New()
108 b.ReportAllocs()
109 b.ResetTimer()
110 b.SetBytes(int64(size))
111 for i := 0; i < b.N; i++ {
112 h.Reset()
113 h.Write(buf[:size])
114 _ = h.Sum64()
115 }
116 })
117 bn.Run("seed", func(b *testing.B) {
118 h := NewSeed(seed)
119 b.ReportAllocs()
120 b.ResetTimer()
121 b.SetBytes(int64(size))
122 for i := 0; i < b.N; i++ {
123 h.Reset()
124 h.Write(buf[:size])
125 _ = h.Sum64()
126 }
127 })
128 }
129
130 b.Run("go", func(b *testing.B) {
131 bn = b
132 withGeneric(check)
133 })
134 if hasAVX512 {
135 b.Run("avx512", func(b *testing.B) {
136 bn = b
137 withAVX512(check)
138 })
139 }
140 if hasAVX2 {
141 b.Run("avx2", func(b *testing.B) {
142 bn = b
143 withAVX2(check)
144 })
145 }
146 if hasSSE2 {
147 b.Run("sse2", func(b *testing.B) {
148 bn = b
149 withSSE2(check)
150 })
151 }
152 })
153 }
154 }
155
156 func BenchmarkHasher128(b *testing.B) {
157 rng := rand.New(rand.NewSource(42))
158 for n := uint(4); n <= 28; n += 2 {
159 size := 1 << n
160 buf := make([]byte, size)
161 for i := range buf {
162 buf[i] = byte(uint64(i+int(n)+1) * 2654435761)
163 }
164 seed := rng.Uint64() | 1
165 b.Run(fmt.Sprint(size), func(b *testing.B) {
166 var bn *testing.B
167 check := func() {
168 bn.Run("plain", func(b *testing.B) {
169 h := New()
170 b.ReportAllocs()
171 b.ResetTimer()
172 b.SetBytes(int64(size))
173 for i := 0; i < b.N; i++ {
174 h.Reset()
175 h.Write(buf[:size])
176 _ = h.Sum128()
177 }
178 })
179 bn.Run("seed", func(b *testing.B) {
180 h := NewSeed(seed)
181 b.ReportAllocs()
182 b.ResetTimer()
183 b.SetBytes(int64(size))
184 for i := 0; i < b.N; i++ {
185 h.Reset()
186 h.Write(buf[:size])
187 _ = h.Sum128()
188 }
189 })
190 }
191
192 b.Run("go", func(b *testing.B) {
193 bn = b
194 withGeneric(check)
195 })
196 if hasAVX512 {
197 b.Run("avx512", func(b *testing.B) {
198 bn = b
199 withAVX512(check)
200 })
201 }
202 if hasAVX2 {
203 b.Run("avx2", func(b *testing.B) {
204 bn = b
205 withAVX2(check)
206 })
207 }
208 if hasSSE2 {
209 b.Run("sse2", func(b *testing.B) {
210 bn = b
211 withSSE2(check)
212 })
213 }
214 })
215 }
216 }
217
View as plain text