diff --git a/lcthw-remnants-2/liblcthw/tests/lcthw/bstrlib_tests.c b/lcthw-remnants-2/liblcthw/tests/lcthw/bstrlib_tests.c new file mode 100644 index 0000000..f8c8e8d --- /dev/null +++ b/lcthw-remnants-2/liblcthw/tests/lcthw/bstrlib_tests.c @@ -0,0 +1,55 @@ +#include "../minunit.h" +#include + +char *test_bfromcstr() +{ + bstring b = bfromcstr("oh hai"); + mu_assert(b != NULL, "bstring is NULL."); + + return NULL; +} + +char *test_blk2bstr() +{ + bstring b = blk2bstr("hats", 5); + mu_assert(b != NULL, "bstring is NULL."); + + b = blk2bstr(NULL, 42); + mu_assert(b == NULL, "bstring is not NULL."); + + b = blk2bstr("bats", -17); + mu_assert(b == NULL, "bstring is not NULL."); + + return NULL; +} + +char *test_bstrcpy() +{ + bstring b = bfromcstr("mats"); + mu_assert(bstrcpy(b) != NULL, "bstring is NULL."); + + mu_assert(bstrcpy(NULL) == NULL, "bstring is not NULL."); + + int orig_len = b->slen; + b->slen = -1; + mu_assert(bstrcpy(b) == NULL, "bstring is not NULL."); + b->slen = orig_len; + + b->data = NULL; + mu_assert(bstrcpy(b) == NULL, "bstring is not NULL."); + + return NULL; +} + +char *all_tests() +{ + mu_suite_start(); + + mu_run_test(test_bfromcstr); + mu_run_test(test_blk2bstr); + mu_run_test(test_bstrcpy); + + return NULL; +} + +RUN_TESTS(all_tests); diff --git a/lcthw-remnants-2/liblcthw/tests/minunit.h b/lcthw-remnants-2/liblcthw/tests/minunit.h index 5ffc3e9..7ecaa3d 100644 --- a/lcthw-remnants-2/liblcthw/tests/minunit.h +++ b/lcthw-remnants-2/liblcthw/tests/minunit.h @@ -8,7 +8,10 @@ #define mu_suite_start() char *message = NULL -#define mu_assert(test, message) if (!(test)) { log_err(message); return message; } +#define mu_assert(test, message) if (!(test)) {\ + log_err(message); return message;\ + }\ + assertions_made++; #define mu_run_test(test) debug("\n-----%s", " " #test); \ message = test(); tests_run++; if (message) return message; @@ -25,9 +28,11 @@ printf("ALL TESTS PASSED\n");\ }\ printf("Tests run: %d\n", tests_run);\ + printf("Assertions made: %d\n", assertions_made);\ exit(result != 0);\ } int tests_run; +int assertions_made; #endif