some more tests

This commit is contained in:
2026-05-25 21:11:46 +02:00
parent b2ba81c40a
commit 8be1c2254b
3 changed files with 23 additions and 5 deletions
+2 -1
View File
@@ -235,7 +235,7 @@ void arr_qsort(void *this, dyn_array_cmp_fn cmp_fn);
// maps //////////////////////////////////////////////////////////////////// // maps ////////////////////////////////////////////////////////////////////
typedef unsigned int (*key_hash_func_t)(const void *key); typedef uint32_t (*key_hash_func_t)(const void *key);
typedef bool (*key_equals_func_t)(const void *key1, const void *key2); typedef bool (*key_equals_func_t)(const void *key1, const void *key2);
typedef struct map_header { typedef struct map_header {
@@ -323,6 +323,7 @@ unsigned int map_place(void *this, index_pair_t idx_pair);
// Exists to take a hash of a key and modulo it so it fits within the maps // Exists to take a hash of a key and modulo it so it fits within the maps
// capacity. // capacity.
uint32_t map_mod_index(const map_header_t *header, uint32_t i); uint32_t map_mod_index(const map_header_t *header, uint32_t i);
uint32_t map_hash_key(const map_header_t *header, const uint8_t *key);
// TODO: NOT DONE!!! FIRST try to get the existing in // TODO: NOT DONE!!! FIRST try to get the existing in
#define set_add(THIS, VALUE) do { \ #define set_add(THIS, VALUE) do { \
+4 -4
View File
@@ -133,11 +133,11 @@ unsigned int map_place(void *this, index_pair_t idx) {
return header->mapping_arr[idx.new_i_into_mapping]; return header->mapping_arr[idx.new_i_into_mapping];
} }
static uint32_t __map_hash_key_into_mapping_i(const map_header_t *header, const uint8_t *key) { uint32_t map_hash_key(const map_header_t *header, const uint8_t *key) {
if (header->hash != NULL) { if (header->hash != NULL) {
return header->hash(key) % header->mapping_capacity; return map_mod_index(header, header->hash(key));
} else { } else {
return fnv_1a(key, header->key_size) % header->mapping_capacity; return map_mod_index(header, fnv_1a(key, header->key_size));
} }
} }
@@ -166,7 +166,7 @@ index_pair_t map_pair_hash(void *this, const uint8_t *pair) {
const uint8_t *new_key = __cig_key_ptr_from_pair_ptr(header, pair); const uint8_t *new_key = __cig_key_ptr_from_pair_ptr(header, pair);
// find the initial index based on hash // find the initial index based on hash
uint32_t i_into_mapping = __map_hash_key_into_mapping_i(header, new_key); uint32_t i_into_mapping = map_hash_key(header, new_key);
int existing_i_into_bytes = header->mapping_arr[i_into_mapping]; int existing_i_into_bytes = header->mapping_arr[i_into_mapping];
if (existing_i_into_bytes == FLAG_FREE) { if (existing_i_into_bytes == FLAG_FREE) {
+17
View File
@@ -0,0 +1,17 @@
#include <criterion/criterion.h>
#include "cig.h"
#define KEYS_LEN 100
Test(map_hash_key, foo) {
map_header_t header = (map_header_t){0};
long long key_hashes[KEYS_LEN];
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
}
map_hash_key(&header)
}