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 | |
parent | 7a4d16e5ebd45b15b0c4925894c25a81c2e135c6 (diff) | |
download | j1chess-b8bb4d08ef489bc81e9c00ae43d19c3795e22072.tar.gz |
chore: setup primitive testing framework
-rw-r--r-- | src/board.h | 2 | ||||
-rw-r--r-- | src/main.c | 7 | ||||
-rw-r--r-- | src/unittest.c | 33 | ||||
-rw-r--r-- | src/unittest.h | 18 |
4 files changed, 57 insertions, 3 deletions
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[] = { "♟︎", "♙", "♞", "♘", "♝", "♗", @@ -1,7 +1,10 @@ -#include <stdio.h> +#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 <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; +} + 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 <stdio.h> +#include <stdbool.h> + +#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(); |