Skip to content

Commit b15299a

Browse files
committed
test: registering each perft depth as an individual test case
1 parent 1b0057b commit b15299a

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

tests/perft/CMakeLists.txt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,29 @@ file (GLOB testcase_files LIST_DIRECTORIES false CONFIGURE_DEPENDS
2323
foreach (data_file IN LISTS testcase_files)
2424
cmake_path (GET data_file STEM filename)
2525

26-
set (test_name "ben_bot.perft.${filename}")
26+
file (READ "${data_file}" file_content)
2727

28-
add_test (NAME "${test_name}" COMMAND Python::Interpreter "${CMAKE_CURRENT_LIST_DIR}/perft.py"
29-
"--test=${data_file}" "--engine=$<TARGET_FILE:ben_bot>"
30-
)
28+
string (JSON num_depths LENGTH "${file_content}" depths)
29+
30+
# because foreach(RANGE) includes the boundary value
31+
math (EXPR num_depths "${num_depths} - 1")
32+
33+
foreach (depth_idx RANGE 0 "${num_depths}")
34+
# cmake-format: off
35+
string (
36+
JSON
37+
depth GET
38+
"${file_content}"
39+
depths "${depth_idx}" depth
40+
)
41+
# cmake-format: on
42+
43+
add_test (
44+
NAME "ben_bot.perft.${filename}.${depth}"
45+
COMMAND Python::Interpreter "${CMAKE_CURRENT_LIST_DIR}/perft.py" "--test=${data_file}"
46+
"--engine=$<TARGET_FILE:ben_bot>" "--depth=${depth}"
47+
)
48+
endforeach ()
49+
50+
set_property (DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${data_file}")
3151
endforeach ()

tests/perft/perft.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@
1313
import json
1414
import subprocess
1515
from pathlib import Path
16-
from typing import Tuple
16+
from typing import Tuple, Any
1717

1818
#
1919

2020

21+
def find_correct_result(fileContent: Any, depth: int) -> dict[str, int]:
22+
for obj in fileContent["depths"]:
23+
if obj["depth"] == depth:
24+
return obj["results"]
25+
26+
print(f"ERROR! Could not find correct result for depth {depth}")
27+
exit(1)
28+
29+
2130
def check_result(expected: dict[str, int], actual: dict[str, int]) -> bool:
2231
for key in (
2332
"totalNodes",
@@ -36,7 +45,7 @@ def check_result(expected: dict[str, int], actual: dict[str, int]) -> bool:
3645
return True
3746

3847

39-
def parse_args() -> Tuple[Path, Path]:
48+
def parse_args() -> Tuple[Path, Path, int]:
4049
parser = argparse.ArgumentParser(
4150
prog="RunPerft",
4251
description="Run BenBot perft tests",
@@ -49,10 +58,11 @@ def parse_args() -> Tuple[Path, Path]:
4958
parser.add_argument(
5059
"-e", "--engine", required=True, help="Path to engine executable"
5160
)
61+
parser.add_argument("-d", "--depth", required=True, help="Perft depth")
5262

5363
parsed = parser.parse_args()
5464

55-
return Path(parsed.test).resolve(), Path(parsed.engine).resolve()
65+
return Path(parsed.test).resolve(), Path(parsed.engine).resolve(), int(parsed.depth)
5666

5767

5868
#
@@ -100,34 +110,29 @@ def quit(self):
100110

101111
#
102112

103-
TESTCASE_FILE, ENGINE_PATH = parse_args()
113+
TESTCASE_FILE, ENGINE_PATH, DEPTH = parse_args()
104114

105115
with open(TESTCASE_FILE) as file:
106-
CORRECT_DATA = json.load(file)
116+
FILE_DATA = json.load(file)
117+
118+
startingFEN = FILE_DATA["position"]
119+
120+
print(f"FEN: {startingFEN}", flush=True)
107121

108-
startingFEN = CORRECT_DATA["position"]
122+
correctResult = find_correct_result(FILE_DATA, DEPTH)
109123

110-
print(f"Running tests for position {startingFEN}", flush=True)
124+
print(f"Running perft depth {DEPTH}...", flush=True)
111125

112126
engine = Engine(engine_path=ENGINE_PATH, pos_fen=startingFEN)
113127

114128
num_failed = 0
115129
num_passed = 0
116130

117-
for depthObj in CORRECT_DATA["depths"]:
118-
depth = depthObj["depth"]
119-
print(f"Running perft depth {depth}...", flush=True)
120-
121-
result = engine.run_perft(depth)
122-
123-
if check_result(depthObj["results"], result):
124-
num_passed += 1
125-
else:
126-
num_failed += 1
127-
128-
engine.quit()
129-
130-
print(f"{num_passed} depths passed")
131-
print(f"{num_failed} depths failed")
131+
result = engine.run_perft(DEPTH)
132132

133-
exit(num_failed)
133+
if check_result(correctResult, result):
134+
print("Succeeded :-)")
135+
exit(0)
136+
else:
137+
print("Failed :-(")
138+
exit(1)

0 commit comments

Comments
 (0)