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/board.h | 2 +- src/main.c | 7 +++++-- src/unittest.c | 33 +++++++++++++++++++++++++++++++++ src/unittest.h | 18 ++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/unittest.c create mode 100644 src/unittest.h (limited to 'src') diff --git a/src/board.h b/src/board.h index 6cde670..052e104 100644 --- a/src/board.h +++ b/src/board.h @@ -19,7 +19,7 @@ typedef struct { BYTE en_passant_square; } Board; -const char* pieces[] = { +static const char* pieces[] = { "♟︎", "♙", "♞", "♘", "♝", "♗", diff --git a/src/main.c b/src/main.c index e9902b2..a442231 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,10 @@ -#include +#include "unittest.h" #include "board.h" int main() { - printf("Size of board: %lu bytes\n", sizeof(Board)); + unit_test(2 * 2 == 4, "2 * 2 == 4"); + unit_test(2 * 2 == 5, "2 * 2 == 4"); + + report(); return 0; } 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; +} + diff --git a/src/unittest.h b/src/unittest.h new file mode 100644 index 0000000..d2fd4ad --- /dev/null +++ b/src/unittest.h @@ -0,0 +1,18 @@ +#include +#include + +#define RED "\x1B[31m" +#define GRN "\x1B[32m" +#define RESET "\x1B[0m" + +#define PASS "PASS!" +#define FAIL "FAIL!" +#define DEFAULT_ASSERT_MESSAGE "Assertion failed!" + +#define draw(COLOR, TEXT) COLOR TEXT RESET + +bool assert(bool expression, const char* message); + +void unit_test(bool expression, const char* subject); + +bool report(); -- cgit v1.2.3