30#define HMAP_TESTS(X) \
31 X("hashmap::empty", test_hashmap_empty), \
32 X("hashmap::deterministic_hash_fn", test_hashmap_make_sure_hash_is_deterministic), \
33 X("hashmap::insert_new_kv", test_hashmap_insert_kv), \
34 X("hashmap::many_insert_and_lookup", test_hashmap_many_insert_and_lookup), \
35 X("hashmap::collision_lookup", test_hashmap_collision_node_lookup), \
36 X("hashmap::max_depth_divergence", test_hashmap_max_depth_divergence), \
37 X("hashmap::update", test_hashmap_update)
39#define HMAP_TEST_LOG(...) DBG("HMAP_TEST", __VA_ARGS__)
67 int key_missing = 342;
69 hmap_key_t k = {.data = &key, .len =
sizeof(key)};
70 hmap_key_t k_missing = {.data = &key_missing, .len =
sizeof(key_missing)};
79 void *dptr_missing =
hmap_lookup(&h1, &k_missing, &default_v);
95 for (
int i = 0; i < 100; i++) {
96 uint32_t h1 =
hmap_hash(ctx, &x,
sizeof(x));
97 uint32_t h2 =
hmap_hash(ctx, &x,
sizeof(x));
108 constexpr int N = 4096;
110 int *keys =
ALLOCN(ctx,
int, N);
116 for (
int i = 0; i < N; ++i) {
118 ks[i].
data = &keys[i];
119 ks[i].
len =
sizeof(keys[i]);
122 vals[i].
bar = i * 10;
132 for (
int i = 0; i < N; ++i) {
143 for (
int i = 0; i < N; ++i) {
145 vals[i].
bar = i * 10 + i;
155 for (
int i = 0; i < N; ++i) {
165 int missing_key = 0x7fffff;
166 hmap_key_t kmiss = {.data = &missing_key, .len =
sizeof(missing_key)};
186 hmap_key_t ka = {.data = &a, .len =
sizeof(a)};
187 hmap_key_t kb = {.data = &b, .len =
sizeof(b)};
216 memcpy(&h, k->
data,
sizeof(h));
238 struct crafted_key ka_bytes = {.hash = 0x00000001U, .tag = 1};
239 struct crafted_key kb_bytes = {.hash = 0x00000001U | (1U << 30U), .tag = 2};
240 struct crafted_key kc_bytes = {.hash = 0x00000001U, .tag = 3};
242 hmap_key_t ka = {.data = &ka_bytes, .len =
sizeof(ka_bytes)};
243 hmap_key_t kb = {.data = &kb_bytes, .len =
sizeof(kb_bytes)};
244 hmap_key_t kc = {.data = &kc_bytes, .len =
sizeof(kc_bytes)};
276 hmap_key_t ka = {.data = &a, .len =
sizeof(a)};
277 hmap_key_t kb = {.data = &b, .len =
sizeof(b)};
#define TEST_ASSERT(cond)
#define RELEASE_CONTEXT(x)
#define SHUTDOWN_ENGINE(mm, engine)
#define MAKE_ENGINE(mm, engine)
#define MAKE_CONTEXT(engine, x)
#define ALLOCN(ctx, T, N)
hmap_t hmap_insert(const hmap_t *hmap, hmap_key_t *k, void *v)
Insert the given key k with the value v in the given hash hmap and return the new map.
hmap_hash_t hmap_hash(const srn_context_t *ctx, const void *data, size_t len)
This is a simple hack to mock the hash function during tests to force a high collision hashing functi...
hmap_t hmap_empty(const srn_context_t *ctx)
Create, initialize and return a new hashmap pinned to ctx.
void * hmap_lookup_ctl(const hmap_control_t *ctl, const hmap_t *hmap, const hmap_key_t *k, void *default_value)
Just like the hmap_lookup function but, it receives a hmap_control_t to customize the comparison and ...
hmap_t hmap_insert_ctl(const hmap_control_t *ctl, const hmap_t *hmap, hmap_key_t *k, void *v)
Just like the hmap_insert function but, it receives a hmap_control_t to customize the comparison and ...
void * hmap_lookup(const hmap_t *hmap, const hmap_key_t *k, void *default_value)
Lookup the given k in the given hmap and return the value if it's been found.
hmap_control_t hmap_default_control
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
HMAP_HASH_TYPE hmap_hash_t
static void test_hashmap_make_sure_hash_is_deterministic()
static void test_hashmap_empty()
static void test_hashmap_collision_node_lookup()
static void test_hashmap_many_insert_and_lookup()
static hmap_hash_t test_hash_from_key_bytes(const hmap_t *hmap, const hmap_key_t *k)
Hash hook that reads the hash straight from the key's first four bytes, so a test can craft hashes wi...
static void test_hashmap_max_depth_divergence()
Two keys whose hashes agree on bits 0-29 (every full 5 bit fragment) and differ only in bits 30-31 di...
void test_hashmap_insert_kv()
static void test_hashmap_update()
If we ever want to modify some of these behaviours for a new instance of hashmap, we should use this ...
hmap_hash_t(* hash)(const hmap_t *hmap, const hmap_key_t *k)
Note: For key equality we use the memcpy function.
void * data
len 0 -> data == nullptr
size_t len
Number of key/value pairs in the map.