Skip to content

Commit

Permalink
Use PerftTest with weaver for speed
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Oct 20, 2023
1 parent 8dacf34 commit 8e2f09d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 84 deletions.
62 changes: 0 additions & 62 deletions test-kit/src/test/scala/perft/FullPerftTest.scala

This file was deleted.

79 changes: 57 additions & 22 deletions test-kit/src/test/scala/perft/PerftTest.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
package chess
package perft

import cats.syntax.all.*
import weaver.*

import chess.variant.*
import cats.effect.IO
import cats.kernel.Monoid

object PerftTest extends SimpleIOSuite:

given Monoid[Boolean] with
def empty = true
def combine(x: Boolean, y: Boolean) = x && y

val nodeLimits = 1_000L

test("random.perft"):
perfts(Perft.randomPerfts, Chess960, 10_000L)
.map(assert(_))

test("threeCheck.perft"):
perfts(Perft.threeCheckPerfts, ThreeCheck, nodeLimits)
.map(assert(_))

test("antichess.perft"):
perfts(Perft.antichessPerfts, Antichess, nodeLimits)
.map(assert(_))

test("atomic.perft"):
perfts(Perft.atomicPerfts, Atomic, nodeLimits)
.map(assert(_))

test("crazyhouse.perft"):
perfts(Perft.crazyhousePerfts, Crazyhouse, nodeLimits)
.map(assert(_))

test("horde.perft"):
perfts(Perft.hordePerfts, Horde, nodeLimits)
.map(assert(_))

test("racingkings.perft"):
perfts(Perft.racingkingsPerfts, RacingKings, nodeLimits)
.map(assert(_))

test("tricky.perft"):
perfts(Perft.trickyPerfts, Chess960, nodeLimits)
.map(assert(_))

test("chess960.perft"):
perfts(Perft.chess960, Chess960, nodeLimits)
.map(assert(_))

private def perfts(perfts: List[Perft], variant: Variant, nodeLimit: Long): IO[Boolean] =
perfts.parFoldMapA(perft => IO(perftTest(perft, variant, nodeLimit)))

class PerftTest extends ChessTest:

private def genTests(name: String, tests: List[Perft], variant: Variant, nodeLimit: Long)(using
munit.Location
) =
tests.foreach: perft =>
val result = perft.withLimit(nodeLimit).calculate(variant)
result.foreach: r =>
test(s"$name ${perft.id} depth: ${r.depth}"):
assertEquals(r.result, r.expected)

val nodeLimits = 1_000_000L
genTests("calculate ThreeCheck perfts", Perft.threeCheckPerfts, ThreeCheck, nodeLimits)
genTests("calculate Antichess perfts", Perft.antichessPerfts, Antichess, nodeLimits)
genTests("calculate Atomic perfts", Perft.atomicPerfts, Atomic, nodeLimits)
genTests("calculate Crazyhouse perfts", Perft.crazyhousePerfts, Crazyhouse, nodeLimits)
genTests("calculate Horde perfts", Perft.hordePerfts, Horde, nodeLimits)
genTests("calculate RacingKings perfts", Perft.racingkingsPerfts, RacingKings, nodeLimits)
// for the shake of time we only test the first 50 cases in random.peft, run FullRandomPerftTest.scala for all cases
genTests("calculate random perfts", Perft.randomPerfts.take(100), Chess960, nodeLimits)
genTests("calculate tricky perfts", Perft.trickyPerfts, Chess960, nodeLimits)
genTests("calculate chess960 perfts", Perft.chess960, Chess960, nodeLimits)
private def perftTest(perft: Perft, variant: Variant, nodeLimit: Long): Boolean =
perft
.withLimit(nodeLimit)
.calculate(variant)
.forall(r => r.result === r.expected)

0 comments on commit 8e2f09d

Please sign in to comment.