add-piece-move-cache (#1)
Reviewed-on: #1 Co-authored-by: Josh <josh@joshuaschuett.com> Co-committed-by: Josh <josh@joshuaschuett.com>
This commit is contained in:
0
test/__init__.py
Normal file
0
test/__init__.py
Normal file
79
test/base.py
Normal file
79
test/base.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import ctypes
|
||||
import platform
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
WHITE, BLACK = 0, 1
|
||||
FILES = {c:i for i,c in enumerate("abcdefgh")}
|
||||
|
||||
|
||||
def sq(name: str) -> int:
|
||||
f = FILES[name[0].lower()]
|
||||
r = int(name[1]) - 1
|
||||
return f + 8 * r
|
||||
|
||||
|
||||
def bb_from(*algebraic):
|
||||
m = 0
|
||||
for s in algebraic:
|
||||
m |= (1 << sq(s))
|
||||
return m
|
||||
|
||||
|
||||
def popcount(x: int) -> int:
|
||||
return x.bit_count()
|
||||
|
||||
|
||||
def draw_bb(mask: int, origin: int | None = None) -> str:
|
||||
print("\n")
|
||||
lines = []
|
||||
for r in range(7, -1, -1):
|
||||
row = []
|
||||
for f in range(8):
|
||||
sqi = r * 8 + f
|
||||
bit = (mask >> sqi) & 1
|
||||
if origin is not None and sqi == origin:
|
||||
ch = 'O'
|
||||
elif bit:
|
||||
ch = 'x'
|
||||
else:
|
||||
ch = '.'
|
||||
row.append(ch)
|
||||
lines.append(f"{r+1} " + " ".join(row))
|
||||
lines.append(" " + " ".join(FILES))
|
||||
lines = "\n".join(lines)
|
||||
print(lines, "\n")
|
||||
|
||||
|
||||
class ChessLibTestBase(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
libname = "libchess.so"
|
||||
libpath = root / "build" / libname
|
||||
cls.lib = ctypes.CDLL(str(libpath))
|
||||
|
||||
attack_cache_functions = [
|
||||
"create_knight_attack_cache",
|
||||
"create_pawn_attack_cache",
|
||||
"create_king_attack_cache",
|
||||
]
|
||||
|
||||
# init functions
|
||||
for fn in attack_cache_functions:
|
||||
getattr(cls.lib, fn).argtypes = []
|
||||
getattr(cls.lib, fn).restype = None
|
||||
|
||||
cls.lib.create_knight_attack_cache()
|
||||
cls.lib.create_pawn_attack_cache()
|
||||
cls.lib.create_king_attack_cache()
|
||||
|
||||
KnightArr = ctypes.c_uint64 * 64
|
||||
KingArr = ctypes.c_uint64 * 64
|
||||
PawnRow = ctypes.c_uint64 * 64
|
||||
PawnArr = PawnRow * 2
|
||||
|
||||
cls.KNIGHT_ATTACKS = KnightArr.in_dll(cls.lib, "KNIGHT_ATTACKS")
|
||||
cls.KING_ATTACKS = KingArr.in_dll(cls.lib, "KING_ATTACKS")
|
||||
cls.PAWN_ATTACKS = PawnArr.in_dll(cls.lib, "PAWN_ATTACKS")
|
||||
83
test/test_piece_attack_cache.py
Normal file
83
test/test_piece_attack_cache.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from test.base import ChessLibTestBase
|
||||
from test.base import bb_from
|
||||
from test.base import draw_bb
|
||||
from test.base import sq
|
||||
from test.base import BLACK, WHITE
|
||||
|
||||
|
||||
class KnightFixedCases(ChessLibTestBase):
|
||||
def test_knight_a1(self):
|
||||
expected = bb_from("b3", "c2")
|
||||
self.assertEqual(int(self.KNIGHT_ATTACKS[sq("a1")]), expected)
|
||||
|
||||
|
||||
def test_knight_d4(self):
|
||||
expected = bb_from("b5","b3","c6","e6","f5","f3","c2","e2")
|
||||
actual = int(self.KNIGHT_ATTACKS[sq("d4")])
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
def test_knight_h8(self):
|
||||
expected = bb_from("f7","g6")
|
||||
actual = int(self.KNIGHT_ATTACKS[sq("h8")])
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
class PawnFixedCases(ChessLibTestBase):
|
||||
def test_white_pawn_a2(self):
|
||||
expected = bb_from("b3")
|
||||
actual = int(self.PAWN_ATTACKS[WHITE][sq("a2")])
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
def test_white_pawn_b2(self):
|
||||
expected = bb_from("a3", "c3")
|
||||
actual = int(self.PAWN_ATTACKS[WHITE][sq("b2")])
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
def test_black_pawn_a2(self):
|
||||
expected = bb_from("b6")
|
||||
actual = int(self.PAWN_ATTACKS[BLACK][sq("a7")])
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
def test_black_pawn_b2(self):
|
||||
expected = bb_from("a6", "c6")
|
||||
actual = int(self.PAWN_ATTACKS[BLACK][sq("b7")])
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
class KingFixedCases(ChessLibTestBase):
|
||||
def test_king_a1(self):
|
||||
expected = bb_from("a2", "b1", "b2")
|
||||
actual = int(self.KING_ATTACKS[sq("a1")])
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
def test_king_a8(self):
|
||||
expected = bb_from("a7", "b8", "b7")
|
||||
actual = int(self.KING_ATTACKS[sq("a8")])
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
def test_king_h1(self):
|
||||
expected = bb_from("h2", "g1", "g2")
|
||||
actual = int(self.KING_ATTACKS[sq("h1")])
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
def test_king_h8(self):
|
||||
expected = bb_from("h7", "g7", "g8")
|
||||
actual = int(self.KING_ATTACKS[sq("h8")])
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
||||
def test_king_d4(self):
|
||||
expected = bb_from(
|
||||
"c3", "d3", "e3",
|
||||
"c4", "e4",
|
||||
"c5", "d5", "e5",
|
||||
)
|
||||
actual = int(self.KING_ATTACKS[sq("d4")])
|
||||
self.assertEqual(actual, expected)
|
||||
Reference in New Issue
Block a user