fix(hash_map): fixed leak on value update
This commit is contained in:
parent
3cf1960a00
commit
2ebf56dde7
4 changed files with 251 additions and 13 deletions
217
tests/unit/utils/hash_map.c
Normal file
217
tests/unit/utils/hash_map.c
Normal file
|
|
@ -0,0 +1,217 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "../../../src/utils/hash_map/hash_map.h"
|
||||
|
||||
#include <criterion/criterion.h>
|
||||
#include <criterion/new/assert.h>
|
||||
#include <criterion/redirect.h>
|
||||
#include <stdio.h>
|
||||
|
||||
TestSuite(utils_hash_map);
|
||||
|
||||
Test(utils_hash_map, init_free)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, insert_basic)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
bool updated = false;
|
||||
bool res = hash_map_insert(map, strdup("key1"), strdup("value1"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, false);
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, insert_multiple)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
bool updated = false;
|
||||
bool res = hash_map_insert(map, strdup("key1"), strdup("value1"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, false);
|
||||
res = hash_map_insert(map, strdup("key2"), strdup("value2"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, false);
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, insert_update)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
bool updated = false;
|
||||
bool res = hash_map_insert(map, strdup("key1"), strdup("value1"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, false);
|
||||
res = hash_map_insert(map, "key1", strdup("value2"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, true);
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, insert_update_multiple)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
bool updated = false;
|
||||
bool res = hash_map_insert(map, strdup("key1"), strdup("value1"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, false);
|
||||
res = hash_map_insert(map, strdup("key2"), strdup("value2"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, false);
|
||||
res = hash_map_insert(map, "key1", strdup("value2"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, true);
|
||||
res = hash_map_insert(map, "key1", strdup("value3"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, true);
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, get_basic)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
bool updated = false;
|
||||
bool res = hash_map_insert(map, strdup("key1"), strdup("value1"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, false);
|
||||
|
||||
char *value = (char *)hash_map_get(map, "key1");
|
||||
cr_expect_str_eq(value, "value1");
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, get_after_update)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
bool updated = false;
|
||||
bool res = hash_map_insert(map, strdup("key1"), strdup("value1"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, false);
|
||||
|
||||
res = hash_map_insert(map, "key1", strdup("value2"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, true);
|
||||
|
||||
char *value = (char *)hash_map_get(map, "key1");
|
||||
cr_expect_str_eq(value, "value2");
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, get_unknown_key)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
char *value = (char *)hash_map_get(map, "unknown_key");
|
||||
cr_expect_null(value);
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, delete_key)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
bool updated = false;
|
||||
bool res = hash_map_insert(map, strdup("key1"), strdup("value1"), &updated);
|
||||
cr_expect_eq(res, true);
|
||||
cr_expect_eq(updated, false);
|
||||
|
||||
res = hash_map_remove(map, "key1");
|
||||
cr_expect_eq(res, true);
|
||||
|
||||
char *value = (char *)hash_map_get(map, "key1");
|
||||
cr_expect_null(value);
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, delete_unknown_key)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
bool res = hash_map_remove(map, "unknown_key");
|
||||
cr_expect_eq(res, false);
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, free_nonnull_map)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
bool res = hash_map_insert(map, strdup("key1"), strdup("value1"), NULL);
|
||||
cr_expect_eq(res, true);
|
||||
res = hash_map_insert(map, strdup("key2"), strdup("value2"), NULL);
|
||||
cr_expect_eq(res, true);
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
|
||||
Test(utils_hash_map, free_null_map)
|
||||
{
|
||||
hash_map_free(NULL);
|
||||
}
|
||||
|
||||
static size_t count = 0;
|
||||
void foreach_fn(const char *key, const void *value)
|
||||
{
|
||||
printf("Key: %s, Value: %s\n", key, (const char *)value);
|
||||
count++;
|
||||
}
|
||||
|
||||
Test(utils_hash_map, foreach, .init = cr_redirect_stdout)
|
||||
{
|
||||
struct hash_map *map = hash_map_init(10);
|
||||
cr_expect_not_null(map);
|
||||
cr_expect_eq(map->size, 10);
|
||||
|
||||
bool res = hash_map_insert(map, strdup("key1"), strdup("value1"), NULL);
|
||||
cr_expect_eq(res, true);
|
||||
res = hash_map_insert(map, strdup("key2"), strdup("value2"), NULL);
|
||||
cr_expect_eq(res, true);
|
||||
|
||||
count = 0;
|
||||
hash_map_foreach(map, foreach_fn);
|
||||
fflush(stdout);
|
||||
cr_expect_eq(count, 2);
|
||||
cr_expect_stdout_eq_str(
|
||||
"Key: key2, Value: value2\nKey: key1, Value: value1\n");
|
||||
|
||||
hash_map_free(&map);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue