diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-10-06 17:13:01 -0400 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-10-06 17:13:01 -0400 |
| commit | 7bd050ccf74222e1b659f2fd6826a86e6afbcc94 (patch) | |
| tree | 30bae9ca5dd110f32cd4abb10d42ea4d872b4d82 /test/test.c | |
| parent | de65adbc1b2d1b3efdbe6a023bb7435f84b2f3a7 (diff) | |
basic tests
Diffstat (limited to 'test/test.c')
| -rw-r--r-- | test/test.c | 77 |
1 files changed, 77 insertions, 0 deletions
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"); +} |
