From 7bd050ccf74222e1b659f2fd6826a86e6afbcc94 Mon Sep 17 00:00:00 2001 From: nodist Date: Tue, 6 Oct 2026 17:13:01 -0400 Subject: basic tests --- test/README | 31 +++++++++++++++++++++++++ test/test.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 test/README create mode 100644 test/test.c (limited to 'test') diff --git a/test/README b/test/README new file mode 100644 index 0000000..4ef67e0 --- /dev/null +++ b/test/README @@ -0,0 +1,31 @@ +Test Files + +--- + +Testing: + +Each test is a function wrapped in a macro + +KPL_TEST(my_test) { + // code here +} + +Each test passes by default + +--- + +Testing Macros: + +The testing framework provides two macros KPL_ASSERT and KPL_FAIL + +Both macros can only be used inside a KPL_TEST function + +Asserting: + +KPL_ASSSERT(condition, "Fail Message"); + +--- + +Failing: + +KPL_FAIL("The test failed"); diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..6cede92 --- /dev/null +++ b/test/test.c @@ -0,0 +1,77 @@ + +#include "kpl_test.h" + +static int32_t kpl_test_tree_compare(const void *node_a, const void *node_b) { + const kpl_test *test_a = node_a, *test_b = node_b; + return strcmp(test_a->name, test_b->name); +} + +static kpl_tree test_tree = KPL_TREE_INIT_STATIC(kpl_test_tree_compare, NULL); + +static kpl_test *test_head = NULL, *test_tail = NULL, *error_head = NULL, *error_tail = NULL; + +void _kpl_test_add(kpl_test *_test) { + if (!test_head) + test_head = test_tail = _test; + else + test_tail = test_tail->list_next = _test; + kpl_tree_insert(&test_tree, _test); +} + +static void kpl_test_report(const kpl_test *_test) { + printf(KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_CYAN) "%s" KPL_COLOR(RESET), + _test->source_file, _test->name); + if (_test->error_line_number == KPL_TEST_ERROR_LINE_NUMBER_INIT) { + printf(KPL_COLOR(LIGHT_GREEN) " PASS\n" KPL_COLOR(RESET)); + return; + } + printf("::" KPL_COLOR(LIGHT_MAGENTA) "%d" KPL_COLOR(RESET) KPL_COLOR(LIGHT_RED) " %s\n" KPL_COLOR(RESET), + _test->error_line_number, _test->error_message); +} + +static int32_t kpl_test_report_error(void) { + if (!error_head) { + printf(KPL_COLOR2(BOLD, GREEN) "PASSED\n" KPL_COLOR(RESET)); + return KPL_EXIT_CODE_SUCCESS; + } + printf(KPL_COLOR2(BOLD, RED) "FAILED\n" KPL_COLOR(RESET)); + while (error_head) { + kpl_test *_test = error_head; + error_head = error_head->list_next; + kpl_test_report(_test); + } + return KPL_EXIT_CODE_ERROR; +} + +static void kpl_test_run_one(kpl_test *_test) { + _test->list_next = NULL; + _test->test_fn(_test); + kpl_test_report(_test); + if (_test->error_line_number == KPL_TEST_ERROR_LINE_NUMBER_INIT) + return; + if (!error_head) + error_head = error_tail = _test; + else + error_tail = error_tail->list_next = _test; +} + +static int32_t kpl_test_run_all(void) { + while (test_head) { + kpl_test *_test = test_head; + test_head = test_head->list_next; + kpl_test_run_one(_test); + } + return kpl_test_report_error(); +} + +int32_t main(int32_t argc, char *argv[]) { + if (argc < 2) + return kpl_test_run_all(); + // TODO run by name + return kpl_test_report_error(); +} + +KPL_TEST(pass_by_default) { + // pass, uncomment below to fail + // KPL_FAIL("example fail"); +} -- cgit v1.2.3