Commit bf25dcf1 authored by Jakob Schöttl's avatar Jakob Schöttl
Browse files

Add tests for train model and fix bug

parent 5456f584
......@@ -9,20 +9,47 @@ public class Compartment {
private TrainModel trainModel;
private int index;
private List<SeatGroup> seatGroups;
public Compartment(TrainModel trainModel, int index) {
this.trainModel = trainModel;
this.index = index;
seatGroups = new ArrayList<>(4);
if (index == 0) {
addFirstHalfCompartment();
} else if (index == trainModel.getNumberOfEntranceAreas()) {
addLastHalfCompartment();
} else {
addNormalCompartment();
}
}
private void addNormalCompartment() {
final int startSeatGroupIndex = index * 4 - 2;
seatGroups.add(trainModel.getSeatGroup(startSeatGroupIndex));
seatGroups.add(trainModel.getSeatGroup(startSeatGroupIndex + 1));
seatGroups.add(trainModel.getSeatGroup(startSeatGroupIndex + 2));
seatGroups.add(trainModel.getSeatGroup(startSeatGroupIndex + 3));
}
private void addFirstHalfCompartment() {
seatGroups.add(null);
seatGroups.add(null);
seatGroups.add(trainModel.getSeatGroup(0));
seatGroups.add(trainModel.getSeatGroup(1));
}
private void addLastHalfCompartment() {
final int n = trainModel.getSeatGroups().size();
seatGroups.add(trainModel.getSeatGroup(n - 2));
seatGroups.add(trainModel.getSeatGroup(n - 1));
seatGroups.add(null);
seatGroups.add(null);
}
public List<SeatGroup> getSeatGroups() {
List<SeatGroup> result = new ArrayList<>(4);
final int startSeatGroupIndex = index * 4;
result.add(trainModel.getSeatGroups().get(startSeatGroupIndex));
result.add(trainModel.getSeatGroups().get(startSeatGroupIndex + 1));
result.add(trainModel.getSeatGroups().get(startSeatGroupIndex + 2));
result.add(trainModel.getSeatGroups().get(startSeatGroupIndex + 3));
return result;
return seatGroups;
}
public Target getInterimTargetCloserTo(int entranceAreaIndex) {
......
This diff is collapsed.
package org.vadere.simulator.models.seating.trainmodel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.Collection;
import java.util.Scanner;
import org.junit.Before;
import org.junit.Test;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.simulator.projects.io.TextOutOfNodeException;
import org.vadere.state.scenario.Et423Geometry;
import org.vadere.state.scenario.Topography;
public class TestTrainModel {
private static final int nEntranceAreas = 12;
private static final int nCompartments = nEntranceAreas + 1; // includes 2 half-compartments
private static final int nInterimDestinations = nCompartments * 3 - 4; // includes 2 targets from half-compartments
private static final int nSeatGroups = nCompartments * 4 - 4;
private static final int nSeats = nSeatGroups * 4;
private TrainModel trainModel;
@Before
public void setUp() {
try {
@SuppressWarnings("resource")
final String json = new Scanner(TestTrainModel.class.getResourceAsStream("/data/test-train-topography.json"), "UTF-8").useDelimiter("\\A").next();
final Topography topography = JsonConverter.deserializeTopography(json);
trainModel = new TrainModel(topography, new Et423Geometry());
} catch (IOException | TextOutOfNodeException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Test
public void testBasicModelProperties() {
assertEquals(nEntranceAreas, trainModel.getNumberOfEntranceAreas());
checkSize(nInterimDestinations, trainModel.getInterimDestinations());
checkSize(nSeatGroups, trainModel.getSeatGroups());
checkSize(nSeats, trainModel.getSeats());
// assertEquals(10, trainModel.getPedestrians().size());
}
@Test
public void testGetSeatGroupLimits() {
assertTrue(trainModel.getSeatGroup(0) != null);
try {
trainModel.getSeatGroup(-1);
fail("exception expected");
} catch (IndexOutOfBoundsException e) { }
assertTrue(trainModel.getSeatGroup(nSeatGroups - 1) != null);
try {
trainModel.getSeatGroup(nSeatGroups);
fail("exception expected");
} catch (IndexOutOfBoundsException e) { }
}
@Test
public void testGetCompartmentLimits() {
assertTrue(trainModel.getCompartment(0) != null); // first half-compartment
assertTrue(trainModel.getCompartment(1) != null); // first normal compartment
try {
trainModel.getCompartment(-1);
fail("exception expected");
} catch (IndexOutOfBoundsException e) { }
assertTrue(trainModel.getCompartment(nCompartments - 2) != null); // last normal compartment
assertTrue(trainModel.getCompartment(nCompartments - 1) != null); // last half-compartment
try {
trainModel.getCompartment(nCompartments);
fail("exception expected");
} catch (IndexOutOfBoundsException e) { }
}
@Test
public void testFirstHalfCompartment() {
final Compartment c = trainModel.getCompartment(0);
assertTrue(c.getSeatGroups().get(0) == null);
assertTrue(c.getSeatGroups().get(1) == null);
assertTrue(c.getSeatGroups().get(2) == trainModel.getSeatGroup(0));
assertTrue(c.getSeatGroups().get(3) == trainModel.getSeatGroup(1));
assertTrue(c.getSeatGroups().get(2).getSeat(0) == trainModel.getSeats().get(0));
}
@Test
public void testLastHalfCompartment() {
final Compartment c = trainModel.getCompartment(nCompartments - 1);
assertTrue(c.getSeatGroups().get(0) == trainModel.getSeatGroup(nSeatGroups - 2));
assertTrue(c.getSeatGroups().get(1) == trainModel.getSeatGroup(nSeatGroups - 1));
assertTrue(c.getSeatGroups().get(2) == null);
assertTrue(c.getSeatGroups().get(3) == null);
assertTrue(c.getSeatGroups().get(1).getSeat(3) == trainModel.getSeats().get(nSeats - 1));
}
private void checkSize(int expected, Collection<?> actualCollection) {
assertEquals(expected, actualCollection.size());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment