From 884b3490770849e8a8136306fef785b3b959b956 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 11 Mar 2022 16:01:49 +0100 Subject: [PATCH] - TrackTest done Track done --- .../pm2/racetrack/PositionVectorNotValid.java | 9 + .../java/ch/zhaw/pm2/racetrack/Track.java | 64 +++--- src/test/InvalidTracks/sameCar.txt | 26 +++ .../java/ch/zhaw/pm2/racetrack/TrackTest.java | 205 +++++++++++++----- 4 files changed, 223 insertions(+), 81 deletions(-) create mode 100644 src/main/java/ch/zhaw/pm2/racetrack/PositionVectorNotValid.java create mode 100644 src/test/InvalidTracks/sameCar.txt diff --git a/src/main/java/ch/zhaw/pm2/racetrack/PositionVectorNotValid.java b/src/main/java/ch/zhaw/pm2/racetrack/PositionVectorNotValid.java new file mode 100644 index 0000000..2b70333 --- /dev/null +++ b/src/main/java/ch/zhaw/pm2/racetrack/PositionVectorNotValid.java @@ -0,0 +1,9 @@ +package ch.zhaw.pm2.racetrack; + +public class PositionVectorNotValid extends Throwable { + public PositionVectorNotValid(String message) { + super(message); + } + + public PositionVectorNotValid() {} +} diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index e89b960..f3e51f0 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -6,7 +6,6 @@ import ch.zhaw.pm2.racetrack.given.TrackSpecification; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Scanner; @@ -59,11 +58,9 @@ import java.util.Scanner; public class Track implements TrackSpecification { public static final char CRASH_INDICATOR = 'X'; - - // TODO: Add necessary variables private List track; private List cars; - private List finishLine; + private final List finishLine; /** * Initialize a Track from the given track file. @@ -72,7 +69,7 @@ public class Track implements TrackSpecification { * @throws FileNotFoundException if the given track file could not be found * @throws InvalidTrackFormatException if the track file contains invalid data (no tracklines, ...) */ - public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException { + public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException, PositionVectorNotValid { track = new ArrayList<>(); cars = new ArrayList<>(); finishLine = new ArrayList<>(); @@ -159,8 +156,17 @@ public class Track implements TrackSpecification { private void drawCharOnTrackIndicator(PositionVector positionVector, char symbol) { String line = track.get(positionVector.getY()); - line = line.substring(0,positionVector.getX()) + symbol + line.substring(positionVector.getX()+1); - track.add(positionVector.getY(),line); + line = line.substring(0, positionVector.getX()) + symbol + line.substring(positionVector.getX() + 1); + track.remove(positionVector.getY()); + track.add(positionVector.getY(), line); + } + + private void isPositionVectorOnTrack(PositionVector positionVector) throws PositionVectorNotValid { + try{ + track.get(positionVector.getY()).charAt(positionVector.getX()); + }catch (IndexOutOfBoundsException e) { + throw new PositionVectorNotValid(); + } } /** @@ -187,6 +193,7 @@ public class Track implements TrackSpecification { /** * This Method will update the Car on the track * and will make the Car move to the next position + * * @param carIndex representing the current Car */ public void moveCar(int carIndex) { @@ -197,38 +204,43 @@ public class Track implements TrackSpecification { /** * This class does change the Position of the car only in the track. - * @param carIndex + * + * @param carIndex of the current car */ private void makeCarMoveInTrack(int carIndex) { PositionVector positionVector = findChar(getCarId(carIndex)); //Removes the Car at Current Pos - drawCharOnTrackIndicator(positionVector,ConfigSpecification.SpaceType.TRACK.getValue()); + drawCharOnTrackIndicator(positionVector, ConfigSpecification.SpaceType.TRACK.getValue()); //Adds Car at new Position positionVector = cars.get(carIndex).nextPosition(); - drawCharOnTrackIndicator(positionVector,cars.get(carIndex).getID()); + drawCharOnTrackIndicator(positionVector, cars.get(carIndex).getID()); } /** * This Method will check if the Car could crash at the specific position + * * @param positionVector the position to check if the car could crash * @return true if car would crash. Else false. */ - public boolean willCrashAtPosition(PositionVector positionVector) { + public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) throws PositionVectorNotValid { + isPositionVectorOnTrack(positionVector); char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX()); - return charAtPosition != ConfigSpecification.SpaceType.TRACK.value; + if (getCarId(carIndex) == charAtPosition) return false; + return (charAtPosition == ConfigSpecification.SpaceType.WALL.value); } /** * This Method will make the Car Crash. In Track and in the Car Object - * @param carIndex representing current Car + * + * @param carIndex representing current Car * @param positionVector where the Crash did happen */ - public void carDoesCrash(int carIndex,PositionVector positionVector) { + public void carDoesCrash(int carIndex, PositionVector positionVector) throws PositionVectorNotValid{ + isPositionVectorOnTrack(positionVector); Car car = cars.get(carIndex); car.crash(); - makeCarMoveInTrack(carIndex); - drawCharOnTrackIndicator(new PositionVector(positionVector.getX()+1,positionVector.getY()),CRASH_INDICATOR); - + car.setPosition(positionVector); + drawCharOnTrackIndicator(new PositionVector(positionVector.getX(), positionVector.getY()), CRASH_INDICATOR); } /** @@ -240,9 +252,8 @@ public class Track implements TrackSpecification { */ @Override public Config.SpaceType getSpaceType(PositionVector position) { - + //isPositionVectorOnTrack(position); Should be used but we are not allowed to change method head. We don't use function anyway char charAtPosition = track.get(position.getY()).charAt(position.getX()); - ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values(); for (ConfigSpecification.SpaceType spaceType : spaceTypes) { if (spaceType.getValue() == charAtPosition) { @@ -320,14 +331,13 @@ public class Track implements TrackSpecification { @Override public char getCharAtPosition(int y, int x, Config.SpaceType currentSpace) { char charAtPos = track.get(y).charAt(x); + PositionVector positionVector = new PositionVector(x, y); for (Car car : cars) { - if(charAtPos == car.getID()) { - if(car.isCrashed()) { - return CRASH_INDICATOR; - } + if (charAtPos == car.getID()) { return charAtPos; } } + if (positionVector.equals(findChar(CRASH_INDICATOR))) return CRASH_INDICATOR; return currentSpace.getValue(); } @@ -338,10 +348,8 @@ public class Track implements TrackSpecification { */ @Override public String toString() { - String str = ""; - for (String line : track) { - str += line + "\n"; - } - return str; + StringBuilder str = new StringBuilder(); + for (String line : track) str.append(line).append("\n"); + return str.toString(); } } diff --git a/src/test/InvalidTracks/sameCar.txt b/src/test/InvalidTracks/sameCar.txt new file mode 100644 index 0000000..26dc7cc --- /dev/null +++ b/src/test/InvalidTracks/sameCar.txt @@ -0,0 +1,26 @@ +############################################################### +################# ############# +############### ########### +############### ######### +############### ################## ######### +############### #################### ######### +############## ##################### ######### +############ ###################### ########## +######### ###################### ############ +######### ###################### ############## +######### ##################### ################ +######### ################# ################## +######### ################ ################## +########## ################## ############### +########### #################### ############# +########### ####################### ########## +########## ########################## ######### +######### ############################ ######## +######## ############################# ######## +####### ############################## ######## +###### ############################# ######## +###### ############################ ######### +###### > a ########### +###### > a ############## +######## > b ################# +############################################################### diff --git a/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java b/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java index a4decfb..d5cff22 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java @@ -1,10 +1,7 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.given.ConfigSpecification; -import org.junit.Before; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import java.io.File; import java.io.FileNotFoundException; @@ -15,64 +12,166 @@ import java.util.List; public class TrackTest { Track trackObj; - @BeforeEach - void setup() { - File file = new File("C:\\Studium\\Semester2\\PM2\\Projekt1\\racetrack\\tracks\\challenge.txt"); - try { - trackObj = new Track(file); + @Nested + @DisplayName("Positiv Test Cases") + class positivClass { - } catch (Exception e) { - System.err.println("Error in Test compareTrack" + e.getMessage()); + @BeforeEach + void setup() { + File file = new File(".\\tracks\\challenge.txt"); + try { + trackObj = new Track(file); + + } catch (Exception | PositionVectorNotValid e) { + System.err.println("Error in Test compareTrack" + e.getMessage()); + } + } + + @Test + @DisplayName("Create correct amount of Car instance") + void checkCars() { + Assertions.assertEquals(2, trackObj.getCarCount()); + } + + @Test + @DisplayName("Create Car instance with correct Symbols / Id") + void checkCarId() { + Assertions.assertEquals('a', trackObj.getCarId(0)); + Assertions.assertEquals('b', trackObj.getCarId(1)); + } + + + @Test + @DisplayName("Check getSpaceTyp") + void getSpaceTyp() { + Assertions.assertEquals(ConfigSpecification.SpaceType.FINISH_RIGHT, trackObj.getSpaceType(new PositionVector(22, 24))); + } + + @Test + @DisplayName("Find FinishLine") + void findFinish() { + List expected = new ArrayList<>(); + expected.add(new PositionVector(22, 22)); + expected.add(new PositionVector(22, 23)); + expected.add(new PositionVector(22, 24)); + Assertions.assertEquals(expected, trackObj.getFinishLine()); + } + + @Test + @DisplayName("Converts Trackfile correctly to List") + void checkTrack() { + Track trackObj; + try { + trackObj = new Track(new File(".\\tracks\\oval-anticlock-right.txt")); + List track = new ArrayList<>(); + track.add("##################################################"); + track.add("##################################################"); + track.add("############## #############"); + track.add("########## ##########"); + track.add("####### #######"); + track.add("###### ################# ######"); + track.add("##### ################### #####"); + track.add("##### ################### #####"); + track.add("###### ################# ######"); + track.add("####### > a #######"); + track.add("########## > ##########"); + track.add("############## > b ##############"); + track.add("##################################################"); + track.add("##################################################"); + Assertions.assertLinesMatch(track, trackObj.getTrack()); + } catch (FileNotFoundException | InvalidTrackFormatException | PositionVectorNotValid e) { + e.printStackTrace(); + } + } + + @Test + @DisplayName("Make Car move down on track") + void makeCarMoveDown() { + PositionVector beforeMove = trackObj.getCarPos(0); + trackObj.getCar(0).accelerate(PositionVector.Direction.DOWN); + trackObj.moveCar(0); + PositionVector afterMove = trackObj.getCarPos(0); + Assertions.assertEquals(beforeMove.getY() + 1, afterMove.getY()); + Assertions.assertEquals(beforeMove.getX(), afterMove.getX()); + } + + @Test + @DisplayName("Make Car move with (0,0) acceleration on track") + void makeCarStay() { + PositionVector beforeMove = trackObj.getCarPos(0); + trackObj.moveCar(0); + PositionVector afterMove = trackObj.getCarPos(0); + Assertions.assertEquals(beforeMove.getY(), afterMove.getY()); + Assertions.assertEquals(beforeMove.getX(), afterMove.getX()); + } + + @Test + @DisplayName("Will Car Crash") + void willCarCrash() { + try { + //Car will Crash + Assertions.assertTrue(trackObj.willCrashAtPosition(0, new PositionVector(25, 21))); + //Car will not Crash and is on track + Assertions.assertFalse(trackObj.willCrashAtPosition(0, new PositionVector(7, 22))); + //Car will not Crash and is on finishLine + Assertions.assertFalse(trackObj.willCrashAtPosition(0, trackObj.getFinishLine().get(0))); + } catch (PositionVectorNotValid positionVectorNotValid) { + positionVectorNotValid.printStackTrace(); + Assertions.fail("Test should not throw error"); + } + } + + @Test + @DisplayName("Make Car Crash") + void makeCarCrash() { + try { + trackObj.carDoesCrash(0, new PositionVector(6, 22)); + } catch (PositionVectorNotValid positionVectorNotValid) { + positionVectorNotValid.printStackTrace(); + Assertions.fail("Test should not throw exception"); + } + Assertions.assertEquals(Track.CRASH_INDICATOR, trackObj.getTrack().get(22).charAt(6)); + Assertions.assertTrue(trackObj.getCar(0).isCrashed()); } } - @Test - void canReadFile() { - File file = new File("C:\\Studium\\Semester2\\PM2\\Projekt1\\racetrack\\tracks\\challenge.txt"); - Assertions.assertThrows(FileNotFoundException.class,() -> new Track(file)); - } + @Nested + @DisplayName("Negative TestCase") + class negativeClass { + File file; - /** - * Dirty test... - */ - @Test - void printOutTrack() { - System.out.println(trackObj); - } + @BeforeEach + void setup() { + file = new File(".\\tracks\\challenge.txt"); + try { + trackObj = new Track(file); - @Test - void getSpaceTyp() { - Assertions.assertEquals(ConfigSpecification.SpaceType.FINISH_RIGHT,trackObj.getSpaceType(new PositionVector(22,24))); - } + } catch (Exception | PositionVectorNotValid e) { + System.err.println("Error in Test compareTrack" + e.getMessage()); + } + } - //TODO: - @Test - void addCarAtInit() { - try { - trackObj.addCar(); - } catch (InvalidTrackFormatException e) { - e.printStackTrace(); + @Test + @DisplayName("Throw error if File not found") + void canReadFile() { + file = new File(".\\tracks\\NotExisting.txt"); + Assertions.assertThrows(FileNotFoundException.class, () -> new Track(file)); + } + + @Test + @DisplayName("Throw error if File is invalid") + void invalidTrackFile() { + File testfile = new File(".\\src\\test\\InvalidTracks\\sameCar.txt"); + Assertions.assertThrows(InvalidTrackFormatException.class, () -> new Track(testfile)); + } + + @Test + @DisplayName("Invalid Position Vector used") + void invalidPositionVector() { + Assertions.assertThrows(PositionVectorNotValid.class, () -> trackObj.willCrashAtPosition(0, new PositionVector(100, 200))); + Assertions.assertThrows(PositionVectorNotValid.class, () -> trackObj.carDoesCrash(1,new PositionVector(200,100))); } } - @Test - void findFinish() { - List expected = new ArrayList<>(); - expected.add(new PositionVector(22,22)); - expected.add(new PositionVector(22,23)); - expected.add(new PositionVector(22,24)); - Assertions.assertEquals(expected,trackObj.getFinishLine()); - } - - @Test - void makeTrackObj() { - try { - Track t1 = new Track(new File("C:\\Studium\\Semester2\\PM2\\Projekt1\\racetrack\\tracks\\challenge.txt")); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (InvalidTrackFormatException e) { - e.printStackTrace(); - } - } }