solitaire: make seed-table generator a deterministic unit test (#tests)
- Commit
f35c434eed0ea2c02e33ababa8f2b15a03bbf5ff- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-15 17:18 UTC (20 days ago)
- Parents
a4761d0
SolvableSeedTableGenerator skipped (JUnit assumption) in every normal run unless -PsolitaireSeedGen was passed, so the generation path behind the shipped solvable_deals.bin was never exercised in CI. Without the property it is now a deterministic unit test: it generates a tiny FreeCell table (target 3) into a temp dir and pins the contract - encoded asset decodes to the same sorted table, the report counts match, and every kept seed independently re-proves with DealProofs. With -PsolitaireSeedGen it keeps the full offline generation behaviour unchanged. SeedTableGeneration rounds now scale with the target (historic 2048-seed rounds for the full 10000-seed run, 32-seed rounds for the tiny test), which drops the default test from solving 2048 FreeCell seeds (~28 min) to ~30 s; generated tables are unchanged (each is still the ascending prefix of proven seeds). Verified: :games:solitaire:testDevUnitTest green, 124 tests, 0 skipped (was 1 skipped).
Diff
games/solitaire/src/test/java/com/vayunmathur/games/solitaire/domain/SeedTableGeneration.kt
diff --git a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/domain/SeedTableGeneration.kt b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/domain/SeedTableGeneration.ktindex 848b5acc1b..e7df0d2e4a 100644--- a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/domain/SeedTableGeneration.kt+++ b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/domain/SeedTableGeneration.kt@@ -68,12 +68,17 @@ class SeedTableGeneration( val reports = keys.map { ConfigReport(it) } var nextSeed = 1 val started = System.currentTimeMillis()+ // Round size scales with the target: a full 10 000-seed table keeps+ // the historic 2048-seed rounds, but a tiny unit-test target must not+ // have to solve a whole round before it can stop. The tables are+ // unaffected — each is still the ascending prefix of proven seeds.+ val roundSize = minOf(ROUND_SIZE, maxOf(target * ROUND_TARGET_FACTOR, ROUND_MIN)) while (found.any { it.size < target }) { val needed = BooleanArray(keys.size) { found[it].size < target }- val round = (nextSeed until nextSeed + ROUND_SIZE).map { seed ->+ val round = (nextSeed until nextSeed + roundSize).map { seed -> Callable { evaluate(group, seed, needed) } }- nextSeed += ROUND_SIZE+ nextSeed += roundSize for (outcome in pool.invokeAll(round).map { it.get() }) { record(outcome, needed, found, reports) }@@ -150,6 +155,8 @@ class SeedTableGeneration( companion object { val GROUP_NAMES = listOf("klondike-draw1", "klondike-draw3", "pyramid", "freecell", "spider1", "spider2", "spider4") private const val ROUND_SIZE = 2048+ private const val ROUND_TARGET_FACTOR = 8+ private const val ROUND_MIN = 32 private val RELAXED = com.vayunmathur.games.solitaire.data.KlondikeDifficulty.RELAXED } }
games/solitaire/src/test/java/com/vayunmathur/games/solitaire/domain/SolvableSeedTableGenerator.kt
diff --git a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/domain/SolvableSeedTableGenerator.kt b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/domain/SolvableSeedTableGenerator.ktindex aa5a5077b9..4c646c8bf6 100644--- a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/domain/SolvableSeedTableGenerator.kt+++ b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/domain/SolvableSeedTableGenerator.kt@@ -1,22 +1,36 @@ package com.vayunmathur.games.solitaire.domain +import com.vayunmathur.games.solitaire.data.GameConfig+import com.vayunmathur.games.solitaire.data.GameMode import java.io.File-import org.junit.Assume.assumeTrue+import java.nio.file.Files+import kotlin.test.assertEquals+import kotlin.test.assertTrue import org.junit.Test /**- * Regenerates `src/main/assets/solvable_deals.bin`. Skipped in normal test runs;- * runs only when Gradle is given `-PsolitaireSeedGen=<output dir>` (see the- * module build file and AGENTS.md, "Solvable deals"). Writes the table plus a+ * Regenerates `src/main/assets/solvable_deals.bin`. With+ * `-PsolitaireSeedGen=<output dir>` (see the module build file and+ * AGENTS.md, "Solvable deals") it writes the full tables plus a * `solvable_deals_report.txt` with per-config solvable rates.+ *+ * Without that property it is a deterministic unit test instead of a+ * skip: it generates a tiny FreeCell table (the fastest solver, ~99.9%+ * solvable) into a temp dir and pins the generator's contract — the+ * encoded file decodes, every table is sorted, and every seed it kept+ * really is solver-proven for its config. That keeps the generation+ * path exercised in CI without the 25-minute full run. */ class SolvableSeedTableGenerator { @Test fun generate() { val out = System.getProperty("solitaire.seedgen.out")- assumeTrue("offline generator: pass -PsolitaireSeedGen=<dir>", !out.isNullOrBlank())- val dir = File(out!!).apply { mkdirs() }+ if (out.isNullOrBlank()) {+ generateTinyTable()+ return+ }+ val dir = File(out).apply { mkdirs() } val target = System.getProperty("solitaire.seedgen.target")?.toIntOrNull() ?: DEFAULT_TARGET val threads = System.getProperty("solitaire.seedgen.threads")?.toIntOrNull()?.takeIf { it > 0 } ?: Runtime.getRuntime().availableProcessors()@@ -35,6 +49,40 @@ class SolvableSeedTableGenerator { log("wrote ${SolvableDeals.ASSET_NAME}: ${bytes.size} bytes, ${tables.size} tables") } + /** The hermetic default: a tiny, deterministic FreeCell generation. */+ private fun generateTinyTable() {+ val dir = Files.createTempDirectory("soldeal-tiny").toFile()+ try {+ val target = 3+ val (tables, reports) = SeedTableGeneration(target, 4) { println(it) }+ .generate(listOf("freecell"))+ val key = SolvableDeals.tableKey(GameMode.FREECELL, GameConfig())+ val seeds = tables[key]+ assertTrue(seeds != null, "no FreeCell table generated: ${reports.map { it.line() }}")+ assertEquals(target, seeds.size, "tiny table should hold exactly the target seeds")+ assertTrue(seeds.all { it > 0 } && seeds.toList() == seeds.sorted(), "table not sorted: ${seeds.toList()}")+ assertEquals(1, reports.size)+ assertEquals(target, reports[0].solved)+ assertTrue(reports[0].scanned >= target, "generator scanned too few seeds: ${reports[0].line()}")++ // The written asset decodes to the same table, and every kept+ // seed is independently re-proven by the solver.+ val file = File(dir, SolvableDeals.ASSET_NAME)+ file.writeBytes(SolvableDealTables.encode(tables))+ val decoded = SolvableDealTables.decode(file.readBytes())+ assertEquals(seeds.toList(), decoded.seeds(GameMode.FREECELL, GameConfig())?.toList())+ for (seed in seeds) {+ val proof = DealProofs.solve(GameMode.FREECELL, GameConfig(), seed.toLong())+ assertEquals(+ SolveStatus.SOLVED, proof.status,+ "generated seed $seed does not re-prove: ${proof.status}",+ )+ }+ } finally {+ dir.deleteRecursively()+ }+ }+ private companion object { const val DEFAULT_TARGET = 10_000 }