From 281f49d5e58ce3c4d64b95cf80619f6344de2b11 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 21 Dec 2016 14:37:21 -0800 Subject: [PATCH] Write hash table tests. --- src/main.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 17964ff..1c706f1 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include #include #include "vec.h" +#include "ht.h" int _test_vec_foreach_func(void* data, va_list args){ char* real_data = data; @@ -11,7 +12,6 @@ int _test_vec_foreach_func(void* data, va_list args){ *sum += *real_data; return 0; } - int _test_vec_find_string(void* a, void* b){ return strcmp(a, b) == 0; } @@ -29,8 +29,8 @@ int test_vec_add(){ int return_code; vec_init(&test_vec); - vec_add(&test_vec, test_string); - return_code = *((void**) test_vec.data) == test_string; + return_code = vec_add(&test_vec, test_string) == SUCCESS; + if(return_code) return_code = *((void**) test_vec.data) == test_string; vec_free(&test_vec); return return_code; @@ -96,6 +96,77 @@ int test_vec_index(){ return return_code; } +int _test_ht_foreach_func(void* data, va_list args){ + char* real_data = data; + int* sum = va_arg(args, int*); + + *sum += *real_data; + return 0; +} + +int test_ht_basic() { + ht test_ht; + ht_init(&test_ht); + ht_free(&test_ht); + return 1; +} + +int test_ht_put() { + int return_code; + ht test_ht; + char* test_key = "creamwove"; + char* test_value = "quists"; + unsigned int index = ht_default_hash_func(test_key) % LIBDS_HT_SIZE; + ht_init(&test_ht); + ht_put(&test_ht, test_key, test_value); + return_code = test_ht.data[index] && test_ht.data[index]->data == test_value; + ht_free(&test_ht); + return return_code; +} + +int test_ht_get() { + int return_code; + ht test_ht; + char* test_one = "creamwove"; + char* test_two = "quists"; + ht_init(&test_ht); + ht_put(&test_ht, test_one, test_two); + ht_put(&test_ht, test_two, test_one); + return_code = ht_get(&test_ht, test_one) == test_two && ht_get(&test_ht, test_two) == test_one; + ht_free(&test_ht); + return return_code; +} + +int test_ht_remove(){ + int return_code; + ht test_ht; + char* test_one = "creamwove"; + char* test_two = "quists"; + unsigned int index = ht_default_hash_func(test_one) % LIBDS_HT_SIZE; + ht_init(&test_ht); + ht_put(&test_ht, test_one, test_two); + ht_put(&test_ht, test_two, test_one); + ht_remove(&test_ht, test_two); + return_code = test_ht.data[index] && test_ht.data[index]->data == test_two; + ht_free(&test_ht); + return return_code; +} + +int test_ht_foreach(){ + int return_code; + int sum = 0; + ht test_ht; + ht_init(&test_ht); + char* test_one = "one"; + char* test_two = "two"; + ht_put(&test_ht, test_one, test_two); + ht_put(&test_ht, test_two, test_one); + ht_foreach(&test_ht, NULL, compare_always, _test_ht_foreach_func, &sum); + return_code = *test_one + *test_two == sum; + ht_free(&test_ht); + return return_code; +} + void run_test(char* test_name, int (*test_func)()) { printf("Running test %-15s . . . ", test_name); printf("%s\n", test_func() ? "Passed" : "Failed"); @@ -109,5 +180,11 @@ int main(int argc, char** argv){ run_test("vec_find", test_vec_find); run_test("vec_foreach", test_vec_foreach); run_test("vec_index", test_vec_index); + + run_test("ht_basic", test_ht_basic); + run_test("ht_put", test_ht_put); + run_test("ht_get", test_ht_get); + run_test("ht_remove", test_ht_remove); + run_test("ht_foreach", test_ht_foreach); return EXIT_SUCCESS; }