From 68b28f3c378cb396594c7a085a890d0b0b6eac21 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Tue, 25 Feb 2020 14:58:14 +0100 Subject: [PATCH] Change silly edge case behaviour --- basics_input.txt | 2 +- basics_output.txt | 3 ++- src/edu/kit/informatik/MainTest.java | 5 +++++ src/edu/kit/informatik/model/TrainManager.java | 16 +++++++++++++--- train_one_length_input.txt | 13 +++++++++++++ train_one_length_output.txt | 13 +++++++++++++ 6 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 train_one_length_input.txt create mode 100644 train_one_length_output.txt diff --git a/basics_input.txt b/basics_input.txt index e6d00c9..0729f19 100644 --- a/basics_input.txt +++ b/basics_input.txt @@ -19,7 +19,7 @@ add train 1 T1-Alpha add train 2 T1-Beta put train 1 at (0,5) in direction 0,1 put train 2 at (0,0) in direction 1,0 -step 1 +step 6 put train 1 at (0,0) in direction 0,-1 set switch 3 position (0,0) put train 1 at (0,0) in direction 0,-1 diff --git a/basics_output.txt b/basics_output.txt index 39e755a..a7be407 100644 --- a/basics_output.txt +++ b/basics_output.txt @@ -23,7 +23,8 @@ steam engine T1-Alpha added to train 1 electrical engine T1-Beta added to train 2 OK OK -Crash of train 1,2 +Crash of train 1 +Crash of train 2 OK OK OK diff --git a/src/edu/kit/informatik/MainTest.java b/src/edu/kit/informatik/MainTest.java index 9621be6..f6ae07a 100644 --- a/src/edu/kit/informatik/MainTest.java +++ b/src/edu/kit/informatik/MainTest.java @@ -93,6 +93,11 @@ class MainTest { cmpInOut("testcoverage_input.txt", "testcoverage_output.txt"); } + @Test + void oneLengthDerailEdgecase() throws IOException { + cmpInOut("train_one_length_input.txt", "train_one_length_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/TrainManager.java b/src/edu/kit/informatik/model/TrainManager.java index 3a54745..a575325 100644 --- a/src/edu/kit/informatik/model/TrainManager.java +++ b/src/edu/kit/informatik/model/TrainManager.java @@ -236,7 +236,12 @@ public final class TrainManager { || train.isOnPosition(nextPosition) && !train.getRearPosition().equals(train.getFrontPosition())) { collisions.add(new HashSet<>(Arrays.asList(train))); train.removeFromRails(); - nextOccupiedRails.put(train, occupiedRails.get(train)); + // see https://ilias.studium.kit.edu/goto.php?target=frm_996924_138875&client_id=produktiv + if (train.getLength() > 1) { + nextOccupiedRails.put(train, occupiedRails.get(train)); + } else { + nextOccupiedRails.put(train, null); + } } else { train.moveTo(railNetwork, nextPosition); train.setDirection(direction); @@ -266,7 +271,12 @@ public final class TrainManager { || train.isOnPosition(nextPosition) && !train.getRearPosition().equals(train.getFrontPosition())) { collisions.add(new HashSet<>(Arrays.asList(train))); train.removeFromRails(); - nextOccupiedRails.put(train, occupiedRails.get(train)); + // see https://ilias.studium.kit.edu/goto.php?target=frm_996924_138875&client_id=produktiv + if (train.getLength() > 1) { + nextOccupiedRails.put(train, occupiedRails.get(train)); + } else { + nextOccupiedRails.put(train, null); + } } else { train.moveBackTo(railNetwork, nextPosition); train.setDirection(front.subtract(train.getFrontPosition())); @@ -297,7 +307,7 @@ public final class TrainManager { trains.values().stream().filter(x -> x != train).forEach(otherTrain -> { Set occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain); Set occupiedByOtherTrain = nextOccupiedRails.get(otherTrain); - // if train did not occupy any rails (it is not placed) + // if train did not occupy any rails (it is not placed, or just derailed with length 1) if (occupiedByOtherTrain == null) { // skip return; diff --git a/train_one_length_input.txt b/train_one_length_input.txt new file mode 100644 index 0000000..0c2f6aa --- /dev/null +++ b/train_one_length_input.txt @@ -0,0 +1,13 @@ +add track (0,0) -> (5,0) +add track (5,0) -> (10,0) +create engine diesel T6 Lux 1 true true +create engine diesel T6 Lumen 2 false false +add train 1 T6-Lux +add train 2 T6-Lumen +put train 2 at (10,0) in direction 1,0 +put train 1 at (5,0) in direction 1,0 +step 1 +put train 2 at (5,0) in direction 1,0 +put train 1 at (10,0) in direction 1,0 +step 1 +exit diff --git a/train_one_length_output.txt b/train_one_length_output.txt new file mode 100644 index 0000000..e1200b4 --- /dev/null +++ b/train_one_length_output.txt @@ -0,0 +1,13 @@ +1 +2 +T6-Lux +T6-Lumen +diesel engine T6-Lux added to train 1 +diesel engine T6-Lumen added to train 2 +OK +OK +Crash of train 1,2 +OK +OK +Crash of train 1 +Train 2 at (6,0)