Files
wizapp-stdlib/tests/hasher/test_hasher.c
T
Abdelrahman Said c215486442
Release / release (push) Successful in 3s
Restructure hasher implementation
2026-09-23 00:02:37 +01:00

83 lines
2.8 KiB
C

// vim:fileencoding=utf-8:foldmethod=marker
#include "test_hasher.h"
#include "MurmurHash3.h"
#include "siphash.h"
#include "wapp.h"
WpTestFuncResult test_murmur3(void) {
b8 result = true;
WpStr8 str = wpStr8Lit("Hello world");
WpI32Array arr = wpArray(i32, 1, 2, 3, 4, 5);
u64 num = 287324;
WpHasher hasher = wpX64Mur3HasherWithSeed(873923);
WpU8Stream str_bytes = wpStream(u8, str.buf, str.size);
WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr));
WpU8Stream num_bytes = wpStream(u8, (u8 *)&num, sizeof(u64));
u64 hash[2] = {0};
// Sring hash
WpU128 mur_hash = hasher.func(&str_bytes, hasher.ctx);
MurmurHash3_x64_128(str_bytes.data, str_bytes.count * str_bytes.item_size, hasher.ctx.murmur3, (void *)hash);
result = result && mur_hash.value[0] == hash[0] && mur_hash.value[1] == hash[1];
// Array hash
mur_hash = hasher.func(&arr_bytes, hasher.ctx);
MurmurHash3_x64_128(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, hasher.ctx.murmur3, (void *)hash);
result = result && mur_hash.value[0] == hash[0] && mur_hash.value[1] == hash[1];
// Number hash
mur_hash = hasher.func(&num_bytes, hasher.ctx);
MurmurHash3_x64_128(num_bytes.data, num_bytes.count * num_bytes.item_size, hasher.ctx.murmur3, (void *)hash);
result = result && mur_hash.value[0] == hash[0] && mur_hash.value[1] == hash[1];
return wpTesterResult(result);
}
WpTestFuncResult test_siphash_128(void) {
b8 result = true;
WpStr8 str = wpStr8Lit("Hello world");
WpI32Array arr = wpArray(i32, 1, 2, 3, 4, 5);
u64 num = 287324;
WpU128 key = { .value = { 238742, 671734 } };
WpHasher hasher = wpSip24HasherWithKey(key);
WpU8Stream str_bytes = wpStream(u8, str.buf, str.size);
WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr));
WpU8Stream num_bytes = wpStream(u8, (u8 *)&num, sizeof(u64));
u64 hash[2] = {0};
// Sring hash
WpU128 sip_hash = hasher.func(&str_bytes, hasher.ctx);
siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&hasher.ctx.siphash.key,
(u8 *)hash, sizeof(u64) * 2);
result = result && sip_hash.value[0] == hash[0] && sip_hash.value[1] == hash[1];
// Array hash
sip_hash = hasher.func(&arr_bytes, hasher.ctx);
siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&hasher.ctx.siphash.key,
(u8 *)hash, sizeof(u64) * 2);
result = result && sip_hash.value[0] == hash[0] && sip_hash.value[1] == hash[1];
// Number hash
sip_hash = hasher.func(&num_bytes, hasher.ctx);
siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&hasher.ctx.siphash.key,
(u8 *)hash, sizeof(u64) * 2);
result = result && sip_hash.value[0] == hash[0] && sip_hash.value[1] == hash[1];
return wpTesterResult(result);
}