mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 22:58:46 +00:00
xxhash: Add tests for streaming input.
This commit is contained in:
@@ -22,7 +22,7 @@ XXH3_128_reset :: proc(state: ^XXH3_state) -> (err: Error) {
|
|||||||
if state == nil {
|
if state == nil {
|
||||||
return .Error
|
return .Error
|
||||||
}
|
}
|
||||||
XXH3_reset_internal(state, 0, XXH3_kSecret[:])
|
XXH3_reset_internal(state, 0, XXH3_kSecret[:], len(XXH3_kSecret))
|
||||||
return .None
|
return .None
|
||||||
}
|
}
|
||||||
XXH3_64_reset :: XXH3_128_reset
|
XXH3_64_reset :: XXH3_128_reset
|
||||||
@@ -34,7 +34,7 @@ XXH3_128_reset_with_secret :: proc(state: ^XXH3_state, secret: []u8) -> (err: Er
|
|||||||
if secret == nil || len(secret) < XXH3_SECRET_SIZE_MIN {
|
if secret == nil || len(secret) < XXH3_SECRET_SIZE_MIN {
|
||||||
return .Error
|
return .Error
|
||||||
}
|
}
|
||||||
XXH3_reset_internal(state, 0, secret)
|
XXH3_reset_internal(state, 0, secret, len(secret))
|
||||||
return .None
|
return .None
|
||||||
}
|
}
|
||||||
XXH3_64_reset_with_secret :: XXH3_128_reset_with_secret
|
XXH3_64_reset_with_secret :: XXH3_128_reset_with_secret
|
||||||
@@ -46,15 +46,18 @@ XXH3_128_reset_with_seed :: proc(state: ^XXH3_state, seed: XXH64_hash) -> (err:
|
|||||||
if seed != state.seed {
|
if seed != state.seed {
|
||||||
XXH3_init_custom_secret(state.custom_secret[:], seed)
|
XXH3_init_custom_secret(state.custom_secret[:], seed)
|
||||||
}
|
}
|
||||||
|
XXH3_reset_internal(state, seed, nil, XXH_SECRET_DEFAULT_SIZE)
|
||||||
XXH3_reset_internal(state, seed, nil)
|
|
||||||
return .None
|
return .None
|
||||||
}
|
}
|
||||||
XXH3_64_reset_with_seed :: XXH3_128_reset_with_seed
|
XXH3_64_reset_with_seed :: XXH3_128_reset_with_seed
|
||||||
|
|
||||||
XXH3_128_update :: proc(state: ^XXH3_state, input: []u8) -> (err: Error) {
|
XXH3_128_update :: proc(state: ^XXH3_state, input: []u8) -> (err: Error) {
|
||||||
|
if len(input) < XXH3_MIDSIZE_MAX {
|
||||||
|
return .Error
|
||||||
|
}
|
||||||
return XXH3_update(state, input, XXH3_accumulate_512, XXH3_scramble_accumulator)
|
return XXH3_update(state, input, XXH3_accumulate_512, XXH3_scramble_accumulator)
|
||||||
}
|
}
|
||||||
|
XXH3_64_update :: XXH3_128_update
|
||||||
|
|
||||||
XXH3_128_digest :: proc(state: ^XXH3_state) -> (hash: XXH3_128_hash) {
|
XXH3_128_digest :: proc(state: ^XXH3_state) -> (hash: XXH3_128_hash) {
|
||||||
secret := state.custom_secret[:] if len(state.external_secret) == 0 else state.external_secret[:]
|
secret := state.custom_secret[:] if len(state.external_secret) == 0 else state.external_secret[:]
|
||||||
@@ -137,7 +140,7 @@ XXH3_copy_state :: proc(dest, src: ^XXH3_state) {
|
|||||||
mem_copy(dest, src, size_of(XXH3_state))
|
mem_copy(dest, src, size_of(XXH3_state))
|
||||||
}
|
}
|
||||||
|
|
||||||
XXH3_reset_internal :: proc(state: ^XXH3_state, seed: XXH64_hash, secret: []u8) {
|
XXH3_reset_internal :: proc(state: ^XXH3_state, seed: XXH64_hash, secret: []u8, secret_size: uint) {
|
||||||
assert(state != nil)
|
assert(state != nil)
|
||||||
|
|
||||||
init_start := offset_of(XXH3_state, buffered_size)
|
init_start := offset_of(XXH3_state, buffered_size)
|
||||||
@@ -162,10 +165,9 @@ XXH3_reset_internal :: proc(state: ^XXH3_state, seed: XXH64_hash, secret: []u8)
|
|||||||
state.seed = seed
|
state.seed = seed
|
||||||
state.external_secret = secret
|
state.external_secret = secret
|
||||||
|
|
||||||
secret_length := uint(len(secret))
|
assert(secret_size >= XXH3_SECRET_SIZE_MIN)
|
||||||
assert(secret_length > XXH3_SECRET_SIZE_MIN)
|
|
||||||
|
|
||||||
state.secret_limit = secret_length - XXH_STRIPE_LEN
|
state.secret_limit = secret_size - XXH_STRIPE_LEN
|
||||||
state.stripes_per_block = state.secret_limit / XXH_SECRET_CONSUME_RATE
|
state.stripes_per_block = state.secret_limit / XXH_SECRET_CONSUME_RATE
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,6 +213,8 @@ XXH3_update :: #force_inline proc(
|
|||||||
length := len(input)
|
length := len(input)
|
||||||
secret := state.custom_secret[:] if len(state.external_secret) == 0 else state.external_secret[:]
|
secret := state.custom_secret[:] if len(state.external_secret) == 0 else state.external_secret[:]
|
||||||
|
|
||||||
|
assert(len(input) > 0)
|
||||||
|
|
||||||
state.total_length += u64(length)
|
state.total_length += u64(length)
|
||||||
assert(state.buffered_size <= XXH3_INTERNAL_BUFFER_SIZE)
|
assert(state.buffered_size <= XXH3_INTERNAL_BUFFER_SIZE)
|
||||||
|
|
||||||
@@ -265,10 +269,6 @@ XXH3_update :: #force_inline proc(
|
|||||||
return .None
|
return .None
|
||||||
}
|
}
|
||||||
|
|
||||||
XXH3_64_update :: proc(state: ^XXH3_state, input: []u8) -> (err: Error) {
|
|
||||||
return XXH3_update(state, input, XXH3_accumulate_512, XXH3_scramble_accumulator)
|
|
||||||
}
|
|
||||||
|
|
||||||
XXH3_digest_long :: #force_inline proc(acc: []u64, state: ^XXH3_state, secret: []u8) {
|
XXH3_digest_long :: #force_inline proc(acc: []u64, state: ^XXH3_state, secret: []u8) {
|
||||||
/*
|
/*
|
||||||
Digest on a local copy. This way, the state remains unaltered, and it can
|
Digest on a local copy. This way, the state remains unaltered, and it can
|
||||||
|
|||||||
@@ -202,15 +202,39 @@ test_xxhash_vectors :: proc(t: ^testing.T) {
|
|||||||
|
|
||||||
xxh32 := xxhash.XXH32(b, u32(seed))
|
xxh32 := xxhash.XXH32(b, u32(seed))
|
||||||
xxh64 := xxhash.XXH64(b, seed)
|
xxh64 := xxhash.XXH64(b, seed)
|
||||||
|
xxh3_64 := xxhash.XXH3_64(b, seed)
|
||||||
xxh3_128 := xxhash.XXH3_128(b, seed)
|
xxh3_128 := xxhash.XXH3_128(b, seed)
|
||||||
|
|
||||||
xxh32_error := fmt.tprintf("[ XXH32(%03d)] Expected: %08x. Got: %08x.", i, v.xxh_32, xxh32)
|
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)
|
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)
|
|
||||||
|
xxh3_64_error := fmt.tprintf("[XXH3_64(%03d) ] Expected: %16x. Got: %16x.", i, v.xxh3_64, xxh3_64)
|
||||||
|
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, xxh32 == v.xxh_32, xxh32_error)
|
||||||
expect(t, xxh64 == v.xxh_64, xxh64_error)
|
expect(t, xxh64 == v.xxh_64, xxh64_error)
|
||||||
|
expect(t, xxh3_64 == v.xxh3_64, xxh3_64_error)
|
||||||
expect(t, xxh3_128 == v.xxh3_128, xxh3_128_error)
|
expect(t, xxh3_128 == v.xxh3_128, xxh3_128_error)
|
||||||
|
|
||||||
|
if len(b) > xxhash.XXH3_MIDSIZE_MAX {
|
||||||
|
fmt.printf("XXH3 - size: %v\n", len(b))
|
||||||
|
|
||||||
|
xxh3_state, _ := xxhash.XXH3_create_state()
|
||||||
|
xxhash.XXH3_64_reset_with_seed(xxh3_state, seed)
|
||||||
|
xxhash.XXH3_64_update(xxh3_state, b)
|
||||||
|
xxh3_64_streamed := xxhash.XXH3_64_digest(xxh3_state)
|
||||||
|
xxhash.XXH3_destroy_state(xxh3_state)
|
||||||
|
xxh3_64s_error := fmt.tprintf("[XXH3_64s(%03d) ] Expected: %16x. Got: %16x.", i, v.xxh3_64, xxh3_64_streamed)
|
||||||
|
expect(t, xxh3_64_streamed == v.xxh3_64, xxh3_64s_error)
|
||||||
|
|
||||||
|
xxh3_state2, _ := xxhash.XXH3_create_state()
|
||||||
|
xxhash.XXH3_128_reset_with_seed(xxh3_state2, seed)
|
||||||
|
xxhash.XXH3_128_update(xxh3_state2, b)
|
||||||
|
xxh3_128_streamed := xxhash.XXH3_128_digest(xxh3_state2)
|
||||||
|
xxhash.XXH3_destroy_state(xxh3_state2)
|
||||||
|
xxh3_128s_error := fmt.tprintf("[XXH3_128s(%03d) ] Expected: %32x. Got: %32x.", i, v.xxh3_128, xxh3_128_streamed)
|
||||||
|
expect(t, xxh3_128_streamed == v.xxh3_128, xxh3_128s_error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user