Merge pull request #2398 from JoePfeiffer/fix-sim-unit-test

Clean up FlightEventsTest unit test
This commit is contained in:
Joe Pfeiffer 2023-11-17 09:01:43 -07:00 committed by GitHub
commit 9c97a9adb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,6 +32,10 @@ public class FlightEventsTest extends BaseTestCase {
@Test
public void testSingleStage() throws SimulationException {
final Rocket rocket = TestRockets.makeEstesAlphaIII();
final AxialStage stage = rocket.getStage(0);
final InnerTube motorMountTube = (InnerTube) stage.getChild(1).getChild(2);
final Parachute parachute = (Parachute) stage.getChild(1).getChild(3);
final Simulation sim = new Simulation(rocket);
sim.getOptions().setISAAtmosphere(true);
sim.getOptions().setTimeStep(0.05);
@ -43,41 +47,21 @@ public class FlightEventsTest extends BaseTestCase {
final int branchCount = sim.getSimulatedData().getBranchCount();
assertEquals(" Single stage simulation invalid branch count ", 1, branchCount);
final FlightEvent.Type[] expectedEventTypes = {FlightEvent.Type.LAUNCH, FlightEvent.Type.IGNITION, FlightEvent.Type.LIFTOFF,
FlightEvent.Type.LAUNCHROD, FlightEvent.Type.BURNOUT, FlightEvent.Type.EJECTION_CHARGE, FlightEvent.Type.RECOVERY_DEVICE_DEPLOYMENT,
FlightEvent.Type.APOGEE, FlightEvent.Type.GROUND_HIT, FlightEvent.Type.SIMULATION_END};
final double[] expectedEventTimes = {0.0, 0.0, 0.1275, 0.13, 2.0, 2.0, 2.001, 2.48338}; // Ground hit time is too variable, so don't include it
final AxialStage stage = rocket.getStage(0);
final InnerTube motorMountTube = (InnerTube) stage.getChild(1).getChild(2);
final Parachute parachute = (Parachute) stage.getChild(1).getChild(3);
final RocketComponent[] expectedSources = {rocket, motorMountTube, null, null, motorMountTube,
stage, parachute, rocket, null, null};
// Test event count
FlightDataBranch branch = sim.getSimulatedData().getBranch(0);
List<FlightEvent> eventList = branch.getEvents();
List<FlightEvent.Type> eventTypes = eventList.stream().map(FlightEvent::getType).collect(Collectors.toList());
assertEquals(" Single stage simulation invalid number of events ", expectedEventTypes.length, eventTypes.size());
// Test that all expected events are present, and in the right order
for (int i = 0; i < expectedEventTypes.length; i++) {
assertSame(" Flight type " + expectedEventTypes[i] + " not found in single stage simulation ",
expectedEventTypes[i], eventTypes.get(i));
}
// Test that the event times are correct
for (int i = 0; i < expectedEventTimes.length; i++) {
assertEquals(" Flight type " + expectedEventTypes[i] + " has wrong time ",
expectedEventTimes[i], eventList.get(i).getTime(), EPSILON);
}
// Test that the event sources are correct
for (int i = 0; i < expectedSources.length; i++) {
assertEquals(" Flight type " + expectedEventTypes[i] + " has wrong source ",
expectedSources[i], eventList.get(i).getSource());
}
}
final FlightEvent[] expectedEvents = new FlightEvent[] {
new FlightEvent(FlightEvent.Type.LAUNCH, 0.0, rocket),
new FlightEvent(FlightEvent.Type.IGNITION, 0.0, motorMountTube),
new FlightEvent(FlightEvent.Type.LIFTOFF, 0.1275, null),
new FlightEvent(FlightEvent.Type.LAUNCHROD, 0.13, null),
new FlightEvent(FlightEvent.Type.BURNOUT, 2.0, motorMountTube),
new FlightEvent(FlightEvent.Type.EJECTION_CHARGE, 2.0, stage),
new FlightEvent(FlightEvent.Type.RECOVERY_DEVICE_DEPLOYMENT, 2.001, parachute),
new FlightEvent(FlightEvent.Type.APOGEE, 2.48338, rocket),
new FlightEvent(FlightEvent.Type.GROUND_HIT, 1200, null),
new FlightEvent(FlightEvent.Type.SIMULATION_END, 1200, null)
};
checkEvents(expectedEvents, sim, 0);
}
/**
* Tests for a multi-stage design.
@ -156,29 +140,34 @@ public class FlightEventsTest extends BaseTestCase {
throw new IllegalStateException("Invalid branch number " + b);
}
// Test event count
final FlightDataBranch branch = sim.getSimulatedData().getBranch(b);
final FlightEvent[] events = branch.getEvents().toArray(new FlightEvent[0]);
assertEquals(" Multi-stage simulation, branch " + b + " invalid number of events ", expectedEvents.length, events.length);
checkEvents(expectedEvents, sim, b);
}
}
// Test that all expected events are present, in the right order, at the right time, from the right sources
for (int i = 0; i < events.length; i++) {
final FlightEvent expected = expectedEvents[i];
final FlightEvent actual = events[i];
assertSame("Branch " + b + " FlightEvent " + i + " type " + expected.getType() + " not found; FlightEvent " + actual.getType() + " found instead",
expected.getType(), actual.getType());
private void checkEvents(FlightEvent[] expectedEvents, Simulation sim, int branchNo) {
if (1200 != expected.getTime()) {
// Tumbling can have a very large time error, so implement a more course epsilon (otherwise unit tests just keep failing...)
double epsilon = actual.getType() == FlightEvent.Type.TUMBLE || actual.getType() == FlightEvent.Type.APOGEE ? 0.05 : EPSILON;
assertEquals("Branch " + b + " FlightEvent " + i + " type " + expected.getType() + " has wrong time ",
expected.getTime(), actual.getTime(), epsilon);
}
FlightEvent[] actualEvents = sim.getSimulatedData().getBranch(branchNo).getEvents().toArray(new FlightEvent[0]);
// Test event count
assertEquals("Branch " + branchNo + " invalid number of events ", expectedEvents.length, actualEvents.length);
// Test that the event sources are correct
assertEquals("Branch " + b + " FlightEvent " + i + " type " + expected.getType() + " has wrong source ",
expected.getSource(), actual.getSource());
}
}
}
// Test that all expected events are present, in the right order, at the right time, from the right sources
for (int i = 0; i < actualEvents.length; i++) {
final FlightEvent expected = expectedEvents[i];
final FlightEvent actual = actualEvents[i];
assertSame("Branch " + branchNo + " FlightEvent " + i + " type " + expected.getType() + " not found; FlightEvent " + actual.getType() + " found instead",
expected.getType(), actual.getType());
if (1200 != expected.getTime()) {
// event times that are dependent on simulation step time shouldn't be held to tighter bounds than that
double epsilon = actual.getType() == FlightEvent.Type.TUMBLE || actual.getType() == FlightEvent.Type.APOGEE ? sim.getOptions().getTimeStep() : EPSILON;
assertEquals("Branch " + branchNo + " FlightEvent " + i + " type " + expected.getType() + " has wrong time ",
expected.getTime(), actual.getTime(), epsilon);
}
// Test that the event sources are correct
assertEquals("Branch " + branchNo + " FlightEvent " + i + " type " + expected.getType() + " has wrong source ",
expected.getSource(), actual.getSource());
}
}
}