#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(); kpl_test find = {}; for (int32_t arg_index = 1; arg_index < argc; arg_index++) { find.name = argv[arg_index]; kpl_test *found = kpl_tree_find(&test_tree, &find); if (!found) { printf(KPL_COLOR2(BOLD, RED) "TEST: " KPL_COLOR(RESET) KPL_COLOR2(BOLD, WHITE) "%s " KPL_COLOR(RESET) KPL_COLOR2(BOLD, RED) "NOT FOUND\n" KPL_COLOR(RESET), find.name); return KPL_EXIT_CODE_ERROR; } kpl_test *duplicates = found->tree_duplicates; while (duplicates) { kpl_test_run_one(duplicates); duplicates = duplicates->tree_duplicates; } kpl_test_run_one(found); } return kpl_test_report_error(); } KPL_TEST(pass_by_default) { // pass, uncomment below to fail // KPL_FAIL("example fail"); }