| 1 | /* |
| 2 | * Copyright (C) 2016 Edward d'Auvergne |
| 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 | |
| 21 | #include "testNasalSys.hxx" |
| 22 | |
| 23 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 24 | #include "test_suite/FGTestApi/NavDataCache.hxx" |
| 25 | |
| 26 | #include <simgear/structure/commands.hxx> |
| 27 | |
| 28 | #include <Main/fg_props.hxx> |
| 29 | #include <Main/globals.hxx> |
| 30 | #include <Main/util.hxx> |
| 31 | |
| 32 | #include <Airports/airport.hxx> |
| 33 | |
| 34 | #include <Scripting/NasalSys.hxx> |
| 35 | |
| 36 | #include <Main/FGInterpolator.hxx> |
| 37 | |
| 38 | // Set up function for each test. |
| 39 | void NasalSysTests::setUp() |
| 40 | { |
| 41 | FGTestApi::setUp::initTestGlobals("NasalSys"); |
| 42 | FGTestApi::setUp::initNavDataCache(); |
| 43 | |
| 44 | fgInitAllowedPaths(); |
| 45 | globals->get_props()->getNode("nasal", true); |
| 46 | |
| 47 | globals->get_subsystem_mgr()->add<FGInterpolator>(); |
| 48 | |
| 49 | globals->get_subsystem_mgr()->bind(); |
| 50 | globals->get_subsystem_mgr()->init(); |
| 51 | |
| 52 | globals->get_subsystem_mgr()->add<FGNasalSys>(); |
| 53 | |
| 54 | globals->get_subsystem_mgr()->postinit(); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | // Clean up after each test. |
| 59 | void NasalSysTests::tearDown() |
| 60 | { |
| 61 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 62 | } |
| 63 | |
| 64 | bool NasalSysTests::checkNoNasalErrors() |
| 65 | { |
| 66 | auto nasalSys = globals->get_subsystem<FGNasalSys>(); |
| 67 | auto errors = nasalSys->getAndClearErrorList(); |
| 68 | return errors.empty(); |
| 69 | } |
| 70 | |
| 71 | // Test test |
| 72 | void NasalSysTests::testStructEquality() |
| 73 | { |
| 74 | bool ok = FGTestApi::executeNasal(R"( |
| 75 | var foo = { |
| 76 | "name": "Bob", |
| 77 | "size": [512, 512], |
| 78 | "mipmapping": 1.9 |
| 79 | }; |
| 80 | |
| 81 | var bar = { |
| 82 | "name": "Bob", |
| 83 | "size": [512, 512], |
| 84 | "mipmapping": 1.9 |
| 85 | }; |
| 86 | |
| 87 | unitTest.assert_equal(foo, bar); |
| 88 | |
| 89 | append(bar.size, "Wowow"); |
| 90 | unitTest.assert(unitTest.equal(foo, bar) == 0); |
| 91 | |
| 92 | append(foo.size, "Wowow"); |
| 93 | unitTest.assert_equal(foo, bar); |
| 94 | |
| 95 | foo.wibble = 99.1; |
| 96 | unitTest.assert(unitTest.equal(foo, bar) == 0); |
| 97 | |
| 98 | bar.wibble = 99; |
| 99 | unitTest.assert(unitTest.equal(foo, bar) == 0); |
| 100 | bar.wibble = 99.1; |
| 101 | unitTest.assert_equal(foo, bar); |
| 102 | |
| 103 | )"); |
| 104 | CPPUNIT_ASSERT(ok); |
| 105 | } |
| 106 | |
| 107 | void NasalSysTests::testCommands() |
| 108 | { |
| 109 | auto nasalSys = globals->get_subsystem<FGNasalSys>(); |
| 110 | nasalSys->getAndClearErrorList(); |
| 111 | |
| 112 | fgSetInt("/foo/test", 7); |
| 113 | bool ok = FGTestApi::executeNasal(R"( |
| 114 | var f = func { |
| 115 | var i = getprop('/foo/test'); |
| 116 | setprop('foo/test', i + 4); |
| 117 | }; |
| 118 | |
| 119 | addcommand('do-foo', f); |
| 120 | var ok = fgcommand('do-foo'); |
| 121 | unitTest.assert(ok); |
| 122 | )"); |
| 123 | CPPUNIT_ASSERT(ok); |
| 124 | |
| 125 | CPPUNIT_ASSERT_EQUAL(11, fgGetInt("/foo/test")); |
| 126 | |
| 127 | SGPropertyNode_ptr args(new SGPropertyNode); |
| 128 | ok = globals->get_commands()->execute("do-foo", args); |
| 129 | CPPUNIT_ASSERT(ok); |
| 130 | CPPUNIT_ASSERT_EQUAL(15, fgGetInt("/foo/test")); |
| 131 | |
| 132 | ok = FGTestApi::executeNasal(R"( |
| 133 | var g = func { print('fail'); }; |
| 134 | addcommand('do-foo', g); |
| 135 | )"); |
| 136 | |
| 137 | CPPUNIT_ASSERT(ok); |
| 138 | auto errors = nasalSys->getAndClearErrorList(); |
| 139 | CPPUNIT_ASSERT_EQUAL(errors.size(), static_cast<size_t>(1)); |
| 140 | |
| 141 | // old command should still be registered and work |
| 142 | ok = globals->get_commands()->execute("do-foo", args); |
| 143 | CPPUNIT_ASSERT(ok); |
| 144 | CPPUNIT_ASSERT_EQUAL(19, fgGetInt("/foo/test")); |
| 145 | |
| 146 | ok = FGTestApi::executeNasal(R"( |
| 147 | removecommand('do-foo'); |
| 148 | )"); |
| 149 | CPPUNIT_ASSERT(ok); |
| 150 | |
| 151 | ok = FGTestApi::executeNasal(R"( |
| 152 | var ok = fgcommand('do-foo'); |
| 153 | unitTest.assert(!ok); |
| 154 | )"); |
| 155 | CPPUNIT_ASSERT(ok); |
| 156 | |
| 157 | errors = nasalSys->getAndClearErrorList(); |
| 158 | CPPUNIT_ASSERT_EQUAL(0UL, (unsigned long) errors.size()); |
| 159 | |
| 160 | // should fail, command is removed |
| 161 | ok = globals->get_commands()->execute("do-foo", args); |
| 162 | CPPUNIT_ASSERT(!ok); |
| 163 | CPPUNIT_ASSERT_EQUAL(19, fgGetInt("/foo/test")); |
| 164 | } |
| 165 | |
| 166 | void NasalSysTests::testAirportGhost() |
| 167 | { |
| 168 | auto nasalSys = globals->get_subsystem<FGNasalSys>(); |
| 169 | nasalSys->getAndClearErrorList(); |
| 170 | |
| 171 | bool ok = FGTestApi::executeNasal(R"( |
| 172 | var apt = airportinfo('LFBD'); |
| 173 | var taxiways = apt.taxiways; |
| 174 | unitTest.assert_equal(size(taxiways), 0); |
| 175 | )"); |
| 176 | CPPUNIT_ASSERT(ok); |
| 177 | |
| 178 | } |
| 179 | |
| 180 | void NasalSysTests::testFindComm() |
| 181 | { |
| 182 | FGAirportRef apt = FGAirport::getByIdent("EDDM"); |
| 183 | FGTestApi::setPositionAndStabilise(apt->geod()); |
| 184 | |
| 185 | auto nasalSys = globals->get_subsystem<FGNasalSys>(); |
| 186 | nasalSys->getAndClearErrorList(); |
| 187 | |
| 188 | bool ok = FGTestApi::executeNasal(R"( |
| 189 | var comm = findCommByFrequencyMHz(123.125); |
| 190 | unitTest.assert_equal(comm.id, "ATIS"); |
| 191 | |
| 192 | # explicit filter, should't match |
| 193 | var noComm = findCommByFrequencyMHz(123.125, "tower"); |
| 194 | unitTest.assert_equal(noComm, nil); |
| 195 | |
| 196 | # match with filter |
| 197 | var comm2 = findCommByFrequencyMHz(121.725, "clearance"); |
| 198 | unitTest.assert_equal(comm2.id, "CLNC DEL"); |
| 199 | )"); |
| 200 | |
| 201 | CPPUNIT_ASSERT(ok && checkNoNasalErrors()); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | // https://sourceforge.net/p/flightgear/codetickets/2246/ |
| 206 | |
| 207 | void NasalSysTests::testCompileLarge() |
| 208 | { |
| 209 | // auto nasalSys = globals->get_subsystem<FGNasalSys>(); |
| 210 | // nasalSys->getAndClearErrorList(); |
| 211 | // |
| 212 | // |
| 213 | // string code = "var foo = 0;\n"; |
| 214 | // for (int i=0; i<14; ++i) { |
| 215 | // code = code + code; |
| 216 | // } |
| 217 | // |
| 218 | // nasalSys->parseAndRun(code); |
| 219 | |
| 220 | // bool ok = FGTestApi::executeNasal(R"( |
| 221 | //var try_compile = func(code) { |
| 222 | // call(compile, [code], nil,nil,var err=[]); |
| 223 | // return size(err); |
| 224 | //} |
| 225 | // |
| 226 | //var expression = "var foo = 0;\n"; |
| 227 | //var code = ""; |
| 228 | // |
| 229 | //for(var i=0;i<=10000;i+=1) { |
| 230 | // code ~= expression; |
| 231 | // if (try_compile(code) == 1) { |
| 232 | // print("Error compiling, LOC count is:", i+1); |
| 233 | // break; |
| 234 | // } |
| 235 | //} |
| 236 | // )"); |
| 237 | // CPPUNIT_ASSERT(ok); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | void NasalSysTests::testRoundFloor() |
| 242 | { |
| 243 | auto nasalSys = globals->get_subsystem<FGNasalSys>(); |
| 244 | nasalSys->getAndClearErrorList(); |
| 245 | |
| 246 | bool ok = FGTestApi::executeNasal(R"( |
| 247 | unitTest.assert_equal(math.round(121266, 1000), 121000); |
| 248 | unitTest.assert_equal(math.round(121.1234, 0.01), 121.12); |
| 249 | unitTest.assert_equal(math.round(121266, 10), 121270); |
| 250 | |
| 251 | unitTest.assert_equal(math.floor(121766, 1000), 121000); |
| 252 | unitTest.assert_equal(math.floor(121.1299, 0.01), 121.12); |
| 253 | |
| 254 | # floor towards lower value |
| 255 | unitTest.assert_equal(math.floor(-121.1229, 0.01), -121.13); |
| 256 | |
| 257 | # truncate towards zero |
| 258 | unitTest.assert_equal(math.trunc(-121.1229, 0.01), -121.12); |
| 259 | unitTest.assert_equal(math.trunc(-121.1299, 0.01), -121.12); |
| 260 | )"); |
| 261 | CPPUNIT_ASSERT(ok); |
| 262 | } |
| 263 | |
| 264 | void NasalSysTests::testRange() |
| 265 | { |
| 266 | auto nasalSys = globals->get_subsystem<FGNasalSys>(); |
| 267 | nasalSys->getAndClearErrorList(); |
| 268 | |
| 269 | bool ok = FGTestApi::executeNasal(R"( |
| 270 | unitTest.assert_equal(range(5), [0, 1, 2, 3, 4]); |
| 271 | unitTest.assert_equal(range(2, 8), [2, 3, 4, 5, 6, 7]); |
| 272 | unitTest.assert_equal(range(2, 10, 3), [2, 5, 8]); |
| 273 | |
| 274 | )"); |
| 275 | CPPUNIT_ASSERT(ok); |
| 276 | } |
| 277 | |
| 278 | void NasalSysTests::testKeywordArgInHash() |
| 279 | { |
| 280 | auto nasalSys = globals->get_subsystem<FGNasalSys>(); |
| 281 | nasalSys->getAndClearErrorList(); |
| 282 | |
| 283 | bool ok = FGTestApi::executeNasal(R"( |
| 284 | var foo = func(arg1, kw1 = "", kw2 = nil) |
| 285 | { |
| 286 | return {'a':kw1, 'b':kw2}; |
| 287 | } |
| 288 | |
| 289 | var d = foo(arg1:42, kw2:'apples', kw1:'pears'); |
| 290 | unitTest.assert_equal(d.a, 'pears'); |
| 291 | unitTest.assert_equal(d.b, 'apples'); |
| 292 | |
| 293 | )"); |
| 294 | CPPUNIT_ASSERT(ok); |
| 295 | |
| 296 | ok = FGTestApi::executeNasal(R"( |
| 297 | var bar = func(h) { |
| 298 | return h; |
| 299 | } |
| 300 | |
| 301 | var foo = func(arg1, kw1 = "", kw2 = nil) |
| 302 | { |
| 303 | return bar({'a':kw1, 'b':kw2}); |
| 304 | } |
| 305 | |
| 306 | var d = foo(arg1:42, kw2:'apples', kw1:'pears'); |
| 307 | unitTest.assert_equal(d.a, 'pears'); |
| 308 | unitTest.assert_equal(d.b, 'apples'); |
| 309 | |
| 310 | )"); |
| 311 | CPPUNIT_ASSERT(ok); |
| 312 | |
| 313 | ok = FGTestApi::executeNasal(R"( |
| 314 | var bar = func(h) { |
| 315 | unitTest.assert_equal(h.a, 'pears'); |
| 316 | unitTest.assert_equal(h.b, 'apples'); |
| 317 | } |
| 318 | |
| 319 | var foo = func(arg1, kw1 = "", kw2 = nil) |
| 320 | { |
| 321 | return bar({'a':kw1, 'b':kw2}); |
| 322 | } |
| 323 | |
| 324 | var d = foo(arg1:42, kw2:'apples', kw1:'pears'); |
| 325 | )"); |
| 326 | CPPUNIT_ASSERT(ok); |
| 327 | } |
| 328 | |
| 329 | void NasalSysTests::testNullAccess() |
| 330 | { |
| 331 | auto nasalSys = globals->get_subsystem<FGNasalSys>(); |
| 332 | nasalSys->getAndClearErrorList(); |
| 333 | |
| 334 | bool ok = FGTestApi::executeNasal(R"( |
| 335 | var s = { |
| 336 | bar: 42 |
| 337 | }; |
| 338 | |
| 339 | unitTest.assert_equal(s?.bar, 42); |
| 340 | |
| 341 | var t = nil; |
| 342 | var z = t?.bar; |
| 343 | unitTest.assert_equal(z, nil); |
| 344 | |
| 345 | |
| 346 | )"); |
| 347 | CPPUNIT_ASSERT(ok); |
| 348 | auto errors = nasalSys->getAndClearErrorList(); |
| 349 | CPPUNIT_ASSERT_EQUAL(errors.size(), static_cast<size_t>(0)); |
| 350 | } |
| 351 | |
| 352 | void NasalSysTests::testNullishChain() |
| 353 | { |
| 354 | auto nasalSys = globals->get_subsystem<FGNasalSys>(); |
| 355 | nasalSys->getAndClearErrorList(); |
| 356 | |
| 357 | bool ok = FGTestApi::executeNasal(R"( |
| 358 | var t = nil; |
| 359 | var s = 'abc'; |
| 360 | |
| 361 | unitTest.assert_equal(t ?? 99, 99); |
| 362 | unitTest.assert_equal(s ?? 'default', 'abc'); |
| 363 | unitTest.assert_equal(t ?? 'default', 'default'); |
| 364 | |
| 365 | # check 0 is used, only nil should fail |
| 366 | unitTest.assert_equal(0 ?? 'default', 0); |
| 367 | )"); |
| 368 | |
| 369 | CPPUNIT_ASSERT(ok); |
| 370 | auto errors = nasalSys->getAndClearErrorList(); |
| 371 | CPPUNIT_ASSERT_EQUAL(errors.size(), static_cast<size_t>(0)); |
| 372 | } |