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