diff options
author | sanine-a <sanine.not@pm.me> | 2021-12-21 11:00:32 -0600 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2021-12-21 11:00:32 -0600 |
commit | b712a7ac4c6a7a8bb1d81d0adad8f98084b4d2fe (patch) | |
tree | c7c1a3add4eea07f3580e9c9c2fa9d9a8a088a8a /src | |
parent | 98ab574f51a223bbd53052ded32dce6aebca9298 (diff) |
refactor: move lily-test.h into root directory and move src/ -> tests/
Diffstat (limited to 'src')
-rw-r--r-- | src/lily-test.h | 32 | ||||
-rw-r--r-- | src/test.c | 116 |
2 files changed, 0 insertions, 148 deletions
diff --git a/src/lily-test.h b/src/lily-test.h deleted file mode 100644 index 9f166b1..0000000 --- a/src/lily-test.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef LILY_TEST_H -#define LILY_TEST_H - -#define lily_test const char * - -struct lily_test_data_t { - int tests_run; - int tests_failed; -}; - -#define LILY_INIT() \ - struct lily_test_data_t lily_test_data = { 0, 0 }; - - -// helper macros to turn numerical constants into strings -#define STR_IMPL(x) #x -#define STR(X) STR_IMPL(x) - - -// assertion macros -#define lily_indent " " -#define lily_assert_msg(statement, message) \ - do { \ - if (!(statement)) \ - return "" message \ - "\n" lily_indent " [" __FILE__ ":" STR(__LINE__) "]"; \ - } while(0) - -#define lily_assert_equal(a, b) lily_assert_msg((a) == (b), "'" #a "' is not equal to '" #b "'") - - -#endif diff --git a/src/test.c b/src/test.c deleted file mode 100644 index 85c1ad7..0000000 --- a/src/test.c +++ /dev/null @@ -1,116 +0,0 @@ -#include <stdio.h> -#include <stdbool.h> -#include <string.h> -#include <stdlib.h> - -#include "lily-test.h" - -LILY_INIT(); - -void run_test(const char *name, lily_test (*fp)()) -{ - printf("%s... ", name); - const char *result = fp(); - if (result != 0) { - printf("FAILED (%s)\n", result); - } - else - printf("OK\n"); -} - - -lily_test check_init() -{ - // should fail to compile if lily_test_data is undefined - if (lily_test_data.tests_run != 0) - return "tests_run is not equal to zero!"; - if (lily_test_data.tests_failed != 0) - return "tests_failed is not equal to zero!"; - return 0; -} - - -int get_message(char **destination, const char *source) -{ - const char *s = source; - size_t size = 0; - while (*s != '\n') { - if (*s == 0) - return false; - s++; - size++; - } - - *destination = malloc((size+1) * sizeof(char)); - strncpy(*destination, source, size); - (*destination)[size] = 0; - return true; -} - -#define assert_msg "message" - -lily_test wrap_assert(bool statement) -{ - lily_assert_msg(statement, assert_msg); - return 0; -} - -lily_test check_assert() -{ - if (wrap_assert(true) != 0) - return "true assertion did not return 0!"; - - const char *result = wrap_assert(false); - - if (result == 0) - return "false assertion returned zero!"; - - char *message; - if (!get_message(&message, result)) - return "false assertion contained malformed message!"; - - if (strcmp(message, assert_msg) != 0) - return "false assertion message was not '" assert_msg "'!"; - free(message); - - return 0; -} - - -const char *assert_eq_true() -{ - lily_assert_equal(15, 15); - return 0; -} -const char *assert_eq_false() -{ - lily_assert_equal(14, 15); - return 0; -} - -lily_test check_other_asserts() -{ - if (assert_eq_true() != 0) - return "equality assertion returned non-zero when true!"; - const char *result = assert_eq_false(); - if (result == 0) - return "equality assertion returned zero when false!"; - char *message; - if (!get_message(&message, result)) - return "false equality assertion returned malformed message!"; - if (strcmp(message, "'14' is not equal to '15'") != 0) - return "false equality assertion returned incorrect message!"; - - return 0; -} - - -int main() -{ - run_test("check LILY_INIT()", check_init); - run_test("check basic assertion", check_assert); - run_test("check other asserts", check_other_asserts); - - printf("all tests finished.\n"); - return 0; -} |