From 7da3670cb88e174fd265ab21e066a0c9fc839e48 Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Mon, 25 Mar 2024 05:34:14 +0100 Subject: [PATCH] Fix SimpleMM test --- .../org/jeudego/pairgoth/store/MemoryStore.kt | 11 ++++++++++- .../kotlin/org/jeudego/pairgoth/store/Store.kt | 4 +--- api-webapp/src/test/kotlin/PairingTests.kt | 18 ++++++++++++++---- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/MemoryStore.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/MemoryStore.kt index 4ffb968..eb0cca7 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/MemoryStore.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/MemoryStore.kt @@ -2,10 +2,19 @@ package org.jeudego.pairgoth.store import org.jeudego.pairgoth.model.ID import org.jeudego.pairgoth.model.Tournament +import java.util.concurrent.atomic.AtomicInteger -class MemoryStore: Store { +object MemoryStore: Store { private val tournaments = mutableMapOf>() + // for tests + fun reset() { + tournaments.clear() + _nextTournamentId.set(0) + _nextPlayerId.set(0) + _nextGameId.set(0) + } + override fun getTournaments(): Map> = tournaments.mapValues { mapOf("name" to it.value.shortName) } diff --git a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/Store.kt b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/Store.kt index e45667f..b98a69a 100644 --- a/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/Store.kt +++ b/api-webapp/src/main/kotlin/org/jeudego/pairgoth/store/Store.kt @@ -28,14 +28,12 @@ interface Store { fun deleteTournament(tournament: Tournament<*>) } -private val memoryStore: Store = MemoryStore() - fun getStore(request: HttpServletRequest): Store { val storeType = WebappManager.getMandatoryProperty("store") return when (val auth = WebappManager.getMandatoryProperty("auth")) { "none", "sesame" -> when (storeType) { - "memory" -> memoryStore + "memory" -> MemoryStore "file" -> { val filePath = WebappManager.properties.getProperty("store.file.path") ?: "." FileStore(filePath) diff --git a/api-webapp/src/test/kotlin/PairingTests.kt b/api-webapp/src/test/kotlin/PairingTests.kt index 272514c..85f8906 100644 --- a/api-webapp/src/test/kotlin/PairingTests.kt +++ b/api-webapp/src/test/kotlin/PairingTests.kt @@ -5,7 +5,9 @@ import org.jeudego.pairgoth.model.Game import org.jeudego.pairgoth.model.ID import org.jeudego.pairgoth.model.fromJson import org.jeudego.pairgoth.pairing.solver.BaseSolver +import org.jeudego.pairgoth.store.MemoryStore import org.jeudego.pairgoth.store.lastPlayerId +import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import java.io.File import java.io.FileWriter @@ -20,6 +22,11 @@ import kotlin.test.assertTrue //@Disabled("pairings differ") class PairingTests: TestBase() { + @BeforeEach + fun reset() { + MemoryStore.reset() + } + fun compare_weights(file1: File, file2: File, skipSeeding: Boolean = false):Boolean { BaseSolver.weightsLogger!!.flush() // Maps to store name pairs and costs @@ -136,7 +143,6 @@ class PairingTests: TestBase() { @Test fun `008 simple swiss tournament`() { - // read tournament with pairing var file = getTestFile("opengotha/pairings/simpleswiss_7R.xml") logger.info("read from file $file") @@ -197,7 +203,6 @@ class PairingTests: TestBase() { @Test fun `009 not so simple swiss tournament`() { - // read tournament with pairing var file = getTestFile("opengotha/pairings/notsosimpleswiss_10R.xml") logger.info("read from file $file") @@ -281,7 +286,10 @@ class PairingTests: TestBase() { val pairingsR2 = """[{"id":9,"w":14,"b":3,"h":0,"r":"b","dd":0},{"id":10,"w":10,"b":5,"h":0,"r":"b","dd":0},{"id":11,"w":6,"b":9,"h":0,"r":"b","dd":0},{"id":12,"w":15,"b":12,"h":0,"r":"w","dd":0},{"id":13,"w":2,"b":11,"h":0,"r":"w","dd":0},{"id":14,"w":8,"b":13,"h":0,"r":"b","dd":0},{"id":15,"w":7,"b":4,"h":0,"r":"b","dd":0},{"id":16,"w":16,"b":1,"h":7,"r":"b","dd":0}]""" val pairingsR3 = """[{"id":17,"w":5,"b":14,"h":0,"r":"b","dd":0},{"id":18,"w":10,"b":9,"h":0,"r":"w","dd":0},{"id":19,"w":15,"b":3,"h":0,"r":"w","dd":0},{"id":20,"w":12,"b":2,"h":0,"r":"b","dd":0},{"id":21,"w":6,"b":13,"h":0,"r":"b","dd":0},{"id":22,"w":11,"b":8,"h":0,"r":"w","dd":0},{"id":23,"w":16,"b":7,"h":3,"r":"w","dd":0},{"id":24,"w":4,"b":1,"h":3,"r":"b","dd":0}]""" val pairingsR4 = """[{"id":25,"w":3,"b":10,"h":0,"r":"w","dd":0},{"id":26,"w":14,"b":15,"h":0,"r":"b","dd":0},{"id":27,"w":5,"b":2,"h":0,"r":"w","dd":0},{"id":28,"w":12,"b":6,"h":0,"r":"w","dd":0},{"id":29,"w":9,"b":11,"h":0,"r":"w","dd":0},{"id":30,"w":16,"b":4,"h":3,"r":"b","dd":0},{"id":31,"w":13,"b":7,"h":5,"r":"w","dd":0},{"id":32,"w":8,"b":1,"h":6,"r":"w","dd":0}]""" - val pairingsR5 = """[{"id":33,"w":15,"b":5,"h":0,"r":"w","dd":0},{"id":34,"w":14,"b":10,"h":0,"r":"b","dd":0},{"id":35,"w":9,"b":3,"h":0,"r":"w","dd":0},{"id":36,"w":13,"b":2,"h":0,"r":"w","dd":0},{"id":37,"w":16,"b":12,"h":0,"r":"b","dd":0},{"id":38,"w":11,"b":4,"h":3,"r":"b","dd":0},{"id":39,"w":8,"b":7,"h":5,"r":"w","dd":0},{"id":40,"w":6,"b":1,"h":7,"r":"b","dd":0}]""" + // Opengotha R5 + // val pairingsR5 = """[{"id":33,"w":15,"b":5,"h":0,"r":"w","dd":0},{"id":34,"w":14,"b":10,"h":0,"r":"b","dd":0},{"id":35,"w":9,"b":3,"h":0,"r":"w","dd":0},{"id":36,"w":13,"b":2,"h":0,"r":"w","dd":0},{"id":37,"w":16,"b":12,"h":0,"r":"b","dd":0},{"id":38,"w":11,"b":4,"h":3,"r":"b","dd":0},{"id":39,"w":8,"b":7,"h":5,"r":"w","dd":0},{"id":40,"w":6,"b":1,"h":7,"r":"b","dd":0}]""" + // Add a valid permutation at the end: 11-1(7) & 6-4(3) instead of 11-4(7) 6-1(3) + val pairingsR5 = """[{"id":33,"w":15,"b":5,"h":0,"r":"w","dd":0},{"id":34,"w":14,"b":10,"h":0,"r":"b","dd":0},{"id":35,"w":9,"b":3,"h":0,"r":"w","dd":0},{"id":36,"w":13,"b":2,"h":0,"r":"w","dd":0},{"id":37,"w":16,"b":12,"h":0,"r":"b","dd":0},{"id":38,"w":11,"b":1,"h":3,"r":"b","dd":0},{"id":39,"w":8,"b":7,"h":5,"r":"w","dd":0},{"id":40,"w":6,"b":4,"h":7,"r":"b","dd":0}]""" val pairings = mutableListOf() pairings.add(pairingsR1) pairings.add(pairingsR2) @@ -315,6 +323,7 @@ class PairingTests: TestBase() { assertTrue(compare_weights(getOutputFile("weights.txt"), getTestFile("opengotha/simplemm/simplemm_weights_R$round.txt"), skipSeeding), "Not matching opengotha weights for round $round") logger.info("Weights for round $round match OpenGotha") + /* // fix players ids (TODO - reset memstore between each test for simplicity) val maxId = games.flatMap { listOf((it as Json.Object).getInt("b")!!, (it as Json.Object).getInt("w")!!) }.max() val fixedGames = games.mapTo(Json.MutableArray()) { @@ -323,8 +332,9 @@ class PairingTests: TestBase() { .set("b", game.getInt("b")!! - maxId + 16) .set("w", game.getInt("w")!! - maxId + 16) } + */ - assertTrue(compare_games(fixedGames, Json.parse(pairings[round - 1])!!.asArray(), skipColor=true),"pairings for round $round differ") + assertTrue(compare_games(games, Json.parse(pairings[round - 1])!!.asArray(), skipColor=true),"pairings for round $round differ") logger.info("Pairing for round $round match OpenGotha") forcedGames = Json.parse(pairings[round-1])!!.asArray()