aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/README31
-rw-r--r--test/test.c77
2 files changed, 108 insertions, 0 deletions
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");
+}