Debian Codegraph @

test_suite/unit_tests/Navaids/test_flightplan.cxx

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 James Turner
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
23 #include "test_flightplan.hxx"
24
25 #include <algorithm>
26
27 #include "test_suite/FGTestApi/testGlobals.hxx"
28 #include "test_suite/FGTestApi/NavDataCache.hxx"
29
30 #include <simgear/misc/strutils.hxx>
31 #include <simgear/misc/sg_dir.hxx>
32 #include <simgear/io/iostreams/sgstream.hxx>
33 #include <simgear/structure/exception.hxx>
34 #include <simgear/magvar/magvar.hxx>
35 #include <simgear/timing/sg_time.hxx>
36
37 #include <Navaids/FlightPlan.hxx>
38 #include <Navaids/routePath.hxx>
39 #include <Navaids/NavDataCache.hxx>
40 #include <Navaids/waypoint.hxx>
41 #include <Navaids/navlist.hxx>
42 #include <Navaids/navrecord.hxx>
43 #include <Navaids/airways.hxx>
44 #include <Navaids/fix.hxx>
45
46 #include <Airports/airport.hxx>
47 #include <Main/globals.hxx>
48
49 using namespace std::string_literals;
50 using namespace flightgear;
51
52 static bool static_haveProcedures = false;
53
54
55 /////////////////////////////////////////////////////////////////////////////
56
57 namespace {
58 class TestFPDelegate : public FlightPlan::Delegate
59 {
60 public:
61 TestFPDelegate(FlightPlan* plan) :
62 _plan(plan)
63 {}
64
65 virtual ~TestFPDelegate()
66 {
67 }
68
69 void sequence() override
70 {
71 }
72
73 void currentWaypointChanged() override
74 {
75 }
76
77 void waypointsChanged() override
78 {
79 sawWaypointsChange = true;
80 }
81
82 void departureChanged() override
83 {
84 sawDepartureChange = true;
85
86
87 // mimic the default delegate, inserting the SID waypoints
88
89 // clear anything existing
90 _plan->clearWayptsWithFlag(WPT_DEPARTURE);
91
92 // insert waypt for the dpearture runway
93 auto dr = new RunwayWaypt(_plan->departureRunway(), _plan);
94 dr->setFlag(WPT_DEPARTURE);
95 dr->setFlag(WPT_GENERATED);
96 _plan->insertWayptAtIndex(dr, 0);
97
98 if (_plan->sid()) {
99 WayptVec sidRoute;
100 bool ok = _plan->sid()->route(_plan->departureRunway(), _plan->sidTransition(), sidRoute);
101 if (!ok)
102 throw sg_exception("failed to route via SID");
103 int insertIndex = 1;
104 for (auto w : sidRoute) {
105 w->setFlag(WPT_DEPARTURE);
106 w->setFlag(WPT_GENERATED);
107 _plan->insertWayptAtIndex(w, insertIndex++);
108 }
109 }
110 }
111
112 void arrivalChanged() override
113 {
114 sawArrivalChange = true;
115
116 // mimic the default delegate, inserting the STAR waypoints
117
118 // clear anything existing
119 _plan->clearWayptsWithFlag(WPT_ARRIVAL);
120
121 if (!_plan->destinationAirport()) {
122 return;
123 }
124
125 if (!_plan->destinationRunway()) {
126 auto ap = new NavaidWaypoint(_plan->destinationAirport(), _plan);
127 ap->setFlag(WPT_ARRIVAL);
128 ap->setFlag(WPT_GENERATED);
129 _plan->insertWayptAtIndex(ap, -1);
130 return;
131 }
132
133 // insert waypt for the destination runway
134 auto dr = new RunwayWaypt(_plan->destinationRunway(), _plan);
135 dr->setFlag(WPT_ARRIVAL);
136 dr->setFlag(WPT_GENERATED);
137 auto leg = _plan->insertWayptAtIndex(dr, -1);
138
139 if (_plan->star()) {
140 WayptVec starRoute;
141 bool ok = _plan->star()->route(_plan->destinationRunway(), _plan->starTransition(), starRoute);
142 if (!ok)
143 throw sg_exception("failed to route via STAR");
144 int insertIndex = leg->index();
145 for (auto w : starRoute) {
146 w->setFlag(WPT_ARRIVAL);
147 w->setFlag(WPT_GENERATED);
148 _plan->insertWayptAtIndex(w, insertIndex++);
149 }
150 }
151
152 if (_plan->approach()) {
153 WayptVec approachRoute;
154 bool ok = _plan->approach()->routeFromVectors(approachRoute);
155 if (!ok)
156 throw sg_exception("failed to route approach");
157 int insertIndex = leg->index();
158 for (auto w : approachRoute) {
159 w->setFlag(WPT_ARRIVAL);
160 w->setFlag(WPT_GENERATED);
161 _plan->insertWayptAtIndex(w, insertIndex++);
162 }
163 }
164 }
165
166
167 void loaded() override
168 {
169 didLoad = true;
170 }
171
172 FlightPlan* _plan = nullptr;
173 bool didLoad = false;
174 bool sawDepartureChange = false;
175 bool sawArrivalChange = false;
176 bool sawWaypointsChange = false;
177 };
178
179 class TestFPDelegateFactory : public FlightPlan::DelegateFactory
180 {
181 public:
182 TestFPDelegateFactory() = default;
183 virtual ~TestFPDelegateFactory() = default;
184
185 FlightPlan::Delegate* createFlightPlanDelegate(FlightPlan* fp) override
186 {
187 auto d = new TestFPDelegate(fp);
188 _instances.push_back(d);
189 return d;
190 }
191
192 void destroyFlightPlanDelegate(FlightPlan* fp, FlightPlan::Delegate* d) override
193 {
194 auto it = std::find_if(_instances.begin(), _instances.end(), [d] (TestFPDelegate* fpd) {
195 return fpd == d;
196 });
197
198 CPPUNIT_ASSERT(it != _instances.end());
199
200 _instances.erase(it);
201 delete d;
202 }
203
204 static TestFPDelegate* delegateForPlan(FlightPlan* fp);
205
206 std::vector<TestFPDelegate*> _instances;
207 };
208
209 std::shared_ptr<TestFPDelegateFactory> static_factory;
210
211 TestFPDelegate* TestFPDelegateFactory::delegateForPlan(FlightPlan* fp)
212 {
213 auto it = std::find_if(static_factory->_instances.begin(),
214 static_factory->_instances.end(),
215 [fp] (TestFPDelegate* del) {
216 return del->_plan == fp;
217 });
218
219 if (it == static_factory->_instances.end())
220 return nullptr;
221
222 return *it;
223 }
224
225 } // of anonymous namespace
226
227 /////////////////////////////////////////////////////////////////////////////
228
229
230 // Set up function for each test.
231 void FlightplanTests::setUp()
232 {
233 FGTestApi::setUp::initTestGlobals("flightplan"s);
234 FGTestApi::setUp::initNavDataCache();
235
236 SGPath proceduresPath = SGPath::fromEnv("FG_PROCEDURES_PATH");
237 if (proceduresPath.exists()) {
238 static_haveProcedures = true;
239 globals->append_fg_scenery(proceduresPath);
240 }
241
242 globals->get_subsystem_mgr()->bind();
243 globals->get_subsystem_mgr()->init();
244 globals->get_subsystem_mgr()->postinit();
245 }
246
247
248 // Clean up after each test.
249 void FlightplanTests::tearDown()
250 {
251 FGTestApi::tearDown::shutdownTestGlobals();
252
253 if (static_factory) {
254 FlightPlan::unregisterDelegateFactory(static_factory);
255 static_factory.reset();
256 }
257 }
258
259 static FlightPlanRef makeTestFP(const std::string& depICAO, const std::string& depRunway,
260 const std::string& destICAO, const std::string& destRunway,
261 const std::string& waypoints, bool asRoute = false)
262 {
263 FlightPlanRef f = asRoute ? FlightPlan::createRoute() : FlightPlan::create();
264 FGTestApi::setUp::populateFPWithoutNasal(f, depICAO, depRunway, destICAO, destRunway, waypoints);
265 return f;
266 }
267
268 void FlightplanTests::testBasic()
269 {
270 FlightPlanRef fp1 = makeTestFP("EGCC"s, "23L"s, "EHAM"s, "24"s,
271 "TNT CLN"s);
272 fp1->setIdent("testplan"s);
273
274 CPPUNIT_ASSERT(fp1->ident() == "testplan"s);
275 CPPUNIT_ASSERT(fp1->departureAirport()->ident() == "EGCC"s);
276 CPPUNIT_ASSERT(fp1->departureRunway()->ident() == "23L"s);
277 CPPUNIT_ASSERT(fp1->destinationAirport()->ident() == "EHAM"s);
278 CPPUNIT_ASSERT(fp1->destinationRunway()->ident() == "24"s);
279
280 CPPUNIT_ASSERT_EQUAL(5, fp1->numLegs());
281
282 CPPUNIT_ASSERT(fp1->legAtIndex(0)->waypoint()->source()->ident() == "23L"s);
283
284 CPPUNIT_ASSERT(fp1->legAtIndex(1)->waypoint()->source()->ident() == "TNT"s);
285 CPPUNIT_ASSERT(fp1->legAtIndex(1)->waypoint()->source()->name() == "TRENT VOR-DME"s);
286
287 CPPUNIT_ASSERT(fp1->legAtIndex(2)->waypoint()->source()->ident() == "CLN"s);
288 CPPUNIT_ASSERT(fp1->legAtIndex(2)->waypoint()->source()->name() == "CLACTON VOR-DME"s);
289
290 CPPUNIT_ASSERT(fp1->legAtIndex(4)->waypoint()->source()->ident() == "24"s);
291
292 }
293
294 void FlightplanTests::testRoutePathBasic()
295 {
296 FlightPlanRef fp1 = makeTestFP("EGHI"s, "20"s, "EDDM"s, "08L"s,
297 "SFD LYD BNE CIV ELLX LUX SAA KRH WLD"s);
298
299
300 RoutePath rtepath(fp1);
301 const int legCount = fp1->numLegs();
302 for (int leg = 0; leg < legCount; ++leg) {
303 rtepath.trackForIndex(leg);
304 rtepath.pathForIndex(leg);
305 rtepath.distanceForIndex(leg);
306 }
307
308 rtepath.distanceBetweenIndices(2, 5);
309
310 // check some leg parameters
311
312 // BOLOUGNE SUR MER, near LFAY (AMIENS)
313 FGNavRecordRef bne = FGNavList::findByFreq(113.8, FGAirport::getByIdent("LFAY"s)->geod());
314
315 // CHIEVRES
316 FGNavRecordRef civ = FGNavList::findByFreq(113.2, FGAirport::getByIdent("EBCI"s)->geod());
317
318 double distM = SGGeodesy::distanceM(bne->geod(), civ->geod());
319 double trackDeg = SGGeodesy::courseDeg(bne->geod(), civ->geod());
320
321 CPPUNIT_ASSERT_DOUBLES_EQUAL(trackDeg, rtepath.trackForIndex(4), 0.5);
322 CPPUNIT_ASSERT_DOUBLES_EQUAL(distM, rtepath.distanceForIndex(4), 2000); // 2km precision, allow for turns
323
324 }
325
326 // https://sourceforge.net/p/flightgear/codetickets/1703/
327 // https://sourceforge.net/p/flightgear/codetickets/1939/
328
329 void FlightplanTests::testRoutePathSkipped()
330 {
331 FlightPlanRef fp1 = makeTestFP("EHAM"s, "24"s, "EDDM"s, "08L"s,
332 "EHEH KBO TAU FFM FFM/100/0.01 FFM/120/0.02 WUR WLD"s);
333
334
335 RoutePath rtepath(fp1);
336
337 // skipped point uses inbound track
338 CPPUNIT_ASSERT_EQUAL(rtepath.trackForIndex(4), rtepath.trackForIndex(5));
339
340 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, rtepath.distanceForIndex(5), 1e-9);
341 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, rtepath.distanceForIndex(6), 1e-9);
342
343 CPPUNIT_ASSERT_DOUBLES_EQUAL(101000, rtepath.distanceForIndex(7), 1000);
344
345
346 // this tests skipping two preceeding points works as it should
347 SGGeodVec vec = rtepath.pathForIndex(7);
348 CPPUNIT_ASSERT(vec.size() == 9);
349 }
350
351 void FlightplanTests::testRoutePathTrivialFlightPlan()
352 {
353 FlightPlanRef fp1 = makeTestFP("EGPH"s, "24"s, "EGPH"s, "06"s,
354 ""s);
355
356
357 RoutePath rtepath(fp1);
358 const int legCount = fp1->numLegs();
359 for (int leg = 0; leg < legCount; ++leg) {
360 rtepath.trackForIndex(leg);
361 rtepath.pathForIndex(leg);
362 rtepath.distanceForIndex(leg);
363 }
364
365 CPPUNIT_ASSERT_DOUBLES_EQUAL(19.5, fp1->totalDistanceNm(), 0.1);
366 }
367
368 void FlightplanTests::testRoutePathVec()
369 {
370 FlightPlanRef fp1 = makeTestFP("KNUQ"s, "14L"s, "PHNL"s, "22R"s,
371 "ROKME WOVAB"s);
372 RoutePath rtepath(fp1);
373
374 SGGeodVec vec = rtepath.pathForIndex(0);
375
376 FGAirportRef ksfo = FGAirport::findByIdent("KSFO"s);
377 FGFixRef rokme = fgpositioned_cast<FGFix>(FGPositioned::findClosestWithIdent("ROKME"s, ksfo->geod()));
378 auto depRwy = fp1->departureRunway();
379
380 CPPUNIT_ASSERT_DOUBLES_EQUAL(depRwy->geod().getLongitudeDeg(), vec.front().getLongitudeDeg(), 0.01);
381 CPPUNIT_ASSERT_DOUBLES_EQUAL(depRwy->geod().getLatitudeDeg(), vec.front().getLatitudeDeg(), 0.01);
382
383 SGGeodVec vec1 = rtepath.pathForIndex(1);
384 CPPUNIT_ASSERT_DOUBLES_EQUAL(rokme->geod().getLongitudeDeg(), vec1.back().getLongitudeDeg(), 0.01);
385 CPPUNIT_ASSERT_DOUBLES_EQUAL(rokme->geod().getLatitudeDeg(), vec1.back().getLatitudeDeg(), 0.01);
386
387 SGGeodVec vec2 = rtepath.pathForIndex(2);
388 CPPUNIT_ASSERT_DOUBLES_EQUAL(rokme->geod().getLongitudeDeg(), vec2.front().getLongitudeDeg(), 0.01);
389 CPPUNIT_ASSERT_DOUBLES_EQUAL(rokme->geod().getLatitudeDeg(), vec2.front().getLatitudeDeg(), 0.01);
390
391 //CPPUNIT_ASSERT(vec.front()
392 }
393
394 void FlightplanTests::testRoutePathFinalLegVQPR15()
395 {
396 // test behaviour of RoutePath when the last leg prior to the arrival runway
397 // is beyond the runway. This occurs in Paro RNAVZ15 approach.
398
399 if (!static_haveProcedures)
400 return;
401
402 static_factory = std::make_shared<TestFPDelegateFactory>();
403 FlightPlan::registerDelegateFactory(static_factory);
404
405 FlightPlanRef f = FlightPlan::create();
406 // auto ourDelegate = TestFPDelegateFactory::delegateForPlan(f);
407
408 auto vidp = FGAirport::findByIdent("VIDP"s);
409 f->setDeparture(vidp->getRunwayByIdent("09"s));
410
411 auto vqpr = FGAirport::findByIdent("VQPR"s);
412 f->setDestination(vqpr->getRunwayByIdent("15"s));
413 f->setApproach(vqpr->findApproachWithIdent("RNVZ15"s));
414 RoutePath rtepath(f);
415 CPPUNIT_ASSERT_DOUBLES_EQUAL(1100, rtepath.distanceForIndex(14), 100); // calculated from raw lat lon.
416 }
417
418 void FlightplanTests::testRoutPathWpt0Midflight()
419 {
420 // test behaviour of RoutePath when WP0 is not a runway
421 // happens for the Airbus ND which removes past wpts when sequencing
422
423 FlightPlanRef fp1 = makeTestFP("KNUQ"s, "14L"s, "PHNL"s, "22R"s,
424 "ROKME WOVAB"s);
425 // actually delete leg 0 so we start at ROKME
426 fp1->deleteIndex(0);
427
428 RoutePath rtepath(fp1);
429
430 SGGeodVec vec = rtepath.pathForIndex(0);
431
432 FGAirportRef ksfo = FGAirport::findByIdent("KSFO"s);
433 FGFixRef rokme = fgpositioned_cast<FGFix>(FGPositioned::findClosestWithIdent("ROKME"s, ksfo->geod()));
434
435 CPPUNIT_ASSERT_DOUBLES_EQUAL(rokme->geod().getLongitudeDeg(), vec.front().getLongitudeDeg(), 0.01);
436 CPPUNIT_ASSERT_DOUBLES_EQUAL(rokme->geod().getLatitudeDeg(), vec.front().getLatitudeDeg(), 0.01);
437
438 SGGeodVec vec2 = rtepath.pathForIndex(1);
439 CPPUNIT_ASSERT_DOUBLES_EQUAL(rokme->geod().getLongitudeDeg(), vec2.front().getLongitudeDeg(), 0.01);
440 CPPUNIT_ASSERT_DOUBLES_EQUAL(rokme->geod().getLatitudeDeg(), vec2.front().getLatitudeDeg(), 0.01);
441
442 //CPPUNIT_ASSERT(vec.front()
443 }
444
445 void FlightplanTests::testBasicAirways()
446 {
447 Airway* awy = Airway::findByIdent("J547"s, Airway::HighLevel);
448 CPPUNIT_ASSERT_EQUAL(awy->ident(), "J547"s);
449
450 FGAirportRef kord = FGAirport::findByIdent("KORD"s);
451 FlightPlanRef f = FlightPlan::create();
452 f->setDeparture(kord);
453
454 CPPUNIT_ASSERT(awy->findEnroute("KITOK"s));
455 CPPUNIT_ASSERT(awy->findEnroute("LESUB"s));
456
457 auto wpt = awy->findEnroute("FNT"s);
458 CPPUNIT_ASSERT(wpt);
459 CPPUNIT_ASSERT(wpt->ident() == "FNT"s);
460 CPPUNIT_ASSERT(wpt->source() == FGNavRecord::findClosestWithIdent("FNT"s, kord->geod()));
461
462 auto wptKUBBS = f->waypointFromString("KUBBS"s);
463 auto wptFNT = f->waypointFromString("FNT"s);
464
465 CPPUNIT_ASSERT(awy->canVia(wptKUBBS, wptFNT));
466
467 WayptVec path = awy->via(wptKUBBS, wptFNT);
468 CPPUNIT_ASSERT_EQUAL(4, static_cast<int>(path.size()));
469
470 CPPUNIT_ASSERT_EQUAL("PMM"s, path.at(0)->ident());
471 CPPUNIT_ASSERT_EQUAL("HASTE"s, path.at(1)->ident());
472 CPPUNIT_ASSERT_EQUAL("DEWIT"s, path.at(2)->ident());
473 CPPUNIT_ASSERT_EQUAL("FNT"s, path.at(3)->ident());
474
475 f->insertWayptsAtIndex(path, 0);
476 CPPUNIT_ASSERT_EQUAL(4, static_cast<int>(f->numLegs()));
477 CPPUNIT_ASSERT_EQUAL("DEWIT"s, f->legAtIndex(2)->waypoint()->ident());
478 CPPUNIT_ASSERT_EQUAL(static_cast<RouteBase*>(awy), f->legAtIndex(2)->waypoint()->owner());
479 }
480
481 void FlightplanTests::testViaInsertIntoFP()
482 {
483 Airway* awy = Airway::findByIdent("J547"s, Airway::HighLevel);
484 CPPUNIT_ASSERT_EQUAL(awy->ident(), "J547"s);
485
486 FGAirportRef kord = FGAirport::findByIdent("KORD"s);
487 FlightPlanRef f = FlightPlan::create();
488 f->setDeparture(kord);
489
490 CPPUNIT_ASSERT(awy->findEnroute("KITOK"s));
491 CPPUNIT_ASSERT(awy->findEnroute("LESUB"s));
492
493 auto wpt = awy->findEnroute("FNT"s);
494 CPPUNIT_ASSERT(wpt);
495
496 auto wptKUBBS = f->waypointFromString("KUBBS"s);
497 auto wptFNT = f->waypointFromString("FNT"s);
498 f->insertWayptAtIndex(wptKUBBS, -1);
499 CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(f->numLegs()));
500
501 Via* via = new Via(f, awy, wptFNT->source());
502 f->insertWayptAtIndex(via, -1);
503 CPPUNIT_ASSERT_EQUAL(5, static_cast<int>(f->numLegs()));
504 CPPUNIT_ASSERT_EQUAL("PMM"s, f->legAtIndex(1)->waypoint()->ident());
505 CPPUNIT_ASSERT_EQUAL(static_cast<RouteBase*>(awy), f->legAtIndex(1)->waypoint()->owner());
506 }
507
508 void FlightplanTests::testViaInsertIntoRoute()
509 {
510 Airway* awy = Airway::findByIdent("J547"s, Airway::HighLevel);
511 CPPUNIT_ASSERT_EQUAL(awy->ident(), "J547"s);
512
513 FGAirportRef kord = FGAirport::findByIdent("KORD"s);
514 FlightPlanRef f = FlightPlan::createRoute();
515 f->setDeparture(kord);
516
517 CPPUNIT_ASSERT(awy->findEnroute("KITOK"s));
518 CPPUNIT_ASSERT(awy->findEnroute("LESUB"s));
519
520 auto wpt = awy->findEnroute("FNT"s);
521 CPPUNIT_ASSERT(wpt);
522
523 auto wptKUBBS = f->waypointFromString("KUBBS"s);
524 auto wptFNT = f->waypointFromString("FNT"s);
525 f->insertWayptAtIndex(wptKUBBS, -1);
526 CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(f->numLegs()));
527
528 Via* via = new Via(f, awy, wptFNT->source());
529 f->insertWayptAtIndex(via, -1);
530 CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(f->numLegs()));
531
532 // clone and convert to active flight-plan; Vias
533 // should be expanded
534 FlightPlanRef activeFP = f->clone("ACT", true);
535
536 CPPUNIT_ASSERT_EQUAL(5, static_cast<int>(activeFP->numLegs()));
537 CPPUNIT_ASSERT_EQUAL("PMM"s, activeFP->legAtIndex(1)->waypoint()->ident());
538 CPPUNIT_ASSERT_EQUAL(static_cast<RouteBase*>(awy), activeFP->legAtIndex(1)->waypoint()->owner());
539 }
540
541 void FlightplanTests::testAirwayNetworkRoute()
542 {
543 FGAirportRef egph = FGAirport::findByIdent("EGPH"s);
544 FlightPlanRef f = FlightPlan::create();
545 f->setDeparture(egph);
546
547 auto highLevelNet = Airway::highLevel();
548
549 auto wptTLA = f->waypointFromString("TLA"s);
550 auto wptCNA = f->waypointFromString("CNA"s);
551
552 WayptVec route;
553 bool ok = highLevelNet->route(wptTLA, wptCNA, route);
554 CPPUNIT_ASSERT(ok);
555
556 CPPUNIT_ASSERT_EQUAL(static_cast<int>(route.size()), 18);
557 }
558
559 void FlightplanTests::testParseICAORoute()
560 {
561 FGAirportRef kord = FGAirport::findByIdent("KORD"s);
562 FlightPlanRef f = FlightPlan::create();
563 f->setDeparture(kord);
564 f->setDestination(FGAirport::findByIdent("KSAN"s));
565
566 const char* route = "DCT JOT J26 IRK J96 SLN J18 GCK J96 CIM J134 GUP J96 KEYKE J134 DRK J78 LANCY J96 PKE";
567 // const char* route = "DCT KUBBS J547 FNT Q824 HOCKE Q905 SIKBO Q907 MIILS N400A TUDEP NATW GISTI DCT SLANY UL9 DIKAS UL18 GAVGO UL9 KONAN UL607 UBIDU Y863 RUDUS T109 HAREM T104 ANORA STAR";
568 bool ok = f->parseICAORouteString(route);
569 CPPUNIT_ASSERT(ok);
570
571
572
573 }
574
575 void FlightplanTests::testParseICANLowLevelRoute()
576 {
577 const char* route = "DCT DPA V6 IOW V216 LAA V210 GOSIP V83 ACHES V210 BLOKE V83 ALS V210 RSK V95 INW V12 HOXOL V264 OATES V12 JUWSO V264 PKE";
578
579 FGAirportRef kord = FGAirport::findByIdent("KORD"s);
580 FlightPlanRef f = FlightPlan::create();
581 f->setDeparture(kord);
582 f->setDestination(FGAirport::findByIdent("KSAN"s));
583
584 bool ok = f->parseICAORouteString(route);
585 CPPUNIT_ASSERT(ok);
586 }
587
588 // https://sourceforge.net/p/flightgear/codetickets/1814/
589 void FlightplanTests::testBug1814()
590 {
591 const std::string fpXML = R"(<?xml version="1.0" encoding="UTF-8"?>
592 <PropertyList>
593 <version type="int">2</version>
594 <departure>
595 <airport type="string">SAWG</airport>
596 <runway type="string">25</runway>
597 </departure>
598 <destination>
599 <airport type="string">SUMU</airport>
600 </destination>
601 <route>
602 <wp n="6">
603 <type type="string">navaid</type>
604 <ident type="string">PUGLI</ident>
605 <lon type="double">-60.552200</lon>
606 <lat type="double">-40.490000</lat>
607 </wp>
608 <wp n="7">
609 <type type="string">navaid</type>
610 <ident type="string">SIGUL</ident>
611 <lon type="double">-59.655800</lon>
612 <lat type="double">-38.312800</lat>
613 </wp>
614 <wp n="8">
615 <type type="string">navaid</type>
616 <ident type="string">MDP</ident>
617 <lon type="double">-57.576400</lon>
618 <lat type="double">-37.929800</lat>
619 </wp>
620 </route>
621 </PropertyList>
622 )";
623
624 std::istringstream stream(fpXML);
625 FlightPlanRef f = FlightPlan::create();
626 bool ok = f->load(stream);
627 CPPUNIT_ASSERT(ok);
628
629 auto leg = f->legAtIndex(1);
630 CPPUNIT_ASSERT_EQUAL("SIGUL"s, leg->waypoint()->ident());
631
632 CPPUNIT_ASSERT_DOUBLES_EQUAL(137, leg->distanceNm(), 0.5);
633 CPPUNIT_ASSERT_DOUBLES_EQUAL(101, f->legAtIndex(2)->distanceNm(), 0.5);
634 }
635
636 void FlightplanTests::testLoadSaveMachRestriction()
637 {
638 const std::string fpXML = R"(<?xml version="1.0" encoding="UTF-8"?>
639 <PropertyList>
640 <version type="int">2</version>
641 <departure>
642 <airport type="string">SAWG</airport>
643 <runway type="string">25</runway>
644 </departure>
645 <destination>
646 <airport type="string">SUMU</airport>
647 </destination>
648 <route>
649 <wp n="0">
650 <type type="string">navaid</type>
651 <ident type="string">PUGLI</ident>
652 <lon type="double">-60.552200</lon>
653 <lat type="double">-40.490000</lat>
654 </wp>
655 <wp n="1">
656 <type type="string">basic</type>
657 <alt-restrict type="string">at</alt-restrict>
658 <altitude-ft type="double">36000</altitude-ft>
659 <speed-restrict type="string">mach</speed-restrict>
660 <speed type="double">1.24</speed>
661 <ident type="string">SV002</ident>
662 <lon type="double">-115.50531</lon>
663 <lat type="double">37.89523</lat>
664 </wp>
665 <wp n="2">
666 <type type="string">navaid</type>
667 <ident type="string">SIGUL</ident>
668 <lon type="double">-60.552200</lon>
669 <lat type="double">-40.490000</lat>
670 </wp>
671 </route>
672 </PropertyList>
673 )";
674
675 std::istringstream stream(fpXML);
676 FlightPlanRef f = FlightPlan::create();
677 bool ok = f->load(stream);
678 CPPUNIT_ASSERT(ok);
679
680 auto leg = f->legAtIndex(1);
681 CPPUNIT_ASSERT_EQUAL(SPEED_RESTRICT_MACH, leg->speedRestriction());
682 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.24, leg->speedMach(), 0.01);
683
684 auto firstLeg = f->legAtIndex(0);
685 firstLeg->setSpeed(SPEED_RESTRICT_MACH, 1.56);
686
687 // upgrade to a hold and set the count
688 f->legAtIndex(2)->setHoldCount(8);
689
690 // round trip through XML to check :)
691 std::ostringstream ss;
692 f->save(ss);
693
694 std::istringstream iss(ss.str());
695 FlightPlanRef f2 = FlightPlan::create();
696 ok = f2->load(iss);
697 CPPUNIT_ASSERT(ok);
698
699 auto leg3 = f2->legAtIndex(0);
700 CPPUNIT_ASSERT_EQUAL(SPEED_RESTRICT_MACH, leg3->speedRestriction());
701 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.56, leg3->speedMach(), 0.01);
702
703 CPPUNIT_ASSERT_EQUAL(8, f2->legAtIndex(2)->holdCount());
704 }
705
706 void FlightplanTests::testLoadSaveBetweenRestriction()
707 {
708 const std::string fpXML = R"(<?xml version="1.0" encoding="UTF-8"?>
709 <PropertyList>
710 <version type="int">2</version>
711 <departure>
712 <airport type="string">SAWG</airport>
713 <runway type="string">25</runway>
714 </departure>
715 <destination>
716 <airport type="string">SUMU</airport>
717 </destination>
718 <route>
719 <wp n="0">
720 <type type="string">navaid</type>
721 <ident type="string">PUGLI</ident>
722 <lon type="double">-60.552200</lon>
723 <lat type="double">-40.490000</lat>
724 </wp>
725 <wp n="1">
726 <type type="string">basic</type>
727 <alt-restrict type="string">between</alt-restrict>
728 <altitude-ft type="double">36000</altitude-ft>
729 <constraint-altitude type="double">32000</constraint-altitude>
730 <ident type="string">SV002</ident>
731 <lon type="double">-115.50531</lon>
732 <lat type="double">37.89523</lat>
733 </wp>
734 <wp n="2">
735 <type type="string">navaid</type>
736 <ident type="string">SIGUL</ident>
737 <lon type="double">-60.552200</lon>
738 <lat type="double">-40.490000</lat>
739 </wp>
740 </route>
741 </PropertyList>
742 )";
743
744 std::istringstream stream(fpXML);
745 FlightPlanRef f = FlightPlan::create();
746 bool ok = f->load(stream);
747 CPPUNIT_ASSERT(ok);
748
749 auto leg = f->legAtIndex(1);
750 CPPUNIT_ASSERT_EQUAL(RESTRICT_BETWEEN, leg->altitudeRestriction());
751 CPPUNIT_ASSERT_DOUBLES_EQUAL(36000, leg->altitudeFt(), 0.01);
752 CPPUNIT_ASSERT_DOUBLES_EQUAL(32000, leg->waypoint()->constraintAltitude(ALTITUDE_FEET), 0.01);
753 }
754
755 void FlightplanTests::testRestrictionUnits()
756 {
757 FlightPlanRef fp1 = makeTestFP("LIRF"s, "34L"s, "LEBL"s, "25R"s,
758 "ESINO GITRI BALEN MUREN TOSNU"s);
759 auto leg = fp1->legAtIndex(2);
760 leg->setAltitude(RESTRICT_ABOVE, 120, ALTITUDE_FLIGHTLEVEL);
761 CPPUNIT_ASSERT_DOUBLES_EQUAL(leg->altitude(), 12000.0, 0.1);
762 CPPUNIT_ASSERT_DOUBLES_EQUAL(leg->altitude(ALTITUDE_METER), 12000.0 * SG_FEET_TO_METER, 0.1);
763
764 leg->setSpeed(RESTRICT_AT, 300);
765 CPPUNIT_ASSERT_DOUBLES_EQUAL(leg->speedKts(), 300, 0.1);
766 CPPUNIT_ASSERT_DOUBLES_EQUAL(leg->speed(), 300, 0.1);
767 CPPUNIT_ASSERT_DOUBLES_EQUAL(leg->speed(SPEED_KPH), 300 * SG_KT_TO_MPS * SG_MPS_TO_KMH, 0.1);
768
769 CPPUNIT_ASSERT_DOUBLES_EQUAL(leg->speedMach(), 0.473, 0.1);
770
771 // setting a mach restriction assumes Mach units by default
772 leg->setSpeed(SPEED_RESTRICT_MACH, 0.7);
773 leg->setAltitude(RESTRICT_AT, 38000);
774 CPPUNIT_ASSERT_DOUBLES_EQUAL(leg->speedKts(), 401, 1);
775 }
776
777 void FlightplanTests::testBasicDiscontinuity()
778 {
779 FlightPlanRef fp1 = makeTestFP("LIRF"s, "34L"s, "LEBL"s, "25R"s,
780 "ESINO GITRI BALEN MUREN TOSNU"s);
781
782 const auto tdBefore = fp1->totalDistanceNm();
783
784
785 const SGGeod balenPos = fp1->legAtIndex(3)->waypoint()->position();
786 const SGGeod murenPos = fp1->legAtIndex(4)->waypoint()->position();
787 const auto crs = SGGeodesy::courseDeg(balenPos, murenPos);
788
789 // total distance should not change
790 fp1->insertWayptAtIndex(new Discontinuity(fp1), 4); // betwee BALEN and MUREN
791
792 CPPUNIT_ASSERT_DOUBLES_EQUAL(tdBefore, fp1->totalDistanceNm(), 1.0);
793
794
795 CPPUNIT_ASSERT_DOUBLES_EQUAL(fp1->legAtIndex(4)->courseDeg(),
796 SGGeodesy::courseDeg(balenPos, murenPos), 0.1);
797 CPPUNIT_ASSERT_DOUBLES_EQUAL(fp1->legAtIndex(4)->distanceNm(), 0.0, 0.1);
798 CPPUNIT_ASSERT_DOUBLES_EQUAL(fp1->legAtIndex(5)->courseDeg(),
799 SGGeodesy::courseDeg(balenPos, murenPos), 0.1);
800 CPPUNIT_ASSERT_DOUBLES_EQUAL(fp1->legAtIndex(5)->distanceNm(),
801 SGGeodesy::distanceNm(balenPos, murenPos), 0.1);
802
803 // ensure that pointAlongRoute works correctly going into the DISCON
804
805
806 const auto pos1 = fp1->pointAlongRoute(3, 20.0);
807 const auto validP1 = SGGeodesy::direct(balenPos, crs, 20.0 * SG_NM_TO_METER);
808
809 CPPUNIT_ASSERT(SGGeodesy::distanceM(pos1, validP1) < 500.0);
810
811 const auto pos2 = fp1->pointAlongRoute(5, -10.0);
812 const auto crs2 = SGGeodesy::courseDeg(murenPos, balenPos);
813 const auto validP2 = SGGeodesy::direct(murenPos, crs2, 10.0 * SG_NM_TO_METER);
814
815 CPPUNIT_ASSERT(SGGeodesy::distanceM(pos2, validP2) < 500.0);
816
817 // remove the discontinuity
818 fp1->deleteIndex(4);
819 CPPUNIT_ASSERT_DOUBLES_EQUAL(fp1->legAtIndex(4)->courseDeg(),
820 SGGeodesy::courseDeg(balenPos, murenPos), 0.1);
821 CPPUNIT_ASSERT_DOUBLES_EQUAL(fp1->legAtIndex(4)->distanceNm(),
822 SGGeodesy::distanceNm(balenPos, murenPos), 0.1);
823
824 CPPUNIT_ASSERT_DOUBLES_EQUAL(tdBefore, fp1->totalDistanceNm(), 1.0);
825
826 }
827
828 void FlightplanTests::testOnlyDiscontinuityRoute()
829 {
830 FlightPlanRef f = FlightPlan::create();
831 FGAirportRef depApt = FGAirport::getByIdent("LFBD"s);
832 f->setDeparture(depApt);
833
834
835 FGAirportRef destApt = FGAirport::getByIdent("EHAM"s);
836 f->setDestination(destApt);
837
838 f->insertWayptAtIndex(new Discontinuity(f), 0);
839
840 RoutePath rp1(f);
841
842 // discontinuity should act like a straight segment between preceeding and following
843 const double d = SGGeodesy::distanceNm(depApt->geod(), destApt->geod());
844 // CPPUNIT_ASSERT_DOUBLES_EQUAL(rp1.distanceForIndex(0), d, 0.5);
845
846 // start inserting waypoints ahead of the DISCON, Boeing FPL style
847 WayptRef wpt = f->waypointFromString("LMG"s);
848 f->insertWayptAtIndex(wpt, 0);
849
850 wpt = f->waypointFromString("KUKOR"s);
851 f->insertWayptAtIndex(wpt, 1);
852
853 wpt = f->waypointFromString("EPL"s);
854 f->insertWayptAtIndex(wpt, 2);
855 }
856
857 void FlightplanTests::testLeadingWPDynamic()
858 {
859 FlightPlanRef f = FlightPlan::create();
860 // plan has no departure, so this discon is floating
861 f->insertWayptAtIndex(new Discontinuity(f), 0);
862
863 auto ha = new HeadingToAltitude(f, "TO_3000"s, 90);
864 ha->setAltitude(3000, RESTRICT_AT);
865 f->insertWayptAtIndex(ha, 1);
866
867 RoutePath rp1(f);
868 // distance will be invalid, but shouldn;t assert or crash :)
869 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, f->totalDistanceNm(), 0.001);
870 }
871
872 static double magVarFor(const SGGeod& geod)
873 {
874 double jd = globals->get_time_params()->getJD();
875 return sgGetMagVar(geod, jd) * SG_RADIANS_TO_DEGREES;
876 }
877
878 void FlightplanTests::testRadialIntercept()
879 {
880 // replicate AJO1R departure
881 FlightPlanRef f = makeTestFP("LFKC"s, "36"s, "LIRF"s, "25"s, "BUNAX BEBEV AJO"s);
882
883 // set BUNAX as overflight
884 f->legAtIndex(1)->waypoint()->setFlag(WPT_OVERFLIGHT);
885 f->insertWayptAtIndex(new BasicWaypt(SGGeod::fromDeg(8.78333, 42.566), "KC502"s, f), 1);
886
887 SGGeod pos = SGGeod::fromDeg(8.445556,42.216944);
888 auto intc = new RadialIntercept(f, "INTC"s, pos, 230, 5);
889 f->insertWayptAtIndex(intc, 3); // between BUNAX and BEBEV
890
891 RoutePath rtepath(f);
892 CPPUNIT_ASSERT_DOUBLES_EQUAL(230 + magVarFor(pos), f->legAtIndex(3)->courseDeg(), 1.0);
893 }
894
895 void FlightplanTests::loadFGFPWithoutDepartureArrival()
896 {
897 static_factory = std::make_shared<TestFPDelegateFactory>();
898 FlightPlan::registerDelegateFactory(static_factory);
899
900 FlightPlanRef f = FlightPlan::create();
901
902 SGPath fgfpPath = simgear::Dir::current().path() / "test_fgfp_without_dep_arr.fgfp"s;
903 {
904 sg_ofstream s(fgfpPath);
905 s << R"(<?xml version="1.0" encoding="UTF-8"?>
906 <PropertyList>
907 <version type="int">2</version>
908 <departure>
909 <airport type="string">EDDM</airport>
910 <runway type="string">08R</runway>
911 </departure>
912 <destination>
913 <airport type="string">EDDF</airport>
914 </destination>
915 <route>
916 <wp n="1">
917 <type type="string">navaid</type>
918 <ident type="string">GIVMI</ident>
919 <lon type="double">11.364700</lon>
920 <lat type="double">48.701100</lat>
921 </wp>
922 <wp n="2">
923 <type type="string">navaid</type>
924 <ident type="string">ERNAS</ident>
925 <lon type="double">11.219400</lon>
926 <lat type="double">48.844700</lat>
927 </wp>
928 <wp n="3">
929 <type type="string">navaid</type>
930 <ident type="string">TALAL</ident>
931 <lon type="double">11.085300</lon>
932 <lat type="double">49.108300</lat>
933 </wp>
934 <wp n="4">
935 <type type="string">navaid</type>
936 <ident type="string">ERMEL</ident>
937 <lon type="double">11.044700</lon>
938 <lat type="double">49.187800</lat>
939 </wp>
940 <wp n="5">
941 <type type="string">navaid</type>
942 <ident type="string">PSA</ident>
943 <lon type="double">9.348300</lon>
944 <lat type="double">49.862200</lat>
945 </wp>
946 </route>
947 </PropertyList>
948 )";
949 }
950
951 auto ourDelegate = TestFPDelegateFactory::delegateForPlan(f);
952 CPPUNIT_ASSERT(!ourDelegate->didLoad);
953
954 CPPUNIT_ASSERT(f->load(fgfpPath));
955
956 CPPUNIT_ASSERT(ourDelegate->didLoad);
957 CPPUNIT_ASSERT(ourDelegate->sawArrivalChange);
958 CPPUNIT_ASSERT(ourDelegate->sawDepartureChange);
959 }
960
961 void FlightplanTests::loadFGFPWithEmbeddedProcedures()
962 {
963 static_factory = std::make_shared<TestFPDelegateFactory>();
964 FlightPlan::registerDelegateFactory(static_factory);
965
966 FlightPlanRef f = FlightPlan::create();
967
968 SGPath fgfpPath = simgear::Dir::current().path() / "test_fgfp_with_dep_arr.fgfp"s;
969 {
970 sg_ofstream s(fgfpPath);
971 s << R"(<?xml version="1.0" encoding="UTF-8"?>
972 <PropertyList>
973 <version type="int">2</version>
974 <departure>
975 <airport type="string">EDDM</airport>
976 <runway type="string">08R</runway>
977 </departure>
978 <destination>
979 <airport type="string">EDDF</airport>
980 </destination>
981 <route>
982 <wp>
983 <type type="string">runway</type>
984 <departure type="bool">true</departure>
985 <ident type="string">08R</ident>
986 <icao type="string">EDDM</icao>
987 </wp>
988 <wp n="1">
989 <type type="string">navaid</type>
990 <ident type="string">GIVMI</ident>
991 <lon type="double">11.364700</lon>
992 <lat type="double">48.701100</lat>
993 </wp>
994 <wp n="2">
995 <type type="string">navaid</type>
996 <ident type="string">ERNAS</ident>
997 <lon type="double">11.219400</lon>
998 <lat type="double">48.844700</lat>
999 </wp>
1000 <wp n="3">
1001 <type type="string">navaid</type>
1002 <ident type="string">TALAL</ident>
1003 <lon type="double">11.085300</lon>
1004 <lat type="double">49.108300</lat>
1005 </wp>
1006 <wp n="4">
1007 <type type="string">navaid</type>
1008 <ident type="string">ERMEL</ident>
1009 <lon type="double">11.044700</lon>
1010 <lat type="double">49.187800</lat>
1011 </wp>
1012 <wp n="5">
1013 <type type="string">navaid</type>
1014 <ident type="string">PSA</ident>
1015 <lon type="double">9.348300</lon>
1016 <lat type="double">49.862200</lat>
1017 </wp>
1018
1019 <wp>
1020 <type type="string">runway</type>
1021 <arrival type="bool">true</arrival>
1022 <ident type="string">36</ident>
1023 <icao type="string">EDDF</icao>
1024 </wp>
1025 </route>
1026 </PropertyList>
1027 )";
1028 }
1029
1030 auto ourDelegate = TestFPDelegateFactory::delegateForPlan(f);
1031 CPPUNIT_ASSERT(!ourDelegate->didLoad);
1032
1033 CPPUNIT_ASSERT(f->load(fgfpPath));
1034
1035 CPPUNIT_ASSERT(ourDelegate->didLoad);
1036 CPPUNIT_ASSERT(!ourDelegate->sawArrivalChange);
1037 CPPUNIT_ASSERT(!ourDelegate->sawDepartureChange);
1038 }
1039
1040 void FlightplanTests::loadFGFPWithOldProcedures()
1041 {
1042 if (!static_haveProcedures)
1043 return;
1044
1045 FlightPlanRef f = FlightPlan::create();
1046
1047 SGPath fgfpPath = simgear::Dir::current().path() / "test_fgfp_old_procedure_idents.fgfp"s;
1048 {
1049 sg_ofstream s(fgfpPath);
1050 s << R"(<?xml version="1.0" encoding="UTF-8"?>
1051 <PropertyList>
1052 <version type="int">2</version>
1053 <departure>
1054 <airport type="string">KJFK</airport>
1055 <runway type="string">13L</runway>
1056 <sid type="string">CANDR</sid>
1057 </departure>
1058 <destination>
1059 <airport type="string">EHAM</airport>
1060 <star type="string">BEDUM</star>
1061 </destination>
1062 <route>
1063 <wp>
1064 <type type="string">runway</type>
1065 <arrival type="bool">true</arrival>
1066 <ident type="string">36</ident>
1067 <icao type="string">EDDF</icao>
1068 </wp>
1069 </route>
1070 </PropertyList>
1071 )";
1072 }
1073
1074 auto kjfk = FGAirport::findByIdent("KJFK"s);
1075 auto eham = FGAirport::findByIdent("EHAM"s);
1076 CPPUNIT_ASSERT(f->load(fgfpPath));
1077
1078 CPPUNIT_ASSERT(f->sid() == nullptr);
1079 CPPUNIT_ASSERT(f->star() == nullptr);
1080 }
1081
1082 void FlightplanTests::loadFGFPWithProcedureIdents()
1083 {
1084 if (!static_haveProcedures)
1085 return;
1086
1087 FlightPlanRef f = FlightPlan::create();
1088
1089 SGPath fgfpPath = simgear::Dir::current().path() / "test_fgfp_procedure_idents.fgfp"s;
1090 {
1091 sg_ofstream s(fgfpPath);
1092 s << R"(<?xml version="1.0" encoding="UTF-8"?>
1093 <PropertyList>
1094 <version type="int">2</version>
1095 <departure>
1096 <airport type="string">KJFK</airport>
1097 <runway type="string">13L</runway>
1098 <sid type="string">DEEZZ5.13L</sid>
1099 <sid_trans type="string">CANDR</sid_trans>
1100 </departure>
1101 <destination>
1102 <airport type="string">EHAM</airport>
1103 <star type="string">EEL1A</star>
1104 <star_trans type="string">KUBAT</star_trans>
1105 </destination>
1106 <route>
1107 <wp>
1108 <type type="string">runway</type>
1109 <arrival type="bool">true</arrival>
1110 <ident type="string">36</ident>
1111 <icao type="string">EDDF</icao>
1112 </wp>
1113 </route>
1114 </PropertyList>
1115 )";
1116 }
1117
1118 auto kjfk = FGAirport::findByIdent("KJFK"s);
1119 auto eham = FGAirport::findByIdent("EHAM"s);
1120 CPPUNIT_ASSERT(f->load(fgfpPath));
1121
1122 CPPUNIT_ASSERT(f->sid());
1123 CPPUNIT_ASSERT(f->star());
1124
1125 CPPUNIT_ASSERT_EQUAL(f->sid()->ident(), "DEEZZ5.13L"s);
1126 CPPUNIT_ASSERT_EQUAL(f->sidTransition()->ident(), "CANDR"s);
1127
1128 CPPUNIT_ASSERT_EQUAL(f->star()->ident(), "EEL1A"s);
1129 CPPUNIT_ASSERT_EQUAL(f->starTransition()->ident(), "KUBAT"s);
1130 }
1131
1132 void FlightplanTests::loadFGFPAsRoute()
1133 {
1134 static_factory = std::make_shared<TestFPDelegateFactory>();
1135 FlightPlan::registerDelegateFactory(static_factory);
1136
1137 FlightPlanRef f = FlightPlan::createRoute();
1138
1139 SGPath fgfpPath = simgear::Dir::current().path() / "test_fgfp_as_route.fgfp"s;
1140 {
1141 sg_ofstream s(fgfpPath);
1142 s << R"(<?xml version="1.0" encoding="UTF-8"?>
1143 <PropertyList>
1144 <version type="int">2</version>
1145 <is-route>true</is-route>
1146 <departure>
1147 <airport type="string">EGLL</airport>
1148 <runway type="string">27R</runway>
1149 </departure>
1150 <destination>
1151 <airport type="string">EDDS</airport>
1152 </destination>
1153 <route>
1154 <wp n="1">
1155 <type type="string">navaid</type>
1156 <ident type="string">BIG</ident>
1157 <lat>51.33087500</lat>
1158 <lon>000.03481100</lon>
1159 </wp>
1160 <wp n="2">
1161 <type type="string">navaid</type>
1162 <ident type="string">CLN</ident>
1163 <lat> 51.84841700</lat>
1164 <lon>001.14761100</lon>
1165 </wp>
1166 <wp n="3">
1167 <type type="string">via</type>
1168 <airway type="string">L620</airway>
1169 <level>1</level>
1170 <to type="string">BASNO</to>
1171 <lat>52.114586</lat>
1172 <lon>2.487947</lon>
1173 </wp>
1174 </route>
1175 </PropertyList>
1176 )";
1177 }
1178
1179 auto ourDelegate = TestFPDelegateFactory::delegateForPlan(f);
1180 CPPUNIT_ASSERT(!ourDelegate->didLoad);
1181
1182 CPPUNIT_ASSERT(f->load(fgfpPath));
1183 CPPUNIT_ASSERT(f->isRoute());
1184
1185 CPPUNIT_ASSERT(ourDelegate->didLoad);
1186 CPPUNIT_ASSERT(ourDelegate->sawArrivalChange);
1187 CPPUNIT_ASSERT(ourDelegate->sawDepartureChange);
1188
1189 CPPUNIT_ASSERT_EQUAL(5, f->numLegs());
1190 CPPUNIT_ASSERT_EQUAL("via"s, f->legAtIndex(3)->waypoint()->type());
1191
1192 auto actFP = f->clone("ACT", true);
1193 CPPUNIT_ASSERT(!actFP->isRoute());
1194 CPPUNIT_ASSERT(!actFP->isActive());
1195 // check Via was expanded
1196 CPPUNIT_ASSERT_EQUAL(8, actFP->numLegs());
1197 }
1198
1199
1200 void FlightplanTests::testCloningBasic()
1201 {
1202 FlightPlanRef fp1 = makeTestFP("EGCC"s, "23L"s, "EHAM"s, "24"s,
1203 "TNT CLN"s);
1204 fp1->setIdent("testplan"s);
1205
1206 fp1->setCruiseAltitudeFt(24000);
1207 fp1->setCruiseSpeedKnots(448);
1208
1209 auto fp2 = fp1->clone("testplan2"s);
1210
1211 CPPUNIT_ASSERT(fp2->ident() == "testplan2"s);
1212 CPPUNIT_ASSERT(fp2->departureAirport()->ident() == "EGCC"s);
1213 CPPUNIT_ASSERT(fp2->departureRunway()->ident() == "23L"s);
1214 CPPUNIT_ASSERT(fp2->destinationAirport()->ident() == "EHAM"s);
1215 CPPUNIT_ASSERT(fp2->destinationRunway()->ident() == "24"s);
1216 CPPUNIT_ASSERT_EQUAL(24000, fp2->cruiseAltitudeFt());
1217 CPPUNIT_ASSERT_EQUAL(448, fp2->cruiseSpeedKnots());
1218
1219 CPPUNIT_ASSERT_EQUAL(5, fp2->numLegs());
1220
1221 CPPUNIT_ASSERT(fp2->legAtIndex(0)->waypoint()->source()->ident() == "23L"s);
1222 CPPUNIT_ASSERT(fp2->legAtIndex(1)->waypoint()->source()->ident() == "TNT"s);
1223 CPPUNIT_ASSERT(fp2->legAtIndex(2)->waypoint()->source()->ident() == "CLN"s);
1224
1225 }
1226
1227 void FlightplanTests::testCloningFGFP()
1228 {
1229 static_factory = std::make_shared<TestFPDelegateFactory>();
1230 FlightPlan::registerDelegateFactory(static_factory);
1231
1232 FlightPlanRef fp1 = FlightPlan::create();
1233
1234 SGPath fgfpPath = simgear::Dir::current().path() / "test_fgfp_cloning.fgfp"s;
1235 {
1236 sg_ofstream s(fgfpPath);
1237 s << R"(<?xml version="1.0" encoding="UTF-8"?>
1238 <PropertyList>
1239 <version type="int">2</version>
1240 <departure>
1241 <airport type="string">EDDM</airport>
1242 <runway type="string">08R</runway>
1243 </departure>
1244 <destination>
1245 <airport type="string">EDDF</airport>
1246 </destination>
1247 <route>
1248 <wp n="1">
1249 <type type="string">navaid</type>
1250 <ident type="string">GIVMI</ident>
1251 <lon type="double">11.364700</lon>
1252 <lat type="double">48.701100</lat>
1253 </wp>
1254 <wp n="2">
1255 <type type="string">navaid</type>
1256 <ident type="string">ERNAS</ident>
1257 <lon type="double">11.219400</lon>
1258 <lat type="double">48.844700</lat>
1259 </wp>
1260 <wp n="3">
1261 <type type="string">navaid</type>
1262 <ident type="string">TALAL</ident>
1263 <lon type="double">11.085300</lon>
1264 <lat type="double">49.108300</lat>
1265 </wp>
1266 <wp n="4">
1267 <type type="string">navaid</type>
1268 <ident type="string">ERMEL</ident>
1269 <lon type="double">11.044700</lon>
1270 <lat type="double">49.187800</lat>
1271 </wp>
1272 <wp n="5">
1273 <type type="string">navaid</type>
1274 <ident type="string">PSA</ident>
1275 <lon type="double">9.348300</lon>
1276 <lat type="double">49.862200</lat>
1277 </wp>
1278 </route>
1279 </PropertyList>
1280 )";
1281 }
1282
1283 auto ourDelegate = TestFPDelegateFactory::delegateForPlan(fp1);
1284 CPPUNIT_ASSERT(!ourDelegate->didLoad);
1285
1286 CPPUNIT_ASSERT(fp1->load(fgfpPath));
1287
1288 CPPUNIT_ASSERT(ourDelegate->didLoad);
1289 CPPUNIT_ASSERT(ourDelegate->sawArrivalChange);
1290 CPPUNIT_ASSERT(ourDelegate->sawDepartureChange);
1291
1292 auto fp2 = fp1->clone();
1293 auto secondDelegate = TestFPDelegateFactory::delegateForPlan(fp2);
1294
1295 CPPUNIT_ASSERT(secondDelegate->didLoad);
1296 CPPUNIT_ASSERT(secondDelegate->sawWaypointsChange);
1297 CPPUNIT_ASSERT(!secondDelegate->sawArrivalChange);
1298 CPPUNIT_ASSERT(!secondDelegate->sawDepartureChange);
1299
1300 CPPUNIT_ASSERT(fp2->departureAirport()->ident() == "EDDM"s);
1301 CPPUNIT_ASSERT(fp2->departureRunway()->ident() == "08R"s);
1302 CPPUNIT_ASSERT(fp2->destinationAirport()->ident() == "EDDF"s);
1303
1304 CPPUNIT_ASSERT_EQUAL(fp2->legAtIndex(0)->waypoint()->source()->ident(), "08R"s);
1305 CPPUNIT_ASSERT_EQUAL(fp2->legAtIndex(1)->waypoint()->source()->ident(), "GIVMI"s);
1306 CPPUNIT_ASSERT_EQUAL(fp2->legAtIndex(5)->waypoint()->source()->ident(), "PSA"s);
1307 CPPUNIT_ASSERT_EQUAL(fp2->legAtIndex(6)->waypoint()->source()->ident(), "EDDF"s);
1308 CPPUNIT_ASSERT_EQUAL(7, fp2->numLegs());
1309
1310 }
1311
1312 void FlightplanTests::testCloningProcedures() {
1313 // procedures not loaded, abandon test
1314 if (!static_haveProcedures)
1315 return;
1316
1317 static_factory = std::make_shared<TestFPDelegateFactory>();
1318 FlightPlan::registerDelegateFactory(static_factory);
1319
1320 auto egkk = FGAirport::findByIdent("EGKK"s);
1321 auto sid = egkk->findSIDWithIdent("SAM3P"s);
1322
1323 FlightPlanRef fp1 = makeTestFP("EGKK"s, "08R"s, "EHAM"s, "18R"s,
1324 ""s);
1325 //auto ourDelegate = TestFPDelegateFactory::delegateForPlan(fp1);
1326
1327 fp1->setSID(sid);
1328 auto eham = FGAirport::findByIdent("EHAM"s);
1329 auto eel1A = eham->findSTARWithIdent("EEL1A"s);
1330 CPPUNIT_ASSERT(eel1A);
1331
1332 fp1->setSTAR(eel1A, "BEDUM"s);
1333
1334 auto fp2 = fp1->clone();
1335 CPPUNIT_ASSERT(fp2->departureAirport()->ident() == "EGKK"s);
1336 CPPUNIT_ASSERT(fp2->departureRunway()->ident() == "08R"s);
1337 CPPUNIT_ASSERT(fp2->destinationAirport()->ident() == "EHAM"s);
1338 CPPUNIT_ASSERT(fp2->destinationRunway()->ident() == "18R"s);
1339
1340 CPPUNIT_ASSERT(fp2->legAtIndex(0)->waypoint()->source()->ident() == "08R"s);
1341 CPPUNIT_ASSERT_EQUAL(fp2->sid()->ident(), "SAM3P"s);
1342
1343 CPPUNIT_ASSERT_EQUAL(fp2->star()->ident(), "EEL1A"s);
1344 CPPUNIT_ASSERT_EQUAL(fp2->starTransition()->ident(), "BEDUM"s);
1345 }
1346
1347
1348 void FlightplanTests::testBug2616()
1349 {
1350 auto edty = FGAirport::findByIdent("EDTY"s);
1351 edty->testSuiteInjectProceduresXML(SGPath::fromUtf8(FG_TEST_SUITE_DATA) / "EDTY.procedures.xml");
1352
1353 auto ils28Approach = edty->findApproachWithIdent("ILS28"s);
1354 CPPUNIT_ASSERT(ils28Approach);
1355
1356 FlightPlanRef fp1 = makeTestFP("EDDS"s, "25"s, "EDTY"s, "28"s,
1357 ""s);
1358
1359
1360
1361 fp1->setApproach(ils28Approach);
1362
1363 CPPUNIT_ASSERT(fp1->destinationRunway()->ident() == "28"s);
1364 CPPUNIT_ASSERT(fp1->approach()->ident() == "ILS28"s);
1365
1366 fp1->activate();
1367 CPPUNIT_ASSERT(fp1->isActive());
1368 }
1369
1370 void FlightplanTests::testRoute()
1371 {
1372 FlightPlanRef fp1 = makeTestFP("EGKK"s, "08R"s, "EHAM"s, "18R"s,
1373 "CLN VIA-L620/REDFA SPY"s, true);
1374
1375 CPPUNIT_ASSERT(fp1->isRoute());
1376 CPPUNIT_ASSERT_EQUAL("via"s, fp1->legAtIndex(2)->waypoint()->type());
1377
1378 // activating a route should fail
1379 fp1->activate();
1380 CPPUNIT_ASSERT(!fp1->isActive());
1381
1382 // setting index should still work
1383 fp1->setCurrentIndex(2);
1384 CPPUNIT_ASSERT_EQUAL(2, fp1->currentIndex());
1385 CPPUNIT_ASSERT(!fp1->isActive());
1386
1387 }