diff --git a/test/test_movegen.py b/test/test_movegen.py index f84ef4d..c8146ff 100644 --- a/test/test_movegen.py +++ b/test/test_movegen.py @@ -6,6 +6,7 @@ from test.chess_ffi import gen_white_pawn_capture_promotions from test.chess_ffi import gen_white_pawn_captures from test.chess_ffi import gen_knight_moves from test.chess_ffi import gen_bishop_moves +from test.chess_ffi import gen_rook_moves from test.chess_ffi import Move from test.chess_ffi import Board @@ -14,6 +15,22 @@ MAX_MOVES = 256 class TestPseudoMoveGeneration(ChessLibTestBase): + def run_subtests(self, cases, move_function, captures_only=None): + for fen, expected, msg in cases: + with self.subTest(msg=msg, fen=fen): + cnt = ctypes.c_int(0) + moves = (Move * MAX_MOVES)() + b = Board() + + self.load_fen(fen, board=b) + if captures_only is not None: + move_function(b, moves, ctypes.byref(cnt), captures_only) + else: + # Account for pawn move gen. + move_function(b, moves, ctypes.byref(cnt)) + self.assertEqual(expected, cnt.value) + + def test_knight_move_gen(self): all_move_types = [ ("8/8/8/8/3N4/8/8/8 w - - 0 1", 8, "center d4: 8 moves"), @@ -34,36 +51,15 @@ class TestPseudoMoveGeneration(ChessLibTestBase): ("8/8/8/8/8/1p6/2P5/N7 w - - 0 1", 1, "a1, b3 black (capture), c2 white (blocked)"), ] - captures_only = False - for fen, expected, msg in all_move_types: - with self.subTest(msg=msg, fen=fen): - cnt = ctypes.c_int(0) - moves = (Move * MAX_MOVES)() - b = Board() - - self.load_fen(fen, board=b) - gen_knight_moves(b, moves, ctypes.byref(cnt), captures_only) - - self.assertEqual(expected, cnt.value) - - cases_knight_captures = [ + captures_only = [ ("8/8/8/8/3N4/8/8/8 w - - 0 1", 0, "center d4 but no enemies"), ("8/8/2p5/1p6/3N4/5p2/8/8 w - - 0 1", 3, "d4 capturing c6,b5,f3"), ("8/8/8/8/3N4/1P3p2/4P3/8 w - - 0 1", 1, "d4 with f3 black (1 cap), b3/e2 white (not capturable)"), ("8/8/2p1p3/1p3p2/3N4/1p3p2/2p1p3/8 w - - 0 1", 8, "d4 with all 8 targets black: 8 captures"), ] + self.run_subtests(all_move_types, gen_knight_moves, captures_only=False) + self.run_subtests(captures_only, gen_knight_moves, captures_only=True) - captures_only = True - for fen, expected, msg in cases_knight_captures: - with self.subTest(msg=msg, fen=fen): - cnt = ctypes.c_int(0) - moves = (Move * MAX_MOVES)() - b = Board() - - self.load_fen(fen, board=b) - gen_knight_moves(b, moves, ctypes.byref(cnt), captures_only) - - self.assertEqual(expected, cnt.value) def test_bishop_move_gen(self): @@ -83,18 +79,6 @@ class TestPseudoMoveGeneration(ChessLibTestBase): ("8/8/1p3P2/8/3B4/4p3/1p6/8 w - - 0 1", 6, ""), ] - captures_only = False - for fen, expected, msg in cases_bishop_all: - with self.subTest(msg=msg, fen=fen): - cnt = ctypes.c_int(0) - moves = (Move * MAX_MOVES)() - b = Board() - - self.load_fen(fen, board=b) - gen_bishop_moves(b, moves, ctypes.byref(cnt), captures_only) - - self.assertEqual(expected, cnt.value) - cases_bishop_caps = [ ("8/8/8/8/3B4/8/8/8 w - - 0 1", 0, "no enemies to capture"), @@ -107,22 +91,47 @@ class TestPseudoMoveGeneration(ChessLibTestBase): # mixed: only two rays have a capture ("8/8/1p3P2/8/3B4/2P5/5p2/8 w - - 0 1", 2, "d4: NW capture b6, SE capture f2; NE/SW blocked by own"), ] - - captures_only = True - for fen, expected, msg in cases_bishop_caps: - with self.subTest(msg=msg, fen=fen): - cnt = ctypes.c_int(0) - moves = (Move * MAX_MOVES)() - b = Board() - - self.load_fen(fen, board=b) - gen_bishop_moves(b, moves, ctypes.byref(cnt), captures_only) - - self.assertEqual(expected, cnt.value) + self.run_subtests(cases_bishop_all, gen_bishop_moves, captures_only=False) + self.run_subtests(cases_bishop_caps, gen_bishop_moves, captures_only=True) def test_rook_move_gen(self): - pass + cases_rook_all = [ + ("8/8/8/8/3R4/8/8/8 w - - 0 1", 14, "center d4: 14 moves on empty board"), + ("8/8/8/8/R7/8/8/8 w - - 0 1", 14, "edge a4: still 14 on empty board"), + ("8/8/8/8/8/8/8/R7 w - - 0 1", 14, "corner a1: 14 on empty board"), + + # all four adjacents are own pieces → no squares available + ("8/8/8/3P4/2PRP3/3P4/8/8 w - - 0 1", 0, "d4 with white pawns on c4,d5,e4,d3 (fully blocked)"), + + # all four adjacents are enemy pieces → exactly 4 captures + ("8/8/8/3p4/2pRp3/3p4/8/8 w - - 0 1", 4, "d4 with black pawns on c4,d5,e4,d3 (adjacent captures)"), + + # mixed: horizontal blocked by own, vertical has one capture each way + ("8/8/3p4/8/2PRP3/8/3p4/8 w - - 0 1", 4, "d4; own c4/e4 block left/right; captures at d5,d3"), + + # two rooks on a1 and h1 block each other along rank 1 + ("8/8/8/8/8/8/8/R6R w - - 0 1", 26, "a1 and h1: 13 each (not 14) because they block each other on rank"), + ] + + cases_rook_caps = [ + ("8/8/8/8/3R4/8/8/8 w - - 0 1", 0, + "no enemies at all"), + + # adjacent enemies in all 4 directions → 4 captures + ("8/8/8/3p4/2pRp3/3p4/8/8 w - - 0 1", 4, + "adjacent captures at c4,d5,e4,d3"), + + # two far enemies on clear rays → 2 captures (h4 and d8) + ("3r4/8/8/8/3R3r/8/8/8 w - - 0 1", 2, + "captures on d8 and h4"), + + # mixed: own pieces block two rays; captures on the other two (a4 and d1) + ("8/8/8/3P4/p2RP3/8/8/3p4 w - - 0 1", 2, + "own on d5,e4; captures on a4 and d1"), + ] + self.run_subtests(cases_rook_all, gen_rook_moves, captures_only=False) + self.run_subtests(cases_rook_caps, gen_rook_moves, captures_only=True) def test_queen_move_gen(self): @@ -142,17 +151,7 @@ class TestPseudoMoveGeneration(ChessLibTestBase): # Although legal move, we have a separate function that calculates this move type. ("8/9P/8/8/8/8/8/8 w - - 0 1", 0, "no promotion push"), ] - - for fen, expected, msg in cases: - with self.subTest(msg=msg, fen=fen): - cnt = ctypes.c_int(0) - moves = (Move * MAX_MOVES)() - b = Board() - - self.load_fen(fen, board=b) - gen_white_pawn_quiet_pushes(b, moves, ctypes.byref(cnt)) - - self.assertEqual(expected, cnt.value) + self.run_subtests(cases, gen_white_pawn_quiet_pushes) def test_quiet_pawn_promotions_white(self): @@ -162,17 +161,7 @@ class TestPseudoMoveGeneration(ChessLibTestBase): ("7n/7P/8/8/8/8/8/8 w - - 0 1", 0, "blocked by enemy"), ("7N/7P/8/8/8/8/8/8 w - - 0 1", 0, "blocked by friendly"), ] - - for fen, expected, msg in cases: - with self.subTest(msg=msg, fen=fen): - cnt = ctypes.c_int(0) - moves = (Move * MAX_MOVES)() - b = Board() - - self.load_fen(fen, board=b) - gen_white_pawn_push_promotions(b, moves, ctypes.byref(cnt)) - - self.assertEqual(expected, cnt.value) + self.run_subtests(cases, gen_white_pawn_push_promotions) def test_capture_pawn_promotions_white(self): @@ -181,17 +170,7 @@ class TestPseudoMoveGeneration(ChessLibTestBase): ("5n1n/6P1/8/8/8/8/8/8 w - - 0 1", 2, "two captures"), ("8/7P/8/8/8/8/8/8 w - - 0 1", 0, "no capture"), ] - - for fen, expected, msg in cases: - with self.subTest(msg=msg, fen=fen): - cnt = ctypes.c_int(0) - moves = (Move * MAX_MOVES)() - b = Board() - - self.load_fen(fen, board=b) - gen_white_pawn_capture_promotions(b, moves, ctypes.byref(cnt)) - - self.assertEqual(expected, cnt.value) + self.run_subtests(cases, gen_white_pawn_capture_promotions) def test_capture_pawn_white(self): @@ -207,14 +186,4 @@ class TestPseudoMoveGeneration(ChessLibTestBase): ("8/8/5n2/3pP3/8/8/8/8 w - d6 0 1", 2, "EP e5xd6 and normal e5xf6"), ("8/8/8/3p4/8/4P3/8/8 w - d6 0 1", 0, "EP square present but no white pawn can take"), ] - - for fen, expected, msg in cases: - with self.subTest(msg=msg, fen=fen): - cnt = ctypes.c_int(0) - moves = (Move * MAX_MOVES)() - b = Board() - - self.load_fen(fen, board=b) - gen_white_pawn_captures(b, moves, ctypes.byref(cnt)) - - self.assertEqual(expected, cnt.value) \ No newline at end of file + self.run_subtests(cases, gen_white_pawn_captures) \ No newline at end of file