1313import json
1414import subprocess
1515from 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+
2130def 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
105115with 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
112126engine = Engine (engine_path = ENGINE_PATH , pos_fen = startingFEN )
113127
114128num_failed = 0
115129num_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