| 1 | /* |
| 2 | * Copyright (C) 2021 Keith Paterson |
| 3 | * |
| 4 | * This file is part of the program FlightGear. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | |
| 22 | #include "test_TrafficMgr.hxx" |
| 23 | |
| 24 | #include <cstring> |
| 25 | #include <memory> |
| 26 | |
| 27 | #include "test_suite/FGTestApi/NavDataCache.hxx" |
| 28 | #include "test_suite/FGTestApi/TestDataLogger.hxx" |
| 29 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 30 | |
| 31 | #include <Airports/airport.hxx> |
| 32 | #include <Traffic/TrafficMgr.hxx> |
| 33 | |
| 34 | #include <Main/fg_props.hxx> |
| 35 | #include <Main/globals.hxx> |
| 36 | |
| 37 | // Set up function for each test. |
| 38 | void TrafficMgrTests::setUp() |
| 39 | { |
| 40 | FGTestApi::setUp::initTestGlobals("TrafficMgr"); |
| 41 | FGTestApi::setUp::initNavDataCache(); |
| 42 | fgSetBool("sim/ai/enabled", true); |
| 43 | fgSetBool("sim/traffic-manager/enabled", true); |
| 44 | fgSetBool("/environment/realwx/enabled", false); |
| 45 | fgSetBool("/environment/metar/valid", false); |
| 46 | //Otherwise TrafficMgr won't load |
| 47 | fgSetBool("sim/signals/fdm-initialized", true); |
| 48 | globals->set_fg_root(SGPath::fromUtf8(FG_TEST_SUITE_DATA)); |
| 49 | } |
| 50 | |
| 51 | // Clean up after each test. |
| 52 | void TrafficMgrTests::tearDown() |
| 53 | { |
| 54 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 55 | } |
| 56 | |
| 57 | void TrafficMgrTests::testParse() { |
| 58 | globals->get_subsystem_mgr()->add<FGTrafficManager>(); |
| 59 | |
| 60 | globals->get_subsystem_mgr()->bind(); |
| 61 | globals->get_subsystem_mgr()->init(); |
| 62 | globals->get_subsystem_mgr()->postinit(); |
| 63 | |
| 64 | auto tmgr = globals->get_subsystem<FGTrafficManager>(); |
| 65 | FGScheduledFlightVecIterator fltBegin, fltEnd; |
| 66 | |
| 67 | for (size_t i = 0; i < 1000000; i++) |
| 68 | { |
| 69 | FGTestApi::runForTime(10.0); |
| 70 | // We have to wait for async parser |
| 71 | fltBegin = tmgr->getFirstFlight("TST_BN_2"); |
| 72 | fltEnd = tmgr->getLastFlight("TST_BN_2"); |
| 73 | if (fltBegin != fltEnd) { |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | int counter = 0; |
| 79 | for (FGScheduledFlightVecIterator i = fltBegin; i != fltEnd; i++) { |
| 80 | std::cout << (*i)->getCallSign() << counter++ << std::endl; |
| 81 | } |
| 82 | CPPUNIT_ASSERT_EQUAL(2, counter); |
| 83 | } |
| 84 | |
| 85 | void TrafficMgrTests::testTrafficManager() |
| 86 | { |
| 87 | FGAirportRef egeo = FGAirport::getByIdent("EGEO"); |
| 88 | |
| 89 | fgSetString("/sim/presets/airport-id", "EGEO"); |
| 90 | |
| 91 | std::cout << globals->get_fg_root() << "\r\n"; |
| 92 | globals->set_fg_root(SGPath::fromUtf8(FG_TEST_SUITE_DATA)); |
| 93 | std::cout << globals->get_fg_root() << "\r\n"; |
| 94 | |
| 95 | fgSetBool("/sim/traffic-manager/enabled", true); |
| 96 | fgSetBool("/sim/traffic-manager/active", false); |
| 97 | fgSetBool("/sim/ai/enabled", true); |
| 98 | fgSetBool("/environment/realwx/enabled", false); |
| 99 | fgSetBool("/environment/metar/valid", false); |
| 100 | fgSetBool("/sim/terrasync/ai-data-update-now", false); |
| 101 | |
| 102 | fgSetBool("/sim/traffic-manager/instantaneous-action", true); |
| 103 | fgSetBool("/sim/traffic-manager/heuristics", true); |
| 104 | fgSetBool("/sim/traffic-manager/dumpdata", false); |
| 105 | |
| 106 | fgSetBool("/sim/signals/fdm-initialized", true); |
| 107 | |
| 108 | FGTestApi::setPositionAndStabilise(egeo->geod()); |
| 109 | |
| 110 | auto tmgr = globals->get_subsystem_mgr()->add<FGTrafficManager>(); |
| 111 | |
| 112 | tmgr->bind(); |
| 113 | tmgr->init(); |
| 114 | |
| 115 | for( int i = 0; i < 30; i++) { |
| 116 | bool active = fgGetBool("/sim/traffic-manager/inited"); |
| 117 | // std::cout << "Inited " << "\t" << i << "\t" << active << "\r\n"; |
| 118 | FGTestApi::runForTime(5.0); |
| 119 | if(active) { |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | const SGPropertyNode *tm = fgGetNode("/sim/traffic-manager", true); |
| 125 | |
| 126 | for (int i = 0; i < tm->nChildren(); i++) { |
| 127 | const SGPropertyNode *model = tm->getChild(i); |
| 128 | std::cout << "TM : " << model->getDisplayName() << "\t" << model->nChildren() << "\n"; |
| 129 | for (int g = 0; g < model->nChildren(); g++) { |
| 130 | const SGPropertyNode *v; |
| 131 | v = model->getChild(g); |
| 132 | std::cout << "Node " << g << "\t" << v->getDisplayName() << "\n"; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | FGTestApi::runForTime(360.0); |
| 137 | |
| 138 | FGScheduledFlightVecIterator fltBegin, fltEnd; |
| 139 | fltBegin = tmgr->getFirstFlight("HBR_BN_2"); |
| 140 | fltEnd = tmgr->getLastFlight("HBR_BN_2"); |
| 141 | |
| 142 | if (fltBegin == fltEnd) { |
| 143 | CPPUNIT_FAIL("No Traffic found"); |
| 144 | } |
| 145 | |
| 146 | int counter = 0; |
| 147 | for (FGScheduledFlightVecIterator i = fltBegin; i != fltEnd; i++) { |
| 148 | std::cout << (*i)->getDepartureAirport()->getId() << "\t" << (*i)->getArrivalAirport()->getId() << "\t" << (*i)->getDepartureTime() << "\n"; |
| 149 | counter++; |
| 150 | } |
| 151 | CPPUNIT_ASSERT_EQUAL(25, counter); |
| 152 | } |