some test for hash function

This commit is contained in:
2026-05-27 22:37:51 +02:00
parent ed9e69bc06
commit 459af00c16
+26 -7
View File
@@ -1,17 +1,36 @@
#include <stdbool.h>
#include <criterion/criterion.h>
#include "cig.h"
#define KEYS_LEN 100
typedef long long T;
#define CAPACITY 50
Test(map_hash_key, foo) {
map_header_t header = (map_header_t){0};
long long key_hashes[KEYS_LEN];
header.key_size = sizeof(T);
header.mapping_capacity = CAPACITY;
for (long long i = 0; i < KEYS_LEN; i++) {
header.key_size = sizeof(i);
key_hashes[i] = map_hash_key(&header, (const uint8_t*)(&i));
// TODO
bool hashed[CAPACITY] = {0};
for (T i = 0; i < CAPACITY; i++) {
uint32_t index = map_hash_key(&header, (const uint8_t*)(&i));
cr_assert(index < CAPACITY);
hashed[index] = true;
}
map_hash_key(&header)
int n_unique = 0;
for (int i = 0; i < CAPACITY; i++) {
if (hashed[i]) {
n_unique++;
}
}
cr_assert_gt(n_unique, 1);
cr_assert_lt(n_unique, CAPACITY);
printf("Expect high number below 50. Got %d unique hashes.\nThis means %d collisions\n", n_unique, (int)(CAPACITY - n_unique));
}
// TODO: test hash with custom hash function.