test/test-fen.c

Fri, 04 Sep 2026 13:49:48 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 04 Sep 2026 13:49:48 +0200
changeset 218
1e9751f8eb0d
permissions
-rw-r--r--

add function to reconstruct a board from a FEN string

resolves #939 in principle, but there are many TODOs left

218
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2026 Mike Becker. All rights reserved.
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "chess/fen.h"
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include "ucxtest.h"
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 static CX_TEST(test_fen_basic) {
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 /* a test that simply checks if an arbitrary FEN is parsed correctly */
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 const char * fen = "r1n3k1/5qbp/p5p1/1p6/4Q3/B6P/P4PP1/R4RK1 b - - 0 27";
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 Board expected_board = {
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 {WROOK, 0, 0, 0, 0, WROOK, WKING, 0},
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 {WPAWN, 0, 0, 0, 0, WPAWN, WPAWN, 0},
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 {WBISHOP, 0, 0, 0, 0, 0, 0, WPAWN},
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 {0, 0, 0, 0, WQUEEN, 0, 0, 0},
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 {0, BPAWN, 0, 0, 0, 0, 0, 0},
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 {BPAWN, 0, 0, 0, 0, 0, BPAWN, 0},
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 {0, 0, 0, 0, 0, BQUEEN, BBISHOP, BPAWN},
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 {BROOK, 0, BKNIGHT, 0, 0, 0, BKING, 0}
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 };
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 GameState gs;
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 int result = fen_parse(fen, &gs);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 CX_TEST_DO {
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 CX_TEST_ASSERT(result == 0);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 CX_TEST_ASSERT(strcmp(gs.fen_start, fen) == 0);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 /* counter for fifty-move rule initialized with zero */
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 CX_TEST_ASSERT(gs.fifty_cntr_start == 0);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 /* we start after 53 half-moves have been played */
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 CX_TEST_ASSERT(gs.move_start == 53);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 /* we have no move history */
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 CX_TEST_ASSERT(gs.movecount == 0);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 CX_TEST_ASSERT(gs.movecapacity == 0);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 CX_TEST_ASSERT(gs.moves == NULL);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 CX_TEST_ASSERT(gs.fen == NULL);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 /* all castling rights should be revoked */
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 CX_TEST_ASSERT(gs.castling.K == true);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 CX_TEST_ASSERT(gs.castling.Q == true);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 CX_TEST_ASSERT(gs.castling.k == true);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 CX_TEST_ASSERT(gs.castling.q == true);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 /* check the board */
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 for (File f = 0 ; f < 8 ; f++) {
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 for (Rank r = 0 ; r < 8 ; r++) {
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 Piece exp = expected_board[r][f];
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 Piece p = piece_at(&gs, f, r);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 char msg[64];
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 sprintf(msg, "expected %u at %c:%c but got %u",
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 exp, filechr(f), rankchr(r), p);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 CX_TEST_ASSERTM(exp == p, msg);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 }
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 }
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 /* check that other stuff is empty-initialized */
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 CX_TEST_ASSERT(gs.bname[0] == 0);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 CX_TEST_ASSERT(gs.wname[0] == 0);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 CX_TEST_ASSERT(gs.premove[0] == 0);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 CX_TEST_ASSERT(!gs.checkmate);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 CX_TEST_ASSERT(!gs.stalemate);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 CX_TEST_ASSERT(!gs.threefold);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 CX_TEST_ASSERT(!gs.nomaterial);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 CX_TEST_ASSERT(!gs.remis);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 CX_TEST_ASSERT(!gs.wresign);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 CX_TEST_ASSERT(!gs.bresign);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 CX_TEST_ASSERT(!gs.ragequit);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 CX_TEST_ASSERT(!gs.review);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 /* check that we get the original FEN back */
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 char fen_chk[FEN_MAX_LENGTH];
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 fen_compute(fen_chk, &gs);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 CX_TEST_ASSERTM(strcmp(fen, fen_chk) == 0, fen_chk);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 }
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 gamestate_cleanup(&gs);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 }
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 CxTestSuite* test_fen_suite(void) {
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 CxTestSuite* suite = cx_test_suite_new("Test FEN API");
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 cx_test_register(suite, test_fen_basic);
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 // TODO: test FENs that describe definitely ended games (for each type of ending)
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 // TODO: test FEN that comes with a non-zero fifty-move rule counter
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 // TODO: test FEN with en-passant threat
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 return suite;
1e9751f8eb0d add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 }

mercurial