From 3b40f704fd7bab1bd998040b39d9d7fa6d0c3660 Mon Sep 17 00:00:00 2001 From: Lars Toenning Date: Sun, 27 Mar 2022 11:56:24 +0200 Subject: [PATCH] Add swift unittests --- test_suite/unit_tests/CMakeLists.txt | 1 + test_suite/unit_tests/Network/CMakeLists.txt | 14 ++ test_suite/unit_tests/Network/TestSuite.cxx | 11 ++ .../Network/test_swiftAircraftManager.cxx | 105 +++++++++++++ .../Network/test_swiftAircraftManager.hxx | 37 +++++ .../unit_tests/Network/test_swiftService.cxx | 140 ++++++++++++++++++ .../unit_tests/Network/test_swiftService.hxx | 31 ++++ 7 files changed, 339 insertions(+) create mode 100644 test_suite/unit_tests/Network/CMakeLists.txt create mode 100644 test_suite/unit_tests/Network/TestSuite.cxx create mode 100644 test_suite/unit_tests/Network/test_swiftAircraftManager.cxx create mode 100644 test_suite/unit_tests/Network/test_swiftAircraftManager.hxx create mode 100644 test_suite/unit_tests/Network/test_swiftService.cxx create mode 100644 test_suite/unit_tests/Network/test_swiftService.hxx diff --git a/test_suite/unit_tests/CMakeLists.txt b/test_suite/unit_tests/CMakeLists.txt index 23ba7d576..fdbea71b6 100644 --- a/test_suite/unit_tests/CMakeLists.txt +++ b/test_suite/unit_tests/CMakeLists.txt @@ -6,6 +6,7 @@ foreach( unit_test_category Input Main Navaids + Network Instrumentation Scripting AI diff --git a/test_suite/unit_tests/Network/CMakeLists.txt b/test_suite/unit_tests/Network/CMakeLists.txt new file mode 100644 index 000000000..67d91ee43 --- /dev/null +++ b/test_suite/unit_tests/Network/CMakeLists.txt @@ -0,0 +1,14 @@ +set(TESTSUITE_SOURCES + ${TESTSUITE_SOURCES} + ${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx + ${CMAKE_CURRENT_SOURCE_DIR}/test_swiftService.cxx + ${CMAKE_CURRENT_SOURCE_DIR}/test_swiftAircraftManager.cxx + PARENT_SCOPE + ) + +set(TESTSUITE_HEADERS + ${TESTSUITE_HEADERS} + ${CMAKE_CURRENT_SOURCE_DIR}/test_swiftService.hxx + ${CMAKE_CURRENT_SOURCE_DIR}/test_swiftAircraftManager.hxx + PARENT_SCOPE + ) diff --git a/test_suite/unit_tests/Network/TestSuite.cxx b/test_suite/unit_tests/Network/TestSuite.cxx new file mode 100644 index 000000000..f5cdfbbbf --- /dev/null +++ b/test_suite/unit_tests/Network/TestSuite.cxx @@ -0,0 +1,11 @@ +/* + * SPDX-FileCopyrightText: (C) 2022 Lars Toenning + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "test_swiftAircraftManager.hxx" +#include "test_swiftService.hxx" + +// Set up the unit tests. +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SwiftAircraftManagerTest, "Unit tests"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SwiftServiceTest, "Unit tests"); diff --git a/test_suite/unit_tests/Network/test_swiftAircraftManager.cxx b/test_suite/unit_tests/Network/test_swiftAircraftManager.cxx new file mode 100644 index 000000000..f7f474f9d --- /dev/null +++ b/test_suite/unit_tests/Network/test_swiftAircraftManager.cxx @@ -0,0 +1,105 @@ +/* + * SPDX-FileCopyrightText: (C) 2022 Lars Toenning + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "test_swiftAircraftManager.hxx" + +#include "test_suite/FGTestApi/testGlobals.hxx" + +#include "Network/Swift/SwiftAircraftManager.h" +#include
+#include
+ +void SwiftAircraftManagerTest::setUp() +{ + FGTestApi::setUp::initTestGlobals("SwiftService"); +} + +void SwiftAircraftManagerTest::tearDown() +{ + FGTestApi::tearDown::shutdownTestGlobals(); +} + + +std::vector> SwiftAircraftManagerTest::getAIList() +{ + return globals->get_subsystem()->get_ai_list(); +} + +void SwiftAircraftManagerTest::testAircraftManager() +{ + globals->add_new_subsystem(SGSubsystemMgr::POST_FDM); + globals->get_subsystem()->bind(); + globals->get_subsystem()->init(); + + FGSwiftAircraftManager acm; + acm.addPlane("BER123", "PATH_TO_MODEL"); + CPPUNIT_ASSERT_EQUAL(globals->get_subsystem()->get_ai_list().size(), (size_t)1); + + acm.addPlane("BAW123", "PATH_TO_MODEL"); + CPPUNIT_ASSERT_EQUAL(globals->get_subsystem()->get_ai_list().size(), (size_t)2); + + for (auto& aircraft : getAIList()) { + CPPUNIT_ASSERT(!aircraft->getDie()); + } + + acm.removeAllPlanes(); + for (auto& aircraft : getAIList()) { + CPPUNIT_ASSERT(aircraft->getDie()); + } + + acm.addPlane("BER123", "PATH_TO_MODEL"); + CPPUNIT_ASSERT(!getAIList()[2]->getDie()); + acm.removePlane("BER123"); + CPPUNIT_ASSERT(getAIList()[2]->getDie()); + + // Test position updates + acm.addPlane("SAS123", "PATH_TO_MODEL"); + SGGeod position; + position.setLatitudeDeg(50.0); + position.setLongitudeDeg(6.0); + position.setElevationM(1024); + + acm.updatePlanes({"SAS123"}, {position}, {SGVec3d(1.0, 2.0, 3.0)}, {200}, {false}); + CPPUNIT_ASSERT_EQUAL(fgGetString("/ai/models/swift[3]/callsign"), std::string("SAS123")); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/pitch-deg"), 1.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/roll-deg"), 2.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/true-heading-deg"), 3.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/position/latitude-deg"), 50.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/position/longitude-deg"), 6.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/velocities/true-airspeed-kt"), 200, 0.1); + + position.setLatitudeDeg(20.0); + position.setLongitudeDeg(4.0); + acm.updatePlanes({"SAS123"}, {position}, {SGVec3d(5.0, 6.0, 7.0)}, {400}, {false}); + CPPUNIT_ASSERT_EQUAL(fgGetString("/ai/models/swift[3]/callsign"), std::string("SAS123")); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/pitch-deg"), 5.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/roll-deg"), 6.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/true-heading-deg"), 7.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/position/latitude-deg"), 20.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/position/longitude-deg"), 4.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/velocities/true-airspeed-kt"), 400, 0.1); + + + // Update another aircraft + acm.addPlane("DAL123", "PATH_TO_MODEL"); + position.setLatitudeDeg(-20.0); + position.setLongitudeDeg(5.0); + acm.updatePlanes({"DAL123"}, {position}, {SGVec3d(1.0, 1.0, 1.0)}, {250}, {false}); + CPPUNIT_ASSERT_EQUAL(fgGetString("/ai/models/swift[4]/callsign"), std::string("DAL123")); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[4]/orientation/pitch-deg"), 1.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[4]/orientation/roll-deg"), 1.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[4]/orientation/true-heading-deg"), 1.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[4]/position/latitude-deg"), -20.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[4]/position/longitude-deg"), 5.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[4]/velocities/true-airspeed-kt"), 250, 0.1); + + CPPUNIT_ASSERT_EQUAL(fgGetString("/ai/models/swift[3]/callsign"), std::string("SAS123")); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/pitch-deg"), 5.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/roll-deg"), 6.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/true-heading-deg"), 7.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/position/latitude-deg"), 20.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/position/longitude-deg"), 4.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/velocities/true-airspeed-kt"), 400, 0.1); +} diff --git a/test_suite/unit_tests/Network/test_swiftAircraftManager.hxx b/test_suite/unit_tests/Network/test_swiftAircraftManager.hxx new file mode 100644 index 000000000..74aaa0ff9 --- /dev/null +++ b/test_suite/unit_tests/Network/test_swiftAircraftManager.hxx @@ -0,0 +1,37 @@ +/* + * SPDX-FileCopyrightText: (C) 2022 Lars Toenning + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef FLIGHTGEAR_TEST_SWIFTAIRCRAFTMANAGER_H +#define FLIGHTGEAR_TEST_SWIFTAIRCRAFTMANAGER_H + +#include +#include + +#include + + +class SwiftAircraftManagerTest : public CppUnit::TestFixture +{ + // Set up the test suite. + CPPUNIT_TEST_SUITE(SwiftAircraftManagerTest); + CPPUNIT_TEST(testAircraftManager); + CPPUNIT_TEST_SUITE_END(); + +public: + // Set up function for each test. + void setUp(); + + // Clean up after each test. + void tearDown(); + + // Test + void testAircraftManager(); + + // Helper + std::vector> getAIList(); +}; + + +#endif //FLIGHTGEAR_TEST_SWIFTAIRCRAFTMANAGER_H diff --git a/test_suite/unit_tests/Network/test_swiftService.cxx b/test_suite/unit_tests/Network/test_swiftService.cxx new file mode 100644 index 000000000..c41e66c29 --- /dev/null +++ b/test_suite/unit_tests/Network/test_swiftService.cxx @@ -0,0 +1,140 @@ +/* + * SPDX-FileCopyrightText: (C) 2022 Lars Toenning + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "test_swiftService.hxx" + +#include "test_suite/FGTestApi/testGlobals.hxx" + +#include "Network/Swift/service.h" +#include
+ +void SwiftServiceTest::setUp() +{ + FGTestApi::setUp::initTestGlobals("SwiftService"); + + // Setup properties + fgSetBool("/sim/freeze/master", true); + fgSetDouble("/position/latitude-deg", 50.12); + fgSetDouble("/position/longitude-deg", 6.3); + fgSetDouble("/position/altitude-ft", 12000.0); + fgSetDouble("/position/altitude-agl-ft", 1020.0); + fgSetDouble("/velocities/groundspeed-kt", 242.0); + fgSetDouble("/orientation/pitch-deg", 3.0); + fgSetDouble("/orientation/roll-deg", 1.0); + fgSetDouble("/orientation/heading-deg", 230.0); + fgSetBool("/gear/gear/wow", false); + fgSetDouble("/instrumentation/comm/frequencies/selected-mhz", 122.8); + fgSetDouble("/instrumentation/comm/frequencies/standby-mhz", 135.65); + fgSetDouble("/instrumentation/comm[1]/frequencies/selected-mhz", 121.5); + fgSetDouble("/instrumentation/comm[1]/frequencies/standby-mhz", 118.3); + fgSetInt("/instrumentation/transponder/id-code", 1234); + fgSetInt("/instrumentation/transponder/inputs/knob-mode", 1); + fgSetBool("/instrumentation/transponder/ident", true); + fgSetBool("/controls/lighting/beacon", true); + fgSetBool("/controls/lighting/landing-lights", false); + fgSetBool("/controls/lighting/nav-lights", true); + fgSetBool("/controls/lighting/strobe", true); + fgSetBool("/controls/lighting/taxi-light", false); + fgSetBool("/instrumentation/altimeter/serviceable", true); + fgSetDouble("/instrumentation/altimeter/pressure-alt-ft", 24000.0); + fgSetDouble("/surface-positions/flap-pos-norm", 0.0); + fgSetDouble("/gear/gear/position-norm", 0.7); + fgSetDouble("/surface-positions/speedbrake-pos-norm", 0.4); + fgSetString("/sim/aircraft", "glider"); + fgSetDouble("/position/ground-elev-m", 778.0); + fgSetDouble("/velocities/speed-east-fps", 20.0); + fgSetDouble("/velocities/speed-down-fps", -30.0); + fgSetDouble("/velocities/speed-north-fps", -10.2); + fgSetDouble("/orientation/roll-rate-degps", 1.0); + fgSetDouble("/orientation/pitch-rate-degps", 0.0); + fgSetDouble("/orientation/yaw-rate-degps", -2.0); + fgSetDouble("/instrumentation/comm/volume", 42.0); + fgSetDouble("/instrumentation/comm[1]/volume", 100.0); +} + +void SwiftServiceTest::tearDown() +{ + FGTestApi::tearDown::shutdownTestGlobals(); +} + +void SwiftServiceTest::testService() +{ + FGSwiftBus::CService service; + CPPUNIT_ASSERT(service.isPaused()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getLatitude(), 50.12, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getLongitude(), 6.3, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getAltitudeMSL(), 12000.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getHeightAGL(), 1020.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getGroundSpeed(), 242.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getPitch(), 3.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getRoll(), 1.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getTrueHeading(), 230.0, 0.1); + CPPUNIT_ASSERT(!service.getAllWheelsOnGround()); + CPPUNIT_ASSERT_EQUAL(service.getCom1Active(), 122800); + CPPUNIT_ASSERT_EQUAL(service.getCom1Standby(), 135650); + CPPUNIT_ASSERT_EQUAL(service.getCom2Active(), 121500); + CPPUNIT_ASSERT_EQUAL(service.getCom2Standby(), 118300); + + CPPUNIT_ASSERT_EQUAL(service.getTransponderCode(), 1234); + CPPUNIT_ASSERT_EQUAL(service.getTransponderMode(), 1); + CPPUNIT_ASSERT(service.getTransponderIdent()); + CPPUNIT_ASSERT(service.getBeaconLightsOn()); + CPPUNIT_ASSERT(!service.getLandingLightsOn()); + CPPUNIT_ASSERT(service.getNavLightsOn()); + CPPUNIT_ASSERT(service.getStrobeLightsOn()); + CPPUNIT_ASSERT(!service.getTaxiLightsOn()); + + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getFlapsDeployRatio(), 0.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getGearDeployRatio(), 0.7, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getSpeedBrakeRatio(), 0.4, 0.1); + + CPPUNIT_ASSERT_EQUAL(service.getAircraftName(), std::string("glider")); + + + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getGroundElevation(), 778.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getVelocityX(), 20.0 * SG_FEET_TO_METER, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getVelocityY(), -30.0 * SG_FEET_TO_METER * -1, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getVelocityZ(), -10.2 * SG_FEET_TO_METER, 0.1); + + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getRollRate(), 1.0 * SG_DEGREES_TO_RADIANS, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getPitchRate(), 0.0 * SG_DEGREES_TO_RADIANS, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getYawRate(), -2.0 * SG_DEGREES_TO_RADIANS, 0.1); + + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getCom1Volume(), 42.0, 0.1); + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getCom2Volume(), 100.0, 0.1); + + + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getPressAlt(), 24000.0, 0.1); + fgSetBool("/instrumentation/altimeter/serviceable", false); + // Fallback if altimeter is not serviceable + CPPUNIT_ASSERT_DOUBLES_EQUAL(service.getPressAlt(), service.getAltitudeMSL(), 0.1); + + // Test setter + service.setCom1Active(128550); + CPPUNIT_ASSERT_EQUAL(service.getCom1Active(), 128550); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/instrumentation/comm/frequencies/selected-mhz"), 128.550, 0.1); + + service.setCom1Standby(128650); + CPPUNIT_ASSERT_EQUAL(service.getCom1Standby(), 128650); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/instrumentation/comm/frequencies/standby-mhz"), 128.650, 0.1); + + + service.setCom2Active(121900); + CPPUNIT_ASSERT_EQUAL(service.getCom2Active(), 121900); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz"), 121.900, 0.1); + + service.setCom2Standby(121600); + CPPUNIT_ASSERT_EQUAL(service.getCom2Standby(), 121600); + CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/instrumentation/comm[1]/frequencies/standby-mhz"), 121.600, 0.1); + + service.setTransponderCode(2000); + CPPUNIT_ASSERT_EQUAL(service.getTransponderCode(), 2000); + CPPUNIT_ASSERT_EQUAL(fgGetInt("/instrumentation/transponder/id-code"), 2000); + + + service.setTransponderMode(0); + CPPUNIT_ASSERT_EQUAL(service.getTransponderMode(), 0); + CPPUNIT_ASSERT_EQUAL(fgGetInt("/instrumentation/transponder/inputs/knob-mode"), 0); +} diff --git a/test_suite/unit_tests/Network/test_swiftService.hxx b/test_suite/unit_tests/Network/test_swiftService.hxx new file mode 100644 index 000000000..eb29c10d4 --- /dev/null +++ b/test_suite/unit_tests/Network/test_swiftService.hxx @@ -0,0 +1,31 @@ +/* + * SPDX-FileCopyrightText: (C) 2022 Lars Toenning + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef FLIGHTGEAR_TEST_SWIFTSERVICE_H +#define FLIGHTGEAR_TEST_SWIFTSERVICE_H + +#include +#include + +class SwiftServiceTest : public CppUnit::TestFixture +{ + // Set up the test suite. + CPPUNIT_TEST_SUITE(SwiftServiceTest); + CPPUNIT_TEST(testService); + CPPUNIT_TEST_SUITE_END(); + +public: + // Set up function for each test. + void setUp(); + + // Clean up after each test. + void tearDown(); + + // Test + void testService(); +}; + + +#endif //FLIGHTGEAR_TEST_SWIFTSERVICE_H