1
0
Fork 0

Add swift unittests

This commit is contained in:
Lars Toenning 2022-03-27 11:56:24 +02:00 committed by James Turner
parent 9ec7d6b855
commit 3b40f704fd
7 changed files with 339 additions and 0 deletions

View file

@ -6,6 +6,7 @@ foreach( unit_test_category
Input
Main
Navaids
Network
Instrumentation
Scripting
AI

View file

@ -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
)

View file

@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: (C) 2022 Lars Toenning <dev@ltoenning.de>
* 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");

View file

@ -0,0 +1,105 @@
/*
* SPDX-FileCopyrightText: (C) 2022 Lars Toenning <dev@ltoenning.de>
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "test_swiftAircraftManager.hxx"
#include "test_suite/FGTestApi/testGlobals.hxx"
#include "Network/Swift/SwiftAircraftManager.h"
#include <Main/fg_props.hxx>
#include <Main/globals.hxx>
void SwiftAircraftManagerTest::setUp()
{
FGTestApi::setUp::initTestGlobals("SwiftService");
}
void SwiftAircraftManagerTest::tearDown()
{
FGTestApi::tearDown::shutdownTestGlobals();
}
std::vector<SGSharedPtr<FGAIBase>> SwiftAircraftManagerTest::getAIList()
{
return globals->get_subsystem<FGAIManager>()->get_ai_list();
}
void SwiftAircraftManagerTest::testAircraftManager()
{
globals->add_new_subsystem<FGAIManager>(SGSubsystemMgr::POST_FDM);
globals->get_subsystem<FGAIManager>()->bind();
globals->get_subsystem<FGAIManager>()->init();
FGSwiftAircraftManager acm;
acm.addPlane("BER123", "PATH_TO_MODEL");
CPPUNIT_ASSERT_EQUAL(globals->get_subsystem<FGAIManager>()->get_ai_list().size(), (size_t)1);
acm.addPlane("BAW123", "PATH_TO_MODEL");
CPPUNIT_ASSERT_EQUAL(globals->get_subsystem<FGAIManager>()->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);
}

View file

@ -0,0 +1,37 @@
/*
* SPDX-FileCopyrightText: (C) 2022 Lars Toenning <dev@ltoenning.de>
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef FLIGHTGEAR_TEST_SWIFTAIRCRAFTMANAGER_H
#define FLIGHTGEAR_TEST_SWIFTAIRCRAFTMANAGER_H
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <AIModel/AIBase.hxx>
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<SGSharedPtr<FGAIBase>> getAIList();
};
#endif //FLIGHTGEAR_TEST_SWIFTAIRCRAFTMANAGER_H

View file

@ -0,0 +1,140 @@
/*
* SPDX-FileCopyrightText: (C) 2022 Lars Toenning <dev@ltoenning.de>
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "test_swiftService.hxx"
#include "test_suite/FGTestApi/testGlobals.hxx"
#include "Network/Swift/service.h"
#include <Main/fg_props.hxx>
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);
}

View file

@ -0,0 +1,31 @@
/*
* SPDX-FileCopyrightText: (C) 2022 Lars Toenning <dev@ltoenning.de>
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef FLIGHTGEAR_TEST_SWIFTSERVICE_H
#define FLIGHTGEAR_TEST_SWIFTSERVICE_H
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
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