From 63039394e4f8ef056289805bcee6ad1e7519de0a Mon Sep 17 00:00:00 2001 From: "Peter B." Date: Sat, 25 Jul 2026 22:10:18 +0200 Subject: implement pawn movement logic and add unit tests for pawn moves --- tests/pawn_moves_test.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/pawn_moves_test.cpp (limited to 'tests') diff --git a/tests/pawn_moves_test.cpp b/tests/pawn_moves_test.cpp new file mode 100644 index 0000000..47b8ceb --- /dev/null +++ b/tests/pawn_moves_test.cpp @@ -0,0 +1,54 @@ +#include "moves.hpp" +#include + +bool HasMove(const std::vector &moves, int from, int to) { + Position expectedFrom = IndexToPosition(from); + Position expectedTo = IndexToPosition(to); + for (const Move &move : moves) { + if (move.From.rank == expectedFrom.rank && + move.From.file == expectedFrom.file && move.To.rank == expectedTo.rank && + move.To.file == expectedTo.file) { + return true; + } + } + return false; +} + +int CountMovesFrom(const std::vector &moves, int from) { + Position expected = IndexToPosition(from); + int count = 0; + for (const Move &move : moves) { + if (move.From.rank == expected.rank && move.From.file == expected.file) { + count++; + } + } + return count; +} + +int main() { + board white{}; + white.turn = true; + white.pieces[52] = createPiece(PAWN, true); // e2 + white.pieces[43] = createPiece(ROOK, false); // d3 + white.pieces[45] = createPiece(ROOK, true); // f3 + + std::vector whiteMoves = GetLegalMoves(&white); + assert(CountMovesFrom(whiteMoves, 52) == 3); + assert(HasMove(whiteMoves, 52, 44)); // e2-e3 + assert(HasMove(whiteMoves, 52, 36)); // e2-e4 + assert(HasMove(whiteMoves, 52, 43)); // e2xd3 + + white.pieces[44] = createPiece(KING, true); // block e3 and e4 + std::vector blockedMoves = GetLegalMoves(&white); + assert(CountMovesFrom(blockedMoves, 52) == 1); + assert(HasMove(blockedMoves, 52, 43)); // capture remains legal + + board black{}; + black.turn = false; + black.pieces[12] = createPiece(PAWN, false); // e7 + + std::vector blackMoves = GetLegalMoves(&black); + assert(CountMovesFrom(blackMoves, 12) == 2); + assert(HasMove(blackMoves, 12, 20)); // e7-e6 + assert(HasMove(blackMoves, 12, 28)); // e7-e5 +} -- cgit v1.2.3