Increase test coverage

This commit is contained in:
Arne Keller 2020-03-05 15:27:34 +01:00
parent 61b6739ab4
commit 802c5deef5
8 changed files with 81 additions and 19 deletions

View File

@ -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();

View File

@ -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;

View File

@ -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

View File

@ -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));
}
/**

View File

@ -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() {

View File

@ -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());
}
}

30
testcoverage2_input.txt Normal file
View File

@ -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

29
testcoverage2_output.txt Normal file
View File

@ -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