Skip to content

Commit

Permalink
Adapt selftest
Browse files Browse the repository at this point in the history
  • Loading branch information
alex65536 committed Aug 6, 2023
1 parent 2f72963 commit 4e1f977
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/core/test/selftest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() + "
Expand Down Expand Up @@ -186,7 +188,7 @@ void runSelfTest(Board b) {
}

// Check that a well-formed move is generated by `genAllMoves()` iff isMoveValid returns true
std::vector<Move> validMoves;
std::vector<Move> pseudoLegalMoves;
const MoveKind kinds[] = {MoveKind::Null,
MoveKind::Simple,
MoveKind::PawnDoubleMove,
Expand All @@ -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) + "\"");
Expand Down

0 comments on commit 4e1f977

Please sign in to comment.