Mon, 07 Sep 2026 17:01:21 +0200
FEN parser: add test cases for stalemate and no-material positions
relates to #939
|
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); |
|
219
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
55 | /* it's black's turn */ |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
56 | CX_TEST_ASSERT(current_color(&gs) == BLACK); |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | /* 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
|
58 | 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
|
59 | 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
|
60 | 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
|
61 | 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
|
62 | /* 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
|
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 | 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
|
66 | 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
|
67 | /* check the board */ |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | 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
|
69 | 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
|
70 | 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
|
71 | 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
|
72 | char msg[64]; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | 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
|
74 | 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
|
75 | 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
|
76 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | /* 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
|
79 | 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
|
80 | 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
|
81 | 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
|
82 | 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
|
83 | 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
|
84 | 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
|
85 | 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
|
86 | 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
|
87 | 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
|
88 | 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
|
89 | 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
|
90 | 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
|
91 | /* 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
|
92 | 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
|
93 | 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
|
94 | 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
|
95 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | gamestate_cleanup(&gs); |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | } |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | |
|
219
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
99 | static CX_TEST(test_fen_with_castling_rights) { |
|
220
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
100 | /* this test contains castling rights and a non-zero fifty-moves counter */ |
|
219
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
101 | const char * fen = "1rbqk1nr/ppp2ppp/2np4/2b1p3/2P5/2N1P1P1/PP1P1PBP/R1BQK1NR w KQk - 1 6"; |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
102 | Board expected_board = { |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
103 | {WROOK, 0, WBISHOP, WQUEEN, WKING, 0, WKNIGHT, WROOK}, |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
104 | {WPAWN, WPAWN, 0, WPAWN, 0, WPAWN, WBISHOP, WPAWN}, |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
105 | {0, 0, WKNIGHT, 0, WPAWN, 0, WPAWN, 0}, |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
106 | {0, 0, WPAWN, 0, 0, 0, 0, 0}, |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
107 | {0, 0, BBISHOP, 0, BPAWN, 0, 0, 0}, |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
108 | {0, 0, BKNIGHT, BPAWN, 0, 0, 0, 0}, |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
109 | {BPAWN, BPAWN, BPAWN, 0, 0, BPAWN, BPAWN, BPAWN}, |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
110 | {0, BROOK, BBISHOP, BQUEEN, BKING, 0, BKNIGHT, BROOK} |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
111 | }; |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
112 | GameState gs; |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
113 | int result = fen_parse(fen, &gs); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
114 | CX_TEST_DO { |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
115 | CX_TEST_ASSERT(result == 0); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
116 | CX_TEST_ASSERT(strcmp(gs.fen_start, fen) == 0); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
117 | /* counter for fifty-move rule */ |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
118 | CX_TEST_ASSERT(gs.fifty_cntr_start == 1); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
119 | /* we start after 10 half-moves have been played */ |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
120 | CX_TEST_ASSERT(gs.move_start == 10); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
121 | /* it's white's turn */ |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
122 | CX_TEST_ASSERT(current_color(&gs) == WHITE); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
123 | /* we have no move history */ |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
124 | CX_TEST_ASSERT(gs.movecount == 0); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
125 | CX_TEST_ASSERT(gs.movecapacity == 0); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
126 | CX_TEST_ASSERT(gs.moves == NULL); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
127 | CX_TEST_ASSERT(gs.fen == NULL); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
128 | /* black queen-side castling right revoked */ |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
129 | CX_TEST_ASSERT(gs.castling.K == false); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
130 | CX_TEST_ASSERT(gs.castling.Q == false); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
131 | CX_TEST_ASSERT(gs.castling.k == false); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
132 | CX_TEST_ASSERT(gs.castling.q == true); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
133 | /* check the board */ |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
134 | for (File f = 0 ; f < 8 ; f++) { |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
135 | for (Rank r = 0 ; r < 8 ; r++) { |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
136 | Piece exp = expected_board[r][f]; |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
137 | Piece p = piece_at(&gs, f, r); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
138 | char msg[64]; |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
139 | sprintf(msg, "expected %u at %c:%c but got %u", |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
140 | exp, filechr(f), rankchr(r), p); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
141 | CX_TEST_ASSERTM(exp == p, msg); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
142 | } |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
143 | } |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
144 | /* check that other stuff is empty-initialized */ |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
145 | CX_TEST_ASSERT(gs.bname[0] == 0); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
146 | CX_TEST_ASSERT(gs.wname[0] == 0); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
147 | CX_TEST_ASSERT(gs.premove[0] == 0); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
148 | CX_TEST_ASSERT(!gs.checkmate); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
149 | CX_TEST_ASSERT(!gs.stalemate); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
150 | CX_TEST_ASSERT(!gs.threefold); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
151 | CX_TEST_ASSERT(!gs.nomaterial); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
152 | CX_TEST_ASSERT(!gs.remis); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
153 | CX_TEST_ASSERT(!gs.wresign); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
154 | CX_TEST_ASSERT(!gs.bresign); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
155 | CX_TEST_ASSERT(!gs.ragequit); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
156 | CX_TEST_ASSERT(!gs.review); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
157 | /* check that we get the original FEN back */ |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
158 | char fen_chk[FEN_MAX_LENGTH]; |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
159 | fen_compute(fen_chk, &gs); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
160 | CX_TEST_ASSERTM(strcmp(fen, fen_chk) == 0, fen_chk); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
161 | } |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
162 | gamestate_cleanup(&gs); |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
163 | } |
|
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
164 | |
|
220
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
165 | static CX_TEST(test_fen_checkmate) { |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
166 | /* this test contains a full game where white is checkmated */ |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
167 | const char * fen = "5rk1/1pp4p/p1n3p1/4b1N1/8/1P1N4/P1P3P1/qKB4R w - - 2 26"; |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
168 | GameState gs; |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
169 | int result = fen_parse(fen, &gs); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
170 | CX_TEST_DO { |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
171 | CX_TEST_ASSERT(result == 0); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
172 | CX_TEST_ASSERT(strcmp(gs.fen_start, fen) == 0); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
173 | CX_TEST_ASSERT(gs.fifty_cntr_start == 2); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
174 | CX_TEST_ASSERT(gs.move_start == 50); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
175 | CX_TEST_ASSERT(current_color(&gs) == WHITE); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
176 | CX_TEST_ASSERT(!gs.stalemate); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
177 | CX_TEST_ASSERT(!gs.threefold); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
178 | CX_TEST_ASSERT(!gs.nomaterial); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
179 | CX_TEST_ASSERT(!gs.remis); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
180 | CX_TEST_ASSERT(!gs.wresign); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
181 | CX_TEST_ASSERT(!gs.bresign); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
182 | CX_TEST_ASSERT(!gs.ragequit); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
183 | CX_TEST_ASSERT(!gs.review); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
184 | /* the checkmate flag must be set */ |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
185 | CX_TEST_ASSERT(gs.checkmate); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
186 | /* check that we get the original FEN back */ |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
187 | char fen_chk[FEN_MAX_LENGTH]; |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
188 | fen_compute(fen_chk, &gs); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
189 | CX_TEST_ASSERTM(strcmp(fen, fen_chk) == 0, fen_chk); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
190 | } |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
191 | gamestate_cleanup(&gs); |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
192 | } |
|
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
193 | |
|
222
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
194 | static CX_TEST(test_fen_stalemate) { |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
195 | const char * fen = "8/8/kBK5/P7/8/8/8/8 b - - 6 57"; |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
196 | GameState gs; |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
197 | int result = fen_parse(fen, &gs); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
198 | CX_TEST_DO { |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
199 | CX_TEST_ASSERT(result == 0); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
200 | CX_TEST_ASSERT(strcmp(gs.fen_start, fen) == 0); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
201 | CX_TEST_ASSERT(gs.fifty_cntr_start == 6); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
202 | CX_TEST_ASSERT(gs.move_start == 113); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
203 | CX_TEST_ASSERT(current_color(&gs) == BLACK); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
204 | CX_TEST_ASSERT(gs.stalemate); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
205 | CX_TEST_ASSERT(!gs.threefold); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
206 | CX_TEST_ASSERT(!gs.nomaterial); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
207 | CX_TEST_ASSERT(!gs.remis); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
208 | CX_TEST_ASSERT(!gs.wresign); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
209 | CX_TEST_ASSERT(!gs.bresign); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
210 | CX_TEST_ASSERT(!gs.ragequit); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
211 | CX_TEST_ASSERT(!gs.review); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
212 | CX_TEST_ASSERT(!gs.checkmate); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
213 | /* check that we get the original FEN back */ |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
214 | char fen_chk[FEN_MAX_LENGTH]; |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
215 | fen_compute(fen_chk, &gs); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
216 | CX_TEST_ASSERTM(strcmp(fen, fen_chk) == 0, fen_chk); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
217 | } |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
218 | gamestate_cleanup(&gs); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
219 | } |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
220 | |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
221 | static CX_TEST(test_fen_nomaterial) { |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
222 | const char * fen = "8/5K1k/8/8/8/8/8/8 w - - 0 77"; |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
223 | GameState gs; |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
224 | int result = fen_parse(fen, &gs); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
225 | CX_TEST_DO { |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
226 | CX_TEST_ASSERT(result == 0); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
227 | CX_TEST_ASSERT(strcmp(gs.fen_start, fen) == 0); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
228 | CX_TEST_ASSERT(gs.fifty_cntr_start == 0); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
229 | CX_TEST_ASSERT(gs.move_start == 152); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
230 | CX_TEST_ASSERT(current_color(&gs) == WHITE); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
231 | CX_TEST_ASSERT(!gs.stalemate); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
232 | CX_TEST_ASSERT(!gs.threefold); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
233 | CX_TEST_ASSERT(gs.nomaterial); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
234 | CX_TEST_ASSERT(!gs.remis); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
235 | CX_TEST_ASSERT(!gs.wresign); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
236 | CX_TEST_ASSERT(!gs.bresign); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
237 | CX_TEST_ASSERT(!gs.ragequit); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
238 | CX_TEST_ASSERT(!gs.review); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
239 | CX_TEST_ASSERT(!gs.checkmate); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
240 | /* check that we get the original FEN back */ |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
241 | char fen_chk[FEN_MAX_LENGTH]; |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
242 | fen_compute(fen_chk, &gs); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
243 | CX_TEST_ASSERTM(strcmp(fen, fen_chk) == 0, fen_chk); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
244 | } |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
245 | gamestate_cleanup(&gs); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
246 | } |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
247 | |
|
221
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
248 | static CX_TEST(test_fen_enpassant) { |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
249 | /* this test contains a full game where white is checkmated */ |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
250 | const char * fen = "rnQ4r/pp4kp/3p2pn/1BpPp1N1/5P2/2BK4/P1P3qP/8 w - e6 0 19"; |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
251 | GameState gs; |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
252 | int result = fen_parse(fen, &gs); |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
253 | CX_TEST_DO { |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
254 | CX_TEST_ASSERT(result == 0); |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
255 | /* verify that only e5 has an active enpassant threat */ |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
256 | for (File f = 0 ; f < 8 ; f++) { |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
257 | for (Rank r = 0 ; r < 8 ; r++) { |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
258 | if (f == fileidx('e') && r == rankidx('5')) { |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
259 | CX_TEST_ASSERT(enpassant_threat_exists(&gs, f, r)); |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
260 | } else { |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
261 | CX_TEST_ASSERT(!enpassant_threat_exists(&gs, f, r)); |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
262 | } |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
263 | } |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
264 | } |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
265 | |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
266 | /* check that we get the original FEN back */ |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
267 | char fen_chk[FEN_MAX_LENGTH]; |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
268 | fen_compute(fen_chk, &gs); |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
269 | CX_TEST_ASSERTM(strcmp(fen, fen_chk) == 0, fen_chk); |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
270 | } |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
271 | gamestate_cleanup(&gs); |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
272 | } |
|
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
273 | |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
274 | 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
|
275 | 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
|
276 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
277 | cx_test_register(suite, test_fen_basic); |
|
219
24b866230dd4
identify all and fix some occurrences where the new move_start offset matters
Mike Becker <universe@uap-core.de>
parents:
218
diff
changeset
|
278 | cx_test_register(suite, test_fen_with_castling_rights); |
|
220
04da225a5677
implement that fen_parse() sets the game-end flags correctly
Mike Becker <universe@uap-core.de>
parents:
219
diff
changeset
|
279 | cx_test_register(suite, test_fen_checkmate); |
|
222
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
280 | cx_test_register(suite, test_fen_stalemate); |
|
6388f9340a09
FEN parser: add test cases for stalemate and no-material positions
Mike Becker <universe@uap-core.de>
parents:
221
diff
changeset
|
281 | cx_test_register(suite, test_fen_nomaterial); |
|
221
338795f5837b
fixes parsing of en-passant threat in fen_parse()
Mike Becker <universe@uap-core.de>
parents:
220
diff
changeset
|
282 | cx_test_register(suite, test_fen_enpassant); |
|
218
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
283 | |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
284 | return suite; |
|
1e9751f8eb0d
add function to reconstruct a board from a FEN string
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
285 | } |