From 7be9dc238a3b4318e58cacf6121cb8765c70f3d4 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Wed, 28 Sep 2022 13:12:57 +0200 Subject: [PATCH] Add unit tests for flight events --- .../simulation/FlightEventsTest.java | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 core/test/net/sf/openrocket/simulation/FlightEventsTest.java diff --git a/core/test/net/sf/openrocket/simulation/FlightEventsTest.java b/core/test/net/sf/openrocket/simulation/FlightEventsTest.java new file mode 100644 index 000000000..6a03e8c9c --- /dev/null +++ b/core/test/net/sf/openrocket/simulation/FlightEventsTest.java @@ -0,0 +1,115 @@ +package net.sf.openrocket.simulation; + +import net.sf.openrocket.document.Simulation; +import net.sf.openrocket.rocketcomponent.FlightConfigurationId; +import net.sf.openrocket.rocketcomponent.Rocket; +import net.sf.openrocket.simulation.exception.SimulationException; +import net.sf.openrocket.util.BaseTestCase.BaseTestCase; +import net.sf.openrocket.util.TestRockets; +import org.junit.Test; + +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +/** + * Tests to verify that simulations contain all the expected flight events. + */ +public class FlightEventsTest extends BaseTestCase { + /** + * Tests for a single stage design. + */ + @Test + public void testSingleStage() throws SimulationException { + Rocket rocket = TestRockets.makeEstesAlphaIII(); + Simulation sim = new Simulation(rocket); + sim.getOptions().setISAAtmosphere(true); + sim.getOptions().setTimeStep(0.05); + sim.setFlightConfigurationId(TestRockets.TEST_FCID_0); + + sim.simulate(); + + // Test branch count + int branchCount = sim.getSimulatedData().getBranchCount(); + assertEquals(" Single stage simulation invalid branch count", 1, branchCount); + + // Test event count + FlightDataBranch branch = sim.getSimulatedData().getBranch(0); + List eventList = branch.getEvents(); + List eventTypes = eventList.stream().map(FlightEvent::getType).collect(Collectors.toList()); + assertEquals(" Single stage simulation invalid number of events", 10, eventTypes.size()); + + // Test that all expected events are present, and in the right order + 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}; + for (int i = 0; i < expectedEventTypes.length; i++) { + assertSame(" Flight type " + expectedEventTypes[i] + " not found in single stage simulation", + eventTypes.get(i), expectedEventTypes[i]); + } + } + + /** + * Tests for a multi-stage design. + */ + @Test + public void testMultiStage() throws SimulationException { + Rocket rocket = TestRockets.makeFalcon9Heavy(); + Simulation sim = new Simulation(rocket); + sim.getOptions().setISAAtmosphere(true); + sim.getOptions().setTimeStep(0.05); + rocket.getSelectedConfiguration().setAllStages(); + FlightConfigurationId fcid = rocket.getSelectedConfiguration().getFlightConfigurationID(); + sim.setFlightConfigurationId(fcid); + + sim.simulate(); + + // Test branch count + int branchCount = sim.getSimulatedData().getBranchCount(); + assertEquals(" Multi-stage simulation invalid branch count", 3, branchCount); + + for (int b = 0; b < 3; b++) { + int expectedEventsCount; + FlightEvent.Type[] expectedEventTypes; + switch (b) { + // TODO: change expected values + case 0: + expectedEventsCount = 15; + expectedEventTypes = new FlightEvent.Type[]{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}; + break; + case 1: + expectedEventsCount = 15; + expectedEventTypes = new FlightEvent.Type[]{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}; + break; + case 2: + expectedEventsCount = 15; + expectedEventTypes = new FlightEvent.Type[]{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}; + break; + default: + throw new IllegalStateException("Invalid branch number " + b); + } + + // Test event count + FlightDataBranch branch = sim.getSimulatedData().getBranch(b); + List eventList = branch.getEvents(); + List eventTypes = eventList.stream().map(FlightEvent::getType).collect(Collectors.toList()); + assertEquals(" Multi-stage simulation, branch " + b + " invalid number of events", expectedEventsCount, eventTypes.size()); + System.out.println(eventTypes); + + // 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] + ", branch " + b + " not found in multi-stage simulation", + eventTypes.get(i), expectedEventTypes[i]); + } + } + } + +}