summaryrefslogtreecommitdiff
path: root/test/hash_table_test.c
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2022-04-13 02:06:11 +0200
committerJohnny Richard <johnny@johnnyrichard.com>2022-04-13 02:32:16 +0200
commit729aac88a58b6589c0d1aeb82bd729b886555771 (patch)
tree4d633aff60090d129b755ea9fc5aafc0eaad09d1 /test/hash_table_test.c
parenta1148b9c8102ac8ffb9f8ebcd270e19bd72cb942 (diff)
hash_table: Implement hash_table data struct
Diffstat (limited to 'test/hash_table_test.c')
-rw-r--r--test/hash_table_test.c105
1 files changed, 102 insertions, 3 deletions
diff --git a/test/hash_table_test.c b/test/hash_table_test.c
index 075c9d2..e2f7027 100644
--- a/test/hash_table_test.c
+++ b/test/hash_table_test.c
@@ -21,24 +21,123 @@
#include <stdlib.h>
-static MunitResult
-test_create_new(const MunitParameter params[],
+static MunitResult
+test_create_new(const MunitParameter params[],
void *user_data_or_fixture)
{
hash_table_t* table = hash_table_new();
assert_not_null(table);
+ assert_int(table->capacity, >, 0);
+ assert_int(table->length, ==, 0);
+ hash_table_destroy(table);
+ return MUNIT_OK;
+}
+
+static MunitResult
+test_insert_and_get(const MunitParameter params[],
+ void *user_data_or_fixture)
+{
+ hash_table_t* table = hash_table_new();
+
+ hash_table_insert(table, "key1", "value1");
+ hash_table_insert(table, "key2", "value2");
+
+ char* value = hash_table_get(table, "key1");
+ assert_string_equal("value1", value);
+
+ value = hash_table_get(table, "key2");
+ assert_string_equal("value2", value);
+
+ value = hash_table_get(table, "invalid_key");
+ assert_null(value);
+
+ hash_table_destroy(table);
+ return MUNIT_OK;
+}
+
+static MunitResult
+test_remove(const MunitParameter params[],
+ void *user_data_or_fixture)
+{
+ hash_table_t* table = hash_table_new();
+
+ hash_table_insert(table, "key", "value");
+
+ char* value = hash_table_get(table, "key");
+ assert_string_equal("value", value);
+
+ bool removed = hash_table_remove(table, "key");
+
+ assert_int(hash_table_length(table), ==, 0);
+ assert_null(hash_table_get(table, "key"));
+ assert_true(removed);
+
+ hash_table_destroy(table);
+ return MUNIT_OK;
+}
+
+static MunitResult
+test_length(const MunitParameter params[],
+ void *user_data_or_fixture)
+{
+ hash_table_t* table = hash_table_new();
+
+ hash_table_insert(table, "key1", "value1");
+ hash_table_insert(table, "key2", "value2");
+
+ assert_int(2, ==, hash_table_length(table));
+
+ hash_table_destroy(table);
+ return MUNIT_OK;
+}
+
+static MunitResult
+test_iterator(const MunitParameter params[],
+ void *user_data_or_fixture)
+{
+ hash_table_t* table = hash_table_new();
+
+ hash_table_insert(table, "key1", "value1");
+ hash_table_insert(table, "key2", "value2");
+
+ hash_table_iterator_t it = hash_table_get_iterator(table);
+
+ size_t count;
+ for (count = 0; hash_table_iterator_next(&it); ++count) {
+ if (strcmp(it.key, "key1") != 0 &&
+ strcmp(it.key, "key2") != 0) {
+ fprintf(stderr, "key not found: %s\n", it.key);
+ hash_table_destroy(table);
+ return MUNIT_FAIL;
+ }
+
+ if (strcmp(it.value, "value1") != 0 &&
+ strcmp(it.value, "value2") != 0) {
+ fprintf(stderr, "value not found: %s\n", it.value);
+ hash_table_destroy(table);
+ return MUNIT_FAIL;
+ }
+ }
+
+ assert_int(hash_table_length(table), ==, count);
+
+ hash_table_destroy(table);
return MUNIT_OK;
}
static MunitTest tests[] = {
{ "/test_create_new", test_create_new, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/test_insert_and_get", test_insert_and_get, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/test_remove", test_remove, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/test_length", test_length, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/test_iterator", test_iterator, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
static const MunitSuite suite = {
- "/hash_table", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
+ "/hash_table", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
};
int