| 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_history.hxx" |
| 7 | |
| 8 | #include <cmath> |
| 9 | #include <cstddef> |
| 10 | #include <cstring> |
| 11 | #include <memory> |
| 12 | |
| 13 | #include "cppunit/TestAssert.h" |
| 14 | #include "simgear/structure/exception.hxx" |
| 15 | #include "test_suite/FGTestApi/NavDataCache.hxx" |
| 16 | #include "test_suite/FGTestApi/TestPilot.hxx" |
| 17 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 18 | |
| 19 | #include <Airports/airport.hxx> |
| 20 | #include <Main/fg_props.hxx> |
| 21 | #include <Main/globals.hxx> |
| 22 | |
| 23 | using namespace std::chrono_literals; |
| 24 | |
| 25 | HistoryTests::~HistoryTests() = default; |
| 26 | |
| 27 | // Set up function for each test. |
| 28 | void HistoryTests::setUp() |
| 29 | { |
| 30 | FGTestApi::setUp::initTestGlobals("FlightHistory"); |
| 31 | FGTestApi::setUp::initNavDataCache(); |
| 32 | |
| 33 | fgSetBool("/sim/history/enabled", true); |
| 34 | history.reset(new FGFlightHistory); |
| 35 | |
| 36 | globals->get_subsystem_mgr()->add("history", history); |
| 37 | |
| 38 | globals->get_subsystem_mgr()->bind(); |
| 39 | globals->get_subsystem_mgr()->init(); |
| 40 | } |
| 41 | |
| 42 | // Clean up after each test. |
| 43 | void HistoryTests::tearDown() |
| 44 | { |
| 45 | history->unbind(); |
| 46 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 47 | } |
| 48 | |
| 49 | void HistoryTests::testClear() |
| 50 | { |
| 51 | auto lfpg = FGAirport::getByIdent("LFPG"); |
| 52 | auto pilot = SGSharedPtr<FGTestApi::TestPilot>(new FGTestApi::TestPilot); |
| 53 | FGTestApi::setPosition(lfpg->geod()); |
| 54 | pilot->resetAtPosition(lfpg->geod()); |
| 55 | |
| 56 | pilot->setSpeedKts(120); |
| 57 | pilot->setCourseTrue(180.0); |
| 58 | pilot->setTargetAltitudeFtMSL(10000); |
| 59 | |
| 60 | FGTestApi::runForTime(180); |
| 61 | |
| 62 | auto path = history->pathForHistory(); |
| 63 | CPPUNIT_ASSERT_EQUAL(size_t{174}, path.size()); |
| 64 | |
| 65 | auto pp = history->pagedPathForHistory(100); |
| 66 | CPPUNIT_ASSERT_EQUAL(size_t{100}, pp->path.size()); |
| 67 | |
| 68 | // clear completely |
| 69 | history->clear(); |
| 70 | |
| 71 | auto path2 = history->pathForHistory(); |
| 72 | CPPUNIT_ASSERT(path2.empty()); |
| 73 | |
| 74 | auto pp2 = history->pagedPathForHistory(100); |
| 75 | CPPUNIT_ASSERT(pp2->path.empty()); |
| 76 | |
| 77 | |
| 78 | FGTestApi::runForTime(3000); |
| 79 | // partial clear |
| 80 | history->clearOlderThan(1500s); |
| 81 | |
| 82 | auto path4 = history->pathForHistory(); |
| 83 | CPPUNIT_ASSERT_EQUAL(size_t{1888}, path4.size()); |
| 84 | } |