Skip to content

Commit 56d2848

Browse files
transposition table unit tests
1 parent 564553a commit 56d2848

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/TranspositionTable.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,5 @@ void TranspositionTable::clear() {
7777
resetCounters();
7878
for (Entry&entry: entries) {
7979
entry.nodeType = EMPTY;
80-
entry.depth = 0;
81-
entry.score = 0;
82-
entry.key = 0;
83-
entry.bestMove = NULL_MOVE;
8480
}
8581
}

tests/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
1616

1717
add_executable(tests_run RepetitionTest.cpp
1818
HashTest.cpp
19-
UtilTest.cpp)
19+
UtilTest.cpp
20+
TranspositionTest.cpp)
2021
target_link_libraries(tests_run PRIVATE src)
2122
target_link_libraries(tests_run PRIVATE Catch2::Catch2WithMain)
2223

tests/TranspositionTest.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include "CLI.h"
4+
#include "TranspositionTable.h"
5+
#include "catch2/internal/catch_windows_h_proxy.hpp"
6+
7+
#define MATE_SCORE 32768 // 1 << 15
8+
9+
auto tt = TranspositionTable();
10+
11+
TEST_CASE("TT - rw", "[TranspositionTests]") {
12+
ChessBoard board;
13+
board.setPosition("8/RP1qb1B1/2p2P2/6n1/P7/p3k3/Q3b1BK/6R1 w - - 0 1");
14+
tt.setEntry(board, Interface::CLI::parseMove("f6e7", board), 1, 1, TranspositionTable::EXACT, 1);
15+
auto readEntry = tt.getEntry(board.hashCode, 1);
16+
CHECK(readEntry.bestMove == Interface::CLI::parseMove("f6e7", board));
17+
CHECK(readEntry.depth == 1);
18+
CHECK(readEntry.score == 1);
19+
CHECK(readEntry.nodeType == TranspositionTable::EXACT);
20+
}
21+
22+
TEST_CASE("TT - overwrite", "[TranspositionTests]") {
23+
ChessBoard board;
24+
board.setPosition("6k1/5bPp/8/n3p1PR/7Q/4P1b1/2r3Pp/2NK4 w - - 0 1");
25+
26+
tt.setEntry(board, Interface::CLI::parseMove("h4b4", board), 2, 1, TranspositionTable::LOWERBOUND, 1);
27+
auto readEntry = tt.getEntry(board.hashCode, 1);
28+
CHECK(readEntry.score == 1);
29+
30+
tt.setEntry(board, Interface::CLI::parseMove("h4b4", board), 1, 2, TranspositionTable::LOWERBOUND, 1);
31+
readEntry = tt.getEntry(board.hashCode, 1);
32+
CHECK(readEntry.score == 1);
33+
34+
tt.setEntry(board, Interface::CLI::parseMove("h4b4", board), 3, 3, TranspositionTable::LOWERBOUND, 1);
35+
readEntry = tt.getEntry(board.hashCode, 1);
36+
CHECK(readEntry.score == 3);
37+
38+
tt.setEntry(board, Interface::CLI::parseMove("h4b4", board), 2, 4, TranspositionTable::EXACT, 1);
39+
readEntry = tt.getEntry(board.hashCode, 1);
40+
CHECK(readEntry.score == 4);
41+
}
42+
43+
TEST_CASE("TT - mate recall", "[TranspositionTests]") {
44+
ChessBoard board;
45+
board.setPosition("1b6/2N1Pnk1/pp2Pr2/8/2P5/2qp1RN1/7K/5b2 w - - 0 1");
46+
// mate is 8 ply from root, 3 ply from current node
47+
tt.setEntry(board, Interface::CLI::parseMove("g3h5", board), 1, MATE_SCORE - 8, TranspositionTable::EXACT, 5);
48+
// current node is 2 ply from root
49+
auto readEntry = tt.getEntry(board.hashCode, 2);
50+
// thus mate is 5 ply from current root
51+
CHECK(readEntry.score == MATE_SCORE - 5);
52+
}

0 commit comments

Comments
 (0)