29#define HMAP_TESTS(X) \
30 X("hashmap::empty", test_hashmap_empty), \
31 X("hashmap::deterministic_hash_fn", \
32 test_hashmap_make_sure_hash_is_deterministic), \
33 X("hashmap::insert_new_kv", test_hashmap_insert_kv), \
34 X("hashmap::many_insert_and_lookup", \
35 test_hashmap_many_insert_and_lookup), \
36 X("hashmap::collision_lookup", test_hashmap_collision_node_lookup), \
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(ctx, &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;
112 int *keys =
ALLOCN(ctx,
int, N);
118 for (
int i = 0; i < N; ++i) {
120 ks[i].
data = &keys[i];
121 ks[i].
len =
sizeof(keys[i]);
124 vals[i].
bar = i * 10;
134 for (
int i = 0; i < N; ++i) {
145 for (
int i = 0; i < N; ++i) {
147 vals[i].
bar = i * 10 + i;
157 for (
int i = 0; i < N; ++i) {
167 int missing_key = 0x7fffff;
168 hmap_key_t kmiss = {.data = &missing_key, .len =
sizeof(missing_key)};
171 void *p =
hmap_lookup(ctx, &h, &kmiss, &default_v);
188 hmap_key_t ka = {.data = &a, .len =
sizeof(a)};
189 hmap_key_t kb = {.data = &b, .len =
sizeof(b)};
222 hmap_key_t ka = {.data = &a, .len =
sizeof(a)};
223 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_empty(srn_context_t *ctx)
Create, initialize and return a new hashmap.
void * hmap_lookup(srn_context_t *ctx, 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_hash_t hmap_hash(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_insert(srn_context_t *ctx, 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.
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
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()
void test_hashmap_insert_kv()
static void test_hashmap_update()
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.