From 02ab4888ddcd0d4670dff682ba9f77c011e640ec Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 16 Apr 2016 16:41:20 -0400 Subject: [PATCH] ex30 bits --- lcthw-remnants-2/c-skeleton/tests/minunit.h | 32 ++++++++ lcthw-remnants-2/ex29/tests/ex29_tests.c | 83 +++++++++++++-------- lcthw-remnants-2/ex29/tests/minunit.h | 32 ++++++++ 3 files changed, 114 insertions(+), 33 deletions(-) create mode 100644 lcthw-remnants-2/c-skeleton/tests/minunit.h create mode 100644 lcthw-remnants-2/ex29/tests/minunit.h diff --git a/lcthw-remnants-2/c-skeleton/tests/minunit.h b/lcthw-remnants-2/c-skeleton/tests/minunit.h new file mode 100644 index 0000000..62e4368 --- /dev/null +++ b/lcthw-remnants-2/c-skeleton/tests/minunit.h @@ -0,0 +1,32 @@ +#undef DNDEBUG +#ifndef _minunit_h +#define _minunit_h + +#include +#include +#include + +#define mu_suite_start() char *message = NULL + +#define mu_assert(test, message) if (!(test)) { log_err(message); return message; } +#define mu_run_test(test) debug("\n-----%s", " " #test); \ + message = test(); tests_run++; if (message) return message; + +#define RUN_TESTS(name) int main(int argc, char *argv[]) {\ + argc = 1; \ + debug("----- RUNNING: %s", argv[0]);\ + printf("----\nRUNNING: %s\n", argv[0]);\ + char *result = name();\ + if (result != 0) {\ + printf("FAILED: %s\n", result);\ + }\ + else {\ + printf("ALL TESTS PASSED\n");\ + }\ + printf("Tests run: %d\n", tests_run);\ + exit(result != 0);\ +} + +int tests_run; + +#endif diff --git a/lcthw-remnants-2/ex29/tests/ex29_tests.c b/lcthw-remnants-2/ex29/tests/ex29_tests.c index c31a6d3..e62033b 100644 --- a/lcthw-remnants-2/ex29/tests/ex29_tests.c +++ b/lcthw-remnants-2/ex29/tests/ex29_tests.c @@ -1,48 +1,65 @@ -#include -#include -#include "dbg.h" +#include "minunit.h" #include typedef int (*lib_function)(const char *data, int count); +char *lib_file = "build/libex29.so"; +void *lib = NULL; -int test_ex29(char *lib_file, char *func_to_run, char *data) +int check_function(const char *func_to_run, const char *data, int expected) { - int rc = 0; - - void *lib = dlopen(lib_file, RTLD_NOW); - check(lib != NULL, "Failed to open the library %s: %s", lib_file, dlerror()); - lib_function func = dlsym(lib, func_to_run); check(func != NULL, "Did not find %s function in the library %s: %s", func_to_run, lib_file, dlerror()); - rc = func(data, (int)strlen(data)); - check(rc == 0, "Function %s return %d for data: %s", func_to_run, rc, data); + int rc = func(data, (int)strlen(data)); + check(rc == expected, "Function %s return %d for data: %s", func_to_run, rc, data); - rc = dlclose(lib); - check(rc == 0, "Failed to close %s", lib_file); - - return 0; - -error: return 1; +error: + return 0; } -int main() +char *test_dlopen() { - char *dllib = getenv("EX29_DLLIB"); - if(dllib == NULL) { - dllib = "build/libex29.so"; - } + lib = dlopen(lib_file, RTLD_NOW); + mu_assert(lib != NULL, "Failed to open the library to test."); - check(test_ex29(dllib, "print_a_message", "hello there") == 0, "print failed"); - check(test_ex29(dllib, "uppercase", "hello there") == 0, "uppercase failed"); - check(test_ex29(dllib, "lowercase", "HELLO tHeRe") == 0, "lowercase failed"); - check(test_ex29(dllib, "fail_on_purpose", "i fail") == 1, "fail failed to fail"); - check(test_ex29(dllib, "fail_on_purpose", "") == 1, "fail failed to fail"); - check(test_ex29(dllib, "adfasfasdf", "asdfadff") == 1, "adfasfasdf failed to fail"); - check(test_ex29("libex.so", "adfasfasdf", "asdfadff") == 1, "libex failed to fail"); - - return 0; -error: - return 1; + return NULL; } + +char *test_functions() +{ + mu_assert(check_function("print_a_message", "Hello", 0), "print_a_message failed."); + mu_assert(check_function("uppercase", "Hello", 0), "uppercase failed."); + mu_assert(check_function("lowercase", "Hello", 0), "lowercase failed."); + + return NULL; +} + +char *test_failures() +{ + mu_assert(check_function("fail_on_purpose", "Hello", 1), "fail_on_purpose should fail."); + + return NULL; +} + +char *test_dlclose() +{ + int rc = dlclose(lib); + mu_assert(rc == 0, "Failed to close lib."); + + return NULL; +} + +char *all_tests() +{ + mu_suite_start(); + + mu_run_test(test_dlopen); + mu_run_test(test_functions); + mu_run_test(test_failures); + mu_run_test(test_dlclose); + + return NULL; +} + +RUN_TESTS(all_tests); diff --git a/lcthw-remnants-2/ex29/tests/minunit.h b/lcthw-remnants-2/ex29/tests/minunit.h new file mode 100644 index 0000000..62e4368 --- /dev/null +++ b/lcthw-remnants-2/ex29/tests/minunit.h @@ -0,0 +1,32 @@ +#undef DNDEBUG +#ifndef _minunit_h +#define _minunit_h + +#include +#include +#include + +#define mu_suite_start() char *message = NULL + +#define mu_assert(test, message) if (!(test)) { log_err(message); return message; } +#define mu_run_test(test) debug("\n-----%s", " " #test); \ + message = test(); tests_run++; if (message) return message; + +#define RUN_TESTS(name) int main(int argc, char *argv[]) {\ + argc = 1; \ + debug("----- RUNNING: %s", argv[0]);\ + printf("----\nRUNNING: %s\n", argv[0]);\ + char *result = name();\ + if (result != 0) {\ + printf("FAILED: %s\n", result);\ + }\ + else {\ + printf("ALL TESTS PASSED\n");\ + }\ + printf("Tests run: %d\n", tests_run);\ + exit(result != 0);\ +} + +int tests_run; + +#endif