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 2464cd3654418e55c79b9347ae29dafe0c41fb1c..5aacdb6bbfcc6fb27408b6d5a47d38ac713fc98b 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 4f68915e3da0f81ac2be7b509ca4cec2b97b92da..b8bdcf22a4979be2f30cd6780ac846a7bdad6659 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 15eb1dcaed7a21d2dda7fc8d3b811e67f32f8862..3eaa6bc2d0ab2628d850763f689af039344239ee 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 6aca547e2956a35f7d33f7e2dbe623dbd836d1dc..59d38b15307c3fa61f6c7d37969659951f3b9737 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 0000000000000000000000000000000000000000..0968b23c5b5a0e05220aa02d571301cfa7adb3c1 --- /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 0000000000000000000000000000000000000000..b7ab6478779910802ea1d7107420b55ea1854d34 --- /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 64e5ff4fa8def26c8631d3fe15d6a2ec333401be..8d1486f04669431c3a193c0a19487209583136c0 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 0000000000000000000000000000000000000000..9412f9ca37ca548752ef040871f30604a7064815 --- /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()); + } +}