From 419bd1326e310babbc77cd4974263c4b90eb1388 Mon Sep 17 00:00:00 2001 From: Mosder Date: Sat, 12 Sep 2026 19:05:36 +0200 Subject: feat: added deleting from hashmap and changed type of tests to be nicer --- tests/ds/hashmap_tests.c | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) (limited to 'tests/ds/hashmap_tests.c') diff --git a/tests/ds/hashmap_tests.c b/tests/ds/hashmap_tests.c index 288bb4e..844ffb2 100644 --- a/tests/ds/hashmap_tests.c +++ b/tests/ds/hashmap_tests.c @@ -8,7 +8,9 @@ #include "moslib/ds/array.h" #include "moslib/string.h" -Test put_get() { +#define INIT_CAP 16 + +TEST(put_get) { HM_DEF(E, float, int); E *hm = NULL; @@ -21,22 +23,21 @@ Test put_get() { test_assert(hm_get(hm, 0.0f) == 2137, "hm_put didn't update value properly"); test_assert(hm_size(hm) == 2, "hm_size returned wrong entry count"); - // a lot of puts and gets to test expansion of hashmap - for (int i = 2; i < 100; i++) { + for (int i = 2; i < 2 * INIT_CAP + 2; i++) { hm_put(hm, (float)i, i); } int flag = 1; - for (int i = 2; i < 100; i++) { + for (int i = 2; i < 2 * INIT_CAP + 2; i++) { if (hm_get(hm, (float)i) != i) flag = 0; } test_assert(flag, "hm_put / hm_get failed when expansion was needed"); - test_assert(hm_size(hm) == 100, "hm_size returned wrong entry count"); + test_assert(hm_size(hm) == 2 * INIT_CAP + 2, "hm_size returned wrong entry count"); hm_free(hm); } -Test put_e_get_e() { +TEST(put_e_get_e) { HM_DEF(E, float, int); E *hm = NULL; @@ -54,7 +55,33 @@ Test put_e_get_e() { hm_free(hm); } -Test str_key() { +TEST(deleting) { + HM_DEF(E, long double, float); + E *hm = NULL; + + hm_put(hm, 1.0, 1.0); + hm_put(hm, 2.0, 2.0); + + int del = hm_del(hm, 1.0); + test_assert(hm_get_e(hm, 1.0) == NULL, "hm_del didn't delete entry it supposed to"); + test_assert(del == 1, "hm_del didn't return 1 when it deleted"); + test_assert(hm_size(hm) == 1, "hm_size after successful hm_del is incorrect"); + + del = hm_del(hm, 1.0); + test_assert(del == 0, "hm_del didn't return 0 when it didn't delete"); + test_assert(hm_get(hm, 2.0) == 2.0, "hm_del deleted wrong entry"); + test_assert(hm_size(hm) == 1, "hm_size after failed hm_del is incorrect"); + + for (int i = 3; i < 2 * INIT_CAP + 3; i++) { + hm_put(hm, (long double)i, (float)i); + } + test_assert(hm_get_e(hm, 1.0) == NULL, "expansion revived entry"); + test_assert(hm_size(hm) == 2 * INIT_CAP + 1, "hm_size after expansion is incorrect"); + + hm_free(hm); +} + +TEST(str_key) { HM_DEF(E, char *, int); E *hm = hm_new(.key = STR); @@ -72,7 +99,7 @@ Test str_key() { hm_free(hm); } -Test ss_key() { +TEST(ss_key) { HM_DEF(E, StringSlice, int); E *hm = hm_new(.key = SS); @@ -92,7 +119,7 @@ Test ss_key() { hm_free(hm); } -Test looping() { +TEST(looping) { HM_DEF(E, int, int); E *hm = NULL; @@ -127,6 +154,7 @@ void ds_hashmap_tests(Tester *tester) { TestGroup *group = add_test_group(tester, "moslib/ds/hashmap.h"); add_test(group, put_get); add_test(group, put_e_get_e); + add_test(group, deleting); add_test(group, str_key); add_test(group, ss_key); add_test(group, looping); -- cgit v1.2.3