--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/test-fen.c Fri Sep 04 13:49:48 2026 +0200 @@ -0,0 +1,106 @@ +/* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2026 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "chess/fen.h" +#include "ucxtest.h" + +static CX_TEST(test_fen_basic) { + /* a test that simply checks if an arbitrary FEN is parsed correctly */ + const char * fen = "r1n3k1/5qbp/p5p1/1p6/4Q3/B6P/P4PP1/R4RK1 b - - 0 27"; + Board expected_board = { + {WROOK, 0, 0, 0, 0, WROOK, WKING, 0}, + {WPAWN, 0, 0, 0, 0, WPAWN, WPAWN, 0}, + {WBISHOP, 0, 0, 0, 0, 0, 0, WPAWN}, + {0, 0, 0, 0, WQUEEN, 0, 0, 0}, + {0, BPAWN, 0, 0, 0, 0, 0, 0}, + {BPAWN, 0, 0, 0, 0, 0, BPAWN, 0}, + {0, 0, 0, 0, 0, BQUEEN, BBISHOP, BPAWN}, + {BROOK, 0, BKNIGHT, 0, 0, 0, BKING, 0} + }; + 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 initialized with zero */ + CX_TEST_ASSERT(gs.fifty_cntr_start == 0); + /* we start after 53 half-moves have been played */ + CX_TEST_ASSERT(gs.move_start == 53); + /* 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); + /* all castling rights should be revoked */ + CX_TEST_ASSERT(gs.castling.K == true); + CX_TEST_ASSERT(gs.castling.Q == true); + CX_TEST_ASSERT(gs.castling.k == true); + 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); + // 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; +}