xxhash: Add XXH3_128 + test vectors.

This commit is contained in:
Jeroen van Rijn
2021-09-11 15:27:51 +02:00
parent 1f1d8ef884
commit eaefbc43cb
5 changed files with 2568 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
package test_core_image
package test_core_hash
import "core:hash/xxhash"
import "core:time"
@@ -31,6 +31,7 @@ when ODIN_TEST {
main :: proc() {
t := testing.T{}
test_benchmark_runner(&t)
test_xxhash_vectors(&t)
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
}
@@ -52,7 +53,7 @@ teardown_xxhash :: proc(options: ^time.Benchmark_Options, allocator := context.a
return nil
}
benchmark_xxhash32 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
benchmark_xxh32 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
buf := options.input
h: u32
@@ -65,7 +66,7 @@ benchmark_xxhash32 :: proc(options: ^time.Benchmark_Options, allocator := contex
return nil
}
benchmark_xxhash64 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
benchmark_xxh64 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
buf := options.input
h: u64
@@ -78,6 +79,19 @@ benchmark_xxhash64 :: proc(options: ^time.Benchmark_Options, allocator := contex
return nil
}
benchmark_xxh3_128 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
buf := options.input
h: u128
for _ in 0..=options.rounds {
h = xxhash.XXH3_128bits(buf)
}
options.count = options.rounds
options.processed = options.rounds * options.bytes
options.hash = h
return nil
}
benchmark_print :: proc(name: string, options: ^time.Benchmark_Options) {
fmt.printf("\t[%v] %v rounds, %v bytes processed in %v ns\n\t\t%5.3f rounds/s, %5.3f MiB/s\n",
name,
@@ -93,12 +107,12 @@ benchmark_print :: proc(name: string, options: ^time.Benchmark_Options) {
test_benchmark_runner :: proc(t: ^testing.T) {
fmt.println("Starting benchmarks:")
name := "xxhash32 100 zero bytes"
name := "XXH32 100 zero bytes"
options := &time.Benchmark_Options{
rounds = 1_000,
bytes = 100,
setup = setup_xxhash,
bench = benchmark_xxhash32,
bench = benchmark_xxh32,
teardown = teardown_xxhash,
}
@@ -107,25 +121,65 @@ test_benchmark_runner :: proc(t: ^testing.T) {
expect(t, options.hash == 0x85f6413c, name)
benchmark_print(name, options)
name = "xxhash32 1 MiB zero bytes"
name = "XXH32 1 MiB zero bytes"
options.bytes = 1_048_576
err = time.benchmark(options, context.allocator)
expect(t, err == nil, name)
expect(t, options.hash == 0x9430f97f, name)
benchmark_print(name, options)
name = "xxhash64 100 zero bytes"
name = "XXH64 100 zero bytes"
options.bytes = 100
options.bench = benchmark_xxhash64
options.bench = benchmark_xxh64
err = time.benchmark(options, context.allocator)
expect(t, err == nil, name)
expect(t, options.hash == 0x17bb1103c92c502f, name)
benchmark_print(name, options)
name = "xxhash64 1 MiB zero bytes"
name = "XXH64 1 MiB zero bytes"
options.bytes = 1_048_576
err = time.benchmark(options, context.allocator)
expect(t, err == nil, name)
expect(t, options.hash == 0x87d2a1b6e1163ef1, name)
benchmark_print(name, options)
name = "XXH3_128 100 zero bytes"
options.bytes = 100
options.bench = benchmark_xxh3_128
err = time.benchmark(options, context.allocator)
expect(t, err == nil, name)
expect(t, options.hash == 0x6ba30a4e9dffe1ff801fedc74ccd608c, name)
benchmark_print(name, options)
name = "XXH3_128 1 MiB zero bytes"
options.bytes = 1_048_576
err = time.benchmark(options, context.allocator)
expect(t, err == nil, name)
expect(t, options.hash == 0xb6ef17a3448492b6918780b90550bf34, name)
benchmark_print(name, options)
}
@test
test_xxhash_vectors :: proc(t: ^testing.T) {
fmt.println("Verifying against XXHASH_TEST_VECTOR_ZERO:")
buf := make([]u8, 256)
defer delete(buf)
for v, i in XXHASH_TEST_VECTOR_ZERO[:] {
b := buf[:i]
xxh32 := xxhash.XXH32(b)
xxh64 := xxhash.XXH64(b)
xxh3_128 := xxhash.XXH3_128bits(b)
xxh32_error := fmt.tprintf("[ XXH32(%03d] Expected: %08x. Got: %08x.", i, v.xxh_32, xxh32)
xxh64_error := fmt.tprintf("[ XXH64(%03d] Expected: %16x. Got: %16x.", i, v.xxh_64, xxh64)
xxh3_128_error := fmt.tprintf("[XXH3_128(%03d] Expected: %32x. Got: %32x.", i, v.xxh3_128, xxh3_128)
expect(t, xxh32 == v.xxh_32, xxh32_error)
expect(t, xxh64 == v.xxh_64, xxh64_error)
expect(t, xxh3_128 == v.xxh3_128, xxh3_128_error)
}
}

File diff suppressed because it is too large Load Diff