From 7f6ee4e6f18be7af01bb5077ae45da84e4db9fe1 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Thu, 27 Aug 2026 15:50:00 +0200 Subject: [PATCH] test(movegen): add move generation and perft tests --- tests/MoveGen.test.scala | 49 ++++++++++++++++++++++++++++++++++++++ tests/Perft.test.scala | 34 ++++++++++++++++++++++++++ tests/PerftSlow.test.scala | 33 +++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 tests/MoveGen.test.scala create mode 100644 tests/Perft.test.scala create mode 100644 tests/PerftSlow.test.scala diff --git a/tests/MoveGen.test.scala b/tests/MoveGen.test.scala new file mode 100644 index 0000000..fc6d16c --- /dev/null +++ b/tests/MoveGen.test.scala @@ -0,0 +1,49 @@ +package gambit + +class MoveGenTest extends munit.FunSuite: + + private def legal(fen: String): Vector[Int] = + val moves = new MoveList() + MoveGen.generateLegal(Fen.parse(fen), moves) + (0 until moves.size).map(moves.apply).toVector + + test("en passant generated in matching position") { + val moves = legal("rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 3") + val ep = moves.filter(Move.isEnPassant) + assertEquals(ep.size, 1) + assertEquals(Move.uci(ep.head), "e5d6") + } + + test("both castling moves generated when legal") { + val moves = legal("r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1") + val castles = moves.filter(Move.isCastle).map(Move.uci).toSet + assertEquals(castles, Set("e1g1", "e1c1")) + } + + test("castling blocked when king passes through check") { + val moves = legal("r3k2r/8/8/8/8/8/6q1/R3K2R w KQkq - 0 1") + val castles = moves.filter(Move.isCastle).map(Move.uci).toSet + assertEquals(castles, Set("e1c1")) + } + + test("promotions generated with all four pieces") { + val moves = legal("8/P7/8/8/8/8/8/k6K w - - 0 1") + val promos = moves.filter(Move.isPromotion).map(Move.uci).toSet + assertEquals(promos, Set("a7a8q", "a7a8r", "a7a8b", "a7a8n")) + } + + test("pinned knight cannot move") { + val moves = legal("4r2k/8/8/8/8/8/4N3/4K3 w - - 0 1") + val knightMoves = moves.filter(m => Move.from(m) == Square.make(4, 1)) + assertEquals(knightMoves.size, 0) + } + + test("only check evasions when in check") { + val moves = legal("4k3/8/8/8/8/8/8/r3K2R w K - 0 1") + assert(moves.nonEmpty) + for m <- moves do + val pos = Fen.parse("4k3/8/8/8/8/8/8/r3K2R w K - 0 1") + val undo = pos.make(m) + assert(!pos.isAttacked(pos.kingSquare(Color.White), Color.Black), s"illegal ${Move.uci(m)}") + pos.unmake(m, undo) + } diff --git a/tests/Perft.test.scala b/tests/Perft.test.scala new file mode 100644 index 0000000..ed66a81 --- /dev/null +++ b/tests/Perft.test.scala @@ -0,0 +1,34 @@ +package gambit + +class PerftTest extends munit.FunSuite: + val kiwipete = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1" + val position3 = "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1" + val position4 = "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1" + val position5 = "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8" + val position6 = "r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10" + + def perft(fen: String, depth: Int): Long = Perft.perft(Fen.parse(fen), depth) + + test("startpos perft 1") { assertEquals(perft(Fen.startPosition, 1), 20L) } + test("startpos perft 2") { assertEquals(perft(Fen.startPosition, 2), 400L) } + test("startpos perft 3") { assertEquals(perft(Fen.startPosition, 3), 8902L) } + test("startpos perft 4") { assertEquals(perft(Fen.startPosition, 4), 197281L) } + + test("kiwipete perft 1") { assertEquals(perft(kiwipete, 1), 48L) } + test("kiwipete perft 2") { assertEquals(perft(kiwipete, 2), 2039L) } + test("kiwipete perft 3") { assertEquals(perft(kiwipete, 3), 97862L) } + test("kiwipete perft 4") { assertEquals(perft(kiwipete, 4), 4085603L) } + + test("position3 perft 4") { assertEquals(perft(position3, 4), 43238L) } + test("position3 perft 5") { assertEquals(perft(position3, 5), 674624L) } + + test("position4 perft 3") { assertEquals(perft(position4, 3), 9467L) } + test("position4 perft 4") { assertEquals(perft(position4, 4), 422333L) } + + test("position5 perft 1") { assertEquals(perft(position5, 1), 44L) } + test("position5 perft 2") { assertEquals(perft(position5, 2), 1486L) } + test("position5 perft 3") { assertEquals(perft(position5, 3), 62379L) } + test("position5 perft 4") { assertEquals(perft(position5, 4), 2103487L) } + + test("position6 perft 3") { assertEquals(perft(position6, 3), 89890L) } + test("position6 perft 4") { assertEquals(perft(position6, 4), 3894594L) } diff --git a/tests/PerftSlow.test.scala b/tests/PerftSlow.test.scala new file mode 100644 index 0000000..9950061 --- /dev/null +++ b/tests/PerftSlow.test.scala @@ -0,0 +1,33 @@ +package gambit + +class PerftSlowTest extends munit.FunSuite: + override val munitTimeout = scala.concurrent.duration.Duration(120, "s") + + val kiwipete = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1" + val position3 = "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1" + val position4 = "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1" + val position5 = "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8" + + test("startpos perft 5") { + assertEquals(Perft.perft(Fen.parse(Fen.startPosition), 5), 4865609L) + } + + test("startpos perft 6") { + assertEquals(Perft.perft(Fen.parse(Fen.startPosition), 6), 119060324L) + } + + test("kiwipete perft 5") { + assertEquals(Perft.perft(Fen.parse(kiwipete), 5), 193690690L) + } + + test("position3 perft 6") { + assertEquals(Perft.perft(Fen.parse(position3), 6), 11030083L) + } + + test("position4 perft 5") { + assertEquals(Perft.perft(Fen.parse(position4), 5), 15833292L) + } + + test("position5 perft 5") { + assertEquals(Perft.perft(Fen.parse(position5), 5), 89941194L) + }