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/array_tests.c | 12 ++++++------ tests/ds/hashmap_tests.c | 46 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 15 deletions(-) (limited to 'tests/ds') diff --git a/tests/ds/array_tests.c b/tests/ds/array_tests.c index b292407..c5d7c4d 100644 --- a/tests/ds/array_tests.c +++ b/tests/ds/array_tests.c @@ -4,7 +4,7 @@ #include -Test cap_test() { +TEST(cap_test) { float *arr = NULL; test_assert(arr_cap(arr) == 0, "uninitialized array has capacity different than 0"); @@ -24,7 +24,7 @@ Test cap_test() { arr_free(arr); } -Test len_test() { +TEST(len_test) { char *arr = NULL; test_assert(arr_len(arr) == 0, "uninitialized array has length different than 0"); @@ -39,7 +39,7 @@ Test len_test() { arr_free(arr); } -Test adding_items() { +TEST(adding_items) { int *arr = NULL; // {3} @@ -84,7 +84,7 @@ Test adding_items() { arr_free(arr); } -Test getting_items() { +TEST(getting_items) { struct test { int f1; char f2; @@ -121,7 +121,7 @@ Test getting_items() { arr_free(arr); } -Test deleting_items() { +TEST(deleting_items) { int *arr = NULL; int items[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; arr_append_n(arr, items, 9); @@ -152,7 +152,7 @@ Test deleting_items() { arr_free(arr); } -Test arr_concat_test() { +TEST(arr_concat_test) { unsigned long *arr1 = NULL; unsigned long *arr2 = NULL; 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