From 1251aa55b97b3b4ca7c2c670bdbd683a76eb6daa Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 11 Sep 2020 20:22:24 -0400 Subject: Refactor headers and relocate tests --- tests.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests.c (limited to 'tests.c') diff --git a/tests.c b/tests.c new file mode 100644 index 0000000..522d03d --- /dev/null +++ b/tests.c @@ -0,0 +1,77 @@ +#include "multihome.h" + +#ifdef ENABLE_TESTING +void test_split() { + puts("split()"); + char **result; + size_t result_alloc; + + result = split("one two three", " ", &result_alloc); + assert(strcmp(result[0], "one") == 0 && strcmp(result[1], "two") == 0 && strcmp(result[2], "three") == 0); + assert(result_alloc != 0); + free_array((void *)result, result_alloc); +} + +void test_count_substrings() { + puts("count_substrings()"); + size_t result; + result = count_substrings("one two three", " "); + assert(result == 2); +} + +void test_mkdirs() { + puts("mkdirs()"); + int result; + char *input = "this/is/a/test"; + + if (access(input, F_OK) == 0) { + assert(remove("this/is/a/test") == 0); + assert(remove("this/is/a") == 0); + assert(remove("this/is") == 0); + assert(remove("this") == 0); + } + + result = mkdirs(input); + assert(result == 0); + assert(access(input, F_OK) == 0); +} + +void test_shell() { + puts("shell()"); + assert(shell((char *[]){"/bin/echo", "testing", NULL}) == 0); + assert(shell((char *[]){"/bin/date", NULL}) == 0); + assert(shell((char *[]){"/bin/unlikelyToExistAnywhere", NULL}) != 0); +} + +void test_touch() { + puts("touch()"); + char *input = "touched_file.txt"; + + if (access(input, F_OK) == 0) { + remove(input); + } + + assert(touch(input) == 0); + assert(access(input, F_OK) == 0); +} + +void test_strip_domainname() { + puts("strip_domainname()"); + char *input = "subdomain.domain.tld"; + char *truth = "subdomain"; + char *result; + + result = strip_domainname(input); + assert(strcmp(result, truth) == 0); +} + +void test_main() { + test_count_substrings(); + test_split(); + test_mkdirs(); + test_shell(); + test_touch(); + test_strip_domainname(); + exit(0); +} +#endif -- cgit