diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-09-13 23:26:34 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-09-13 23:29:12 +0300 |
commit | b8bb4d08ef489bc81e9c00ae43d19c3795e22072 (patch) | |
tree | d8bdd3b6bf0ff594aa1e72c0cc07d292334e7015 /src/unittest.c | |
parent | 7a4d16e5ebd45b15b0c4925894c25a81c2e135c6 (diff) | |
download | j1chess-b8bb4d08ef489bc81e9c00ae43d19c3795e22072.tar.gz |
chore: setup primitive testing framework
Diffstat (limited to 'src/unittest.c')
-rw-r--r-- | src/unittest.c | 33 |
1 files changed, 33 insertions, 0 deletions
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 <stdbool.h> +#include <stdio.h> +#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; +} + |