fixes and improved tests
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user