aboutsummaryrefslogtreecommitdiff
path: root/tests/pawn_moves_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/pawn_moves_test.cpp')
-rw-r--r--tests/pawn_moves_test.cpp54
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
+}