Files
worms/OGP1718-Worms/tests/worms/model/WormTest.java
2018-03-11 22:55:37 +01:00

94 lines
3.4 KiB
Java

package worms.model;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.RepeatedTest;
import static org.junit.jupiter.api.Assertions.*;
import worms.util.Tuple;
import java.util.Random;
class WormTest {
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);
}
@RepeatedTest(5000)
void move() {
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 (!Worm.isValidOrientation(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 turnInvalidValue() {
assertThrows(AssertionError.class, () -> worm.turn(Double.NaN));
}
@RepeatedTest(5000)
void jump() {
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());
}
}