Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / test / src / harness_task.cpp
blobfabfe94d9af469f86ad50a8cfb2655b8a379f04b
1 /* Copyright_License {
3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "harness_task.hpp"
24 #include "test_debug.hpp"
26 #include "Task/Factory/AbstractTaskFactory.hpp"
27 #include "Task/Factory/TaskFactoryConstraints.hpp"
28 #include "Engine/Task/Ordered/OrderedTask.hpp"
29 #include "Engine/Task/Ordered/Points/StartPoint.hpp"
30 #include "Engine/Task/Ordered/Points/FinishPoint.hpp"
31 #include "Engine/Task/Ordered/Points/ASTPoint.hpp"
32 #include "Engine/Task/Ordered/Points/AATPoint.hpp"
33 #include "Task/ObservationZones/CylinderZone.hpp"
34 #include "Task/ObservationZones/SymmetricSectorZone.hpp"
35 #include "Task/Visitors/TaskPointVisitor.hpp"
36 #include "Engine/Waypoint/Waypoints.hpp"
37 #include "Util/StaticArray.hpp"
39 #include "harness_waypoints.hpp"
40 #include <string.h>
42 class SectorZone;
43 class LineSectorZone;
44 class KeyholeZone;
45 class AnnularSectorZone;
47 class ObservationZoneVisitorPrint
49 public:
50 void Visit(const KeyholeZone& oz) {
51 printf("# kehole zone\n");
53 void Visit(const SectorZone& oz) {
54 printf("# sector zone\n");
56 void Visit(const AnnularSectorZone& oz) {
57 printf("# annular sector zone\n");
59 void Visit(const LineSectorZone& oz) {
60 printf("# line zone\n");
62 void Visit(const CylinderZone& oz) {
63 printf("# cylinder zone\n");
66 void Visit(const SymmetricSectorZone &oz) {
67 printf("# symmetric quadrant\n");
70 void Visit(const ObservationZonePoint &oz) {
71 switch (oz.GetShape()) {
72 case ObservationZone::Shape::FAI_SECTOR:
73 case ObservationZone::Shape::SECTOR:
74 Visit((const SectorZone &)oz);
75 break;
77 case ObservationZone::Shape::LINE:
78 Visit((const LineSectorZone &)oz);
79 break;
81 case ObservationZone::Shape::MAT_CYLINDER:
82 case ObservationZone::Shape::CYLINDER:
83 Visit((const CylinderZone &)oz);
84 break;
86 case ObservationZone::Shape::CUSTOM_KEYHOLE:
87 case ObservationZone::Shape::DAEC_KEYHOLE:
88 case ObservationZone::Shape::BGAFIXEDCOURSE:
89 case ObservationZone::Shape::BGAENHANCEDOPTION:
90 case ObservationZone::Shape::BGA_START:
91 Visit((const KeyholeZone &)oz);
92 break;
94 case ObservationZone::Shape::ANNULAR_SECTOR:
95 Visit((const AnnularSectorZone &)oz);
96 break;
98 case ObservationZone::Shape::SYMMETRIC_QUADRANT:
99 Visit((const SymmetricSectorZone &)oz);
100 break;
105 class TaskPointVisitorPrint: public TaskPointConstVisitor
107 public:
108 virtual void Visit(const TaskPoint& tp) override {
109 switch (tp.GetType()) {
110 case TaskPointType::UNORDERED:
111 printf("# got a tp\n");
112 break;
114 case TaskPointType::FINISH:
115 printf("# got an ftp\n");
116 ozv.Visit(((const FinishPoint &)tp).GetObservationZone());
117 break;
119 case TaskPointType::START:
120 printf("# got an stp\n");
121 ozv.Visit(((const StartPoint &)tp).GetObservationZone());
122 break;
124 case TaskPointType::AAT:
125 printf("# got an aat\n");
126 ozv.Visit(((const AATPoint &)tp).GetObservationZone());
127 break;
129 case TaskPointType::AST:
130 printf("# got an ast\n");
131 ozv.Visit(((const ASTPoint &)tp).GetObservationZone());
132 break;
136 private:
137 ObservationZoneVisitorPrint ozv;
140 void test_note(const char* text)
142 if (verbose) {
143 printf("%s",text);
147 void task_report(TaskManager& task_manager, const char* text)
149 AircraftState ac;
150 if (verbose) {
151 printf("%s",text);
153 const AbstractTask *task = task_manager.GetActiveTask();
154 if (task != NULL) {
155 switch (task->GetType()) {
156 case TaskType::NONE:
157 printf("# task is none\n");
158 break;
160 case TaskType::ORDERED:
161 printf("# task is ordered\n");
162 break;
164 case TaskType::ABORT:
165 printf("# task is abort\n");
166 break;
168 case TaskType::GOTO:
169 printf("# task is goto\n");
170 break;
173 TaskPointVisitorPrint tpv;
174 task->AcceptTaskPointVisitor(tpv);
175 printf("# - dist nominal %g\n",
176 (double)task->GetStats().distance_nominal);
178 if (task->GetType() == TaskType::ORDERED &&
179 task->GetStats().distance_max > task->GetStats().distance_min) {
180 printf("# - dist max %g\n", (double)task->GetStats().distance_max);
181 printf("# - dist min %g\n", (double)task->GetStats().distance_min);
185 PrintHelper::taskmanager_print(task_manager, ac);
187 if (interactive>1) {
188 WaitPrompt();
193 bool test_task_manip(TaskManager& task_manager,
194 const Waypoints &waypoints)
196 if (!test_task_mixed(task_manager, waypoints)) {
197 return false;
199 AbstractTaskFactory &fact = task_manager.GetFactory();
201 task_report(task_manager, "# removing tp 2\n");
202 if (!fact.Remove(2)) {
203 return false;
206 task_report(task_manager, "# removing tp 0\n");
207 if (!fact.Remove(0)) {
208 return false;
211 task_report(task_manager, "# removing tp -1 (illegal)\n");
212 if (fact.Remove(0-1)) {
213 return false;
216 task_report(task_manager, "# removing tp 50 (illegal)\n");
217 if (fact.Remove(50)) {
218 return false;
221 OrderedTaskPoint *tp;
222 const Waypoint *wp;
224 task_report(task_manager, "# inserting at 3\n");
225 wp = waypoints.LookupId(3);
226 if (wp) {
227 tp = fact.CreateIntermediate(TaskPointFactoryType::AST_CYLINDER,*wp);
228 if (!fact.Insert(*tp,3)) return false;
229 delete tp;
232 task_report(task_manager, "# auto-replacing at 2 (no morph)\n");
233 wp = waypoints.LookupId(9);
234 if (wp) {
235 tp = fact.CreateIntermediate(TaskPointFactoryType::AST_CYLINDER,*wp);
236 if (!fact.Replace(*tp,2)) return false;
237 delete tp;
240 task_report(task_manager, "# auto-replacing at 2 (morph)\n");
241 wp = waypoints.LookupId(9);
242 if (wp) {
243 tp = fact.CreateStart(*wp);
244 if (!fact.Replace(*tp,2)) return false;
245 delete tp;
248 task_report(task_manager, "# auto-replacing at 0 (morph this)\n");
249 wp = waypoints.LookupId(12);
250 if (wp) {
251 tp = fact.CreateIntermediate(TaskPointFactoryType::AST_CYLINDER,*wp);
252 if (!fact.Replace(*tp,0)) return false;
253 delete tp;
256 task_report(task_manager, "# auto-replacing at end (morph this)\n");
257 wp = waypoints.LookupId(14);
258 if (wp) {
259 tp = fact.CreateIntermediate(TaskPointFactoryType::AST_CYLINDER,*wp);
260 if (!fact.Replace(*tp,task_manager.TaskSize()-1)) return false;
261 delete tp;
264 task_report(task_manager, "# removing finish point\n");
265 if (!fact.Remove(task_manager.TaskSize()-1)) {
266 return false;
269 task_report(task_manager, "# inserting at 50 (equivalent to append)\n");
270 wp = waypoints.LookupId(8);
271 if (wp) {
272 tp = fact.CreateFinish(*wp);
273 if (!fact.Insert(*tp,50)) return false;
274 delete tp;
277 task_report(task_manager, "# inserting at 0 (morph this)\n");
278 wp = waypoints.LookupId(3);
279 if (wp) {
280 tp = fact.CreateFinish(*wp);
281 if (!fact.Insert(*tp,0)) return false;
282 delete tp;
285 task_report(task_manager, "# inserting at 2 (morph this)\n");
286 wp = waypoints.LookupId(4);
287 if (wp) {
288 tp = fact.CreateStart(*wp);
289 if (!fact.Insert(*tp,2)) return false;
290 delete tp;
293 task_report(task_manager, "# inserting at 2 (direct)\n");
294 wp = waypoints.LookupId(6);
295 if (wp) {
296 tp = fact.CreateIntermediate(*wp);
297 if (!fact.Insert(*tp,2,false)) return false;
298 delete tp;
301 task_report(task_manager, "# checking task\n");
303 if (task_manager.CheckOrderedTask()) {
304 task_manager.Reset();
305 task_manager.SetActiveTaskPoint(0);
306 task_manager.Resume();
307 } else {
308 return false;
310 return true;
313 bool test_task_type_manip(TaskManager& task_manager,
314 const Waypoints &waypoints, unsigned n_points)
316 if (!test_task_random_RT_AAT_FAI(task_manager, waypoints, n_points))
317 return false;
319 switch (rand() %4) {
320 case 0:
321 task_manager.SetFactory(TaskFactoryType::AAT);
322 test_note("# switched FACTORY TYPE to AAT\n");
323 break;
324 case 1:
325 task_manager.SetFactory(TaskFactoryType::RACING);
326 test_note("# switched FACTORY TYPE to RT\n");
327 break;
328 case 2:
329 task_manager.SetFactory(TaskFactoryType::FAI_GENERAL);
330 test_note("# switched FACTORY TYPE to FAI GENERAL\n");
331 break;
332 case 3:
333 task_manager.SetFactory(TaskFactoryType::MAT);
334 test_note("# switched FACTORY TYPE to MAT\n");
335 break;
336 default:
337 test_note("# unknown task type\n");
340 AbstractTaskFactory &fact = task_manager.GetFactory();
341 fact.MutateTPsToTaskType();
343 test_note("# checking mutated start..\n");
344 if (!fact.IsValidStartType(fact.GetType(task_manager.GetOrderedTask().GetTaskPoint(0))))
345 return false;
348 char tmp[255];
349 sprintf(tmp, "# checking mutated intermediates. task_size():%d..\n",
350 task_manager.TaskSize());
351 test_note(tmp);
353 for (unsigned i = 1; i < (task_manager.TaskSize() - 1); i++) {
354 sprintf(tmp, "# checking mutated intermediate point %d..\n", i);
355 test_note(tmp);
356 if (!fact.IsValidIntermediateType(fact.GetType(task_manager.GetOrderedTask().GetTaskPoint(i))))
357 return false;
360 test_note("# checking mutated finish..\n");
361 if (!fact.IsValidFinishType(
362 fact.GetType(task_manager.GetOrderedTask().GetTaskPoint(task_manager.TaskSize() - 1))))
363 return false;
365 test_note("# validating task..\n");
366 if (!fact.Validate()) {
367 return false;
369 test_note("# checking task..\n");
370 if (!task_manager.CheckOrderedTask()) {
371 return false;
374 if (task_manager.GetOrderedTask().GetFactoryType() ==
375 TaskFactoryType::FAI_GENERAL) {
376 test_note("# checking OZs for FAI task..\n");
377 if (!fact.ValidateFAIOZs())
378 return false;
381 if (task_manager.GetOrderedTask().GetFactoryType() ==
382 TaskFactoryType::MAT) {
383 test_note("# checking OZs for MAT task..\n");
384 if (!fact.ValidateMATOZs())
385 return false;
387 return true;
390 bool test_task_mixed(TaskManager& task_manager,
391 const Waypoints &waypoints)
393 const TaskProjection &projection =
394 task_manager.GetOrderedTask().GetTaskProjection();
396 OrderedTaskPoint *tp;
397 const Waypoint *wp;
399 task_manager.SetFactory(TaskFactoryType::MIXED);
400 AbstractTaskFactory &fact = task_manager.GetFactory();
402 task_report(task_manager, "# adding start\n");
403 wp = waypoints.LookupId(1);
404 if (wp) {
405 tp = fact.CreateStart(TaskPointFactoryType::START_LINE,*wp);
406 if (tp->GetObservationZone().GetShape() == ObservationZone::Shape::CYLINDER) {
407 CylinderZone &cz = (CylinderZone &)tp->GetObservationZone();
408 cz.SetRadius(fixed(5000.0));
409 tp->UpdateOZ(projection);
411 if (!fact.Append(*tp,false)) return false;
412 delete tp;
413 } else {
414 return false;
417 task_manager.SetActiveTaskPoint(0);
418 task_manager.Resume();
420 task_report(task_manager, "# adding intermdiate\n");
421 wp = waypoints.LookupId(2);
422 if (wp) {
423 tp = fact.CreateIntermediate(TaskPointFactoryType::AST_CYLINDER,*wp);
424 if (!fact.Append(*tp,false)) return false;
425 delete tp;
426 } else {
427 return false;
430 task_report(task_manager, "# adding intermdiate\n");
431 wp = waypoints.LookupId(3);
432 if (wp) {
433 tp = fact.CreateIntermediate(TaskPointFactoryType::AAT_CYLINDER,*wp);
434 if (tp->GetObservationZone().GetShape() == ObservationZone::Shape::CYLINDER) {
435 CylinderZone &cz = (CylinderZone &)tp->GetObservationZone();
436 cz.SetRadius(fixed(30000.0));
437 tp->UpdateOZ(projection);
439 if (!fact.Append(*tp,false)) return false;
440 delete tp;
441 } else {
442 return false;
445 task_report(task_manager, "# adding intermediate\n");
446 wp = waypoints.LookupId(4);
447 if (wp) {
448 tp = fact.CreateIntermediate(TaskPointFactoryType::AAT_CYLINDER,*wp);
449 if (!fact.Append(*tp,false)) return false;
450 delete tp;
451 } else {
452 return false;
455 task_report(task_manager, "# adding intermediate\n");
456 wp = waypoints.LookupId(5);
457 if (wp) {
458 tp = fact.CreateIntermediate(TaskPointFactoryType::AAT_CYLINDER,*wp);
459 if (tp->GetObservationZone().GetShape() == ObservationZone::Shape::CYLINDER) {
460 CylinderZone &cz = (CylinderZone &)tp->GetObservationZone();
461 cz.SetRadius(fixed(30000.0));
462 tp->UpdateOZ(projection);
464 if (!fact.Append(*tp,false)) return false;
465 delete tp;
466 } else {
467 return false;
470 task_report(task_manager, "# adding finish\n");
471 wp = waypoints.LookupId(1);
472 if (wp) {
473 tp = fact.CreateFinish(TaskPointFactoryType::FINISH_LINE,*wp);
474 if (!fact.Append(*tp,false)) return false;
475 delete tp;
476 } else {
477 return false;
480 task_report(task_manager, "# checking task\n");
481 if (!fact.Validate()) {
482 return false;
485 if (!task_manager.CheckOrderedTask()) {
486 return false;
488 return true;
492 bool test_task_fai(TaskManager& task_manager,
493 const Waypoints &waypoints)
495 task_manager.SetFactory(TaskFactoryType::FAI_GENERAL);
496 AbstractTaskFactory &fact = task_manager.GetFactory();
497 const Waypoint *wp;
499 task_report(task_manager, "# adding start\n");
500 wp = waypoints.LookupId(1);
501 if (wp) {
502 OrderedTaskPoint *tp = fact.CreateStart(*wp);
503 if (!fact.Append(*tp)) {
504 return false;
506 delete tp;
509 task_manager.SetActiveTaskPoint(0);
510 task_manager.Resume();
512 task_report(task_manager, "# adding intermdiate\n");
513 wp = waypoints.LookupId(2);
514 if (wp) {
515 OrderedTaskPoint *tp = fact.CreateIntermediate(*wp);
516 if (!fact.Append(*tp, false)) {
517 return false;
519 delete tp;
522 task_report(task_manager, "# adding intermdiate\n");
523 wp = waypoints.LookupId(3);
524 if (wp) {
525 OrderedTaskPoint *tp = fact.CreateIntermediate(*wp);
526 if (!fact.Append(*tp,false)) {
527 return false;
529 delete tp;
532 task_report(task_manager, "# adding finish\n");
533 wp = waypoints.LookupId(1);
534 if (wp) {
535 OrderedTaskPoint *tp = fact.CreateFinish(*wp);
536 if (!fact.Append(*tp,false)) {
537 return false;
539 delete tp;
542 task_report(task_manager, "# checking task\n");
543 if (!fact.Validate()) {
544 return false;
547 if (!task_manager.CheckOrderedTask()) {
548 return false;
550 return true;
554 bool test_task_aat(TaskManager& task_manager,
555 const Waypoints &waypoints)
557 const TaskProjection &projection =
558 task_manager.GetOrderedTask().GetTaskProjection();
560 task_manager.SetFactory(TaskFactoryType::AAT);
561 AbstractTaskFactory &fact = task_manager.GetFactory();
562 const Waypoint *wp;
564 task_report(task_manager, "# adding start\n");
565 wp = waypoints.LookupId(1);
566 if (wp) {
567 OrderedTaskPoint *tp = fact.CreateStart(*wp);
568 if (!fact.Append(*tp,false)) {
569 return false;
571 delete tp;
574 task_manager.SetActiveTaskPoint(0);
575 task_manager.Resume();
577 task_report(task_manager, "# adding intermediate\n");
578 wp = waypoints.LookupId(2);
579 if (wp) {
580 OrderedTaskPoint* tp = fact.CreateIntermediate(TaskPointFactoryType::AAT_CYLINDER,*wp);
581 if (tp->GetObservationZone().GetShape() == ObservationZone::Shape::CYLINDER) {
582 CylinderZone &cz = (CylinderZone &)tp->GetObservationZone();
583 cz.SetRadius(fixed(30000.0));
584 tp->UpdateOZ(projection);
586 if (!fact.Append(*tp,false)) {
587 return false;
589 delete tp;
592 task_report(task_manager, "# adding intermediate\n");
593 wp = waypoints.LookupId(3);
594 if (wp) {
595 OrderedTaskPoint* tp = fact.CreateIntermediate(TaskPointFactoryType::AAT_CYLINDER,*wp);
596 if (tp->GetObservationZone().GetShape() == ObservationZone::Shape::CYLINDER) {
597 CylinderZone &cz = (CylinderZone &)tp->GetObservationZone();
598 cz.SetRadius(fixed(40000.0));
599 tp->UpdateOZ(projection);
601 if (!fact.Append(*tp,false)) {
602 return false;
604 delete tp;
607 task_report(task_manager, "# adding finish\n");
608 wp = waypoints.LookupId(1);
609 if (wp) {
610 OrderedTaskPoint *tp = fact.CreateFinish(*wp);
611 if (!fact.Append(*tp,false)) {
612 return false;
614 delete tp;
617 task_report(task_manager, "# checking task..\n");
618 if (!fact.Validate()) {
619 return false;
622 if (!task_manager.CheckOrderedTask()) {
623 return false;
625 return true;
628 static bool
629 test_task_mat(TaskManager &task_manager, const Waypoints &waypoints)
631 const TaskProjection &projection =
632 task_manager.GetOrderedTask().GetTaskProjection();
634 task_manager.SetFactory(TaskFactoryType::MAT);
635 AbstractTaskFactory &fact = task_manager.GetFactory();
636 const Waypoint *wp;
638 task_report(task_manager, "# adding start\n");
639 wp = waypoints.LookupId(1);
640 if (wp) {
641 OrderedTaskPoint *tp = fact.CreateStart(*wp);
642 if (!fact.Append(*tp,false)) {
643 return false;
645 delete tp;
648 task_manager.SetActiveTaskPoint(0);
649 task_manager.Resume();
651 task_report(task_manager, "# adding intermediate\n");
652 wp = waypoints.LookupId(2);
653 if (wp) {
654 OrderedTaskPoint* tp = fact.CreateIntermediate(TaskPointFactoryType::MAT_CYLINDER,*wp);
655 if (tp->GetObservationZone().GetShape() == ObservationZone::Shape::MAT_CYLINDER) {
656 tp->UpdateOZ(projection);
658 if (!fact.Append(*tp,false)) {
659 return false;
661 delete tp;
664 task_report(task_manager, "# adding intermediate\n");
665 wp = waypoints.LookupId(3);
666 if (wp) {
667 OrderedTaskPoint* tp = fact.CreateIntermediate(TaskPointFactoryType::MAT_CYLINDER,*wp);
668 if (tp->GetObservationZone().GetShape() == ObservationZone::Shape::MAT_CYLINDER) {
669 tp->UpdateOZ(projection);
671 if (!fact.Append(*tp,false)) {
672 return false;
674 delete tp;
677 task_report(task_manager, "# adding finish\n");
678 wp = waypoints.LookupId(1);
679 if (wp) {
680 OrderedTaskPoint *tp = fact.CreateFinish(*wp);
681 if (!fact.Append(*tp,false)) {
682 return false;
684 delete tp;
687 task_report(task_manager, "# checking task..\n");
688 if (!fact.Validate()) {
689 return false;
692 if (!task_manager.CheckOrderedTask()) {
693 return false;
695 return true;
699 bool test_task_or(TaskManager& task_manager,
700 const Waypoints &waypoints)
702 const Waypoint *wp;
704 task_manager.SetFactory(TaskFactoryType::FAI_OR);
705 AbstractTaskFactory &fact = task_manager.GetFactory();
707 task_report(task_manager, "# adding start\n");
708 wp = waypoints.LookupId(1);
709 if (wp) {
710 OrderedTaskPoint *tp = fact.CreateStart(*wp);
711 if (!fact.Append(*tp)) {
712 return false;
714 delete tp;
717 task_manager.SetActiveTaskPoint(0);
718 task_manager.Resume();
720 task_report(task_manager, "# adding intermediate\n");
721 wp = waypoints.LookupId(2);
722 if (wp) {
723 OrderedTaskPoint *tp = fact.CreateIntermediate(*wp);
724 if (!fact.Append(*tp)) {
725 return false;
727 delete tp;
730 task_report(task_manager, "# adding finish\n");
731 wp = waypoints.LookupId(1);
732 if (wp) {
733 OrderedTaskPoint *tp = fact.CreateFinish(*wp);
734 if (!fact.Append(*tp)) {
735 return false;
737 delete tp;
740 task_report(task_manager, "# checking task..\n");
741 if (!fact.Validate()) {
742 return false;
745 if (!task_manager.CheckOrderedTask()) {
746 return false;
748 return true;
753 bool test_task_dash(TaskManager& task_manager,
754 const Waypoints &waypoints)
756 const Waypoint *wp;
758 task_manager.SetFactory(TaskFactoryType::TOURING);
759 AbstractTaskFactory &fact = task_manager.GetFactory();
761 task_report(task_manager, "# adding start\n");
762 wp = waypoints.LookupId(1);
763 if (wp) {
764 OrderedTaskPoint *tp = fact.CreateStart(*wp);
765 if (!fact.Append(*tp)) {
766 return false;
768 delete tp;
771 task_manager.SetActiveTaskPoint(0);
772 task_manager.Resume();
774 task_report(task_manager, "# adding finish\n");
775 wp = waypoints.LookupId(3);
776 if (wp) {
777 OrderedTaskPoint *tp = fact.CreateFinish(*wp);
778 if (!fact.Append(*tp)) {
779 return false;
781 delete tp;
784 task_report(task_manager, "# checking task..\n");
785 if (!fact.Validate()) {
786 return false;
789 if (!task_manager.CheckOrderedTask()) {
790 return false;
792 return true;
797 bool test_task_fg(TaskManager& task_manager,
798 const Waypoints &waypoints)
800 const Waypoint *wp;
802 task_manager.SetFactory(TaskFactoryType::FAI_GOAL);
803 AbstractTaskFactory &fact = task_manager.GetFactory();
805 task_report(task_manager, "# adding start\n");
806 wp = waypoints.LookupId(1);
807 if (wp) {
808 OrderedTaskPoint *tp = fact.CreateStart(*wp);
809 if (!fact.Append(*tp, false)) {
810 return false;
812 delete tp;
815 task_manager.SetActiveTaskPoint(0);
816 task_manager.Resume();
818 task_report(task_manager, "# adding finish\n");
819 wp = waypoints.LookupId(6);
820 if (wp) {
821 OrderedTaskPoint *tp = fact.CreateFinish(*wp);
822 if (!fact.Append(*tp, false)) {
823 return false;
825 delete tp;
828 task_report(task_manager, "# checking task..\n");
829 if (!fact.Validate()) {
830 return false;
833 if (!task_manager.CheckOrderedTask()) {
834 return false;
836 return true;
840 const Waypoint* random_waypoint(const Waypoints &waypoints) {
841 static unsigned id_last = 0;
842 unsigned id = 0;
843 do {
844 id = rand() % waypoints.size()+1;
845 } while (id==id_last);
846 id_last = id;
847 return waypoints.LookupId(id);
850 static TaskPointFactoryType
851 GetRandomType(const LegalPointSet &l)
853 StaticArray<TaskPointFactoryType, LegalPointSet::N> types;
854 l.CopyTo(std::back_inserter(types));
855 return types[(rand() % types.size())];
858 bool test_task_random(TaskManager& task_manager,
859 const Waypoints &waypoints,
860 const unsigned num_points)
862 const Waypoint *wp;
864 OrderedTaskPoint *tp;
866 task_manager.SetFactory(TaskFactoryType::MIXED);
867 AbstractTaskFactory &fact = task_manager.GetFactory();
869 task_report(task_manager, "# adding start\n");
870 wp = random_waypoint(waypoints);
871 if (wp) {
872 const TaskPointFactoryType s = GetRandomType(fact.GetStartTypes());
874 tp = fact.CreateStart(s,*wp);
875 if (!fact.Append(*tp,false)) {
876 return false;
878 delete tp;
881 task_manager.SetActiveTaskPoint(0);
882 task_manager.Resume();
884 for (unsigned i=0; i<num_points; i++) {
885 task_report(task_manager, "# adding intermediate\n");
886 wp = random_waypoint(waypoints);
887 if (wp) {
888 const TaskPointFactoryType s = GetRandomType(fact.GetIntermediateTypes());
890 tp = fact.CreateIntermediate(s,*wp);
891 if (!fact.Append(*tp,false)) {
892 return false;
894 delete tp;
898 task_report(task_manager, "# adding finish\n");
899 wp = random_waypoint(waypoints);
900 if (wp) {
901 const TaskPointFactoryType s = GetRandomType(fact.GetFinishTypes());
903 tp = fact.CreateFinish(s,*wp);
904 if (!fact.Append(*tp,false)) {
905 return false;
907 delete tp;
910 task_report(task_manager, "# validating task..\n");
911 if (!fact.Validate()) {
912 return false;
914 task_report(task_manager, "# checking task..\n");
915 if (!task_manager.CheckOrderedTask()) {
916 return false;
918 return true;
921 bool test_task_random_RT_AAT_FAI(TaskManager& task_manager,
922 const Waypoints &waypoints,
923 const unsigned _num_points)
925 const Waypoint *wp;
927 OrderedTaskPoint *tp;
928 char tmp[255];
929 char tskType[20];
930 tskType[0] = '\0';
933 switch (rand() %3) {
934 case 0:
935 task_manager.SetFactory(TaskFactoryType::AAT);
936 strcpy(tskType,"AAT");
937 test_note("# creating random AAT task\n");
938 break;
939 case 1:
940 task_manager.SetFactory(TaskFactoryType::RACING);
941 strcpy(tskType,"RT");
942 test_note("# creating random RT task\n");
943 break;
944 case 2:
945 task_manager.SetFactory(TaskFactoryType::FAI_GENERAL);
946 strcpy(tskType,"FAI");
947 test_note("# creating random FAI GENERAL\n");
948 break;
951 AbstractTaskFactory &fact = task_manager.GetFactory();
953 //max points includes start & finish
954 const TaskFactoryConstraints &constraints =
955 task_manager.GetOrderedTask().GetFactoryConstraints();
956 const unsigned num_points_total =
957 std::max(constraints.min_points,
958 _num_points % constraints.max_points) + 1;
959 const unsigned num_int_points = num_points_total - 2;
961 test_note("# adding start\n");
962 wp = random_waypoint(waypoints);
963 if (wp) {
964 const TaskPointFactoryType s = GetRandomType(fact.GetStartTypes());
966 tp = fact.CreateStart(s,*wp);
967 if (!fact.Append(*tp,false)) {
968 return false;
970 delete tp;
973 for (unsigned i=0; i<num_int_points; i++) {
974 test_note("# adding intermediate\n");
975 wp = random_waypoint(waypoints);
976 if (wp) {
977 const TaskPointFactoryType s = GetRandomType(fact.GetIntermediateTypes());
979 tp = fact.CreateIntermediate(s,*wp);
980 if (!fact.Append(*tp,false)) {
981 return false;
983 delete tp;
987 test_note("# adding finish\n");
988 wp = random_waypoint(waypoints);
989 if (wp) {
990 const TaskPointFactoryType s = GetRandomType(fact.GetFinishTypes());
992 tp = fact.CreateFinish(s,*wp);
993 if (!fact.Append(*tp,false)) {
994 return false;
996 delete tp;
999 test_note("# validating task..\n");
1000 if (!fact.Validate()) {
1001 return false;
1003 if (task_manager.GetOrderedTask().GetFactoryType()
1004 == TaskFactoryType::FAI_GENERAL)
1006 test_note("# checking OZs for FAI General..\n");
1007 if (!fact.ValidateFAIOZs())
1008 return false;
1011 if (task_manager.GetOrderedTask().GetFactoryType()
1012 == TaskFactoryType::MAT)
1014 test_note("# checking OZs for MAT General..\n");
1015 if (!fact.ValidateMATOZs())
1016 return false;
1018 task_manager.Resume();
1019 sprintf(tmp, "# SUCCESS CREATING %s task! task_size():%d..\n",
1020 tskType,
1021 task_manager.TaskSize());
1022 test_note(tmp);
1023 return true;
1026 bool test_task(TaskManager& task_manager,
1027 const Waypoints &waypoints,
1028 int test_num)
1030 unsigned n_points = rand()%8+1;
1031 switch (test_num) {
1032 case 0:
1033 return test_task_mixed(task_manager,waypoints);
1034 case 1:
1035 return test_task_fai(task_manager,waypoints);
1036 case 2:
1037 return test_task_aat(task_manager,waypoints);
1038 case 3:
1039 return test_task_or(task_manager,waypoints);
1040 case 4:
1041 return test_task_dash(task_manager,waypoints);
1042 case 5:
1043 return test_task_fg(task_manager,waypoints);
1044 case 6:
1045 return test_task_manip(task_manager,waypoints);
1046 case 7:
1047 return test_task_random(task_manager,waypoints,n_points);
1048 case 8:
1049 return test_task_mat(task_manager,waypoints);
1050 default:
1051 return false;
1056 const char* task_name(int test_num)
1058 switch (test_num) {
1059 case 0:
1060 return "mixed";
1061 case 1:
1062 return "fai";
1063 case 2:
1064 return "aat";
1065 case 3:
1066 return "or";
1067 case 4:
1068 return "dash";
1069 case 5:
1070 return "fg";
1071 case 6:
1072 return "manip";
1073 case 7:
1074 return "random";
1075 default:
1076 return "unknown";