From b8bb4d08ef489bc81e9c00ae43d19c3795e22072 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 13 Sep 2022 23:26:34 +0300 Subject: chore: setup primitive testing framework --- src/unittest.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/unittest.c (limited to 'src/unittest.c') diff --git a/src/unittest.c b/src/unittest.c new file mode 100644 index 0000000..753dac3 --- /dev/null +++ b/src/unittest.c @@ -0,0 +1,33 @@ +#include +#include +#include "unittest.h" + +static int TOTAL = 0; +static int SUCCESS = 0; + + +bool assert(bool expression, const char* message) { + if (!expression) { + printf("%s\n", draw(RED, DEFAULT_ASSERT_MESSAGE)); + printf("%s\n", message); + } + return expression; +} + +void unit_test(bool expression, const char* subject) { + TOTAL++; + printf( "%d. %s: %s\n", TOTAL, subject, expression? draw(GRN, PASS) : draw(RED, FAIL)); + if (expression) SUCCESS++; +} + +double coverage() { + return 100 * (TOTAL? (double)SUCCESS/TOTAL : 1); +} + +bool report() { + printf("%s ", SUCCESS == TOTAL ? draw(GRN, "All checks successful!") : draw(RED, "Some tests have failed")); + printf("[%d / %d]\n", SUCCESS, TOTAL); + printf("Total coverage: %.2f%%\n", coverage()); + return TOTAL - SUCCESS; +} + -- cgit v1.2.3