some test for hash function
This commit is contained in:
+27
-8
@@ -1,17 +1,36 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include "cig.h"
|
#include "cig.h"
|
||||||
|
|
||||||
#define KEYS_LEN 100
|
typedef long long T;
|
||||||
|
#define CAPACITY 50
|
||||||
|
|
||||||
Test(map_hash_key, foo) {
|
Test(map_hash_key, foo) {
|
||||||
map_header_t header = (map_header_t){0};
|
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++) {
|
bool hashed[CAPACITY] = {0};
|
||||||
header.key_size = sizeof(i);
|
|
||||||
key_hashes[i] = map_hash_key(&header, (const uint8_t*)(&i));
|
for (T i = 0; i < CAPACITY; i++) {
|
||||||
// TODO
|
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.
|
||||||
|
|||||||
Reference in New Issue
Block a user