| 1 | // SPDX-FileCopyrightText: (C) 2025 James Turner <[email protected]> |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "config.h" |
| 5 | |
| 6 | #include "test_controls.hxx" |
| 7 | |
| 8 | #include <cmath> |
| 9 | #include <cstring> |
| 10 | #include <memory> |
| 11 | |
| 12 | #include "cppunit/TestAssert.h" |
| 13 | #include "simgear/structure/exception.hxx" |
| 14 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 15 | |
| 16 | #include <Aircraft/controls.hxx> |
| 17 | #include <Main/fg_props.hxx> |
| 18 | |
| 19 | ControlsTests::~ControlsTests() = default; |
| 20 | |
| 21 | // Set up function for each test. |
| 22 | void ControlsTests::setUp() |
| 23 | { |
| 24 | FGTestApi::setUp::initTestGlobals("Controls"); |
| 25 | controls.reset(new FGControls); |
| 26 | controls->bind(); |
| 27 | } |
| 28 | |
| 29 | // Clean up after each test. |
| 30 | void ControlsTests::tearDown() |
| 31 | { |
| 32 | controls->unbind(); |
| 33 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 34 | } |
| 35 | |
| 36 | void ControlsTests::testClamp() |
| 37 | { |
| 38 | fgSetDouble("/controls/flight/elevator", 0.5); |
| 39 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.5, fgGetDouble("/controls/flight/elevator"), 1e-3); |
| 40 | |
| 41 | fgSetDouble("/controls/flight/elevator", 1.0); |
| 42 | CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, fgGetDouble("/controls/flight/elevator"), 1e-3); |
| 43 | |
| 44 | // check clamping |
| 45 | fgSetDouble("/controls/flight/elevator", 1.1); |
| 46 | CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, fgGetDouble("/controls/flight/elevator"), 1e-3); |
| 47 | |
| 48 | controls->set_elevator(-9999.0); |
| 49 | CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.0, fgGetDouble("/controls/flight/elevator"), 1e-3); |
| 50 | |
| 51 | fgSetDouble("/controls/flight/elevator", 999); |
| 52 | CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, fgGetDouble("/controls/flight/elevator"), 1e-3); |
| 53 | |
| 54 | // check set after clamped |
| 55 | fgSetDouble("/controls/flight/elevator", -0.5); |
| 56 | CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.5, fgGetDouble("/controls/flight/elevator"), 1e-3); |
| 57 | } |
| 58 | |
| 59 | void ControlsTests::testNANRejection() |
| 60 | { |
| 61 | // disabled on stable |
| 62 | #if 0 |
| 63 | CPPUNIT_ASSERT_THROW(fgSetDouble("/controls/flight/rudder-trim", std::nan("")), sg_range_exception); |
| 64 | |
| 65 | CPPUNIT_ASSERT_THROW(fgSetDouble("/controls/engines/engine[3]/condition", std::nan("")), sg_range_exception); |
| 66 | CPPUNIT_ASSERT_THROW(fgSetDouble("/controls/engines/engine[0]/throttle", std::nan("")), sg_range_exception); |
| 67 | |
| 68 | CPPUNIT_ASSERT_THROW(controls->set_condition(FGControls::ALL_ENGINES, std::nan("")), sg_range_exception); |
| 69 | CPPUNIT_ASSERT_THROW(controls->set_aileron(std::nan("")), sg_range_exception); |
| 70 | #endif |
| 71 | } |