114 lines
3.6 KiB
Java
114 lines
3.6 KiB
Java
package cat.hack3.codingtests.marsrover;
|
|
|
|
import org.testng.annotations.BeforeMethod;
|
|
import org.testng.annotations.Test;
|
|
|
|
import java.util.function.IntConsumer;
|
|
import java.util.stream.IntStream;
|
|
|
|
import static cat.hack3.codingtests.marsrover.Direction.*;
|
|
import static org.testng.Assert.assertEquals;
|
|
|
|
public class MarsRoverTest {
|
|
|
|
private MarsRover rover;
|
|
|
|
@BeforeMethod
|
|
public void setUp() {
|
|
int mapWidth = 10;
|
|
int mapHeight = 10;
|
|
int latitudeStartingPoint = 2;
|
|
int longitudeStartingPoint = 3;
|
|
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
|
|
var marsMap = new MarsMap(mapWidth, mapHeight, startingCoordinates);
|
|
rover = new MarsRover(marsMap, SOUTH);
|
|
}
|
|
|
|
@Test
|
|
public void stepForward() {
|
|
Direction originalDirection = rover.getCurrentDirection();
|
|
|
|
Coordinates oneStepForward = rover.moveForward();
|
|
|
|
assertEquals(oneStepForward, Coordinates.of(3, 3));
|
|
assertEquals(rover.getCurrentDirection(), originalDirection);
|
|
}
|
|
|
|
@Test
|
|
public void stepBackwards() {
|
|
Direction originalDirection = rover.getCurrentDirection();
|
|
|
|
Coordinates currentPosition = rover.moveBackwards();
|
|
|
|
assertEquals(currentPosition, Coordinates.of(1, 3));
|
|
assertEquals(rover.getCurrentDirection(), originalDirection);
|
|
}
|
|
|
|
@Test
|
|
public void turnLef() {
|
|
assertEquals(rover.turnLeft(), EAST);
|
|
assertEquals(rover.turnLeft(), NORTH);
|
|
assertEquals(rover.turnLeft(), WEST);
|
|
assertEquals(rover.turnLeft(), SOUTH);
|
|
assertEquals(rover.turnLeft(), EAST);
|
|
}
|
|
|
|
@Test
|
|
public void turnRight() {
|
|
assertEquals(rover.turnRight(), WEST);
|
|
assertEquals(rover.turnRight(), NORTH);
|
|
assertEquals(rover.turnRight(), EAST);
|
|
assertEquals(rover.turnRight(), SOUTH);
|
|
assertEquals(rover.turnRight(), WEST);
|
|
}
|
|
|
|
@Test
|
|
public void loopTheWorldInLatitudeMovingForward() {
|
|
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
|
Direction originalDirection = rover.getCurrentDirection();
|
|
|
|
repeatAction(10, i -> rover.moveForward());
|
|
|
|
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
|
assertEquals(rover.getCurrentDirection(), originalDirection);
|
|
}
|
|
|
|
@Test
|
|
public void loopTheWorldInLatitudeMovingBackwards() {
|
|
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
|
Direction originalDirection = rover.getCurrentDirection();
|
|
|
|
repeatAction(10, i -> rover.moveBackwards());
|
|
|
|
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
|
assertEquals(rover.getCurrentDirection(), originalDirection);
|
|
}
|
|
|
|
@Test
|
|
public void loopTheWorldInLongitudeMovingForward() {
|
|
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
|
|
|
rover.turnLeft();
|
|
repeatAction(10, i -> rover.moveForward());
|
|
|
|
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
|
assertEquals(rover.getCurrentDirection(), EAST);
|
|
}
|
|
|
|
@Test
|
|
public void loopTheWorldInLongitudeMovingBackwards() {
|
|
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
|
|
|
rover.turnLeft();
|
|
repeatAction(10, i -> rover.moveBackwards());
|
|
|
|
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
|
assertEquals(rover.getCurrentDirection(), EAST);
|
|
}
|
|
|
|
private void repeatAction(int times, IntConsumer actionToRepeat) {
|
|
IntStream.rangeClosed(1, times)
|
|
.forEach(actionToRepeat);
|
|
}
|
|
}
|