fixes and improved tests

This commit is contained in:
2018-03-11 19:50:28 +01:00
parent ba22d855a8
commit 021cba609b
2 changed files with 73 additions and 93 deletions

View File

@@ -368,7 +368,7 @@ public class Worm {
* | actionPoints = 0; * | actionPoints = 0;
* |this.actionPoints = actionPoints; * |this.actionPoints = actionPoints;
*/ */
public void setActionPoints(long actionPoints) { private void setActionPoints(long actionPoints) {
if (actionPoints > getMaxActionPoints()) if (actionPoints > getMaxActionPoints())
actionPoints = getMaxActionPoints(); actionPoints = getMaxActionPoints();
else if (actionPoints < 0) else if (actionPoints < 0)
@@ -553,7 +553,7 @@ public class Worm {
if (numberSteps < 0) if (numberSteps < 0)
throw new IllegalArgumentException(); // TODO add decent exception msg 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()) if (cost > getActionPoints())
throw new IllegalArgumentException(); // TODO add decent exception msg throw new IllegalArgumentException(); // TODO add decent exception msg
@@ -589,7 +589,7 @@ public class Worm {
*/ */
public void turn(double angle) { public void turn(double angle) {
assert canTurn(angle); assert canTurn(angle);
setOrientation((getOrientation() + angle) % (2 * PI)); setOrientation(getOrientation() + angle);
subtractActionPoints(abs(angle)); subtractActionPoints(abs(angle));
} }
@@ -605,7 +605,8 @@ public class Worm {
| (getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0) ) | (getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0) )
*/ */
private boolean canTurn(double angle) { 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) && !Double.isNaN(angle) &&
getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0; getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0;
} }

View File

@@ -1,116 +1,95 @@
package worms.model; 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 static org.junit.jupiter.api.Assertions.*;
import worms.util.Tuple; import worms.util.Tuple;
import java.util.Random;
class WormTest { 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 Worm worm;
private static Random r = new Random();
@BeforeAll @BeforeAll
static void setUp() { static void setUp() {
worm = new Worm(Tuple.create(0.0, 0.0), 0, "Test", 1); worm = new Worm(Tuple.create(0.0, 0.0), 0, "Test", 1);
} }
@Test @RepeatedTest(5000)
void move() { 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 @Test
void turn() { void turnInvalidValue() {
assertThrows(AssertionError.class, () -> worm.turn(Double.NaN));
} }
@RepeatedTest(10) @RepeatedTest(5000)
void jump(RepetitionInfo repetitionInfo) { 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()); assertThrows(IllegalStateException.class, () -> worm.jump());
} }
else { 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(); 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());
} }
} }