From 907b35ac481bc81f1b359b0c23b5a90784068974 Mon Sep 17 00:00:00 2001 From: tpariney Date: Thu, 2 Oct 2025 10:28:52 +0200 Subject: [PATCH] Add more tests --- .../m1info/gl/tpariney/BatteryUnitTest.java | 5 +- .../m1info/gl/tpariney/MapToolsUnitTests.java | 15 ++ pom.xml | 9 ++ roadBook/pom.xml | 10 ++ .../tpariney/CalculateRoadBookUnitTests.java | 137 +++++++++++++++++ .../m1info/gl/tpariney/RoadBookUnitTests.java | 36 +++++ .../gl/tpariney/LandSensorUnitTests.java | 17 +++ .../m1info/gl/tpariney/RobotUnitTests.java | 141 ++++++++++++++++++ 8 files changed, 367 insertions(+), 3 deletions(-) create mode 100644 roadBook/src/test/java/fr/ufrst/m1info/gl/tpariney/CalculateRoadBookUnitTests.java create mode 100644 roadBook/src/test/java/fr/ufrst/m1info/gl/tpariney/RoadBookUnitTests.java create mode 100644 robot/src/test/java/fr/ufrst/m1info/gl/tpariney/RobotUnitTests.java diff --git a/battery/src/test/java/fr/ufrst/m1info/gl/tpariney/BatteryUnitTest.java b/battery/src/test/java/fr/ufrst/m1info/gl/tpariney/BatteryUnitTest.java index 2464cd3..5aacdb6 100644 --- a/battery/src/test/java/fr/ufrst/m1info/gl/tpariney/BatteryUnitTest.java +++ b/battery/src/test/java/fr/ufrst/m1info/gl/tpariney/BatteryUnitTest.java @@ -9,7 +9,6 @@ import static org.awaitility.Awaitility.await; public class BatteryUnitTest { - @Test public void testCharge() { Battery cell = new Battery(); @@ -19,7 +18,7 @@ public class BatteryUnitTest { } @Test - public void testSetup() throws InterruptedException { + public void testSetup() { Battery cell = new Battery(); Assert.assertEquals(100f, cell.getChargeLevel(), 0); cell.setUp(); @@ -54,6 +53,6 @@ public class BatteryUnitTest { @Test public void testTimeToSufficientCharge() { Battery cell = new Battery(); - Assert.assertEquals(7000,cell.timeToSufficientCharge(200)); + Assert.assertEquals(7000, cell.timeToSufficientCharge(200)); } } diff --git a/maptools/src/test/java/fr/ufrst/m1info/gl/tpariney/MapToolsUnitTests.java b/maptools/src/test/java/fr/ufrst/m1info/gl/tpariney/MapToolsUnitTests.java index 4f68915..b8bdcf2 100644 --- a/maptools/src/test/java/fr/ufrst/m1info/gl/tpariney/MapToolsUnitTests.java +++ b/maptools/src/test/java/fr/ufrst/m1info/gl/tpariney/MapToolsUnitTests.java @@ -3,7 +3,22 @@ package fr.ufrst.m1info.gl.tpariney; import org.junit.Assert; import org.junit.Test; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + public class MapToolsUnitTests { + @Test(expected = IllegalStateException.class) + public void noConstructor() throws Throwable { + Constructor toolsConstructor = MapTools.class.getDeclaredConstructor(); + toolsConstructor.setAccessible(true); + try { + MapTools tools = toolsConstructor.newInstance(); + } + catch (InvocationTargetException e) { + throw e.getCause(); + } + } + @Test public void nextForwardPosition() { Coordinates position = new Coordinates(0, 0); diff --git a/pom.xml b/pom.xml index 15eb1dc..3eaa6bc 100644 --- a/pom.xml +++ b/pom.xml @@ -13,11 +13,20 @@ 3.8.1 test + + org.mockito + mockito-core + ${mockito.version} + test + ${project.basedir}/target/site/jacoco/jacoco.xml + + 5.20.0 + diff --git a/roadBook/pom.xml b/roadBook/pom.xml index 6aca547..59d38b1 100644 --- a/roadBook/pom.xml +++ b/roadBook/pom.xml @@ -77,5 +77,15 @@ + + + org.apache.maven.plugins + maven-compiler-plugin + + 9 + 9 + + + diff --git a/roadBook/src/test/java/fr/ufrst/m1info/gl/tpariney/CalculateRoadBookUnitTests.java b/roadBook/src/test/java/fr/ufrst/m1info/gl/tpariney/CalculateRoadBookUnitTests.java new file mode 100644 index 0000000..0968b23 --- /dev/null +++ b/roadBook/src/test/java/fr/ufrst/m1info/gl/tpariney/CalculateRoadBookUnitTests.java @@ -0,0 +1,137 @@ +package fr.ufrst.m1info.gl.tpariney; + +import org.junit.Assert; +import org.junit.Test; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; + +public class CalculateRoadBookUnitTests { + @Test(expected = IllegalStateException.class) + public void noConstructor() throws Throwable { + Constructor constructor = RoadBookCalculator.class.getDeclaredConstructor(); + constructor.setAccessible(true); + try { + RoadBookCalculator calculator = constructor.newInstance(); + } + catch (InvocationTargetException e) { + throw e.getCause(); + } + } + + @Test + public void calculateNoMove() { + RoadBook roadBook = RoadBookCalculator.calculateRoadBook( + Direction.NORTH, + new Coordinates(0, 0), + new Coordinates(0, 0), + new ArrayList<>() + ); + Assert.assertFalse(roadBook.hasInstruction()); + } + + @Test + public void calculateForwardOnly() { + RoadBook roadBook = RoadBookCalculator.calculateRoadBook( + Direction.EAST, + new Coordinates(0, 0), + new Coordinates(2, 0), + new ArrayList<>() + ); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.FORWARD, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.FORWARD, roadBook.next()); + Assert.assertFalse(roadBook.hasInstruction()); + } + + @Test + public void calculateTurnAround() { + RoadBook roadBook = RoadBookCalculator.calculateRoadBook( + Direction.NORTH, + new Coordinates(0, 0), + new Coordinates(0, -1), + new ArrayList<>() + ); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.TURNRIGHT, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.TURNRIGHT, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.FORWARD, roadBook.next()); + Assert.assertFalse(roadBook.hasInstruction()); + } + + @Test + public void calculateTurnRight() { + RoadBook roadBook = RoadBookCalculator.calculateRoadBook( + Direction.WEST, + new Coordinates(0, 0), + new Coordinates(0, 1), + new ArrayList<>() + ); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.TURNRIGHT, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.FORWARD, roadBook.next()); + Assert.assertFalse(roadBook.hasInstruction()); + } + + @Test + public void calculateTurnThreeQuarters() { + RoadBook roadBook = RoadBookCalculator.calculateRoadBook( + Direction.SOUTH, + new Coordinates(-1, 0), + new Coordinates(0, 0), + new ArrayList<>() + ); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.TURNRIGHT, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.TURNRIGHT, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.TURNRIGHT, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.FORWARD, roadBook.next()); + Assert.assertFalse(roadBook.hasInstruction()); + } + + @Test + public void calculateDiagonal() { + RoadBook roadBook = RoadBookCalculator.calculateRoadBook( + Direction.NORTH, + new Coordinates(-9, 4), + new Coordinates(-10, 5), + new ArrayList<>() + ); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.FORWARD, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.TURNRIGHT, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.TURNRIGHT, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.TURNRIGHT, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.FORWARD, roadBook.next()); + Assert.assertFalse(roadBook.hasInstruction()); + } + + @Test + public void calculateDiagonalOtherDirection() { + RoadBook roadBook = RoadBookCalculator.calculateRoadBook( + Direction.WEST, + new Coordinates(-9, 4), + new Coordinates(-10, 5), + new ArrayList<>() + ); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.FORWARD, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.TURNRIGHT, roadBook.next()); + Assert.assertTrue(roadBook.hasInstruction()); + Assert.assertEquals(Instruction.FORWARD, roadBook.next()); + Assert.assertFalse(roadBook.hasInstruction()); + } +} diff --git a/roadBook/src/test/java/fr/ufrst/m1info/gl/tpariney/RoadBookUnitTests.java b/roadBook/src/test/java/fr/ufrst/m1info/gl/tpariney/RoadBookUnitTests.java new file mode 100644 index 0000000..b7ab647 --- /dev/null +++ b/roadBook/src/test/java/fr/ufrst/m1info/gl/tpariney/RoadBookUnitTests.java @@ -0,0 +1,36 @@ +package fr.ufrst.m1info.gl.tpariney; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class RoadBookUnitTests { + @Test + public void hasInstruction() { + RoadBook roadBook = new RoadBook(List.of(Instruction.TURNLEFT, Instruction.TURNLEFT)); + Assert.assertTrue(roadBook.hasInstruction()); + } + + @Test + public void hasInstructionEmpty() { + RoadBook roadBook = new RoadBook(List.of()); + Assert.assertFalse(roadBook.hasInstruction()); + } + + @Test + public void nextInstruction() { + RoadBook roadBook = new RoadBook(List.of(Instruction.TURNLEFT, Instruction.FORWARD)); + Assert.assertEquals(Instruction.TURNLEFT, roadBook.next()); + Assert.assertEquals(Instruction.FORWARD, roadBook.next()); + } + + @Test + public void hasInstructionAfterNext() { + RoadBook roadBook = new RoadBook(List.of(Instruction.TURNLEFT, Instruction.FORWARD)); + roadBook.next(); + Assert.assertTrue(roadBook.hasInstruction()); + roadBook.next(); + Assert.assertFalse(roadBook.hasInstruction()); + } +} diff --git a/robot/src/test/java/fr/ufrst/m1info/gl/tpariney/LandSensorUnitTests.java b/robot/src/test/java/fr/ufrst/m1info/gl/tpariney/LandSensorUnitTests.java index 64e5ff4..8d1486f 100644 --- a/robot/src/test/java/fr/ufrst/m1info/gl/tpariney/LandSensorUnitTests.java +++ b/robot/src/test/java/fr/ufrst/m1info/gl/tpariney/LandSensorUnitTests.java @@ -16,4 +16,21 @@ public class LandSensorUnitTests { Assert.assertEquals(1, LandSensor.distance(pos, pos2), 0); Assert.assertEquals(Math.sqrt(2), LandSensor.distance(pos, pos3), 0); } + + @Test + public void pointToEnergyCoefficient() { + Coordinates pos = new Coordinates(0, 0); + Coordinates pos1 = new Coordinates(1, 0); + Coordinates pos2 = new Coordinates(1, 1); + + Random sensorRandom = new Random(1524986531); + Random random = new Random(1524986531); + LandSensor landSensor = new LandSensor(sensorRandom); + + double r1 = random.nextDouble(); + Assert.assertEquals(1.0 + 1.0 / r1, landSensor.getPointToPointEnergyCoefficient(pos, pos1), 0); + + double r2 = random.nextDouble(); + Assert.assertEquals(1.0 + Math.sqrt(2) / (Math.sqrt(2) * r2), landSensor.getPointToPointEnergyCoefficient(pos, pos2), 0); + } } diff --git a/robot/src/test/java/fr/ufrst/m1info/gl/tpariney/RobotUnitTests.java b/robot/src/test/java/fr/ufrst/m1info/gl/tpariney/RobotUnitTests.java new file mode 100644 index 0000000..9412f9c --- /dev/null +++ b/robot/src/test/java/fr/ufrst/m1info/gl/tpariney/RobotUnitTests.java @@ -0,0 +1,141 @@ +package fr.ufrst.m1info.gl.tpariney; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +public class RobotUnitTests { + // getX tests + + @Test(expected = UnlandedRobotException.class) + public void getXUnlanded() throws UnlandedRobotException { + Robot robot = new Robot(); + int x = robot.getXposition(); + } + + @Test + public void getX() throws UnlandedRobotException { + Robot robot = new Robot(); + robot.land(new Coordinates(5, 9), Mockito.mock(LandSensor.class)); + Assert.assertEquals(5, robot.getXposition()); + } + + // getY tests + + @Test(expected = UnlandedRobotException.class) + public void getYUnlanded() throws UnlandedRobotException { + Robot robot = new Robot(); + int y = robot.getYposition(); + } + + @Test + public void getY() throws UnlandedRobotException { + Robot robot = new Robot(); + robot.land(new Coordinates(5, 9), Mockito.mock(LandSensor.class)); + Assert.assertEquals(9, robot.getYposition()); + } + + // getDirection tests + + @Test(expected = UnlandedRobotException.class) + public void getDirectionUnlanded() throws UnlandedRobotException { + Robot robot = new Robot(); + Direction direction = robot.getDirection(); + } + + @Test + public void getDirection() throws UnlandedRobotException { + Robot robot = new Robot(); + robot.land(new Coordinates(5, 9), Mockito.mock(LandSensor.class)); + Assert.assertEquals(Direction.NORTH, robot.getDirection()); + } + + // moveForward tests + + @Test(expected = UnlandedRobotException.class) + public void moveForwardUnlanded() throws UnlandedRobotException, InterruptedException { + Robot robot = new Robot(); + robot.moveForward(); + } + + @Test + public void moveForward() throws UnlandedRobotException, InterruptedException { + Robot robot = new Robot(); + robot.land(new Coordinates(5, 9), Mockito.mock(LandSensor.class)); + robot.moveForward(); + Assert.assertEquals(Direction.NORTH, robot.getDirection()); + Assert.assertEquals(5, robot.getXposition()); + Assert.assertEquals(10, robot.getYposition()); + } + + // moveBackward tests + + @Test(expected = UnlandedRobotException.class) + public void moveBackwardUnlanded() throws UnlandedRobotException { + Robot robot = new Robot(); + robot.moveBackward(); + } + + @Test + public void moveBackward() throws UnlandedRobotException, InterruptedException { + Robot robot = new Robot(); + robot.land(new Coordinates(5, 9), Mockito.mock(LandSensor.class)); + robot.moveBackward(); + Assert.assertEquals(Direction.NORTH, robot.getDirection()); + Assert.assertEquals(5, robot.getXposition()); + Assert.assertEquals(8, robot.getYposition()); + } + + // turnLeft tests + + @Test(expected = UnlandedRobotException.class) + public void turnLeftUnlanded() throws UnlandedRobotException { + Robot robot = new Robot(); + robot.turnLeft(); + } + + @Test + public void turnLeft() throws UnlandedRobotException { + Robot robot = new Robot(); + robot.land(new Coordinates(5, 9), Mockito.mock(LandSensor.class)); + robot.turnLeft(); + Assert.assertEquals(Direction.WEST, robot.getDirection()); + } + + // turnRight tests + + @Test(expected = UnlandedRobotException.class) + public void turnRightUnlanded() throws UnlandedRobotException { + Robot robot = new Robot(); + robot.turnRight(); + } + + @Test + public void turnRight() throws UnlandedRobotException { + Robot robot = new Robot(); + robot.land(new Coordinates(5, 9), Mockito.mock(LandSensor.class)); + robot.turnRight(); + Assert.assertEquals(Direction.EAST, robot.getDirection()); + } + + @Test(expected = UnlandedRobotException.class) + public void computeRoadToUnlanded() throws UnlandedRobotException { + Robot robot = new Robot(); + robot.computeRoadTo(new Coordinates(0, 0)); + } + + @Test(expected = UndefinedRoadbookException.class) + public void goNoRoadbook() throws UnlandedRobotException, UndefinedRoadbookException, InterruptedException { + Robot robot = new Robot(); + robot.letsGo(); + } + + @Test + public void land() throws UnlandedRobotException { + Robot robot = new Robot(); + robot.land(new Coordinates(7, 3), Mockito.mock(LandSensor.class)); + Assert.assertEquals(7, robot.getXposition()); + Assert.assertEquals(3, robot.getYposition()); + Assert.assertEquals(Direction.NORTH, robot.getDirection()); + } +} -- GitLab