/** * 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 java.lang.reflect.Method; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.annotation.ElementType; public class RobotMain { @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") @Disabled 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()); }); } @Test(name = "changes north to east") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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()); }); } @Test(name = "changes north to west") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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()); }); } @Test(name = "facing north increments Y") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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()); }); } @Test(name = "moving east and north from README") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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(); System.out.println("TEST PASSED"); } catch (Exception | AssertionError t) { System.out.println("TEST FAILED: " + t.getMessage()); } } 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 static void main(String[] args) { new RobotMain().runTests(); } /** * Annotation to mark test methods. The name parameter is used to identify the * test in the output. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) private @interface Test { String name(); } /** * Annotation to mark test methods as disabled. Disabled tests will not be * executed when running the tests. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) private @interface Disabled { } private void runTests() { Method[] methods = this.getClass().getDeclaredMethods(); List allTestMethods = Arrays.asList(methods).stream().filter(m -> m.isAnnotationPresent(Test.class)) .collect(Collectors.toList()); List enabledTestMethods = allTestMethods.stream().filter(m -> !m.isAnnotationPresent(Disabled.class)) .collect(Collectors.toList()); System.out .println("Running " + enabledTestMethods.size() + " out of " + allTestMethods.size() + " tests... \n"); for (Method method : enabledTestMethods) { try { System.out.println("Running test: " + method.getAnnotation(Test.class).name()); method.invoke(this); } catch (Exception e) { System.out.println("Test " + method.getAnnotation(Test.class).name() + " failed with an unexpected exception: " + e.getCause()); } } System.out.println("Finished running tests."); if (allTestMethods.size() != enabledTestMethods.size()) { System.out.println("\nThe following tests are disabled and were not executed:"); for (Method method : allTestMethods) { if (method.isAnnotationPresent(Disabled.class)) { System.out.println("- " + method.getAnnotation(Test.class).name()); } } System.out.println( "To enable these tests, remove the @Disabled annotation from the corresponding test methods."); } } }