35#include <gtest/gtest.h>
43TEST(KillerMoveTable, EmptyTableReportsFalse)
46 EXPECT_FALSE(table.is_killer(0, 42));
47 EXPECT_FALSE(table.is_killer(5, 99));
50TEST(KillerMoveTable, RecordAndQuery)
55 EXPECT_TRUE(table.is_killer(0, 10));
56 EXPECT_FALSE(table.is_killer(0, 20));
57 EXPECT_FALSE(table.is_killer(1, 10));
60TEST(KillerMoveTable, TwoSlotsPerDepth)
66 EXPECT_TRUE(table.is_killer(0, 10));
67 EXPECT_TRUE(table.is_killer(0, 20));
70TEST(KillerMoveTable, ThirdRecordEvictsOldest)
77 EXPECT_FALSE(table.is_killer(0, 10));
78 EXPECT_TRUE(table.is_killer(0, 20));
79 EXPECT_TRUE(table.is_killer(0, 30));
82TEST(KillerMoveTable, DuplicateRecordIsNoOp)
90 EXPECT_TRUE(table.is_killer(0, 10));
91 EXPECT_TRUE(table.is_killer(0, 20));
94TEST(KillerMoveTable, IndependentDepths)
100 EXPECT_TRUE(table.is_killer(0, 10));
101 EXPECT_FALSE(table.is_killer(0, 20));
102 EXPECT_TRUE(table.is_killer(1, 20));
103 EXPECT_FALSE(table.is_killer(1, 10));
106TEST(KillerMoveTable, ClearResetsAll)
113 EXPECT_FALSE(table.is_killer(0, 10));
114 EXPECT_FALSE(table.is_killer(1, 20));
117TEST(KillerMoveTable, SparseDepthAccess)
120 table.record(100, 42);
121 EXPECT_TRUE(table.is_killer(100, 42));
122 EXPECT_FALSE(table.is_killer(50, 42));
125TEST(KillerMoveTable, NonComparableMovesUseNoOpTable)
134 EXPECT_FALSE(table.is_killer(0, Opaque{1}));
135 table.record(0, Opaque{1});
136 EXPECT_FALSE(table.is_killer(0, Opaque{1}));
139TEST(KillerMoveTable, StringMoves)
144 table.record(0,
"e2e4");
145 table.record(0,
"d2d4");
147 EXPECT_TRUE(table.is_killer(0,
"e2e4"));
148 EXPECT_TRUE(table.is_killer(0,
"d2d4"));
149 EXPECT_FALSE(table.is_killer(0,
"c2c4"));
154TEST(HistoryHeuristicTable, EmptyTableReturnsZero)
157 EXPECT_EQ(table.
score(42), 0u);
160TEST(HistoryHeuristicTable, RecordAndScore)
164 EXPECT_EQ(table.
score(10), 5u);
167TEST(HistoryHeuristicTable, AccumulatesBonus)
172 EXPECT_EQ(table.
score(10), 10u);
175TEST(HistoryHeuristicTable, DefaultBonusIsOne)
181 EXPECT_EQ(table.
score(10), 3u);
184TEST(HistoryHeuristicTable, IndependentKeys)
190 EXPECT_EQ(table.
score(1), 10u);
191 EXPECT_EQ(table.
score(2), 20u);
192 EXPECT_EQ(table.
score(3), 0u);
195TEST(HistoryHeuristicTable, ClearResetsAll)
202 EXPECT_EQ(table.
score(1), 0u);
203 EXPECT_EQ(table.
score(2), 0u);
206TEST(HistoryHeuristicTable, NullTableIsNoOp)
210 EXPECT_EQ(table.
score(42), 0u);
212 EXPECT_EQ(table.
score(42), 0u);
217TEST(SortRankedMoves, SortsByPriorityDescending)
220 moves.
append({10, 1, 0,
false, 0});
221 moves.
append({20, 3, 1,
false, 0});
222 moves.
append({30, 2, 2,
false, 0});
225 [](
int a,
int b) {
return a > b; },
228 EXPECT_EQ(moves[0].move, 20);
229 EXPECT_EQ(moves[1].move, 30);
230 EXPECT_EQ(moves[2].move, 10);
233TEST(SortRankedMoves, KillerMovesFirst)
236 moves.
append({10, 5, 0,
false, 0});
237 moves.
append({20, 1, 1,
true, 0});
238 moves.
append({30, 3, 2,
false, 0});
241 [](
int a,
int b) {
return a > b; },
244 EXPECT_EQ(moves[0].move, 20);
245 EXPECT_EQ(moves[1].move, 10);
246 EXPECT_EQ(moves[2].move, 30);
249TEST(SortRankedMoves, HistoryBreaksTies)
252 moves.
append({10, 5, 0,
false, 10});
253 moves.
append({20, 5, 1,
false, 50});
254 moves.
append({30, 5, 2,
false, 30});
257 [](
int a,
int b) {
return a > b; },
260 EXPECT_EQ(moves[0].move, 20);
261 EXPECT_EQ(moves[1].move, 30);
262 EXPECT_EQ(moves[2].move, 10);
265TEST(SortRankedMoves, OrdinalBreaksTiesWhenEverythingEqual)
268 moves.
append({10, 5, 2,
false, 0});
269 moves.
append({20, 5, 0,
false, 0});
270 moves.
append({30, 5, 1,
false, 0});
273 [](
int a,
int b) {
return a > b; },
276 EXPECT_EQ(moves[0].move, 20);
277 EXPECT_EQ(moves[1].move, 30);
278 EXPECT_EQ(moves[2].move, 10);
281TEST(SortRankedMoves, KillerThenHistoryThenPriority)
284 moves.
append({1, 100, 0,
false, 0});
285 moves.
append({2, 1, 1,
true, 0});
286 moves.
append({3, 50, 2,
false, 999});
287 moves.
append({4, 90, 3,
false, 1});
290 [](
int a,
int b) {
return a > b; },
293 EXPECT_EQ(moves[0].move, 2);
294 EXPECT_EQ(moves[1].move, 3);
295 EXPECT_EQ(moves[2].move, 4);
296 EXPECT_EQ(moves[3].move, 1);
299TEST(SortRankedMoves, SingleElementIsNoOp)
302 moves.
append({42, 1, 0,
false, 0});
305 [](
int a,
int b) {
return a > b; },
308 EXPECT_EQ(moves[0].move, 42);
311TEST(SortRankedMoves, EmptyIsNoOp)
316 [](
int a,
int b) {
return a > b; },
324TEST(KillerMoveTable, LargeDepthRange)
327 constexpr size_t N = 2000;
330 for (
size_t d = 0; d <
N; ++d)
331 table.record(d,
static_cast<int>(d * 10));
333 for (
size_t d = 0; d <
N; ++d)
334 EXPECT_TRUE(table.is_killer(d,
static_cast<int>(d * 10)));
337 for (
size_t d = 0; d <
N; ++d)
338 EXPECT_FALSE(table.is_killer(d, -1));
341 for (
size_t d = 0; d <
N; ++d)
342 EXPECT_FALSE(table.is_killer(d,
static_cast<int>(d * 10)));
345TEST(KillerMoveTable, EvictionUnderManyInsertsPerDepth)
349 for (
int i = 1; i <= 10; ++i)
352 EXPECT_FALSE(table.is_killer(0, 1));
353 EXPECT_FALSE(table.is_killer(0, 8));
354 EXPECT_TRUE(table.is_killer(0, 9));
355 EXPECT_TRUE(table.is_killer(0, 10));
358TEST(KillerMoveTable, NonComparableLargeInput)
360 struct Opaque {
int v; };
364 for (
size_t d = 0; d < 500; ++d)
366 table.record(d, Opaque{
static_cast<int>(d)});
367 EXPECT_FALSE(table.is_killer(d, Opaque{static_cast<int>(d)}));
372TEST(HistoryHeuristicTable, LargeKeySet)
375 constexpr int N = 5000;
377 for (
int k = 0;
k <
N; ++
k)
378 table.
record(
k,
static_cast<size_t>(
k + 1));
380 for (
int k = 0;
k <
N; ++
k)
381 EXPECT_EQ(table.
score(
k),
static_cast<size_t>(
k + 1));
384 for (
int k = 0;
k <
N; ++
k)
385 EXPECT_EQ(table.
score(
k), 0u);
388TEST(HistoryHeuristicTable, AccumulatesLargeBonuses)
391 constexpr size_t REPS = 1000;
393 for (
size_t i = 0; i < REPS; ++i)
396 EXPECT_EQ(table.
score(42), REPS * 3);
399TEST(HistoryHeuristicTable, NullTableLargeInput)
404 for (
int k = 0;
k < 1000; ++
k)
407 for (
int k = 0;
k < 1000; ++
k)
408 EXPECT_EQ(table.
score(
k), 0u);
415TEST(SortRankedMoves, RandomizedStress)
417 constexpr size_t N = 2000;
418 constexpr unsigned SEED = 42u;
419 std::mt19937
rng(SEED);
421 auto rand_int = [&](
int lo,
int hi)
422 {
return std::uniform_int_distribution<int>(lo, hi)(
rng); };
423 auto rand_uint = [&](
size_t lo,
size_t hi)
424 {
return static_cast<size_t>(std::uniform_int_distribution<size_t>(lo, hi)(
rng)); };
425 auto rand_bool = [&]() {
return static_cast<bool>(
rng() & 1u); };
429 for (
size_t i = 0; i <
N; ++i)
436 for (
bool killer_first : {
false,
true})
437 for (
bool use_history : {
false,
true})
440 size_t comparisons = 0;
441 auto instrumented_cmp = [&](
int a,
int b) {
448 ASSERT_EQ(moves.
size(),
N);
451 const double n_log_n =
N * std::log2(
static_cast<double>(
N));
452 EXPECT_LT(comparisons,
static_cast<size_t>(20.0 * n_log_n))
453 <<
"Performance regression in sort_ranked_moves hot path ("
454 << killer_first <<
", " << use_history <<
")";
456 for (
size_t i = 0; i + 1 <
N; ++i)
458 const auto &a = moves[i];
459 const auto &b = moves[i + 1];
463 if (a.killer
and not b.killer)
465 if (not a.killer
and b.killer)
466 FAIL() <<
"Non-killer before killer at index " << i;
469 if (use_history
and a.history_score != b.history_score)
470 EXPECT_GE(a.history_score, b.history_score)
471 <<
"History ordering violated at index " << i;
474 if (a.priority != b.priority)
475 EXPECT_GE(a.priority, b.priority)
476 <<
"Priority ordering violated at index " << i;
478 EXPECT_LE(a.ordinal, b.ordinal)
479 <<
"Ordinal tiebreak violated at index " << i;
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & append(const T &data)
Append a copy of data
Sparse history heuristic table over Aleph hash maps.
size_t score(const Key &key) const noexcept
Read the current score for a move key.
void clear() noexcept
Reset all history scores to zero.
void record(const Key &key, const size_t bonus=1)
Increment the history score for a move key.
Two-slot killer heuristic table indexed by search depth.
Number Theoretic Transform over Z / MOD Z.
Main namespace for Aleph-w library functions.
void sort_ranked_moves(Array< RankedMove< Move, Priority > > &moves, BetterPriority better_priority, const bool prefer_killer, const bool prefer_history)
Sort one materialized move batch using priority and optional hooks.
and
Check uniqueness with explicit hash + equality functors.
Shared support for configurable successor ordering heuristics.
Null history heuristic hook used when a domain has no move key.
static constexpr bool supported
Marker indicating that this table does nothing.
static void record(const Key &, const size_t=1) noexcept
No-op.
static size_t score(const Key &) noexcept
Always returns zero.
static void clear() noexcept
No-op.
One move plus the metadata used by ordering comparators.