-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestMoveGeneratorKotlin.kt
72 lines (53 loc) · 2.52 KB
/
TestMoveGeneratorKotlin.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package org.computronium.chess
import org.computronium.chess.core.GameRunner
import org.computronium.chess.testcaseeditor.TestCaseGroup
import org.junit.Assert
import org.junit.Test
import java.io.File
/**
* An example test class tests the built-in move generator against the full set of test files.
*/
class TestMoveGeneratorKotlin {
@Test
fun runTest() {
val testFileDir = File("./src/main/resources/testcases")
val testFiles = testFileDir.listFiles()
if (testFiles == null) {
Assert.fail("No test files found.")
return
}
for (testFile in testFiles.filter { file -> file.name.endsWith(".json") }) {
doTest(testFile)
}
}
private fun doTest(testFile: File) {
val testCaseGroup = TestCaseGroup.fromFile(testFile)
println("Testing file $testFile (${testCaseGroup})")
for (testCase in testCaseGroup.testCases) {
println(" Testing case '${testCase}'")
val expectedMoves = testCase.expected.associateBy({ it.move.toString() }, { it.fen })
val runner = GameRunner.fromFEN(testCase.start.fen)
val startPos = runner.generateSearchNode()
val boardState = startPos.boardState
val actualMoves = startPos.moves.associateBy({ it.toString() }, {
// Do the move and save the resulting FEN.
it.apply(boardState)
val fen = boardState.toFEN()
// Rollback the move we just applied.
it.rollback(boardState)
// Confirm that the rollback works by checking we still match the original FEN.
Assert.assertEquals("Rollback failed", testCase.start.fen, boardState.toFEN())
fen
})
if (expectedMoves != actualMoves) {
if (expectedMoves.keys != actualMoves.keys) {
println("Moves: Expected but didn't find: ${expectedMoves.keys.filter { !actualMoves.keys.contains(it) }}")
println("Moves: Found but didn't expect: ${actualMoves.keys.filter { !expectedMoves.keys.contains(it) } }")
}
println("FENs: Expected but didn't find: ${expectedMoves.values.filter { !actualMoves.values.contains(it) }}")
println("FENs: Found but didn't expect: ${actualMoves.values.filter { !expectedMoves.values.contains(it) }}")
}
Assert.assertEquals("Did not match expected board states", expectedMoves, actualMoves)
}
}
}