aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-09-14 04:41:50 +0300
committereug-vs <eugene@eug-vs.xyz>2022-09-14 04:41:50 +0300
commit031ccb2d9e7993ed46bd40a5c3c78af86cb46ba4 (patch)
tree6f95027d51b093f72c1e33f57da176a02d7819d4
parentefa8671eecdabcdccecde87c1d05acbc89295f49 (diff)
downloadj1chess-031ccb2d9e7993ed46bd40a5c3c78af86cb46ba4.tar.gz
test: group unittests into sections
-rw-r--r--src/main.c18
-rw-r--r--src/unittest.c9
-rw-r--r--src/unittest.h9
3 files changed, 28 insertions, 8 deletions
diff --git a/src/main.c b/src/main.c
index 2e95970..97e5d14 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,11 +2,13 @@
#include "board.h"
int main() {
- { // Pop count
+ {
+ start_test_section("Bitboards");
unit_test(pop_count(0b01110) == 3, "Pop count of 01110 is 3");
}
- { // Test knight attacks
+ {
+ start_test_section("Test knight attacks");
Bitboard attacks[64];
precompute_knight_attack_table(attacks);
@@ -17,15 +19,23 @@ int main() {
}
unit_test(max_attacks == 8, "Max amount of knight attacks should be 8");
- { // Knight on b7 attack table
+ {
U64 bit = 1;
unit_test(
attacks[b7] == ((bit << d8) | (bit << d6) | (bit << c5) | (bit << a5)),
- "Knight on b7 attacks d8, d6, c5, a5"
+ "Knight on b7 attacks only d8, d6, c5, a5"
);
}
}
+ {
+ start_test_section("FEN String");
+ Board board = parse_FEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
+ unit_test(pop_count(board.pieces[PAWN] | board.pieces[PAWN | BLACK]) == 16, "Default position has 16 pawns total");
+ unit_test(pop_count(board.pieces[ROOK]) == 2, "Default position has 2 white rooks");
+ unit_test(board.side == WHITE, "Side to moveSide to move is white");
+ }
+
report();
return 0;
}
diff --git a/src/unittest.c b/src/unittest.c
index 753dac3..e4f7929 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -3,6 +3,7 @@
#include "unittest.h"
static int TOTAL = 0;
+static int SECTION_TOTAL = 0;
static int SUCCESS = 0;
@@ -16,10 +17,16 @@ bool assert(bool expression, const char* message) {
void unit_test(bool expression, const char* subject) {
TOTAL++;
- printf( "%d. %s: %s\n", TOTAL, subject, expression? draw(GRN, PASS) : draw(RED, FAIL));
+ SECTION_TOTAL++;
+ printf( "%d.%d %s: %s\n", TOTAL, SECTION_TOTAL, subject, expression? draw(GRN, PASS) : draw(RED, FAIL));
if (expression) SUCCESS++;
}
+void start_test_section(const char* name) {
+ printf("%s %s %s\n", CYAN, name, RESET);
+ SECTION_TOTAL = 0;
+}
+
double coverage() {
return 100 * (TOTAL? (double)SUCCESS/TOTAL : 1);
}
diff --git a/src/unittest.h b/src/unittest.h
index d2fd4ad..e32ee10 100644
--- a/src/unittest.h
+++ b/src/unittest.h
@@ -1,9 +1,10 @@
#include <stdio.h>
#include <stdbool.h>
-#define RED "\x1B[31m"
-#define GRN "\x1B[32m"
-#define RESET "\x1B[0m"
+#define RED "\x1B[31m"
+#define GRN "\x1B[32m"
+#define CYAN "\x1B[36m"
+#define RESET "\x1B[0m"
#define PASS "PASS!"
#define FAIL "FAIL!"
@@ -15,4 +16,6 @@ bool assert(bool expression, const char* message);
void unit_test(bool expression, const char* subject);
+void start_test_section(const char* name);
+
bool report();