287 lines
12 KiB
Java
287 lines
12 KiB
Java
|
|
/**
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2021 Exercism
|
|
* Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework;
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test;
|
|
|
|
public class RobotTest {
|
|
|
|
private static final int MAX_POINTS = 100;
|
|
private static final int TEST_CASES = 18;
|
|
private static final int SUCCESS_POINTS = MAX_POINTS / TEST_CASES;
|
|
private static final int FAILURE_POINTS = 0;
|
|
|
|
private final java.util.List<Integer> testPoints = new java.util.ArrayList<>();
|
|
|
|
/* Create robot */
|
|
|
|
@Test(name = "at origin facing north")
|
|
public void atOriginFacingNorth() {
|
|
runTest(() -> {
|
|
Orientation initialOrientation = Orientation.NORTH;
|
|
GridPosition initialGridPosition = new GridPosition(0, 0);
|
|
Robot robot = new Robot(initialGridPosition, initialOrientation);
|
|
assertEquals(initialOrientation, robot.getOrientation());
|
|
assertEquals(initialGridPosition, robot.getGridPosition());
|
|
});
|
|
}
|
|
|
|
@Test(name = "at negative position facing south")
|
|
public void atNegativePositionFacingSouth() {
|
|
runTest(() -> {
|
|
GridPosition initialGridPosition = new GridPosition(-1, -1);
|
|
Orientation initialOrientation = Orientation.SOUTH;
|
|
Robot robot = new Robot(initialGridPosition, initialOrientation);
|
|
assertEquals(initialOrientation, robot.getOrientation());
|
|
assertEquals(initialGridPosition, robot.getGridPosition());
|
|
});
|
|
}
|
|
|
|
/* Rotating clockwise */
|
|
|
|
@Test(name = "changes north to east")
|
|
public void changesNorthToEast() {
|
|
runTest(() -> {
|
|
GridPosition initialGridPosition = new GridPosition(0, 0);
|
|
Robot robot = new Robot(initialGridPosition, Orientation.NORTH);
|
|
robot.turnRight();
|
|
Orientation expectedOrientation = Orientation.EAST;
|
|
assertEquals(initialGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "changes east to south")
|
|
public void changesEastToSouth() {
|
|
runTest(() -> {
|
|
GridPosition initialGridPosition = new GridPosition(0, 0);
|
|
Robot robot = new Robot(initialGridPosition, Orientation.EAST);
|
|
robot.turnRight();
|
|
Orientation expectedOrientation = Orientation.SOUTH;
|
|
assertEquals(initialGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "changes south to west")
|
|
public void changesSouthToWest() {
|
|
runTest(() -> {
|
|
GridPosition initialGridPosition = new GridPosition(0, 0);
|
|
Robot robot = new Robot(initialGridPosition, Orientation.SOUTH);
|
|
robot.turnRight();
|
|
Orientation expectedOrientation = Orientation.WEST;
|
|
assertEquals(initialGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "changes west to north")
|
|
public void changesWestToNorth() {
|
|
runTest(() -> {
|
|
GridPosition initialGridPosition = new GridPosition(0, 0);
|
|
Robot robot = new Robot(initialGridPosition, Orientation.WEST);
|
|
robot.turnRight();
|
|
Orientation expectedOrientation = Orientation.NORTH;
|
|
assertEquals(initialGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
/* Rotating counter-clockwise */
|
|
|
|
@Test(name = "changes north to west")
|
|
public void changesNorthToWest() {
|
|
runTest(() -> {
|
|
GridPosition initialGridPosition = new GridPosition(0, 0);
|
|
Robot robot = new Robot(initialGridPosition, Orientation.NORTH);
|
|
robot.turnLeft();
|
|
Orientation expectedOrientation = Orientation.WEST;
|
|
assertEquals(initialGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "changes west to south")
|
|
public void changesWestToSouth() {
|
|
runTest(() -> {
|
|
GridPosition initialGridPosition = new GridPosition(0, 0);
|
|
Robot robot = new Robot(initialGridPosition, Orientation.WEST);
|
|
robot.turnLeft();
|
|
Orientation expectedOrientation = Orientation.SOUTH;
|
|
assertEquals(initialGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "changes south to east")
|
|
public void changesSouthToEast() {
|
|
runTest(() -> {
|
|
GridPosition initialGridPosition = new GridPosition(0, 0);
|
|
Robot robot = new Robot(initialGridPosition, Orientation.SOUTH);
|
|
robot.turnLeft();
|
|
Orientation expectedOrientation = Orientation.EAST;
|
|
assertEquals(initialGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "changes east to north")
|
|
public void changesEastToNorth() {
|
|
runTest(() -> {
|
|
GridPosition initialGridPosition = new GridPosition(0, 0);
|
|
Robot robot = new Robot(initialGridPosition, Orientation.EAST);
|
|
robot.turnLeft();
|
|
Orientation expectedOrientation = Orientation.NORTH;
|
|
assertEquals(initialGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
/* Moving forward one */
|
|
|
|
@Test(name = "facing north increments Y")
|
|
public void facingNorthIncrementsY() {
|
|
runTest(() -> {
|
|
Orientation initialOrientation = Orientation.NORTH;
|
|
Robot robot = new Robot(new GridPosition(0, 0), initialOrientation);
|
|
robot.advance();
|
|
GridPosition expectedGridPosition = new GridPosition(0, 1);
|
|
assertEquals(expectedGridPosition, robot.getGridPosition());
|
|
assertEquals(initialOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "facing south decrements Y")
|
|
public void facingSouthDecrementsY() {
|
|
runTest(() -> {
|
|
Orientation initialOrientation = Orientation.SOUTH;
|
|
Robot robot = new Robot(new GridPosition(0, 0), initialOrientation);
|
|
robot.advance();
|
|
GridPosition expectedGridPosition = new GridPosition(0, -1);
|
|
assertEquals(expectedGridPosition, robot.getGridPosition());
|
|
assertEquals(initialOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "facing east increments X")
|
|
public void facingEastIncrementsX() {
|
|
runTest(() -> {
|
|
Orientation initialOrientation = Orientation.EAST;
|
|
Robot robot = new Robot(new GridPosition(0, 0), initialOrientation);
|
|
robot.advance();
|
|
GridPosition expectedGridPosition = new GridPosition(1, 0);
|
|
assertEquals(expectedGridPosition, robot.getGridPosition());
|
|
assertEquals(initialOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "facing west decrements X")
|
|
public void facingWestDecrementsX() {
|
|
runTest(() -> {
|
|
Orientation initialOrientation = Orientation.WEST;
|
|
Robot robot = new Robot(new GridPosition(0, 0), initialOrientation);
|
|
robot.advance();
|
|
GridPosition expectedGridPosition = new GridPosition(-1, 0);
|
|
assertEquals(expectedGridPosition, robot.getGridPosition());
|
|
assertEquals(initialOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
/* Follow series of instructions */
|
|
|
|
@Test(name = "moving east and north from README")
|
|
public void movingEastAndNorthFromReadme() {
|
|
runTest(() -> {
|
|
Robot robot = new Robot(new GridPosition(7, 3), Orientation.NORTH);
|
|
robot.simulate("RAALAL");
|
|
GridPosition expectedGridPosition = new GridPosition(9, 4);
|
|
Orientation expectedOrientation = Orientation.WEST;
|
|
assertEquals(expectedGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "moving west and north")
|
|
public void movingWestAndNorth() {
|
|
runTest(() -> {
|
|
Robot robot = new Robot(new GridPosition(0, 0), Orientation.NORTH);
|
|
robot.simulate("LAAARALA");
|
|
GridPosition expectedGridPosition = new GridPosition(-4, 1);
|
|
Orientation expectedOrientation = Orientation.WEST;
|
|
assertEquals(expectedGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "moving west and south")
|
|
public void movingWestAndSouth() {
|
|
runTest(() -> {
|
|
Robot robot = new Robot(new GridPosition(2, -7), Orientation.EAST);
|
|
robot.simulate("RRAAAAALA");
|
|
GridPosition expectedGridPosition = new GridPosition(-3, -8);
|
|
Orientation expectedOrientation = Orientation.SOUTH;
|
|
assertEquals(expectedGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
@Test(name = "moving east and north")
|
|
public void movingEastAndNorth() {
|
|
runTest(() -> {
|
|
Robot robot = new Robot(new GridPosition(8, 4), Orientation.SOUTH);
|
|
robot.simulate("LAAARRRALLLL");
|
|
GridPosition expectedGridPosition = new GridPosition(11, 5);
|
|
Orientation expectedOrientation = Orientation.NORTH;
|
|
assertEquals(expectedGridPosition, robot.getGridPosition());
|
|
assertEquals(expectedOrientation, robot.getOrientation());
|
|
});
|
|
}
|
|
|
|
private void runTest(Runnable body) {
|
|
try {
|
|
body.run();
|
|
testPoints.add(SUCCESS_POINTS);
|
|
} catch (Exception | AssertionError t) {
|
|
TracingFramework.printError(t.getMessage());
|
|
testPoints.add(FAILURE_POINTS);
|
|
}
|
|
}
|
|
|
|
private void assertEquals(Object expected, Object actual) {
|
|
if (expected == null && actual == null)
|
|
return;
|
|
if (expected != null && expected.equals(actual))
|
|
return;
|
|
throw new AssertionError("Expected '" + expected + "' but got '" + actual + "'");
|
|
}
|
|
|
|
public int getResult() {
|
|
int sum = testPoints.stream().mapToInt(Integer::intValue).sum();
|
|
return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum;
|
|
}
|
|
}
|