Correctly derail

This commit is contained in:
Arne Keller 2020-02-16 19:42:12 +01:00
parent 0cf7368997
commit 9191028934
4 changed files with 29 additions and 2 deletions

10
fuzz8_input.txt Normal file
View File

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

9
fuzz8_output.txt Normal file
View File

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

View File

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

View File

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