From 9191028934deb4fa8565aea26c90329feb9b79eb Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Sun, 16 Feb 2020 19:42:12 +0100 Subject: [PATCH] Correctly derail --- fuzz8_input.txt | 10 ++++++++++ fuzz8_output.txt | 9 +++++++++ src/edu/kit/informatik/MainTest.java | 5 +++++ src/edu/kit/informatik/model/Train.java | 7 +++++-- 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 fuzz8_input.txt create mode 100644 fuzz8_output.txt diff --git a/fuzz8_input.txt b/fuzz8_input.txt new file mode 100644 index 0000000..87b5a09 --- /dev/null +++ b/fuzz8_input.txt @@ -0,0 +1,10 @@ +add track (0,0) -> (0,14) +create engine diesel T3 Sabine 1 false true +add train 1 T3-Sabine +put train 1 at (0,0) in direction 0,-1 +step -20 +create coach passenger 1 true false +add train 1 W1 +put train 1 at (0,0) in direction 0,-1 +step -20 +exit diff --git a/fuzz8_output.txt b/fuzz8_output.txt new file mode 100644 index 0000000..153e406 --- /dev/null +++ b/fuzz8_output.txt @@ -0,0 +1,9 @@ +1 +T3-Sabine +diesel engine T3-Sabine added to train 1 +OK +Crash of train 1 +1 +passenger coach W1 added to train 1 +OK +Crash of train 1 diff --git a/src/edu/kit/informatik/MainTest.java b/src/edu/kit/informatik/MainTest.java index bd1c9f1..4ba2ddf 100644 --- a/src/edu/kit/informatik/MainTest.java +++ b/src/edu/kit/informatik/MainTest.java @@ -68,6 +68,11 @@ class MainTest { cmpInOut("fuzz7_input.txt", "fuzz7_output.txt"); } + @Test + void fuzz8() throws IOException { + cmpInOut("fuzz8_input.txt", "fuzz8_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/Train.java b/src/edu/kit/informatik/model/Train.java index d6bbbf2..f05b838 100644 --- a/src/edu/kit/informatik/model/Train.java +++ b/src/edu/kit/informatik/model/Train.java @@ -301,8 +301,11 @@ public final class Train { } else if (getLength() == 1) { // we evidently just moved into another rail Rail[] nextTouchingRails = railNetwork.findTouchingRails(nextPosition); - Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(position.get(position.size() - 1)); - if (nextTouchingRails[0] == alreadyTouchingRails[0]) { + Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(secondToLastPosition); + if (alreadyTouchingRails.length == 0) { + // we evidently just derailed, be we should still crash with trains entering our last rail + assert !occupiedRails.isEmpty(); + } else if (nextTouchingRails[0] == alreadyTouchingRails[0]) { occupiedRails.add(nextTouchingRails[0]); } else if (nextTouchingRails.length == 2) { if (nextTouchingRails[1] == alreadyTouchingRails[0]) {