Fix attack to tests

This commit is contained in:
2025-08-22 14:28:48 -04:00
parent f3b6b7448e
commit 5ddb359675
3 changed files with 52 additions and 54 deletions

View File

@@ -1,4 +1,5 @@
import unittest
from binding.python_c_ffi import ChessFFI
from test.chess_ffi import Board
from test.chess_ffi import KING_ATTACKS
from test.chess_ffi import KNIGHT_ATTACKS
@@ -11,6 +12,9 @@ from test.chess_ffi import load_fen
class ChessLibTestBase(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.chess_ffi = ChessFFI()
init_attack_caches()
cls.KNIGHT_ATTACKS = KNIGHT_ATTACKS
@@ -25,8 +29,8 @@ class ChessLibTestBase(unittest.TestCase):
def load_fen(self, fen, board=None):
if board:
return load_fen(board, fen)
return load_fen(self.board, fen)
return self.chess_ffi.load_fen(board, fen)
return self.chess_ffi.load_fen(self.board, fen)
def gen(self, captures_only: bool = False, cap: int = 256):

View File

@@ -1,13 +1,8 @@
import ctypes
from binding.python_c_ffi import Board
from binding.python_c_ffi import sq
from binding.python_c_ffi import BLACK
from binding.python_c_ffi import WHITE
from test.base import ChessLibTestBase
from test.chess_ffi import get_attackers_to
from test.chess_ffi import is_square_attacked
from test.chess_ffi import is_in_check
from test.chess_ffi import sq
from test.chess_ffi import Board
from test.chess_ffi import BLACK
from test.chess_ffi import WHITE
from test.chess_ffi import draw_bb
class TestAttackers(ChessLibTestBase):
@@ -50,8 +45,8 @@ class TestAttackers(ChessLibTestBase):
for fen, sq_str, by, expected, msg in cases:
with self.subTest(msg=msg, fen=fen, sq=sq_str, by=by):
b = Board()
self.load_fen(fen, board=b)
got = bool(is_square_attacked(b, sq(sq_str), by))
self.chess_ffi.load_fen(b, fen)
got = self.chess_ffi.square_attacked(b, sq(sq_str), by)
self.assertEqual(expected, got, msg)
@@ -81,8 +76,8 @@ class TestAttackers(ChessLibTestBase):
for fen, side, expected, msg in cases:
with self.subTest(msg=msg, fen=fen, side=side):
b = Board()
self.load_fen(fen, board=b)
actual = bool(is_in_check(b, side))
self.chess_ffi.load_fen(b, fen)
actual = self.chess_ffi.in_check(b, side)
self.assertEqual(expected, actual, msg)
@@ -132,7 +127,6 @@ class TestAttackers(ChessLibTestBase):
for fen, sq_str, by, expected_cnt, msg in cases:
with self.subTest(msg=msg, fen=fen, sq=sq_str, by=by):
b = Board()
self.load_fen(fen, board=b)
mask = get_attackers_to(b, sq(sq_str), by)
self.chess_ffi.load_fen(b, fen)
mask = self.chess_ffi.attackers_to(b, sq(sq_str), by)
self.assertEqual(expected_cnt, int(mask).bit_count())