From 5b2bb4e025919d74090d23cbabadfeec3b1e9dc4 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Tue, 3 Aug 2021 17:47:00 +0200 Subject: a bad testing framework --- src/kernel/main.c | 4 ++++ src/kernel/tests/base.h | 23 +++++++++++++++++++++++ src/kernel/tests/main.c | 19 +++++++++++++++++++ src/kernel/tests/tests.h | 3 +++ 4 files changed, 49 insertions(+) create mode 100644 src/kernel/tests/base.h create mode 100644 src/kernel/tests/main.c create mode 100644 src/kernel/tests/tests.h diff --git a/src/kernel/main.c b/src/kernel/main.c index 7864699..effdd45 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -26,6 +27,9 @@ void kmain(struct kmain_info info) { log_const("mem..."); mem_init(&info); + log_const("tests..."); + tests_all(); + log_const("loading init..."); run_init(&info); diff --git a/src/kernel/tests/base.h b/src/kernel/tests/base.h new file mode 100644 index 0000000..877b179 --- /dev/null +++ b/src/kernel/tests/base.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include +#include + +extern bool _did_tests_fail; + +// tests MUST be used +#pragma GCC diagnostic error "-Wunused-function" + +#define TEST(name) \ + static void __test_##name() + +#define TEST_RUN(name) \ + __test_##name(); + +#define TEST_IF(cond) \ + if (!(cond)) { \ + _did_tests_fail = true; \ + log_const("TEST_IF failed at " \ + __FILE__ ":" NUM2STR(__LINE__) \ + " "); \ + } diff --git a/src/kernel/tests/main.c b/src/kernel/tests/main.c new file mode 100644 index 0000000..a7e9534 --- /dev/null +++ b/src/kernel/tests/main.c @@ -0,0 +1,19 @@ +#include +#include +#include + +bool _did_tests_fail; + +TEST(basic_math) { + TEST_IF(2 + 2 == 4); + TEST_IF(2 * 2 == 4); +} + +void tests_all() { + _did_tests_fail = false; + + TEST_RUN(basic_math); + + if (_did_tests_fail) + panic(); +} diff --git a/src/kernel/tests/tests.h b/src/kernel/tests/tests.h new file mode 100644 index 0000000..d9abce4 --- /dev/null +++ b/src/kernel/tests/tests.h @@ -0,0 +1,3 @@ +#pragma once + +void tests_all(); -- cgit v1.2.3