TaskManager: remove GetStartState() and GetFinishState()
[xcsoar.git] / test / src / TestAirspaceParser.cpp
blobacfb4df87bb14b4cd55d6d0228822445dca59d81
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "Airspace/AirspaceParser.hpp"
25 #include "Engine/Airspace/AbstractAirspace.hpp"
26 #include "Engine/Airspace/AirspaceCircle.hpp"
27 #include "Engine/Airspace/AirspacePolygon.hpp"
28 #include "Engine/Airspace/Airspaces.hpp"
29 #include "Units/System.hpp"
30 #include "Util/Macros.hpp"
31 #include "IO/FileLineReader.hpp"
32 #include "Operation/Operation.hpp"
33 #include "TestUtil.hpp"
35 #include <tchar.h>
36 #include <string.h>
38 struct AirspaceClassTestCouple
40 const TCHAR* name;
41 AirspaceClass type;
44 static bool
45 ParseFile(const TCHAR *path, Airspaces &airspaces)
47 FileLineReader reader(path, ConvertLineReader::AUTO);
49 if (!ok1(!reader.error())) {
50 skip(1, 0, "Failed to read input file");
51 return false;
54 AirspaceParser parser(airspaces);
55 NullOperationEnvironment operation;
57 if (!ok1(parser.Parse(reader, operation)))
58 return false;
60 airspaces.Optimise();
61 return true;
64 static void
65 TestOpenAir()
67 Airspaces airspaces;
68 if (!ParseFile(_T("test/data/airspace/openair.txt"), airspaces)) {
69 skip(3, 0, "Failed to parse input file");
70 return;
73 const AirspaceClassTestCouple classes[] = {
74 { _T("Class-R-Test"), RESTRICT },
75 { _T("Class-Q-Test"), DANGER },
76 { _T("Class-P-Test"), PROHIBITED },
77 { _T("Class-CTR-Test"), CTR },
78 { _T("Class-A-Test"), CLASSA },
79 { _T("Class-B-Test"), CLASSB },
80 { _T("Class-C-Test"), CLASSC },
81 { _T("Class-D-Test"), CLASSD },
82 { _T("Class-GP-Test"), NOGLIDER },
83 { _T("Class-W-Test"), WAVE },
84 { _T("Class-E-Test"), CLASSE },
85 { _T("Class-F-Test"), CLASSF },
86 { _T("Class-TMZ-Test"), TMZ },
87 { _T("Class-G-Test"), CLASSG },
90 ok1(airspaces.size() == 23);
92 for (auto it = airspaces.begin(); it != airspaces.end(); ++it) {
93 const AbstractAirspace &airspace = *it->GetAirspace();
94 if (_tcscmp(_T("Circle-Test"), airspace.GetName()) == 0) {
95 if (!ok1(airspace.GetShape() == AbstractAirspace::Shape::CIRCLE))
96 continue;
98 const AirspaceCircle &circle = (const AirspaceCircle &)airspace;
99 ok1(equals(circle.GetRadius(), Units::ToSysUnit(fixed(5), Unit::NAUTICAL_MILES)));
100 ok1(equals(circle.GetCenter(),
101 Angle::Degrees(1.091667), Angle::Degrees(0.091667)));
102 } else if (_tcscmp(_T("Polygon-Test"), airspace.GetName()) == 0) {
103 if (!ok1(airspace.GetShape() == AbstractAirspace::Shape::POLYGON))
104 continue;
106 const AirspacePolygon &polygon = (const AirspacePolygon &)airspace;
107 const SearchPointVector &points = polygon.GetPoints();
109 if (!ok1(points.size() == 5))
110 continue;
112 ok1(equals(points[0].GetLocation(),
113 Angle::DMS(1, 30, 30),
114 Angle::DMS(1, 30, 30).Flipped()));
115 ok1(equals(points[1].GetLocation(),
116 Angle::DMS(1, 30, 30),
117 Angle::DMS(1, 30, 30)));
118 ok1(equals(points[2].GetLocation(),
119 Angle::DMS(1, 30, 30).Flipped(),
120 Angle::DMS(1, 30, 30)));
121 ok1(equals(points[3].GetLocation(),
122 Angle::DMS(1, 30, 30).Flipped(),
123 Angle::DMS(1, 30, 30).Flipped()));
124 ok1(equals(points[4].GetLocation(),
125 Angle::DMS(1, 30, 30),
126 Angle::DMS(1, 30, 30).Flipped()));
127 } else if (_tcscmp(_T("Radio-Test"), airspace.GetName()) == 0) {
128 ok1(_tcscmp(_T("130.125 MHz"), airspace.GetRadioText().c_str()) == 0);
129 } else if (_tcscmp(_T("Height-Test-1"), airspace.GetName()) == 0) {
130 ok1(airspace.GetBase().IsTerrain());
131 ok1(airspace.GetTop().reference == AltitudeReference::MSL);
132 ok1(equals(airspace.GetTop().altitude,
133 Units::ToSysUnit(fixed(2000), Unit::FEET)));
134 } else if (_tcscmp(_T("Height-Test-2"), airspace.GetName()) == 0) {
135 ok1(airspace.GetBase().reference == AltitudeReference::MSL);
136 ok1(equals(airspace.GetBase().altitude, 0));
137 ok1(airspace.GetTop().reference == AltitudeReference::STD);
138 ok1(equals(airspace.GetTop().flight_level, 65));
139 } else if (_tcscmp(_T("Height-Test-3"), airspace.GetName()) == 0) {
140 ok1(airspace.GetBase().reference == AltitudeReference::AGL);
141 ok1(equals(airspace.GetBase().altitude_above_terrain,
142 Units::ToSysUnit(fixed(100), Unit::FEET)));
143 ok1(airspace.GetTop().reference == AltitudeReference::MSL);
144 ok1(airspace.GetTop().altitude > Units::ToSysUnit(fixed(30000), Unit::FEET));
145 } else if (_tcscmp(_T("Height-Test-4"), airspace.GetName()) == 0) {
146 ok1(airspace.GetBase().reference == AltitudeReference::MSL);
147 ok1(equals(airspace.GetBase().altitude, 100));
148 ok1(airspace.GetTop().reference == AltitudeReference::MSL);
149 ok1(airspace.GetTop().altitude > Units::ToSysUnit(fixed(30000), Unit::FEET));
150 } else if (_tcscmp(_T("Height-Test-5"), airspace.GetName()) == 0) {
151 ok1(airspace.GetBase().reference == AltitudeReference::AGL);
152 ok1(equals(airspace.GetBase().altitude, 100));
153 ok1(airspace.GetTop().reference == AltitudeReference::MSL);
154 ok1(equals(airspace.GetTop().altitude, 450));
155 } else if (_tcscmp(_T("Height-Test-6"), airspace.GetName()) == 0) {
156 ok1(airspace.GetBase().reference == AltitudeReference::AGL);
157 ok1(equals(airspace.GetBase().altitude_above_terrain,
158 Units::ToSysUnit(fixed(50), Unit::FEET)));
159 ok1(airspace.GetTop().reference == AltitudeReference::STD);
160 ok1(equals(airspace.GetTop().flight_level, 50));
161 } else {
162 for (unsigned i = 0; i < ARRAY_SIZE(classes); ++i) {
163 if (_tcscmp(classes[i].name, airspace.GetName()) == 0)
164 ok1(airspace.GetType() == classes[i].type);
170 static void
171 TestTNP()
173 Airspaces airspaces;
174 if (!ParseFile(_T("test/data/airspace/tnp.sua"), airspaces)) {
175 skip(3, 0, "Failed to parse input file");
176 return;
179 const AirspaceClassTestCouple classes[] = {
180 { _T("Class-R-Test"), RESTRICT },
181 { _T("Class-Q-Test"), DANGER },
182 { _T("Class-P-Test"), PROHIBITED },
183 { _T("Class-CTR-Test"), CTR },
184 { _T("Class-A-Test"), CLASSA },
185 { _T("Class-B-Test"), CLASSB },
186 { _T("Class-C-Test"), CLASSC },
187 { _T("Class-D-Test"), CLASSD },
188 { _T("Class-W-Test"), WAVE },
189 { _T("Class-E-Test"), CLASSE },
190 { _T("Class-F-Test"), CLASSF },
191 { _T("Class-TMZ-Test"), TMZ },
192 { _T("Class-G-Test"), CLASSG },
193 { _T("Class-CLASS-C-Test"), CLASSC },
194 { _T("Class-MATZ-Test"), MATZ },
197 ok1(airspaces.size() == 24);
199 for (auto it = airspaces.begin(); it != airspaces.end(); ++it) {
200 const AbstractAirspace &airspace = *it->GetAirspace();
201 if (_tcscmp(_T("Circle-Test"), airspace.GetName()) == 0) {
202 if (!ok1(airspace.GetShape() == AbstractAirspace::Shape::CIRCLE))
203 continue;
205 const AirspaceCircle &circle = (const AirspaceCircle &)airspace;
206 ok1(equals(circle.GetRadius(), Units::ToSysUnit(fixed(5), Unit::NAUTICAL_MILES)));
207 ok1(equals(circle.GetCenter(),
208 Angle::Degrees(1.091667), Angle::Degrees(0.091667)));
209 } else if (_tcscmp(_T("Polygon-Test"), airspace.GetName()) == 0) {
210 if (!ok1(airspace.GetShape() == AbstractAirspace::Shape::POLYGON))
211 continue;
213 const AirspacePolygon &polygon = (const AirspacePolygon &)airspace;
214 const SearchPointVector &points = polygon.GetPoints();
216 if (!ok1(points.size() == 5))
217 continue;
219 ok1(equals(points[0].GetLocation(),
220 Angle::DMS(1, 30, 30),
221 Angle::DMS(1, 30, 30).Flipped()));
222 ok1(equals(points[1].GetLocation(),
223 Angle::DMS(1, 30, 30),
224 Angle::DMS(1, 30, 30)));
225 ok1(equals(points[2].GetLocation(),
226 Angle::DMS(1, 30, 30).Flipped(),
227 Angle::DMS(1, 30, 30)));
228 ok1(equals(points[3].GetLocation(),
229 Angle::DMS(1, 30, 30).Flipped(),
230 Angle::DMS(1, 30, 30).Flipped()));
231 ok1(equals(points[4].GetLocation(),
232 Angle::DMS(1, 30, 30),
233 Angle::DMS(1, 30, 30).Flipped()));
234 } else if (_tcscmp(_T("Radio-Test"), airspace.GetName()) == 0) {
235 ok1(_tcscmp(_T("130.125 MHz"), airspace.GetRadioText().c_str()) == 0);
236 } else if (_tcscmp(_T("Height-Test-1"), airspace.GetName()) == 0) {
237 ok1(airspace.GetBase().IsTerrain());
238 ok1(airspace.GetTop().reference == AltitudeReference::MSL);
239 ok1(equals(airspace.GetTop().altitude,
240 Units::ToSysUnit(fixed(2000), Unit::FEET)));
241 } else if (_tcscmp(_T("Height-Test-2"), airspace.GetName()) == 0) {
242 ok1(airspace.GetBase().reference == AltitudeReference::MSL);
243 ok1(equals(airspace.GetBase().altitude, 0));
244 ok1(airspace.GetTop().reference == AltitudeReference::STD);
245 ok1(equals(airspace.GetTop().flight_level, 65));
246 } else if (_tcscmp(_T("Height-Test-3"), airspace.GetName()) == 0) {
247 ok1(airspace.GetBase().reference == AltitudeReference::AGL);
248 ok1(equals(airspace.GetBase().altitude_above_terrain,
249 Units::ToSysUnit(fixed(100), Unit::FEET)));
250 ok1(airspace.GetTop().reference == AltitudeReference::MSL);
251 ok1(airspace.GetTop().altitude > Units::ToSysUnit(fixed(30000), Unit::FEET));
252 } else if (_tcscmp(_T("Height-Test-4"), airspace.GetName()) == 0) {
253 ok1(airspace.GetBase().reference == AltitudeReference::MSL);
254 ok1(equals(airspace.GetBase().altitude, 100));
255 ok1(airspace.GetTop().reference == AltitudeReference::MSL);
256 ok1(airspace.GetTop().altitude > Units::ToSysUnit(fixed(30000), Unit::FEET));
257 } else if (_tcscmp(_T("Height-Test-5"), airspace.GetName()) == 0) {
258 ok1(airspace.GetBase().reference == AltitudeReference::AGL);
259 ok1(equals(airspace.GetBase().altitude, 100));
260 ok1(airspace.GetTop().reference == AltitudeReference::MSL);
261 ok1(equals(airspace.GetTop().altitude, 450));
262 } else if (_tcscmp(_T("Height-Test-6"), airspace.GetName()) == 0) {
263 ok1(airspace.GetBase().reference == AltitudeReference::AGL);
264 ok1(equals(airspace.GetBase().altitude_above_terrain,
265 Units::ToSysUnit(fixed(50), Unit::FEET)));
266 ok1(airspace.GetTop().reference == AltitudeReference::STD);
267 ok1(equals(airspace.GetTop().flight_level, 50));
268 } else {
269 for (unsigned i = 0; i < ARRAY_SIZE(classes); ++i) {
270 if (_tcscmp(classes[i].name, airspace.GetName()) == 0)
271 ok1(airspace.GetType() == classes[i].type);
277 int main(int argc, char **argv)
279 plan_tests(103);
281 TestOpenAir();
282 TestTNP();
284 return exit_status();