diff options
| author | Peter B. <peter.bezdek@gmail.com> | 2026-07-25 22:10:18 +0200 |
|---|---|---|
| committer | Peter B. <peter.bezdek@gmail.com> | 2026-07-25 22:10:18 +0200 |
| commit | 63039394e4f8ef056289805bcee6ad1e7519de0a (patch) | |
| tree | 33e88a5bec7037042f9a38ab5940f066fda9c414 /tests | |
| parent | e9c8925045ff30c9af08a74000a634559878e96e (diff) | |
implement pawn movement logic and add unit tests for pawn moves
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/pawn_moves_test.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
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 <cassert> + +bool HasMove(const std::vector<Move> &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<Move> &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<Move> 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<Move> 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<Move> blackMoves = GetLegalMoves(&black); + assert(CountMovesFrom(blackMoves, 12) == 2); + assert(HasMove(blackMoves, 12, 20)); // e7-e6 + assert(HasMove(blackMoves, 12, 28)); // e7-e5 +} |
