From: JBM Date: Wed, 10 Jun 2020 23:20:24 +0000 (+0200) Subject: More testing. This code cleanup session supported by @Zorg1. X-Git-Url: https://troll.desast.re/troll.git/commitdiff_plain/1b8132031651654d02bd60fd5dc3a6fd1b724236?ds=inline More testing. This code cleanup session supported by @Zorg1. --- diff --git a/PLAN.org b/PLAN.org index efbf2f0..a838764 100644 --- a/PLAN.org +++ b/PLAN.org @@ -60,7 +60,12 @@ That one's probably never going to be DONE ^^' *** split out what can be *** factor stone throwing ref/view interface - +** Testing +*** DONE basics +*** TODO the @Illedan case +*** TODO more generic "referee crash" +*** TODO parameters feedback +*** TODO cheating cases * BUGS ** GodMode crash Reported by @Illedan diff --git a/src/test/java/TrollTest.java b/src/test/java/TrollTest.java index e54d413..e8dff3f 100644 --- a/src/test/java/TrollTest.java +++ b/src/test/java/TrollTest.java @@ -1,5 +1,6 @@ import java.util.Properties; import java.util.List; +import java.util.ListIterator; import java.util.Map; import java.io.PrintStream; @@ -11,8 +12,7 @@ import com.codingame.gameengine.runner.MultiplayerGameRunner; import com.codingame.gameengine.runner.dto.*; public class TrollTest { - @Test - public void drawGame() { + static GameResult runGame(String left, String right) { MultiplayerGameRunner gameRunner = new MultiplayerGameRunner(); Properties gameParameters = new Properties(); gameParameters.setProperty("roadLength", "6"); @@ -21,26 +21,35 @@ public class TrollTest { gameRunner.setSeed(0l); gameRunner.setLeagueLevel(1); - gameRunner.addAgent(agentOne); - gameRunner.addAgent(agentOne); + gameRunner.addAgent(left); + gameRunner.addAgent(right); - assertIsDraw(gameRunner.simulate()); + return gameRunner.simulate(); } @Test - public void crashGame() { - MultiplayerGameRunner gameRunner = new MultiplayerGameRunner(); - Properties gameParameters = new Properties(); - gameParameters.setProperty("roadLength", "6"); - gameParameters.setProperty("initialStones", "15"); - gameRunner.setGameParameters(gameParameters); - gameRunner.setSeed(0l); - gameRunner.setLeagueLevel(1); + public void drawGame() { + assertIsDraw(runGame(agentOne, agentOne)); + } + + @Test + public void defeatGames() { + assertIsDefeat(runGame(agentCrash, agentCrash)); + assertIsDefeat(runGame(agentGarbage, agentGarbage)); + } + + @Test + public void simpleGames() { + // wins by direct reach, no fastforward + assertWinLose(agentTwo, agentOne); - gameRunner.addAgent(agentCrash); - gameRunner.addAgent(agentCrash); + // win by fastforward after loser exhaustion + assertWinLose(agentOne, "yes 15"); - assertIsDefeat(gameRunner.simulate()); + // win despite fastforward after winner exhaustion + // (harder to construct :-D ) + assertWinLose(agent(1,2,2,2,8), + agent(3,1,1,1,8)); } // great thanks to @dbdr for the intense moral support leading to @@ -48,6 +57,16 @@ public class TrollTest { static String agentOne = "yes 1"; static String agentTwo = "yes 2"; static String agentCrash = "false"; + static String agentGarbage = "yes this_is_assuredly_not_an_int"; + + static String agent(int... tosses) { + String cmd = "echo -e "; + for (int i = 0; i < tosses.length; i++) { + if (i > 0) cmd += "\\n"; + cmd += tosses[i]; + } + return cmd; + } static void dumpGameResult(PrintStream p, GameResult gameResult) { for (AgentDto agent : gameResult.agents) dumpAgent(p, agent); @@ -62,11 +81,11 @@ public class TrollTest { } static void dumpList(PrintStream p, String tag, List list) { - V[] a = (V[]) list.toArray(); - for (int i = 0; i < a.length; i++) { + ListIterator i = list.listIterator(); + while (i.hasNext()) { if (tag != null) p.print(tag + " "); - p.print(i + ": "); - dumpGeneric(p, a[i]); + p.print(i.nextIndex() + ": "); + dumpGeneric(p, i.next()); } } @@ -77,9 +96,9 @@ public class TrollTest { } } - static void dumpGeneric(PrintStream p, V v) { + static void dumpGeneric(PrintStream p, E v) { if (v instanceof List) { - dumpList(p, null, (List) v); + dumpList(p, null, (List) v); } else { p.println(v); @@ -94,6 +113,27 @@ public class TrollTest { p.println("[agent] " + agent.agentId + ": " + agent.avatar + " " + agent.index + " " + agent.name); } + static void assertWinLose(String winner, String loser) { + assertLeftWin(runGame(winner, loser)); + assertRightWin(runGame(loser, winner)); + } + + static void assertLeftWin(GameResult gameResult) { + int[] scores = assertTwoScores(gameResult); + if (scores == null) return; + + int s1 = scores[0], s2 = scores[1]; + assertTrue("Left player has higher score than right player", s1 > s2); + } + + static void assertRightWin(GameResult gameResult) { + int[] scores = assertTwoScores(gameResult); + if (scores == null) return; + + int s1 = scores[0], s2 = scores[1]; + assertTrue("Right player has higher score than right player", s2 > s1); + } + static void assertIsDraw(GameResult gameResult) { int[] scores = assertTwoScores(gameResult); if (scores == null) return;