lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit f0714a43a7c66bd18f32639bfd03a6f650015c29
parent d832efab494bf83ea0bcf90a8381641a4e6263ba
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 24 Jan 2017 17:07:31 +0100

Add(Tests): For more coverage

Diffstat:
Minc/m_test.h | 1+
Msrc/m_test.c | 30+++++++++++++++++-------------
Mtests/main.c | 3+++
Mtests/test.h | 3+++
Mtests/test_args.c | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
Mtests/test_list.c | 38++++++++++++++++++++++++++++++++++++++
Atests/test_print.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/test_tests.c | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 291 insertions(+), 27 deletions(-)

diff --git a/inc/m_test.h b/inc/m_test.h @@ -60,5 +60,6 @@ u32_t test_all(void); void test_free(void); int single_test_free(void *ptr); int single_result_free(void *ptr); +void print_result(const char *title, u32_t success, u32_t failed); #endif /* M_TEST_H */ diff --git a/src/m_test.c b/src/m_test.c @@ -48,7 +48,7 @@ void print_result(const char *title, u32_t success, u32_t failed) { fflush(stdout); if (percent == 100) { fprintf(stdout, "\033[1;32m"); - } else if (percent > 90) { + } else if (percent >= 90) { fprintf(stdout, "\033[1;33m"); } else { fprintf(stdout, "\033[1;31m"); @@ -104,6 +104,12 @@ mtest_results_t test_group(char *group) { res.group_name = NULL; title(group); + if (tests == NULL) + { + m_warning("Could not find any registered tests in %s group.\n", group); + return res; + } + /* Iterate over each test */ list_for_each(tests, tmp, ptr) { if (strcmp(ptr->group, group) == 0) { @@ -126,11 +132,7 @@ mtest_results_t test_group(char *group) { } } - if (res.total == 0) { - m_warning("Could not find any registered tests in %s group.\n", group); - } else { - print_result("Group Results", res.success, res.failed); - } + print_result("Group Results", res.success, res.failed); return res; } @@ -144,6 +146,11 @@ u32_t test_all(void) { mtest_results_t res, *ptr2; u32_t total = 0, success = 0, failed = 0; + if (tests == NULL) + { + m_warning("No tests registered, skipping.\n"); + return 0; + } list_for_each(tests, tmp, ptr) { if (list_get(groups, ptr->group, strlen(ptr->group)) == NULL) { res = test_group(ptr->group); @@ -159,14 +166,11 @@ u32_t test_all(void) { } title("Results"); - if (total == 0) - m_warning("No tests registered, skipping.\n"); - else { - list_for_each(tests_results, tmp, ptr2) { - print_result(ptr2->group_name, ptr2->success, ptr2->failed); - } - print_result("Total", success, failed); + list_for_each(tests_results, tmp, ptr2) { + print_result(ptr2->group_name, ptr2->success, ptr2->failed); } + print_result("Total", success, failed); + list_free(groups, NULL); list_free(tests_results, &single_result_free); diff --git a/tests/main.c b/tests/main.c @@ -3,9 +3,12 @@ int main(void) { u32_t ret; + /* MUST keep test_tests called first */ + register_tests_tests(); register_infos_tests(); register_args_tests(); register_list_tests(); + register_print_tests(); m_info("Beginning tests...\n"); ret = test_all(); diff --git a/tests/test.h b/tests/test.h @@ -3,6 +3,7 @@ # include <morphux.h> # include <unistd.h> +# include <stdlib.h> # include <sys/types.h> # include <sys/wait.h> @@ -22,6 +23,8 @@ void register_infos_tests(void); void register_args_tests(void); void register_list_tests(void); +void register_tests_tests(void); +void register_print_tests(void); void callback_q(const char *s); void callback_w(const char *s); void callback_e(const char *s); diff --git a/tests/test_args.c b/tests/test_args.c @@ -90,8 +90,8 @@ TEST(args_unhandled_2) { pipe(fd); if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); - TEST_ASSERT(read_opt(sizeof(av), av, opt) == 0, "Not handling triple '-' arguments"); - _exit(0); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling triple '-' arguments"); + exit(0); } else { WAIT_AND_CLOSE(pid, st, fd); TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return"); @@ -111,8 +111,8 @@ TEST(args_unhandled_3) { pipe(fd); if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); - TEST_ASSERT(read_opt(sizeof(av), av, opt) == 0, "Not handling alone '-' in arguments"); - _exit(0); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling alone '-' in arguments"); + exit(0); } else { WAIT_AND_CLOSE(pid, st, fd); TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return"); @@ -120,6 +120,69 @@ TEST(args_unhandled_3) { return TEST_SUCCESS; } +TEST(args_unhandled_4) { + margs_t opt[] = { + {'z', "zoiberg", "No idea.", false, NULL}, + ARGS_EOL + }; + char *av[] = {"./test", "-q"}; + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling unknown option"); + exit(0); + } else { + WAIT_AND_CLOSE(pid, st, fd); + TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return"); + } + return TEST_SUCCESS; +} + +TEST(args_unhandled_5) { + margs_t opt[] = { + {'z', "zoiberg", "No idea.", false, NULL}, + ARGS_EOL + }; + char *av[] = {"./test", "--"}; + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling double dash without option"); + exit(0); + } else { + WAIT_AND_CLOSE(pid, st, fd); + TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return"); + } + return TEST_SUCCESS; +} + +TEST(args_unhandled_6) { + margs_t opt[] = { + {'z', "zoiberg", "No idea.", false, NULL}, + ARGS_EOL + }; + char *av[] = {"./test", "-"}; + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling single dash without option"); + exit(1); + } else { + WAIT_AND_CLOSE(pid, st, fd); + TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return"); + } + return TEST_SUCCESS; +} + TEST(args_help_1) { margs_t opt[] = { {'z', "zoiberg", "No idea.", false, NULL}, @@ -132,8 +195,8 @@ TEST(args_help_1) { pipe(fd); if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); - TEST_ASSERT(read_opt(sizeof(av), av, opt) == 0, "Not handling -h arguments"); - _exit(5); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling -h arguments"); + exit(5); } else { WAIT_AND_CLOSE(pid, st, fd); TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return"); @@ -153,8 +216,8 @@ TEST(args_help_2) { pipe(fd); if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); - TEST_ASSERT(read_opt(sizeof(av), av, opt) == 0, "Not handling --help arguments"); - _exit(5); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling --help arguments"); + exit(5); } else { WAIT_AND_CLOSE(pid, st, fd); TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return"); @@ -174,8 +237,8 @@ TEST(args_version_1) { pipe(fd); if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); - TEST_ASSERT(read_opt(sizeof(av), av, opt) == 0, "Not handling --version arguments"); - _exit(5); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling --version arguments"); + exit(5); } else { WAIT_AND_CLOSE(pid, st, fd); TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return"); @@ -195,8 +258,8 @@ TEST(args_version_2) { pipe(fd); if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); - TEST_ASSERT(read_opt(sizeof(av), av, opt) == 0, "Not handling -v arguments"); - _exit(5); + TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling -v arguments"); + exit(5); } else { WAIT_AND_CLOSE(pid, st, fd); TEST_ASSERT((WEXITSTATUS(st) == 0), "Wrong return"); @@ -304,7 +367,7 @@ TEST(args_missing_value_1) { if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling missing argument"); - _exit(5); + exit(5); } else { WAIT_AND_CLOSE(pid, st, fd); TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return"); @@ -323,7 +386,7 @@ TEST(args_missing_value_2) { if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); TEST_ASSERT(read_opt(sizeof(av) / sizeof(av[0]), av, opt) == 0, "Not handling missing argument"); - _exit(5); + exit(5); } else { WAIT_AND_CLOSE(pid, st, fd); TEST_ASSERT((WEXITSTATUS(st) == 1), "Wrong return"); @@ -469,6 +532,9 @@ void register_args_tests(void) { reg_test("m_args", args_unhandled_1); reg_test("m_args", args_unhandled_2); reg_test("m_args", args_unhandled_3); + reg_test("m_args", args_unhandled_4); + reg_test("m_args", args_unhandled_5); + reg_test("m_args", args_unhandled_6); reg_test("m_args", args_help_1); reg_test("m_args", args_help_2); reg_test("m_args", args_version_1); diff --git a/tests/test_list.c b/tests/test_list.c @@ -116,6 +116,10 @@ TEST(list_add_after) { TEST_ASSERT(!strcmp(tmp->member, "Hello4 !\n"), "Order is wrong."); TEST_ASSERT(!tmp->next, "Next pointer is wrong."); list_free(ptr, NULL); + ptr = NULL; + list_add_after(ptr, NULL, test, sizeof(test)); + TEST_ASSERT(!strcmp(ptr->member, "Hello !\n"), "Head is wrong."); + list_free(ptr, NULL); return TEST_SUCCESS; } @@ -140,6 +144,12 @@ TEST(list_add_before) { TEST_ASSERT(!strcmp(tmp->member, "Hello4 !\n"), "Order is wrong."); TEST_ASSERT(!tmp->prev, "Next pointer is wrong."); list_free(ptr, NULL); + ptr = NULL; + list_add_before(ptr, ptr, test, sizeof(test)); + TEST_ASSERT(!strcmp(ptr->member, "Hello !\n"), "Head is wrong."); + list_add_before(ptr, NULL, test2, sizeof(test2)); + TEST_ASSERT(!strcmp(ptr->next->member, "Hello2 !\n"), "Tail is wrong."); + list_free(ptr, NULL); return TEST_SUCCESS; } @@ -177,6 +187,33 @@ TEST(list_size) { list_add(ptr, test3, sizeof(test3)); TEST_ASSERT(list_size(ptr) == 3, "Size is wrong."); list_free(ptr, NULL); + ptr = NULL; + TEST_ASSERT(list_size(ptr) == 0, "Size is wrong."); + return TEST_SUCCESS; +} + +int callback_list_free(void *ptr) { + static bool passed = false; + + (void)ptr; + if (!passed) { + passed = true; + return 0; + } + return 1; +} + +TEST(list_free) { + mlist_t *ptr = NULL; + char test[] = "Toto"; + + list_add(ptr, test, sizeof(test)); + list_add(ptr, test, sizeof(test)); + list_add(ptr, test, sizeof(test)); + list_add(ptr, test, sizeof(test)); + list_add(ptr, test, sizeof(test)); + list_free(ptr, &callback_list_free); + list_free(ptr, &callback_list_free); return TEST_SUCCESS; } @@ -191,4 +228,5 @@ void register_list_tests(void) { reg_test("mlist", list_add_before); reg_test("mlist", list_for_each_rev); reg_test("mlist", list_size); + reg_test("mlist", list_free); } diff --git a/tests/test_print.c b/tests/test_print.c @@ -0,0 +1,68 @@ +#include "test.h" + +TEST(print_info) { + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + m_info("Test"); + exit(0); + } else { + WAIT_AND_CLOSE(pid, st, fd); + } + return TEST_SUCCESS; +} + +TEST(print_warning) { + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + m_warning("Test"); + exit(0); + } else { + WAIT_AND_CLOSE(pid, st, fd); + } + return TEST_SUCCESS; +} + +TEST(print_error) { + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + m_error("Test"); + exit(0); + } else { + WAIT_AND_CLOSE(pid, st, fd); + } + return TEST_SUCCESS; +} + +TEST(print_panic) { + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + m_panic("Test"); + exit(0); + } else { + WAIT_AND_CLOSE(pid, st, fd); + } + return TEST_SUCCESS; +} + +void register_print_tests(void) { + reg_test("mprint", print_info); + reg_test("mprint", print_warning); + reg_test("mprint", print_error); + reg_test("mprint", print_panic); +} diff --git a/tests/test_tests.c b/tests/test_tests.c @@ -0,0 +1,81 @@ +/* Inception BWAAAAAAH */ + +#include "test.h" + +TEST(test_all_empty) { + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + test_free(); + test_all(); + exit(0); + } else { + WAIT_AND_CLOSE(pid, st, fd); + } + return TEST_SUCCESS; + +} + +TEST(test_group_unknown) { + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + test_group("Unknown"); + exit(0); + } else { + WAIT_AND_CLOSE(pid, st, fd); + } + return TEST_SUCCESS; +} + +TEST(test_result) { + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + print_result("Test", 10, 0); + print_result("Test", 9, 1); + print_result("Test", 0, 10); + exit(0); + } else { + WAIT_AND_CLOSE(pid, st, fd); + } + return TEST_SUCCESS; +} + +TEST(test_false) { + TEST_ASSERT(false, "Nope."); + return TEST_SUCCESS; +} + +TEST(test_group_fail) { + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + reg_test("group_fail", test_false); + test_all(); + test_free(); + exit(0); + } else { + WAIT_AND_CLOSE(pid, st, fd); + } + return TEST_SUCCESS; +} + +void register_tests_tests(void) { + test_test_all_empty(); + test_test_group_unknown(); + test_test_result(); + test_test_group_fail(); +}