From 802c5deef5a1849921a83316df494462efea2c95 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Thu, 5 Mar 2020 15:27:34 +0100 Subject: [PATCH] Increase test coverage --- src/edu/kit/informatik/MainTest.java | 5 ++++ src/edu/kit/informatik/model/Switch.java | 11 +++---- src/edu/kit/informatik/model/Track.java | 2 +- .../kit/informatik/model/TrainManager.java | 2 +- src/edu/kit/informatik/model/Vector2D.java | 8 ++--- src/edu/kit/informatik/ui/CommandLine.java | 13 ++++---- testcoverage2_input.txt | 30 +++++++++++++++++++ testcoverage2_output.txt | 29 ++++++++++++++++++ 8 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 testcoverage2_input.txt create mode 100644 testcoverage2_output.txt diff --git a/src/edu/kit/informatik/MainTest.java b/src/edu/kit/informatik/MainTest.java index 04c4d95..426adca 100644 --- a/src/edu/kit/informatik/MainTest.java +++ b/src/edu/kit/informatik/MainTest.java @@ -118,6 +118,11 @@ class MainTest { cmpInOut("long_train_input.txt", "long_train_output.txt"); } + @Test + void testCoverage2() throws IOException { + cmpInOut("testcoverage2_input.txt", "testcoverage2_output.txt"); + } + private void cmpInOut(String in, String out) throws IOException { System.setIn(new ByteArrayInputStream(readFile(in))); ByteArrayOutputStream output = new ByteArrayOutputStream(); diff --git a/src/edu/kit/informatik/model/Switch.java b/src/edu/kit/informatik/model/Switch.java index a02ad87..4aa34ec 100644 --- a/src/edu/kit/informatik/model/Switch.java +++ b/src/edu/kit/informatik/model/Switch.java @@ -96,9 +96,6 @@ public final class Switch extends Rail { @Override public Vector2D getDirectionFrom(Vector2D position) { - if (selection == null) { - return null; - } return selection.getDirectionFrom(position); } @@ -125,13 +122,17 @@ public final class Switch extends Rail { } } + /** + * Check whether two switches are equal, ignoring the current configuration. + * + * @return whether this switch is equal to the other + */ @Override public boolean equals(Object obj) { if (obj != null && getClass().equals(obj.getClass())) { final Switch otherSwitch = (Switch) obj; - return positionOne.equals(otherSwitch.positionOne) && positionTwo.equals(otherSwitch.positionTwo) - && Objects.equals(selection, otherSwitch.selection); + return positionOne.equals(otherSwitch.positionOne) && positionTwo.equals(otherSwitch.positionTwo); } return false; diff --git a/src/edu/kit/informatik/model/Track.java b/src/edu/kit/informatik/model/Track.java index 0773f53..0165567 100644 --- a/src/edu/kit/informatik/model/Track.java +++ b/src/edu/kit/informatik/model/Track.java @@ -49,7 +49,7 @@ public final class Track extends Rail { @Override public boolean canConnectToRail(Rail rail) { - return rail != null && (rail.canConnectTo(start) || rail.canConnectTo(end)); + return rail.canConnectTo(start) || rail.canConnectTo(end); } @Override diff --git a/src/edu/kit/informatik/model/TrainManager.java b/src/edu/kit/informatik/model/TrainManager.java index 87daa47..84475f3 100644 --- a/src/edu/kit/informatik/model/TrainManager.java +++ b/src/edu/kit/informatik/model/TrainManager.java @@ -51,7 +51,7 @@ public final class TrainManager { * @return whether a train is on that rail */ public boolean anyTrainOnRail(Rail rail) { - return rail != null && trains.values().stream().anyMatch(train -> train.isOnRail(rail)); + return trains.values().stream().anyMatch(train -> train.isOnRail(rail)); } /** diff --git a/src/edu/kit/informatik/model/Vector2D.java b/src/edu/kit/informatik/model/Vector2D.java index 63b9950..a6768ca 100644 --- a/src/edu/kit/informatik/model/Vector2D.java +++ b/src/edu/kit/informatik/model/Vector2D.java @@ -55,11 +55,12 @@ public class Vector2D { /** * Calculate the distance between this vector (interpreted as position) and another position. + * * @param other the point to measure distance to * @return the manhattan distance */ public long distanceTo(Vector2D other) { - return other != null ? Math.abs(this.x - other.x) + Math.abs(this.y - other.y) : 0; + return Math.abs(this.x - other.x) + Math.abs(this.y - other.y); } /** @@ -70,13 +71,12 @@ public class Vector2D { * @return the manhattan distance */ public boolean isParallelTo(Vector2D other) { - if (other == null) { - return false; - } return x == 0 && other.x == 0 || y == 0 && other.y == 0; } /** + * Normalize this vector. Will essentially return a new vector with (x/|x|, y/|y|). + * * @return a vector with separately (!) normalized components */ public Vector2D normalized() { diff --git a/src/edu/kit/informatik/ui/CommandLine.java b/src/edu/kit/informatik/ui/CommandLine.java index 2966b92..1720d66 100644 --- a/src/edu/kit/informatik/ui/CommandLine.java +++ b/src/edu/kit/informatik/ui/CommandLine.java @@ -2,11 +2,10 @@ package edu.kit.informatik.ui; import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.Terminal; -import edu.kit.informatik.ui.command.Command; import edu.kit.informatik.ui.command.CommandFactory; /** - * Interactive simulation runner, gets user inputs and processes commands specified. + * Interactive simulation runner: gets user inputs and processes specified commands. * * @author Arne Keller * @version 1.0 @@ -28,11 +27,10 @@ public final class CommandLine { * Start a new interactive user session. Returns when standard in is fully processed or user exits. */ public static void startInteractive() { - // create a new simulation - ModelRailwaySimulation simulation = new ModelRailwaySimulation(); + final ModelRailwaySimulation simulation = new ModelRailwaySimulation(); while (true) { - String input = Terminal.readLine(); + final String input = Terminal.readLine(); if (input == null) { break; // TODO remove } @@ -50,9 +48,8 @@ public final class CommandLine { } try { - Command command = CommandFactory.getCommand(input); - command.apply(simulation); - } catch (NumberFormatException | InvalidInputException e) { + CommandFactory.getCommand(input).apply(simulation); + } catch (final NumberFormatException | InvalidInputException e) { Terminal.printError(e.getMessage()); } } diff --git a/testcoverage2_input.txt b/testcoverage2_input.txt new file mode 100644 index 0000000..87ecbed --- /dev/null +++ b/testcoverage2_input.txt @@ -0,0 +1,30 @@ +add track (0,0) -> (10,0) +add switch (-5,0) -> (0,0),(-5,5) +set switch 2 position (-5,5) +create engine diesel T1 Alpha 1 true true +add train 1 T1-Alpha +put train 1 at (5,0) in direction 1,0 +step -5 +create train-set ICE 1 2 true true +add train 2 ICE-1 +create train-set ICE 2 2 true true +add train 3 ICE-2 +create train-set ICE 2E 2 true true +add train 4 ICE-2E +create train-set ICE 3 2 true true +add train 5 ICE-3 +add track (10,0) -> (20,0) +add track (20,0) -> (30,0) +add track (30,0) -> (40,0) +add track (40,0) -> (50,0) +put train 1 at (9,0) in direction 1,0 +put train 2 at (41,0) in direction -1,0 +put train 3 at (25,0) in direction 1,0 +put train 4 at (19,0) in direction 1,0 +put train 5 at (31,0) in direction -1,0 +step 2 +add switch (50,0) -> (55,0),(50,5) +put train 1 at (51,0) in direction 1,0 +delete track 7 +put train 1 at (0,0) in direction 0,-1 +exit diff --git a/testcoverage2_output.txt b/testcoverage2_output.txt new file mode 100644 index 0000000..97b233a --- /dev/null +++ b/testcoverage2_output.txt @@ -0,0 +1,29 @@ +1 +2 +OK +T1-Alpha +diesel engine T1-Alpha added to train 1 +OK +Crash of train 1 +ICE-1 +train-set ICE-1 added to train 2 +ICE-2 +train-set ICE-2 added to train 3 +ICE-2E +train-set ICE-2E added to train 4 +ICE-3 +train-set ICE-3 added to train 5 +3 +4 +5 +6 +OK +OK +OK +OK +OK +Crash of train 1,2,3,4,5 +7 +Error, switches not set up +OK +Error, could not place train