From fddaf463662bf94d6382bf4e9b4d56a9689315af Mon Sep 17 00:00:00 2001 From: JBM Date: Sun, 14 Jun 2020 16:48:22 +0200 Subject: [PATCH] Clean up testing --- PLAN.org | 2 +- src/test/java/TrollTest.java | 78 ++++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/PLAN.org b/PLAN.org index 6388307..fef8643 100644 --- a/PLAN.org +++ b/PLAN.org @@ -53,7 +53,7 @@ - [X] Cheating - [X] Maps - [ ] Trolls -- [ ] +- [ ] Multiround ** DONE Early termination (need time rationalization) ** DONE Time rationalization (need code reorg) ** TODO Code cleanup diff --git a/src/test/java/TrollTest.java b/src/test/java/TrollTest.java index 2018014..9112b28 100644 --- a/src/test/java/TrollTest.java +++ b/src/test/java/TrollTest.java @@ -15,29 +15,35 @@ import com.codingame.gameengine.runner.MultiplayerGameRunner; import com.codingame.gameengine.runner.dto.*; public class TrollTest implements Cloneable { - int leagueLevel = 1; // @#$%^&* league parameter is *global* despite API! - Long seed; - Integer roadLength; - Integer initialStones; - TrollTest branch() { + + // ==================== Game Runner encapsulation + + private int league = 1; // @#$%^&* this parameter is *global* despite API! + private Long seed; + private Integer roadLength; + private Integer initialStones; + + private TrollTest branch() { try { return (TrollTest) clone(); } catch (CloneNotSupportedException e) { throw new InternalError(e); } } - TrollTest setLeague(int league) { leagueLevel = league; return this; } - TrollTest setSeed(long seed) { this.seed = seed; return this; } - TrollTest setRoadLength(int l) { roadLength = l; return this; } - TrollTest setInitialStones(int s) { initialStones = s; return this; } + private TrollTest setLeague(int l) { league = l; return this; } + private TrollTest setSeed(long s) { seed = s; return this; } + private TrollTest setRoadLength(int l) { roadLength = l; return this; } + private TrollTest setInitialStones(int s) { initialStones = s; return this; } - GameResult runGame(String left, String right) { + private GameResult runGame(String left, String right) { MultiplayerGameRunner gameRunner = new MultiplayerGameRunner(); - gameRunner.setLeagueLevel(leagueLevel); + gameRunner.setLeagueLevel(league); // mandatory if (seed != null) gameRunner.setSeed(seed); else gameRunner.setSeed(0l); + Properties gameParameters = new Properties(); gameRunner.setGameParameters(gameParameters); if (roadLength != null) gameParameters.setProperty("roadLength", roadLength.toString()); if (initialStones != null) - gameParameters.setProperty("initialStones", initialStones.toString()); + gameParameters.setProperty("initialStones", + initialStones.toString()); gameRunner.addAgent(left); gameRunner.addAgent(right); @@ -45,6 +51,8 @@ public class TrollTest implements Cloneable { return gameRunner.simulate(); } + // ==================== Single-round game result testing + @Test public void drawGame() { assertIsDraw(runGame(agentOne, agentOne)); @@ -69,6 +77,8 @@ public class TrollTest implements Cloneable { assertWinLose(agent(1,2,2,2,8), agent(3,1,1,1,8)); } + // ==================== Cheating games (league 1 perk) + @Test public void cheatingGames() { // win by cheating (works in league 1, which is the default) @@ -81,8 +91,18 @@ public class TrollTest implements Cloneable { branch().setLeague(2).setSeed(1) .setRoadLength(6).setInitialStones(15) .assertWinLose(agentTwo, agentCheat); + + // league 3 forbids + Random r = new Random(); + for (int i = 0; i < 16; i++) { + branch().setLeague(3).setSeed(r.nextLong()) + .setRoadLength(6).setInitialStones(15) + .assertWinLose(agentTwo, agentCheat); + } } + // ==================== Map generation testing + static private class GameMap { int roadLength; int initialStones; @@ -165,16 +185,18 @@ public class TrollTest implements Cloneable { return result; } + // ==================== Common test agents + // great thanks to @dbdr for the intense moral support leading to - // the following: - static String agentOne = "yes 1"; - static String agentTwo = "yes 2"; - static String agentAllIn = "yes 15"; - static String agentCrash = "false"; - static String agentGarbage = "yes this_is_assuredly_not_an_int"; - static String agentCheat = agent(-100,25,25,25,25); - - static String agent(int... tosses) { + // the following acceptance of failure: + static private final String agentOne = "yes 1"; + static private final String agentTwo = "yes 2"; + static private final String agentAllIn = "yes 15"; + static private final String agentCrash = "false"; // SLOW, try and avoid + static private final String agentGarbage = "yes assuredly_not_an_int"; + static private final String agentCheat = agent(-100,25,25,25,25); + + static private String agent(int... tosses) { String cmd = "echo -e "; for (int i = 0; i < tosses.length; i++) { if (i > 0) cmd += "\\n"; @@ -183,6 +205,8 @@ public class TrollTest implements Cloneable { return cmd; } + // ==================== Debug routines + static void dumpGameResult(PrintStream p, GameResult gameResult) { for (AgentDto agent : gameResult.agents) dumpAgent(p, agent); dumpMap(p, "errors", gameResult.errors); @@ -195,7 +219,7 @@ public class TrollTest implements Cloneable { // dumpList(p, "views", gameResult.views); } - static void dumpList(PrintStream p, String tag, List list) { + static private void dumpList(PrintStream p, String tag, List list) { ListIterator i = list.listIterator(); while (i.hasNext()) { if (tag != null) p.print(tag + " "); @@ -204,14 +228,14 @@ public class TrollTest implements Cloneable { } } - static void dumpMap(PrintStream p, String tag, Map map) { + static private void dumpMap(PrintStream p, String tag, Map map) { for (Map.Entry kv : map.entrySet()) { p.print(tag + " for " + kv.getKey() + ": "); dumpGeneric(p, kv.getValue()); } } - static void dumpGeneric(PrintStream p, E v) { + static private void dumpGeneric(PrintStream p, E v) { if (v instanceof List) { dumpList(p, null, (List) v); } @@ -220,7 +244,7 @@ public class TrollTest implements Cloneable { } } - static void dumpString(PrintStream p, String tag, String msg) { + static private void dumpString(PrintStream p, String tag, String msg) { p.println(tag + ": " + msg); } @@ -228,7 +252,9 @@ public class TrollTest implements Cloneable { p.println("[agent] " + agent.agentId + ": " + agent.avatar + " " + agent.index + " " + agent.name); } - void assertWinLose(String winner, String loser) { + // ==================== Test assertions + + private void assertWinLose(String winner, String loser) { assertLeftWin(runGame(winner, loser)); assertRightWin(runGame(loser, winner)); } -- 2.30.2