package net.ciklum.icfpc11.controller; import junit.framework.TestCase import net.ciklum.icfpc11.domain.Game import net.ciklum.icfpc11.parser.Command import net.ciklum.icfpc11.parser.Application import net.ciklum.icfpc11.domain.Card import net.ciklum.icfpc11.domain.greenspoon10.Identity import net.ciklum.icfpc11.domain.greenspoon10.ConstantFunction import net.ciklum.icfpc11.domain.Player /** * blabla * @author vic */ public class ControllerTest extends TestCase { private Controller controller void setUp() { controller = new Controller(Game.push(new Player('p0'), new Player('p1'))) } void tearDown() { Game.pop() } void testApplyCommand_inc_I() { Command c = new Command(Application.LEFT, 3, Card.inc) controller.applyCommand(c) assertEquals Identity.IDENTITY, controller.game.currentProponent.getSlot(3).value assertEquals 10000, controller.game.currentProponent.getSlot(3).vitality } void testApplyCommand_zero() { Command c = new Command(Application.LEFT, 3, Card.zero) controller.applyCommand(c) assertEquals Identity.IDENTITY, controller.game.currentProponent.getSlot(3).value c = new Command(Application.RIGHT, 3, Card.zero) controller.applyCommand(c) assertEquals ConstantFunction.ZERO, controller.game.currentProponent.getSlot(3).value assertEquals 10000, controller.game.currentProponent.getSlot(3).vitality } void testApplyCommand_sample1() { List commands = [ c(2, 0, Card.zero), c(2, 0, Card.inc), c(1, 0, Card.succ), c(2, 0, Card.zero), c(1, 0, Card.succ), c(2, 0, Card.dec), c(1, 0, Card.dbl), c(2, 0, Card.zero), c(1, 0, Card.inc), c(1, 0, Card.succ), // error in log here ] List expectedStates = [ '', '', '0={10000,zero}', '0={10000,inc}', '0={10000,const(1)}', '0={10001,I}', '0={10000,const(2)}', '0={10001,dec}', '0={10000,const(4)}\n255={9999,I}', '0={10001,I}', '4={10001,I}\n255={9999,I}' ] assertScriptModel(commands, expectedStates) } void testApplyCommand_sample2() { List commands = [ c(2, 0, Card.help), c(2, 0, Card.zero), c(1, 0, Card.K), c(1, 0, Card.S), c(2, 0, Card.succ), c(2, 0, Card.zero), c(2, 1, Card.zero), c(1, 1, Card.succ), c(1, 1, Card.dbl), c(1, 1, Card.dbl), c(1, 1, Card.dbl), c(1, 1, Card.dbl), c(1, 0, Card.K), c(1, 0, Card.S), c(2, 0, Card.get), c(1, 0, Card.K), c(1, 0, Card.S), c(2, 0, Card.succ), c(2, 0, Card.zero), ] List expectedStates = [ '', '0={10000,help}', '0={10000,help(zero)}', '0={10000,K(help(zero))}', '0={10000,S(K(help(zero)))}', '0={10000,S(K(help(zero)),succ)}', // was: '0={10000,S(K(help(zero)))(succ)}' - why is that? '0={10000,help(zero,const(1))}', '0={10000,help(zero,const(1))}\n1={10000,zero}', '0={10000,help(zero,const(1))}\n1={10000,const(1)}', '0={10000,help(zero,const(1))}\n1={10000,const(2)}', '0={10000,help(zero,const(1))}\n1={10000,const(4)}', '0={10000,help(zero,const(1))}\n1={10000,const(8)}', '0={10000,help(zero,const(1))}\n1={10000,const(16)}', '0={10000,K(help(zero,const(1)))}\n1={10000,const(16)}', '0={10000,S(K(help(zero,const(1))))}\n1={10000,const(16)}', '0={10000,S(K(help(zero,const(1))),get)}\n1={10000,const(16)}', '0={10000,K(S(K(help(zero,const(1))),get))}\n1={10000,const(16)}', '0={10000,S(K(S(K(help(zero,const(1))),get)))}\n1={10000,const(16)}', '0={10000,S(K(S(K(help(zero,const(1))),get)),succ)}\n1={10000,const(16)}', '0={9984,I}\n1={10017,const(16)}', ] assertScriptModel(commands, expectedStates, false) } void testApplyCommand_sample3() { List commands = [ c(2, 0, Card.S), c(2, 0, Card.get), c(2, 0, Card.I), c(2, 0, Card.zero), ] List expectedStates = [ '', '0={10000,S}', '0={10000,S(get)}', '0={10000,S(get,I)}', '', // overflow here ] assertScriptModel(commands, expectedStates, false) assertTrue 'Last turn must cause an infinite loop', controller.errorHappened } private void assertScriptModel(List commands, List expectedStates, boolean changePlayer = true) { for (int i=0; i < commands.size(); i++) { assertEquals "Game state mismatch at turn $i of player ${controller.game.currentProponent.name}", expectedStates[i], controller.game.currentProponent.slotsToString() controller.applyCommand(commands[i]) if (changePlayer) { controller.game.nextTurn() } } assertEquals expectedStates[-1], controller.game.currentProponent.slotsToString() } private Command c(int lor, int slot, Card card) { new Command(lor == 1 ? Application.LEFT : Application.RIGHT, slot, card) } }