From 021cba609bf4b942467d5f3cfa02ab724f18c956 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Sun, 11 Mar 2018 19:50:28 +0100 Subject: [PATCH] fixes and improved tests --- OGP1718-Worms/src/worms/model/Worm.java | 9 +- OGP1718-Worms/tests/worms/model/WormTest.java | 157 ++++++++---------- 2 files changed, 73 insertions(+), 93 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 2f1209e..88bb050 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -368,7 +368,7 @@ public class Worm { * | actionPoints = 0; * |this.actionPoints = actionPoints; */ - public void setActionPoints(long actionPoints) { + private void setActionPoints(long actionPoints) { if (actionPoints > getMaxActionPoints()) actionPoints = getMaxActionPoints(); else if (actionPoints < 0) @@ -553,7 +553,7 @@ public class Worm { if (numberSteps < 0) throw new IllegalArgumentException(); // TODO add decent exception msg - long cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation()))); + long cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation()))) * numberSteps; if (cost > getActionPoints()) throw new IllegalArgumentException(); // TODO add decent exception msg @@ -589,7 +589,7 @@ public class Worm { */ public void turn(double angle) { assert canTurn(angle); - setOrientation((getOrientation() + angle) % (2 * PI)); + setOrientation(getOrientation() + angle); subtractActionPoints(abs(angle)); } @@ -605,7 +605,8 @@ public class Worm { | (getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0) ) */ private boolean canTurn(double angle) { - return 0 <= angle && angle < (2 * PI) && + double currentAngle = this.getOrientation(); + return 0 <= angle + currentAngle && angle + currentAngle < (2 * PI) && !Double.isNaN(angle) && getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0; } diff --git a/OGP1718-Worms/tests/worms/model/WormTest.java b/OGP1718-Worms/tests/worms/model/WormTest.java index 716f10d..9b41ce7 100644 --- a/OGP1718-Worms/tests/worms/model/WormTest.java +++ b/OGP1718-Worms/tests/worms/model/WormTest.java @@ -1,116 +1,95 @@ package worms.model; -import org.junit.jupiter.api.*; - +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.RepetitionInfo; import static org.junit.jupiter.api.Assertions.*; import worms.util.Tuple; +import java.util.Random; + class WormTest { - - /*private Worm worm; - - @BeforeEach - void setUp() { - worm = new Worm(Tuple.create(0.0, 0.0), 0, "Test", 1); - } - - @Test - void getLocation() { - - assertEquals(Tuple.create(0.0, 0.0), worm.getLocation()); - } - - @Test - void getOrientation() { - assertEquals(0, worm.getOrientation()); - } - - @Test - void getRadius() { - assertEquals(1, worm.getRadius()); - } - - @Test - void setRadius() { - worm.setRadius(2); - assertEquals(2, worm.getRadius()); - } - - @Test - void getMinimumRadius() { - assertEquals(0.25, worm.getMinRadius()); - } - - @Test - void getMass1() { - assertEquals(4448.0, worm.getMass()); - - //Leen: ik heb bij de setMass round rond de formule gezet, anders was test fout. - //Moet dit round of ceil zijn? Ik dacht round, maar ben niet zeker - } - - @Test - void getActionPoints() { - assertEquals(4448.0, worm.getActionPoints()); - } - - @Test - void getMaxActionPoints() { - assertEquals(4448.0, worm.getMaxActionPoints()); - } - - @Test - void setActionPoints() { - worm.setActionPoints(300); - assertEquals(300, worm.getActionPoints()); - } - - @Test - void decreaseActionPoints() { - worm.decreaseActionPoints(48); - assertEquals(4400, worm.getActionPoints()); - } - - @Test - void getName() { - } - - @Test - void setName() { - worm.setName("Jefke Janssens"); - assertEquals("Jefke Janssens", worm.getName()); - } - - @Test - void setName2() { - worm.setName("Jefke 'anssens"); - assertEquals("Jefke 'anssens", worm.getName()); - }*/ - + private static Worm worm; + private static Random r = new Random(); @BeforeAll static void setUp() { worm = new Worm(Tuple.create(0.0, 0.0), 0, "Test", 1); } - @Test + @RepeatedTest(5000) void move() { - assertEquals("Test", worm.getName()); + worm = new Worm(Tuple.create(0.0, 0.0), 0, "Test", 1); + int steps = r.nextInt(5101) - 100; + double maxAngle = Math.abs(2 * Math.PI - worm.getOrientation()); + double minAngle = worm.getOrientation(); + double combAngle = maxAngle + minAngle; + double angle = r.nextDouble() * combAngle - minAngle; + worm.turn(angle); + long cost = (long) Math.ceil(Math.abs(Math.cos(worm.getOrientation())) + Math.abs(4 * Math.sin(worm.getOrientation()))) * steps; + if (steps < 0 || cost > worm.getActionPoints()) { + assertThrows(IllegalArgumentException.class, () -> worm.move(steps)); + } + else { + + double newLocX = worm.getLocation().item1 + steps * worm.getRadius() * Math.cos(worm.getOrientation()); + double newLocY = worm.getLocation().item2 + steps * worm.getRadius() * Math.sin(worm.getOrientation()); + worm.move(steps); + assertEquals(Tuple.create(newLocX, newLocY), worm.getLocation()); + } + } + + @RepeatedTest(5000) + void turn() { + double angle = r.nextDouble() * 4 * Math.PI - 2 * Math.PI; + double oldAngle = worm.getOrientation(); + + if (0 > oldAngle + angle || 2 * Math.PI <= oldAngle + angle || worm.getActionPoints() - (long) Math.ceil(Math.toDegrees(angle) / 6) < 0) { + assertThrows(AssertionError.class, () -> worm.turn(angle)); + worm = new Worm(Tuple.create(0.0, 0.0), 0, "Test", 1); + } + else { + worm.turn(angle); + assertEquals(oldAngle + angle, worm.getOrientation()); + } + } @Test - void turn() { + void turnInvalidValue() { + assertThrows(AssertionError.class, () -> worm.turn(Double.NaN)); } - @RepeatedTest(10) + @RepeatedTest(5000) void jump(RepetitionInfo repetitionInfo) { - if (repetitionInfo.getCurrentRepetition() > 1){ + + worm = new Worm(Tuple.create(0.0, 0.0), 0, "Test", 1); + double maxAngle = Math.abs(2 * Math.PI - worm.getOrientation()); + double minAngle = worm.getOrientation(); + double combAngle = maxAngle + minAngle; + double angle = r.nextDouble() * combAngle - minAngle; + worm.turn(angle); + int steps = r.nextInt(100); + worm.move(steps); + + if (worm.getOrientation() >= Math.PI || worm.getActionPoints() == 0){ assertThrows(IllegalStateException.class, () -> worm.jump()); } else { + double orient = worm.getOrientation(); + double force = 5 * worm.getActionPoints() + worm.getMass() * worm.G; + double v = force / worm.getMass() * worm.FORCE_TIME; + double newLocX = worm.getLocation().item1 + Math.pow(v, 2) * Math.sin(2 * orient) / worm.G; worm.jump(); + assertEquals(Tuple.create(newLocX, worm.getLocation().item2), worm.getLocation()); } - + } + @Test + void doubleJump() { + worm = new Worm(Tuple.create(0.0, 0.0), 0, "Test", 1); + worm.jump(); + assertThrows(IllegalStateException.class, () -> worm.jump()); } } \ No newline at end of file