Skip to content

Commit

Permalink
core: add tests for scheduled points
Browse files Browse the repository at this point in the history
  • Loading branch information
flomonster committed Jun 22, 2023
1 parent 2a50f05 commit 07fa0ad
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public RJSStandaloneTrainSchedule(
this(id, rollingStock, initialSpeed, allowances, stops, tag, RJSComfortType.STANDARD, null, null);
}

public RJSStandaloneTrainSchedule(
String id,
String rollingStock,
double initialSpeed,
RJSSchedulePoint[] scheduledPoints,
RJSTrainStop[] stops
) {
this.id = id;
this.rollingStock = rollingStock;
this.initialSpeed = initialSpeed;
this.scheduledPoints = scheduledPoints;
this.stops = stops;
}

@Override
public String getID() {
return id;
Expand Down
40 changes: 40 additions & 0 deletions core/src/test/java/fr/sncf/osrd/api/StandaloneSimulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import fr.sncf.osrd.standalone_sim.result.ResultPosition;
import fr.sncf.osrd.standalone_sim.result.ResultSpeed;
import fr.sncf.osrd.standalone_sim.result.StandaloneSimResult;
import fr.sncf.osrd.train.ScheduledPoint;
import org.junit.jupiter.api.Test;
import org.takes.rq.RqFake;
import java.io.IOException;
Expand Down Expand Up @@ -289,6 +290,45 @@ public void withMarecoAllowanceRanges() throws Exception {
assertNull(simResult.ecoSimulations.get(0));
}

@Test
public void withScheduledPoints() throws Exception {
// load the example infrastructure and build a test path
final var rjsTrainPath = tinyInfraTrainPath();

// build the simulation request
var stops = new RJSTrainStop[]{RJSTrainStop.lastStop(0.1)};
var scheduledPoints = new RJSSchedulePoint[] {
new RJSSchedulePoint(8000., 480.),
new RJSSchedulePoint(-1., 600.)
};
var trains = new ArrayList<RJSStandaloneTrainSchedule>();
trains.add(new RJSStandaloneTrainSchedule("no_allowance", "fast_rolling_stock",
0, null, stops, null));
trains.add(new RJSStandaloneTrainSchedule("allowance", "fast_rolling_stock",
0, scheduledPoints, stops));

var query = new StandaloneSimulationRequest(
"tiny_infra/infra.json",
"1",
2,
getExampleRollingStocks(),
trains,
rjsTrainPath
);

// parse back the simulation result
var simResult = runStandaloneSimulation(query);

var noAllowanceResult = simResult.baseSimulations.get(0);
assertNull(simResult.ecoSimulations.get(0));
var noAllowanceTime =
noAllowanceResult.headPositions.get(noAllowanceResult.headPositions.size() - 1).time;
var marecoResult = simResult.ecoSimulations.get(1);
var marecoTime = marecoResult.headPositions.get(marecoResult.headPositions.size() - 1).time;
assertTrue(marecoTime > noAllowanceTime);
assertEquals(600., marecoTime, 1.);
}

@Test
public void engineeringAllowance() throws Exception {
// load the example infrastructure and build a test path
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package fr.sncf.osrd.standalone_sim;

import static fr.sncf.osrd.standalone_sim.StandaloneSim.generateAllowanceFromScheduledPoints;

import fr.sncf.osrd.envelope.Envelope;
import fr.sncf.osrd.envelope.part.EnvelopePart;
import fr.sncf.osrd.train.ScheduledPoint;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;

public class StandaloneSimTests {
@Test
public void generateAllowanceFromOneScheduledPoint() {
var envelopeFloor = Envelope.make(EnvelopePart.generateTimes(
new double[] {0, 1, 2, 3, 4, 5, 6},
new double[] {1, 1, 1, 1, 1, 1, 1}
));
var scheduledPoints = new ArrayList<ScheduledPoint>();
scheduledPoints.add(new ScheduledPoint(2, 5));
var allowanceOpt = generateAllowanceFromScheduledPoints(envelopeFloor, scheduledPoints);
Assertions.assertFalse(allowanceOpt.isEmpty());
var allowance = allowanceOpt.get();
Assertions.assertEquals(allowance.ranges.size(), 1);
var rangeAllowance = allowance.ranges.get(0);
Assertions.assertEquals(0., rangeAllowance.beginPos, 0.0001);
Assertions.assertEquals(2., rangeAllowance.endPos, 0.0001);
Assertions.assertEquals(3., rangeAllowance.value.getAllowanceTime(2., 2.), 0.0001);
}

@Test
public void generateAllowanceFromMultipleScheduledPoints() {
var envelopeFloor = Envelope.make(EnvelopePart.generateTimes(
new double[] {0, 1, 2, 3, 4, 5, 6},
new double[] {1, 1, 1, 1, 1, 1, 1}
));
var scheduledPoints = new ArrayList<ScheduledPoint>();
scheduledPoints.add(new ScheduledPoint(2, 5));
scheduledPoints.add(new ScheduledPoint(4, 12));
var allowanceOpt = generateAllowanceFromScheduledPoints(envelopeFloor, scheduledPoints);
Assertions.assertFalse(allowanceOpt.isEmpty());
var allowance = allowanceOpt.get();
Assertions.assertEquals(allowance.ranges.size(), 2);
var rangeAllowance = allowance.ranges.get(0);
Assertions.assertEquals(0., rangeAllowance.beginPos, 0.0001);
Assertions.assertEquals(2., rangeAllowance.endPos, 0.0001);
Assertions.assertEquals(3., rangeAllowance.value.getAllowanceTime(2., 2.), 0.0001);
rangeAllowance = allowance.ranges.get(1);
Assertions.assertEquals(2., rangeAllowance.beginPos, 0.0001);
Assertions.assertEquals(4., rangeAllowance.endPos, 0.0001);
Assertions.assertEquals(5., rangeAllowance.value.getAllowanceTime(2., 2.), 0.0001);
}

@Test
public void generateAllowanceFromNoScheduledPoints() {
var envelopeFloor = Envelope.make(EnvelopePart.generateTimes(
new double[] {0, 1, 2, 3, 4, 5, 6},
new double[] {1, 1, 1, 1, 1, 1, 1}
));
var scheduledPoints = new ArrayList<ScheduledPoint>();
var allowanceOpt = generateAllowanceFromScheduledPoints(envelopeFloor, scheduledPoints);
Assertions.assertTrue(allowanceOpt.isEmpty());
}
}

0 comments on commit 07fa0ad

Please sign in to comment.