test/test-fen.c

changeset 219
24b866230dd4
parent 218
1e9751f8eb0d
child 220
04da225a5677
--- a/test/test-fen.c	Fri Sep 04 13:49:48 2026 +0200
+++ b/test/test-fen.c	Sat Sep 05 12:36:04 2026 +0200
@@ -52,6 +52,8 @@
         CX_TEST_ASSERT(gs.fifty_cntr_start == 0);
         /* we start after 53 half-moves have been played */
         CX_TEST_ASSERT(gs.move_start == 53);
+        /* it's black's turn */
+        CX_TEST_ASSERT(current_color(&gs) == BLACK);
         /* we have no move history */
         CX_TEST_ASSERT(gs.movecount == 0);
         CX_TEST_ASSERT(gs.movecapacity == 0);
@@ -94,12 +96,78 @@
     gamestate_cleanup(&gs);
 }
 
+static CX_TEST(test_fen_with_castling_rights) {
+    /* a test that simply checks if an arbitrary FEN is parsed correctly */
+    const char * fen = "1rbqk1nr/ppp2ppp/2np4/2b1p3/2P5/2N1P1P1/PP1P1PBP/R1BQK1NR w KQk - 1 6";
+    Board expected_board = {
+        {WROOK, 0,       WBISHOP, WQUEEN, WKING, 0,       WKNIGHT, WROOK},
+        {WPAWN, WPAWN,   0,       WPAWN,  0,     WPAWN,   WBISHOP, WPAWN},
+        {0,     0,       WKNIGHT, 0,      WPAWN, 0,       WPAWN,   0},
+        {0,     0,       WPAWN,   0,      0,     0,       0,       0},
+        {0,     0,       BBISHOP, 0,      BPAWN, 0,       0,       0},
+        {0,     0,       BKNIGHT, BPAWN,  0,     0,       0,       0},
+        {BPAWN, BPAWN,   BPAWN,   0,      0,     BPAWN,   BPAWN,   BPAWN},
+        {0,     BROOK,   BBISHOP, BQUEEN, BKING, 0,       BKNIGHT, BROOK}
+    };
+    GameState gs;
+    int result = fen_parse(fen, &gs);
+    CX_TEST_DO {
+        CX_TEST_ASSERT(result == 0);
+        CX_TEST_ASSERT(strcmp(gs.fen_start, fen) == 0);
+        /* counter for fifty-move rule */
+        CX_TEST_ASSERT(gs.fifty_cntr_start == 1);
+        /* we start after 10 half-moves have been played */
+        CX_TEST_ASSERT(gs.move_start == 10);
+        /* it's white's turn */
+        CX_TEST_ASSERT(current_color(&gs) == WHITE);
+        /* we have no move history */
+        CX_TEST_ASSERT(gs.movecount == 0);
+        CX_TEST_ASSERT(gs.movecapacity == 0);
+        CX_TEST_ASSERT(gs.moves == NULL);
+        CX_TEST_ASSERT(gs.fen == NULL);
+        /* black queen-side castling right revoked */
+        CX_TEST_ASSERT(gs.castling.K == false);
+        CX_TEST_ASSERT(gs.castling.Q == false);
+        CX_TEST_ASSERT(gs.castling.k == false);
+        CX_TEST_ASSERT(gs.castling.q == true);
+        /* check the board */
+        for (File f = 0 ; f < 8 ; f++) {
+            for (Rank r = 0 ; r < 8 ; r++) {
+                Piece exp = expected_board[r][f];
+                Piece p = piece_at(&gs, f, r);
+                char msg[64];
+                sprintf(msg, "expected %u at %c:%c but got %u",
+                    exp, filechr(f), rankchr(r), p);
+                CX_TEST_ASSERTM(exp == p, msg);
+            }
+        }
+        /* check that other stuff is empty-initialized */
+        CX_TEST_ASSERT(gs.bname[0] == 0);
+        CX_TEST_ASSERT(gs.wname[0] == 0);
+        CX_TEST_ASSERT(gs.premove[0] == 0);
+        CX_TEST_ASSERT(!gs.checkmate);
+        CX_TEST_ASSERT(!gs.stalemate);
+        CX_TEST_ASSERT(!gs.threefold);
+        CX_TEST_ASSERT(!gs.nomaterial);
+        CX_TEST_ASSERT(!gs.remis);
+        CX_TEST_ASSERT(!gs.wresign);
+        CX_TEST_ASSERT(!gs.bresign);
+        CX_TEST_ASSERT(!gs.ragequit);
+        CX_TEST_ASSERT(!gs.review);
+        /* check that we get the original FEN back */
+        char fen_chk[FEN_MAX_LENGTH];
+        fen_compute(fen_chk, &gs);
+        CX_TEST_ASSERTM(strcmp(fen, fen_chk) == 0, fen_chk);
+    }
+    gamestate_cleanup(&gs);
+}
+
 CxTestSuite* test_fen_suite(void) {
     CxTestSuite* suite = cx_test_suite_new("Test FEN API");
 
     cx_test_register(suite, test_fen_basic);
+    cx_test_register(suite, test_fen_with_castling_rights);
     // TODO: test FENs that describe definitely ended games (for each type of ending)
-    // TODO: test FEN that comes with a non-zero fifty-move rule counter
     // TODO: test FEN with en-passant threat
 
     return suite;

mercurial