From 4e1f977c36f40051206a6f2c7997e253615b30ae Mon Sep 17 00:00:00 2001 From: Alexander Kernozhitsky Date: Sun, 6 Aug 2023 19:15:03 +0200 Subject: [PATCH] Adapt selftest --- src/core/test/selftest.cpp | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/core/test/selftest.cpp b/src/core/test/selftest.cpp index 947c2d8..33c967c 100644 --- a/src/core/test/selftest.cpp +++ b/src/core/test/selftest.cpp @@ -141,10 +141,12 @@ void runSelfTest(Board b) { Move movesSeparate[1500]; const size_t simpleCnt = genOnlyLegal(&MoveGen::genSimpleMovesNoPromote, gen, movesSeparate, "SIMPLE_NO_PROMOTE", BUFSZ_MOVES); - const size_t promoteCnt = genOnlyLegal(&MoveGen::genSimplePromotes, gen, movesSeparate + simpleCnt, - "PROMOTE", BUFSZ_SIMPLE_PROMOTES); - const size_t captureCnt = genOnlyLegal( - &MoveGen::genCaptures, gen, movesSeparate + simpleCnt + promoteCnt, "CAPTURE", BUFSZ_CAPTURES); + const size_t promoteCnt = + genOnlyLegal(&MoveGen::genSimplePromotes, gen, movesSeparate + simpleCnt, "PROMOTE", + BUFSZ_SIMPLE_PROMOTES); + const size_t captureCnt = + genOnlyLegal(&MoveGen::genCaptures, gen, movesSeparate + simpleCnt + promoteCnt, "CAPTURE", + BUFSZ_CAPTURES); if (simpleCnt + promoteCnt + captureCnt != moveCnt) { panic( "Moves generated by genAllMoves() differ from genSimpleMovesNoPromote() + " @@ -186,7 +188,7 @@ void runSelfTest(Board b) { } // Check that a well-formed move is generated by `genAllMoves()` iff isMoveValid returns true - std::vector validMoves; + std::vector pseudoLegalMoves; const MoveKind kinds[] = {MoveKind::Null, MoveKind::Simple, MoveKind::PawnDoubleMove, @@ -205,23 +207,33 @@ void runSelfTest(Board b) { continue; } if (isMoveValid(b, move)) { - validMoves.push_back(move); + pseudoLegalMoves.push_back(move); } } } } - validMoves.resize(filterLegalMoves(b, validMoves.data(), validMoves.size(), "VALID")); - std::sort(validMoves.begin(), validMoves.end(), cmpMoves); - if (!std::equal(validMoves.begin(), validMoves.end(), moves, moves + moveCnt)) { + + auto legalMoves = pseudoLegalMoves; + legalMoves.resize(filterLegalMoves(b, legalMoves.data(), legalMoves.size(), "VALID")); + std::sort(legalMoves.begin(), legalMoves.end(), cmpMoves); + if (!std::equal(legalMoves.begin(), legalMoves.end(), moves, moves + moveCnt)) { panic("Valid move list and generated move list mismatch"); } - // Check that board is valid after making moves - for (size_t i = 0; i < moveCnt; ++i) { - const Move move = moves[i]; + // Check that `wasMoveLegal()` and `isMoveLegal()` are identical. Also check that board is valids + // after making moves. + for (const auto &move : pseudoLegalMoves) { + const bool isLegal1 = isMoveLegal(b, move); const Board saved = b; const MovePersistence p = moveMake(b, move); - testBoardValid(b); + const bool isLegal2 = wasMoveLegal(b); + if (isLegal1 != isLegal2) { + panic("Functions isMoveLegal() and wasMoveLegal() yield different result on move \"" + + moveToStr(move) + "\""); + } + if (isLegal2) { + testBoardValid(b); + } moveUnmake(b, move, p); if (!boardsBitCompare(b, saved)) { panic("Board becomes different after making and unmaking move \"" + moveToStr(move) + "\"");