From 00eeb01299a0fc45d3094677ffa4c004bf3a0857 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 21 Dec 2016 01:33:37 -0800 Subject: [PATCH] Convert main.c into tests for libds, and write tests for vec.h. --- src/main.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/src/main.c b/src/main.c index 79de47c..0bec0fc 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,113 @@ #include +#include +#include +#include +#include "vec.h" + +int _test_vec_foreach_func(void* data, va_list args){ + char* real_data = data; + int* sum = va_arg(args, int*); + + *sum += *real_data; + return 0; +} + +int _test_vec_find_string(void* a, void* b){ + return strcmp(a, b) == 0; +} + +int test_vec_basic(){ + vec test_vec; + libds_result result = vec_init(&test_vec); + vec_free(&test_vec); + + return result == SUCCESS; +} +int test_vec_add(){ + vec test_vec; + const char* test_string = "test"; + int return_code; + + vec_init(&test_vec); + vec_add(&test_vec, test_string); + return_code = *((void**) test_vec.data) == test_string; + vec_free(&test_vec); + + return return_code; +} +int test_vec_remove(){ + vec test_vec; + const char* test_string = "test"; + int return_code; + + vec_init(&test_vec); + vec_add(&test_vec, test_string); + vec_remove(&test_vec, test_string); + return_code = *((void**) test_vec.data) == NULL; + vec_free(&test_vec); + + return return_code; +} +int test_vec_find(){ + vec test_vec; + const char* test_string = "test"; + int return_code; + + vec_init(&test_vec); + vec_add(&test_vec, test_string); + return_code = vec_find(&test_vec, test_string, _test_vec_find_string) == test_string; + vec_free(&test_vec); + return return_code; +} +int test_vec_foreach(){ + vec test_vec; + int return_code; + int sum = 0; + char* test_strings[3] = { + "1", + "2", + "3" + }; + + vec_init(&test_vec); + vec_add(&test_vec, test_strings[0]); + vec_add(&test_vec, test_strings[1]); + vec_add(&test_vec, test_strings[2]); + vec_foreach(&test_vec, NULL, compare_always, _test_vec_foreach_func, &sum); + return_code = test_strings[0][0] + test_strings[1][0] + test_strings[2][0] == sum; + vec_free(&test_vec); + return return_code; +} +int test_vec_index(){ + vec test_vec; + int return_code; + char* test_strings[3] = { + "1", + "2", + "3" + }; + + vec_init(&test_vec); + vec_add(&test_vec, test_strings[0]); + vec_add(&test_vec, test_strings[1]); + vec_add(&test_vec, test_strings[2]); + return_code = vec_index(&test_vec, 1) == test_strings[1]; + vec_free(&test_vec); + 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"); +} int main(int argc, char** argv){ + + run_test("vec_basic", test_vec_basic); + run_test("vec_add", test_vec_add); + run_test("vec_remove", test_vec_remove); + run_test("vec_find", test_vec_find); + run_test("vec_foreach", test_vec_foreach); + run_test("vec_index", test_vec_index); return EXIT_SUCCESS; }