Files
chess/test/test_movegen.py
Josh 6005741b10
All checks were successful
Python tests (make) / test (push) Successful in 14s
Add python c binding class interface (#29)
Reviewed-on: #29
Co-authored-by: Josh <josh@joshuaschuett.com>
Co-committed-by: Josh <josh@joshuaschuett.com>
2025-08-22 19:28:03 +00:00

274 lines
13 KiB
Python

import ctypes
from binding.python_c_ffi import Board
from binding.python_c_ffi import Move
from test.base import ChessLibTestBase
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.chess_ffi.load_fen(b , fen)
if captures_only is not None:
move_function(b, moves, cnt, captures_only)
else:
# Account for pawn move gen.
move_function(b, moves, 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"),
("8/8/8/8/N7/8/8/8 w - - 0 1", 4, "edge a4: 4 moves"),
("8/8/8/8/8/8/8/N7 w - - 0 1", 2, "corner a1: 2 moves"),
("8/8/8/8/8/8/8/1N6 w - - 0 1", 3, "b1: 3 moves"),
# own pieces on destinations reduce count
("8/8/8/5P2/3N4/1P6/2P5/8 w - - 0 1", 5, "d4 with own pawns on f5,b3,c2 -> 8-3=5"),
# two knights
("8/8/8/8/8/8/8/1N4N1 w - - 0 1", 6, "b1 & g1, empty board: 3+3"),
# all 8 destinations occupied by black (still 8 total moves, all as captures)
("8/8/2p1p3/1p3p2/3N4/1p3p2/2p1p3/8 w - - 0 1", 8, "enemy on all 8 targets"),
# corner with one capture and one friendly block
("8/8/8/8/8/1p6/2P5/N7 w - - 0 1", 1, "a1, b3 black (capture), c2 white (blocked)"),
]
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, self.chess_ffi._c_gen_knight_moves, captures_only=False)
self.run_subtests(captures_only, self.chess_ffi._c_gen_knight_moves, captures_only=True)
def test_bishop_move_gen(self):
cases_bishop_all = [
("8/8/8/8/3B4/8/8/8 w - - 0 1", 13, "center d4: 13 moves"),
("8/8/8/8/B7/8/8/8 w - - 0 1", 7, "edge a4: 7 moves"),
("8/8/8/8/8/8/8/B7 w - - 0 1", 7, "corner a1: 7 moves"),
("8/8/8/8/8/8/8/1B6 w - - 0 1", 7, "b1: 7 moves"),
# all four diagonals blocked immediately by own pieces -> 0
("8/8/8/2P1P3/3B4/2P1P3/8/8 w - - 0 1", 0, "d4 with white pawns on c3,e3,c5,e5"),
# all four diagonals have a black piece on the first square -> 4 (all captures)
("8/8/8/2p1p3/3B4/2p1p3/8/8 w - - 0 1", 4, "d4 with black pawns on c3,e3,c5,e5"),
# mixed blockers: NE own on f6, SW capture on b2, SE capture on e3, NW open
("8/8/1p3P2/8/3B4/4p3/1p6/8 w - - 0 1", 6, ""),
]
cases_bishop_caps = [
("8/8/8/8/3B4/8/8/8 w - - 0 1", 0, "no enemies to capture"),
# first squares on all rays are enemies → 4 captures
("8/8/8/2p1p3/3B4/2p1p3/8/8 w - - 0 1", 4, "adjacent caps on c3,e3,c5,e5"),
# enemies at far ends on each ray → still 4 captures (one per ray)
("7p/p7/8/8/3B4/8/8/p5p1 w - - 0 1", 4, "targets h8,a7,g1,a1"),
# 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"),
]
self.run_subtests(cases_bishop_all, self.chess_ffi._c_gen_bishop_moves, captures_only=False)
self.run_subtests(cases_bishop_caps, self.chess_ffi._c_gen_bishop_moves, captures_only=True)
def test_rook_move_gen(self):
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, self.chess_ffi._c_gen_rook_moves, captures_only=False)
self.run_subtests(cases_rook_caps, self.chess_ffi._c_gen_rook_moves, captures_only=True)
def test_queen_move_gen(self):
cases_queen_all = [
("8/8/8/8/3Q4/8/8/8 w - - 0 1", 27, "center d4: rook(14)+bishop(13)"),
("8/8/8/8/Q7/8/8/8 w - - 0 1", 21, "edge a4: 14+7"),
("8/8/8/8/8/8/8/Q7 w - - 0 1", 21, "corner a1: 14+7"),
("8/8/8/8/8/8/8/1Q6 w - - 0 1", 21, "b1: 14+7"),
# all 8 adjacent squares are friendly ⇒ blocked immediately on all rays
("8/8/8/2PPP3/2PQP3/2PPP3/8/8 w - - 0 1", 0, "d4 with white pawns on c3,d3,e3,c4,e4,c5,d5,e5"),
# all 8 adjacent squares are enemies ⇒ exactly 8 captures (one per ray), no further squares
("8/8/8/2p1p3/2pQp3/2p1p3/8/8 w - - 0 1", 13, "d4 with black pawns adjacent on all rays"),
# only horizontal blocked by own pieces at c4/e4; vertical+diagonals open
("8/8/8/8/2PQP3/8/8/8 w - - 0 1", 20, "d4, own at c4,e4: rook loses 7→7 (up+down only), bishop 13 ⇒ 20"),
# two queens on a1 & h1 block each other along rank 1
("8/8/8/8/8/8/8/Q6Q w - - 0 1", 40, "a1 & h1: each 21→20 due to mutual block ⇒ 40 total"),
]
cases_queen_caps = [
("8/8/8/8/3Q4/8/8/8 w - - 0 1", 0, "no enemies to capture"),
# adjacent enemies only → 6 captures
("8/8/8/2p1p3/2pQp3/2p1p3/8/8 w - - 0 1", 6, "adjacent caps on all rays"),
# adjacent enemies only → 8 captures
("8/8/8/2ppp3/2pQp3/2ppp3/8/8 w - - 0 1", 8, "adjacent caps on all rays"),
# far targets on two rook rays (d8 and h4) + friendly blocking b1
("3r4/8/8/8/1P1Q3r/8/8/8 w - - 0 1", 2, "captures on d8 and h4; b4 friend blocks left; no other enemies"),
# mixed: captures on two diagonals only
("7p/p7/8/8/3Q4/8/8/p5p1 w - - 0 1", 4, "diagonal targets a1,a7,g1,h8 (4 captures)"),
]
self.run_subtests(cases_queen_all, self.chess_ffi._c_gen_queen_moves, captures_only=False)
self.run_subtests(cases_queen_caps, self.chess_ffi._c_gen_queen_moves, captures_only=True)
def test_king_move_gen(self):
cases_king_all = [
("8/8/8/8/3K4/8/8/8 w - - 0 1", 8, "center d4: 8 moves"),
("8/8/8/8/K7/8/8/8 w - - 0 1", 5, "edge a4: 5 moves"),
("8/8/8/8/8/8/8/K7 w - - 0 1", 3, "corner a1: 3 moves"),
# all eight adjacent squares are WHITE pieces → 0
("8/8/8/2PPP3/2PKP3/2PPP3/8/8 w - - 0 1", 0, "surrounded by own"),
# all eight adjacent squares are BLACK pieces → 8 (all captures)
("8/8/8/2ppp3/2pKp3/2ppp3/8/8 w - - 0 1", 8, "surrounded by enemies"),
# mixed: own block east/west; captures NE & SE; rest quiet
("8/8/8/2p5/2PKP3/4p3/8/8 w - - 0 1", 6, "d4: caps c5,e3; quiet d5,d3,c3,e5"),
]
cases_king_caps = [
("8/8/8/8/3K4/8/8/8 w - - 0 1", 0, "no enemies"),
# eight adjacent enemies → 8 captures
("8/8/8/2ppp3/2pKp3/2ppp3/8/8 w - - 0 1", 8, "all adjacent caps"),
# edge a1 with only b2 enemy → 1 capture
("8/8/8/8/8/8/1p6/K7 w - - 0 1", 1, "a1: only Kxb2"),
# mixed: same as above mixed case → 2 captures
("8/8/8/2p5/2PKP3/4p3/8/8 w - - 0 1", 2, "captures c5 & e3"),
]
cases_king_castle_white = [
("8/8/8/8/8/8/8/R3K2R w KQ - 0 1", 7, "e1 with KQ rights, empty lanes: 5 king steps + O-O + O-O-O"),
("8/8/8/8/8/8/8/4K2R w K - 0 1", 6, "e1 with K only: 5 steps + O-O"),
("8/8/8/8/8/8/8/R3K3 w Q - 0 1", 6, "e1 with Q only: 5 steps + O-O-O"),
# blocked kingside (f1 occupied) → only O-O-O available
("8/8/8/8/8/8/8/R3KB1R w KQ - 0 1", 5, "f1 blocked by own piece; queen-side castle only"),
# blocked queenside (d1 occupied) → only O-O available
("8/8/8/8/8/8/8/R2BK2R w KQ - 0 1", 5, "d1 blocked by own piece; king-side castle only"),
# both sides blocked (d1 and f1 occupied) → no castles, just 5 normal moves
("8/8/8/8/8/8/8/R2BKB1R w KQ - 0 1", 3, "both lanes blocked; no castles"),
]
cases_king_castle_black = [
("r3k2r/8/8/8/8/8/8/8 b kq - 0 1", 7, "e8 with kq rights, empty lanes: 5 steps + O-O + O-O-O"),
("4k2r/8/8/8/8/8/8/8 b k - 0 1", 6, "e8 with k only: 5 steps + O-O"),
("r3k3/8/8/8/8/8/8/8 b q - 0 1", 6, "e8 with q only: 5 steps + O-O-O"),
# blocked kingside (f8 occupied) → only O-O-O available
("r3kb1r/8/8/8/8/8/8/8 b kq - 0 1", 5, "f8 blocked; only queen-side castle"),
# blocked queenside (d8 occupied) → only O-O available
("r2bk2r/8/8/8/8/8/8/8 b kq - 0 1", 5, "d8 blocked; only king-side castle"),
]
self.run_subtests(cases_king_all, self.chess_ffi._c_gen_king_moves, captures_only=False)
self.run_subtests(cases_king_castle_white, self.chess_ffi._c_gen_king_moves, captures_only=False)
self.run_subtests(cases_king_caps, self.chess_ffi._c_gen_king_moves, captures_only=True)
self.run_subtests(cases_king_castle_black, self.chess_ffi._c_gen_king_moves, captures_only=False)
def test_quiet_pawn_pushes_white(self):
cases = [
("8/pppppppp/8/8/8/8/PPPPPPPP/8 w - - 0 1", 16, "open ranks"),
("8/8/8/8/8/8/P7/8 w - - 0 1", 2, "single pawn open"),
("8/8/8/8/8/p7/P7/8 w - - 0 1", 0, "blocked single enemy"),
("8/8/8/8/8/n7/P7/8 w - - 0 1", 0, "blocked single friendly"),
# 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"),
]
self.run_subtests(cases, self.chess_ffi._c_gen_white_pawn_quiet_pushes)
def test_quiet_pawn_promotions_white(self):
cases = [
("8/PPPPPPPP/8/8/8/8/8/8 w - - 0 1", 32, "all open on 7th rank"),
("8/8/7P/8/8/8/8/8 w - - 0 1", 0, "no promotions"),
("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"),
]
self.run_subtests(cases, self.chess_ffi._c_gen_white_pawn_push_promotions)
def test_capture_pawn_promotions_white(self):
cases = [
("6n1/7P/8/8/8/8/8/8 w - - 0 1", 4, "one capture"),
("5n1n/6P1/8/8/8/8/8/8 w - - 0 1", 8, "two captures"),
("8/7P/8/8/8/8/8/8 w - - 0 1", 0, "no capture"),
]
self.run_subtests(cases, self.chess_ffi._c_gen_white_pawn_capture_promotions)
def test_capture_pawn_white(self):
cases = [
# normal diagonal captures
("8/8/8/3n4/4P3/8/8/8 w - - 0 1", 1, "e4xd5"),
("8/8/8/3n1n2/4P3/8/8/8 w - - 0 1", 2, "e4xd5 and e4xf5"),
("8/8/8/1p6/P7/8/8/8 w - - 0 1", 1, "edge file: a4xb5"),
("8/7P/8/8/8/8/8/8 w - - 0 1", 0, "no capture"),
# en passant
("8/8/8/3pP3/8/8/8/8 w - d6 0 1", 1, "EP available: e5xd6 e.p. (black pawn on d5)"),
("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"),
]
self.run_subtests(cases, self.chess_ffi._c_gen_white_pawn_captures)