| 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 |
seed := uint64(n) |
| 28 |
h.ResetSeed(seed) |
| 29 |
h.Write(buf[:n]) |
| 30 |
if exp, got := HashSeed(buf[:n], seed), h.Sum64(); exp != got { |
| 31 |
t.Fatalf("% -4d: %016x != %016x", n, exp, got) |
| 32 |
} |
| 33 |
if exp, got := Hash128Seed(buf[:n], seed), h.Sum128(); exp != got { |
| 34 |
t.Fatalf("% -4d: %016x != %016x", n, exp, got) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
withAVX512(check) |
| 39 |
withAVX2(check) |
| 40 |
withSSE2(check) |
| 41 |
withGeneric(check) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestHasher128Compat(t *testing.T) { |
| 46 |
buf := make([]byte, 40970) |
| 47 |
for i := range buf { |
| 48 |
buf[i] = byte(uint64(i+1) * 2654435761) |
| 49 |
} |
| 50 |
|
| 51 |
for n := range buf { |
| 52 |
check := func() { |
| 53 |
h := New128() |
| 54 |
h.Write(buf[:n/2]) |
| 55 |
h.Reset() |
| 56 |
h.Write(buf[:n]) |
| 57 |
if exp, got := Hash128(buf[:n]), h.Sum128(); exp != got { |
| 58 |
t.Fatalf("% -4d: %016x != %016x", n, exp, got) |
| 59 |
} |
| 60 |
|
| 61 |
seed := uint64(n) |
| 62 |
h.ResetSeed(seed) |
| 63 |
h.Write(buf[:n]) |
| 64 |
if exp, got := Hash128Seed(buf[:n], seed), h.Sum128(); exp != got { |
| 65 |
t.Fatalf("% -4d: %016x != %016x", n, exp, got) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
withAVX512(check) |
| 70 |
withAVX2(check) |
| 71 |
withSSE2(check) |
| 72 |
withGeneric(check) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestHasherZeroValue(t *testing.T) { |
| 77 |
buf := make([]byte, 40970) |
| 78 |
for i := range buf { |
| 79 |
buf[i] = byte(uint64(i+1) * 2654435761) |
| 80 |
} |
| 81 |
|
| 82 |
for n := range buf { |
| 83 |
check := func() { |
| 84 |
var h Hasher |
| 85 |
h.Write(buf[:n]) |
| 86 |
if exp, got := Hash(buf[:n]), h.Sum64(); exp != got { |
| 87 |
t.Fatalf("% -4d: %016x != %016x", n, exp, got) |
| 88 |
} |
| 89 |
if exp, got := Hash128(buf[:n]), h.Sum128(); exp != got { |
| 90 |
t.Fatalf("% -4d: %016x != %016x", n, exp, got) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
withAVX512(check) |
| 95 |
withAVX2(check) |
| 96 |
withSSE2(check) |
| 97 |
withGeneric(check) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestHasher128ZeroValue(t *testing.T) { |
| 102 |
buf := make([]byte, 40970) |
| 103 |
for i := range buf { |
| 104 |
buf[i] = byte(uint64(i+1) * 2654435761) |
| 105 |
} |
| 106 |
|
| 107 |
for n := range buf { |
| 108 |
check := func() { |
| 109 |
var h Hasher128 |
| 110 |
h.Write(buf[:n]) |
| 111 |
if exp, got := Hash128(buf[:n]), h.Sum128(); exp != got { |
| 112 |
t.Fatalf("% -4d: %016x != %016x", n, exp, got) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
withAVX512(check) |
| 117 |
withAVX2(check) |
| 118 |
withSSE2(check) |
| 119 |
withGeneric(check) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func TestHasherCompatSeed(t *testing.T) { |
| 124 |
buf := make([]byte, 40970) |
| 125 |
for i := range buf { |
| 126 |
buf[i] = byte(uint64(i+1) * 2654435761) |
| 127 |
} |
| 128 |
rng := rand.New(rand.NewSource(42)) |
| 129 |
|
| 130 |
for n := range buf { |
| 131 |
seed := rng.Uint64() |
| 132 |
|
| 133 |
check := func() { |
| 134 |
h := NewSeed(seed) |
| 135 |
|
| 136 |
h.Write(buf[:n/2]) |
| 137 |
h.Reset() |
| 138 |
h.Write(buf[:n]) |
| 139 |
|
| 140 |
if exp, got := HashSeed(buf[:n], seed), h.Sum64(); exp != got { |
| 141 |
t.Fatalf("Sum64: % -4d: %016x != %016x, seed:%x", n, exp, got, seed) |
| 142 |
return |
| 143 |
} |
| 144 |
if exp, got := Hash128Seed(buf[:n], seed), h.Sum128(); exp != got { |
| 145 |
t.Errorf("Sum128: % -4d: %016x != %016x", n, exp, got) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
withGeneric(check) |
| 150 |
withAVX512(check) |
| 151 |
withAVX2(check) |
| 152 |
withSSE2(check) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
func TestHasher128CompatSeed(t *testing.T) { |
| 157 |
buf := make([]byte, 40970) |
| 158 |
for i := range buf { |
| 159 |
buf[i] = byte(uint64(i+1) * 2654435761) |
| 160 |
} |
| 161 |
rng := rand.New(rand.NewSource(42)) |
| 162 |
|
| 163 |
for n := range buf { |
| 164 |
seed := rng.Uint64() |
| 165 |
|
| 166 |
check := func() { |
| 167 |
h := NewSeed128(seed) |
| 168 |
|
| 169 |
h.Write(buf[:n/2]) |
| 170 |
h.Reset() |
| 171 |
h.Write(buf[:n]) |
| 172 |
|
| 173 |
if exp, got := Hash128Seed(buf[:n], seed), h.Sum128(); exp != got { |
| 174 |
t.Errorf("Sum128: % -4d: %016x != %016x", n, exp, got) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
withGeneric(check) |
| 179 |
withAVX512(check) |
| 180 |
withAVX2(check) |
| 181 |
withSSE2(check) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func BenchmarkHasher64(b *testing.B) { |
| 186 |
rng := rand.New(rand.NewSource(42)) |
| 187 |
for n := uint(4); n <= 28; n += 2 { |
| 188 |
size := 1 << n |
| 189 |
buf := make([]byte, size) |
| 190 |
for i := range buf { |
| 191 |
buf[i] = byte(uint64(i+int(n)+1) * 2654435761) |
| 192 |
} |
| 193 |
seed := rng.Uint64() | 1 |
| 194 |
b.Run(fmt.Sprint(size), func(b *testing.B) { |
| 195 |
var bn *testing.B |
| 196 |
check := func() { |
| 197 |
bn.Run("plain", func(b *testing.B) { |
| 198 |
h := New() |
| 199 |
b.ReportAllocs() |
| 200 |
b.ResetTimer() |
| 201 |
b.SetBytes(int64(size)) |
| 202 |
for i := 0; i < b.N; i++ { |
| 203 |
h.Reset() |
| 204 |
h.Write(buf[:size]) |
| 205 |
_ = h.Sum64() |
| 206 |
} |
| 207 |
}) |
| 208 |
bn.Run("seed", func(b *testing.B) { |
| 209 |
h := NewSeed(seed) |
| 210 |
b.ReportAllocs() |
| 211 |
b.ResetTimer() |
| 212 |
b.SetBytes(int64(size)) |
| 213 |
for i := 0; i < b.N; i++ { |
| 214 |
h.Reset() |
| 215 |
h.Write(buf[:size]) |
| 216 |
_ = h.Sum64() |
| 217 |
} |
| 218 |
}) |
| 219 |
} |
| 220 |
|
| 221 |
b.Run("go", func(b *testing.B) { |
| 222 |
bn = b |
| 223 |
withGeneric(check) |
| 224 |
}) |
| 225 |
if hasAVX512 { |
| 226 |
b.Run("avx512", func(b *testing.B) { |
| 227 |
bn = b |
| 228 |
withAVX512(check) |
| 229 |
}) |
| 230 |
} |
| 231 |
if hasAVX2 { |
| 232 |
b.Run("avx2", func(b *testing.B) { |
| 233 |
bn = b |
| 234 |
withAVX2(check) |
| 235 |
}) |
| 236 |
} |
| 237 |
if hasSSE2 { |
| 238 |
b.Run("sse2", func(b *testing.B) { |
| 239 |
bn = b |
| 240 |
withSSE2(check) |
| 241 |
}) |
| 242 |
} |
| 243 |
}) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func BenchmarkHasher128(b *testing.B) { |
| 248 |
rng := rand.New(rand.NewSource(42)) |
| 249 |
for n := uint(4); n <= 28; n += 2 { |
| 250 |
size := 1 << n |
| 251 |
buf := make([]byte, size) |
| 252 |
for i := range buf { |
| 253 |
buf[i] = byte(uint64(i+int(n)+1) * 2654435761) |
| 254 |
} |
| 255 |
seed := rng.Uint64() | 1 |
| 256 |
b.Run(fmt.Sprint(size), func(b *testing.B) { |
| 257 |
var bn *testing.B |
| 258 |
check := func() { |
| 259 |
bn.Run("plain", func(b *testing.B) { |
| 260 |
h := New128() |
| 261 |
b.ReportAllocs() |
| 262 |
b.ResetTimer() |
| 263 |
b.SetBytes(int64(size)) |
| 264 |
for i := 0; i < b.N; i++ { |
| 265 |
h.Reset() |
| 266 |
h.Write(buf[:size]) |
| 267 |
_ = h.Sum128() |
| 268 |
} |
| 269 |
}) |
| 270 |
bn.Run("seed", func(b *testing.B) { |
| 271 |
h := NewSeed128(seed) |
| 272 |
b.ReportAllocs() |
| 273 |
b.ResetTimer() |
| 274 |
b.SetBytes(int64(size)) |
| 275 |
for i := 0; i < b.N; i++ { |
| 276 |
h.Reset() |
| 277 |
h.Write(buf[:size]) |
| 278 |
_ = h.Sum128() |
| 279 |
} |
| 280 |
}) |
| 281 |
} |
| 282 |
|
| 283 |
b.Run("go", func(b *testing.B) { |
| 284 |
bn = b |
| 285 |
withGeneric(check) |
| 286 |
}) |
| 287 |
if hasAVX512 { |
| 288 |
b.Run("avx512", func(b *testing.B) { |
| 289 |
bn = b |
| 290 |
withAVX512(check) |
| 291 |
}) |
| 292 |
} |
| 293 |
if hasAVX2 { |
| 294 |
b.Run("avx2", func(b *testing.B) { |
| 295 |
bn = b |
| 296 |
withAVX2(check) |
| 297 |
}) |
| 298 |
} |
| 299 |
if hasSSE2 { |
| 300 |
b.Run("sse2", func(b *testing.B) { |
| 301 |
bn = b |
| 302 |
withSSE2(check) |
| 303 |
}) |
| 304 |
} |
| 305 |
}) |
| 306 |
} |
| 307 |
} |