diff --git a/test_map_hash_key.c b/test_map_hash_key.c index 9ffcf57..c90651c 100644 --- a/test_map_hash_key.c +++ b/test_map_hash_key.c @@ -1,17 +1,36 @@ +#include + #include #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.