first commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
# Add each simgear test category.
|
||||
foreach( simgear_test_category
|
||||
math
|
||||
props
|
||||
canvas
|
||||
structure
|
||||
)
|
||||
|
||||
add_subdirectory(${simgear_test_category})
|
||||
|
||||
endforeach( simgear_test_category )
|
||||
|
||||
|
||||
set(TESTSUITE_SOURCES
|
||||
${TESTSUITE_SOURCES}
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
|
||||
set(TESTSUITE_HEADERS
|
||||
${TESTSUITE_HEADERS}
|
||||
PARENT_SCOPE
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
set(TESTSUITE_SOURCES
|
||||
${TESTSUITE_SOURCES}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/testCanvas.cxx
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
set(TESTSUITE_HEADERS
|
||||
${TESTSUITE_HEADERS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/testCanvas.hxx
|
||||
PARENT_SCOPE
|
||||
)
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "testCanvas.hxx"
|
||||
|
||||
|
||||
// Set up the tests.
|
||||
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CanvasTests, "Simgear unit tests");
|
||||
@@ -0,0 +1,123 @@
|
||||
#include "testCanvas.hxx"
|
||||
|
||||
#include <simgear/canvas/Canvas.hxx>
|
||||
#include <simgear/canvas/elements/CanvasImage.hxx>
|
||||
|
||||
#include "test_suite/FGTestApi/testGlobals.hxx"
|
||||
|
||||
#include <Main/globals.hxx>
|
||||
#include <Main/fg_commands.hxx>
|
||||
|
||||
#include <Canvas/canvas_mgr.hxx>
|
||||
#include <Canvas/FGCanvasSystemAdapter.hxx>
|
||||
extern bool global_nasalMinimalInit;
|
||||
|
||||
|
||||
void verifyPixel(osg::Image* img, int x, int y, const uint8_t* pixel)
|
||||
{
|
||||
auto rawPixel = img->data(x, y);
|
||||
CPPUNIT_ASSERT(memcmp(rawPixel, pixel, 4) == 0);
|
||||
}
|
||||
|
||||
void CanvasTests::setUp()
|
||||
{
|
||||
global_nasalMinimalInit = false;
|
||||
FGTestApi::setUp::initTestGlobals("canvas");
|
||||
|
||||
// Canvas needs loadxml command
|
||||
fgInitCommands();
|
||||
|
||||
simgear::canvas::Canvas::setSystemAdapter(
|
||||
simgear::canvas::SystemAdapterPtr(new canvas::FGCanvasSystemAdapter)
|
||||
);
|
||||
|
||||
globals->add_subsystem("Canvas", new CanvasMgr, SGSubsystemMgr::DISPLAY);
|
||||
|
||||
globals->get_subsystem_mgr()->bind();
|
||||
globals->get_subsystem_mgr()->init();
|
||||
|
||||
FGTestApi::setUp::initStandardNasal(true /* withCanvas */);
|
||||
globals->get_subsystem_mgr()->postinit();
|
||||
}
|
||||
|
||||
// Clean up after each test.
|
||||
void CanvasTests::tearDown()
|
||||
{
|
||||
FGTestApi::tearDown::shutdownTestGlobals();
|
||||
}
|
||||
|
||||
void CanvasTests::testCanvasBasic()
|
||||
{
|
||||
bool ok = FGTestApi::executeNasal(R"(
|
||||
var my_canvas = canvas.new({
|
||||
"name": "PFD-Test",
|
||||
"size": [512, 512],
|
||||
"view": [768, 1024],
|
||||
"mipmapping": 1
|
||||
});
|
||||
|
||||
globals.pfd = my_canvas;
|
||||
)");
|
||||
CPPUNIT_ASSERT(ok);
|
||||
|
||||
auto canvasPtr = globals->get_subsystem<CanvasMgr>()->getCanvas("PFD-Test");
|
||||
CPPUNIT_ASSERT(canvasPtr.valid());
|
||||
|
||||
ok = FGTestApi::executeNasal(R"(
|
||||
globals.pfd.createGroup("wibble");
|
||||
)");
|
||||
CPPUNIT_ASSERT(ok);
|
||||
|
||||
auto wibbleGroupPtr = canvasPtr->getGroup("wibble");
|
||||
CPPUNIT_ASSERT(wibbleGroupPtr.valid());
|
||||
}
|
||||
|
||||
void CanvasTests::testImagePixelOps()
|
||||
{
|
||||
bool ok = FGTestApi::executeNasal(R"(
|
||||
var my_canvas = canvas.new({
|
||||
"name": "ImagePixelOpsCanvas",
|
||||
"size": [512, 512],
|
||||
"view": [768, 1024],
|
||||
"mipmapping": 1
|
||||
});
|
||||
|
||||
var g = my_canvas.createGroup("root");
|
||||
var img = g.createChild('image', 'ops-image');
|
||||
|
||||
# deliberate non-power-of-two sizes here
|
||||
img.setSize(150, 170);
|
||||
|
||||
globals.pixelOps = my_canvas;
|
||||
)");
|
||||
CPPUNIT_ASSERT(ok);
|
||||
|
||||
auto canvasPtr = globals->get_subsystem<CanvasMgr>()->getCanvas("ImagePixelOpsCanvas");
|
||||
CPPUNIT_ASSERT(canvasPtr.valid());
|
||||
|
||||
auto imagePtr = canvasPtr->getRootGroup()->getElementById("ops-image");
|
||||
CPPUNIT_ASSERT(imagePtr.valid());
|
||||
|
||||
auto image = dynamic_cast<simgear::canvas::Image*>(imagePtr.get());
|
||||
image->setFill(osg::Vec4(1.0, 0.0, 0.5, 1.0));
|
||||
|
||||
image->fillRect(SGRecti(20, 40, 70, 55), "#7f3f1f");
|
||||
|
||||
ok = FGTestApi::executeNasal(R"(
|
||||
var canvas = globals.pixelOps;
|
||||
var img = canvas.getGroup('root').getElementById('ops-image');
|
||||
|
||||
print('Image is:', img);
|
||||
img.fillRect([5, 8, 180, 37], '#aabbcc');
|
||||
)");
|
||||
CPPUNIT_ASSERT(ok);
|
||||
|
||||
auto osgImage = image->getImage();
|
||||
const uint8_t testColor1[] = {0xff, 0xcc, 0xbb, 0xaa}; // little endian
|
||||
verifyPixel(osgImage.get(), 5, 8, testColor1);
|
||||
verifyPixel(osgImage.get(), 149, 44, testColor1);
|
||||
|
||||
// test pixles should not have been touched by the second fillRect
|
||||
const uint8_t testColor2[] = {0xff, 0x1f, 0x3f, 0x7f}; // little endian
|
||||
verifyPixel(osgImage.get(), 20, 45, testColor2);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
#include <cppunit/TestFixture.h>
|
||||
|
||||
|
||||
// The unit tests.
|
||||
class CanvasTests : public CppUnit::TestFixture
|
||||
{
|
||||
// Set up the test suite.
|
||||
CPPUNIT_TEST_SUITE(CanvasTests);
|
||||
CPPUNIT_TEST(testCanvasBasic);
|
||||
CPPUNIT_TEST(testImagePixelOps);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
// Set up function for each test.
|
||||
void setUp();
|
||||
|
||||
// Clean up after each test.
|
||||
void tearDown();
|
||||
|
||||
// The tests.
|
||||
void testCanvasBasic();
|
||||
void testImagePixelOps();
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
set(TESTSUITE_SOURCES
|
||||
${TESTSUITE_SOURCES}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-up.cxx
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
set(TESTSUITE_HEADERS
|
||||
${TESTSUITE_HEADERS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/testSGGeodesy.hxx
|
||||
PARENT_SCOPE
|
||||
)
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "testSGGeodesy.hxx"
|
||||
|
||||
|
||||
// Set up the tests.
|
||||
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MathGeodesyTests, "Simgear unit tests");
|
||||
@@ -0,0 +1,172 @@
|
||||
// do some test relating to the concept of "up"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <simgear/math/sg_geodesy.hxx>
|
||||
|
||||
#include "testSGGeodesy.hxx"
|
||||
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
|
||||
// The test-up unit test.
|
||||
void MathGeodesyTests::testUp()
|
||||
{
|
||||
// for each lat/lon given in goedetic coordinates, calculate
|
||||
// geocentric coordinates, cartesian coordinates, the local "up"
|
||||
// vector (based on original geodetic lat/lon), as well as the "Z"
|
||||
// intercept (for which 0 = center of earth)
|
||||
|
||||
|
||||
double lon = 0;
|
||||
double alt = 0;
|
||||
int i;
|
||||
const int n = 19;
|
||||
double lat[n] = {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90};
|
||||
|
||||
// Hard coded expected results.
|
||||
double lat_geoc_expected[n] = {
|
||||
0,
|
||||
4.96669608700923,
|
||||
9.93439421027913,
|
||||
14.9040671396528,
|
||||
19.8766298623747,
|
||||
24.8529125604827,
|
||||
29.8336358098291,
|
||||
34.8193887023496,
|
||||
39.8106105519284,
|
||||
44.8075767840180,
|
||||
49.8103895262907,
|
||||
54.8189733092144,
|
||||
59.8330761504926,
|
||||
64.8522761370246,
|
||||
69.8759934364236,
|
||||
74.9035074740039,
|
||||
79.9339788099657,
|
||||
84.9664750566862,
|
||||
90
|
||||
};
|
||||
double cart_expected[n][3] = {
|
||||
{6378137, 0, 0},
|
||||
{6353866.26310279, 555891.26758132, 0},
|
||||
{6281238.76737403, 1107551.86696002, 0},
|
||||
{6160807.25190988, 1650783.32787306, 0},
|
||||
{5993488.27326157, 2181451.33089075, 0},
|
||||
{5780555.22988658, 2695517.17208404, 0},
|
||||
{5523628.67081747, 3189068.5, 0},
|
||||
{5224663.96230526, 3658349.09101875, 0},
|
||||
{4885936.40630155, 4099787.43648327, 0},
|
||||
{4510023.92403682, 4510023.92403682, 0},
|
||||
{4099787.43648328, 4885936.40630155, 0},
|
||||
{3658349.09101875, 5224663.96230526, 0},
|
||||
{3189068.5, 5523628.67081747, 0},
|
||||
{2695517.17208404, 5780555.22988658, 0},
|
||||
{2181451.33089075, 5993488.27326157, 0},
|
||||
{1650783.32787306, 6160807.25190988, 0},
|
||||
{1107551.86696002, 6281238.76737403, 0},
|
||||
{555891.26758132, 6353866.26310279, 0},
|
||||
{3.90548253078665e-10, 6378137, 0}
|
||||
};
|
||||
double up_expected[n][3] = {
|
||||
{6378137, 0, -0},
|
||||
{6354027.82056206, 0, -552183.96002777},
|
||||
{6281872.82960345, 0, -1100248.54773536},
|
||||
{6162189.08803834, 0, -1640100.14019589},
|
||||
{5995836.38389635, 0, -2167696.78782876},
|
||||
{5784014.11472572, 0, -2679074.46295779},
|
||||
{5528256.63929284, 0, -3170373.73538364},
|
||||
{5230426.84020036, 0, -3637866.9093781},
|
||||
{4892707.60007269, 0, -4077985.57220038},
|
||||
{4517590.87884893, 0, -4487348.40886592},
|
||||
{4107864.09120678, 0, -4862789.03770643},
|
||||
{3666593.52237417, 0, -5201383.52320227},
|
||||
{3197104.58692395, 0, -5500477.13393864},
|
||||
{2702958.82600294, 0, -5757709.84149597},
|
||||
{2187927.64927902, 0, -5971040.00711856},
|
||||
{1655962.9523645, 0, -6138765.68235824},
|
||||
{1111164.87081001, 0, -6259542.96102869},
|
||||
{557747.059253584, 0, -6332400.86398617},
|
||||
{3.91862092481447e-10, 0, -6356752.31424518}
|
||||
};
|
||||
double geod_intercept[n] = {
|
||||
0,
|
||||
552169.92020293,
|
||||
1100137.4939038,
|
||||
1639732.35699488,
|
||||
2166847.86675177,
|
||||
2677472.35586416,
|
||||
3167719.66364641,
|
||||
3633858.71206688,
|
||||
4072341.90150465,
|
||||
4479832.11012469,
|
||||
4853228.0913901,
|
||||
5189688.07642079,
|
||||
5486651.40157058,
|
||||
5741857.99662372,
|
||||
5953365.5852939,
|
||||
6119564.46711958,
|
||||
6239189.76825642,
|
||||
6311331.06793104,
|
||||
6335439.32729282
|
||||
};
|
||||
double geoc_intercept[n] = {
|
||||
0,
|
||||
552169.92020293,
|
||||
1100137.4939038,
|
||||
1639732.35699488,
|
||||
2166847.86675177,
|
||||
2677472.35586416,
|
||||
3167719.66364641,
|
||||
3633858.71206688,
|
||||
4072341.90150465,
|
||||
4479832.11012469,
|
||||
4853228.0913901,
|
||||
5189688.07642079,
|
||||
5486651.40157058,
|
||||
5741857.99662372,
|
||||
5953365.5852939,
|
||||
6119564.46711958,
|
||||
6239189.76825642,
|
||||
6311331.06793104,
|
||||
6378137 // Sole difference with geod, due to truncation artifacts.
|
||||
};
|
||||
|
||||
for (i=0; i < n; i++) {
|
||||
cout << "lon = " << lon << " geod lat = " << lat[i];
|
||||
|
||||
double sl_radius, lat_geoc;
|
||||
sgGeodToGeoc( lat[i] * SGD_DEGREES_TO_RADIANS, alt, &sl_radius, &lat_geoc );
|
||||
cout << " geoc lat = " << lat_geoc * SGD_RADIANS_TO_DEGREES << endl;
|
||||
CPPUNIT_ASSERT_DOUBLES_EQUAL(lat_geoc_expected[i], lat_geoc*SGD_RADIANS_TO_DEGREES, 1e-10);
|
||||
|
||||
double pc[3];
|
||||
sgGeodToCart( lon * SGD_DEGREES_TO_RADIANS, lat[i] * SGD_DEGREES_TO_RADIANS, 0.0, pc );
|
||||
cout << " cartesian = " << SGVec3d(pc[0], pc[1], pc[2]) << endl;
|
||||
for (int j=0; j < 3; j++)
|
||||
CPPUNIT_ASSERT_DOUBLES_EQUAL(cart_expected[i][j], pc[j], 1e-7);
|
||||
|
||||
SGGeod geod_up = SGGeod::fromDeg(lon, -lat[i]);
|
||||
SGVec3d geod_up_vect = SGVec3d::fromGeod(geod_up);
|
||||
cout << " geod up = " << geod_up_vect << endl;
|
||||
for (int j=0; j < 3; j++)
|
||||
CPPUNIT_ASSERT_DOUBLES_EQUAL(up_expected[i][j], geod_up_vect[j], 1e-7);
|
||||
|
||||
SGGeoc geoc_up = SGGeoc::fromGeod(geod_up);
|
||||
SGVec3d geoc_up_vect = SGVec3d::fromGeoc(geoc_up);
|
||||
cout << " geoc up = " << geoc_up_vect << endl;
|
||||
for (int j=0; j < 3; j++)
|
||||
CPPUNIT_ASSERT_DOUBLES_EQUAL(up_expected[i][j], geoc_up_vect[j], 1e-7);
|
||||
|
||||
double slope = geod_up_vect[2] / geod_up_vect[0];
|
||||
double intercept = pc[2] - slope * pc[0];
|
||||
cout << " Z intercept (based on geodetic up) = " << intercept << endl;
|
||||
CPPUNIT_ASSERT_DOUBLES_EQUAL(geod_intercept[i], intercept, 1e-7);
|
||||
|
||||
slope = geoc_up_vect[2] / geoc_up_vect[0];
|
||||
intercept = pc[2] - slope * pc[0];
|
||||
cout << " Z intercept (based on geocentric up) = " << intercept << endl;
|
||||
CPPUNIT_ASSERT_DOUBLES_EQUAL(geoc_intercept[i], intercept, 1e-7);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _FG_MATH_GEODESY_SIMGEAR_UNIT_TESTS_HXX
|
||||
#define _FG_MATH_GEODESY_SIMGEAR_UNIT_TESTS_HXX
|
||||
|
||||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
#include <cppunit/TestFixture.h>
|
||||
|
||||
|
||||
|
||||
// The unit tests.
|
||||
class MathGeodesyTests : public CppUnit::TestFixture
|
||||
{
|
||||
// Set up the test suite.
|
||||
CPPUNIT_TEST_SUITE(MathGeodesyTests);
|
||||
CPPUNIT_TEST(testUp);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
// Set up function for each test.
|
||||
void setUp() {}
|
||||
|
||||
// Clean up after each test.
|
||||
void tearDown() {}
|
||||
|
||||
// The tests.
|
||||
void testUp();
|
||||
};
|
||||
|
||||
|
||||
#endif // _FG_MATH_GEODESY_SIMGEAR_UNIT_TESTS_HXX
|
||||
@@ -0,0 +1,12 @@
|
||||
set(TESTSUITE_SOURCES
|
||||
${TESTSUITE_SOURCES}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_props.cxx
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
set(TESTSUITE_HEADERS
|
||||
${TESTSUITE_HEADERS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_props.hxx
|
||||
PARENT_SCOPE
|
||||
)
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "test_props.hxx"
|
||||
|
||||
|
||||
// Set up the tests.
|
||||
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SimgearPropsTests, "Simgear unit tests");
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "test_props.hxx"
|
||||
|
||||
#include <simgear/props/props_io.hxx>
|
||||
|
||||
// Set up function for each test.
|
||||
void SimgearPropsTests::setUp()
|
||||
{
|
||||
// Create a property tree.
|
||||
tree = new SGPropertyNode;
|
||||
}
|
||||
|
||||
|
||||
// Clean up after each test.
|
||||
void SimgearPropsTests::tearDown()
|
||||
{
|
||||
// Delete the tree (avoiding the memory leak).
|
||||
delete tree;
|
||||
}
|
||||
|
||||
|
||||
// Test property aliasing, to catch possible memory leaks.
|
||||
void SimgearPropsTests::testAliasLeak()
|
||||
{
|
||||
// Declarations.
|
||||
SGPropertyNode *alias;
|
||||
|
||||
// Create a new node.
|
||||
tree->getNode("test-node", true);
|
||||
|
||||
// Aliased node.
|
||||
alias = tree->getNode("test-alias", true);
|
||||
alias->alias("test-node");
|
||||
}
|
||||
|
||||
void SimgearPropsTests::testPropsCopyIf()
|
||||
{
|
||||
// dummy property tree structure
|
||||
tree->setIntValue("a/a/a", 42);
|
||||
tree->setStringValue("a/a/b", "foo");
|
||||
tree->setIntValue("a/a/c[2]", 99);
|
||||
tree->setIntValue("a/a/d", 1);
|
||||
tree->setIntValue("a/b/a[0]", 50);
|
||||
tree->setIntValue("a/b/a[1]", 100);
|
||||
tree->setStringValue("a/b/b", "foo");
|
||||
|
||||
SGPropertyNode_ptr destA(new SGPropertyNode);
|
||||
|
||||
copyPropertiesIf(tree, destA, [](const SGPropertyNode* src) {
|
||||
// always copy non-leaf nodes
|
||||
if (src->nChildren() > 0)
|
||||
return true;
|
||||
|
||||
return (src->getType() == simgear::props::INT) &&
|
||||
src->getIntValue() > 50;
|
||||
});
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(1, destA->getNode("a/a")->nChildren()); // only 99
|
||||
CPPUNIT_ASSERT_EQUAL(99, destA->getIntValue("a/a/c[2]"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, destA->getNode("a/b")->nChildren()); // only 100
|
||||
CPPUNIT_ASSERT_EQUAL(100, destA->getIntValue("a/b/a[1]"));
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _FG_SIMGEAR_PROPS_SIMGEAR_UNIT_TESTS_HXX
|
||||
#define _FG_SIMGEAR_PROPS_SIMGEAR_UNIT_TESTS_HXX
|
||||
|
||||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
#include <cppunit/TestFixture.h>
|
||||
|
||||
#include <simgear/props/props.hxx>
|
||||
|
||||
|
||||
// The unit tests of the simgear property tree.
|
||||
class SimgearPropsTests : public CppUnit::TestFixture
|
||||
{
|
||||
// Set up the test suite.
|
||||
CPPUNIT_TEST_SUITE(SimgearPropsTests);
|
||||
CPPUNIT_TEST(testAliasLeak);
|
||||
CPPUNIT_TEST(testPropsCopyIf);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
// Set up function for each test.
|
||||
void setUp();
|
||||
|
||||
// Clean up after each test.
|
||||
void tearDown();
|
||||
|
||||
// The tests.
|
||||
void testAliasLeak();
|
||||
void testPropsCopyIf();
|
||||
private:
|
||||
// A property tree.
|
||||
SGPropertyNode *tree;
|
||||
};
|
||||
|
||||
#endif // _FG_SIMGEAR_PROPS_SIMGEAR_UNIT_TESTS_HXX
|
||||
@@ -0,0 +1,12 @@
|
||||
set(TESTSUITE_SOURCES
|
||||
${TESTSUITE_SOURCES}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_event.cxx
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
set(TESTSUITE_HEADERS
|
||||
${TESTSUITE_HEADERS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_event.hxx
|
||||
PARENT_SCOPE
|
||||
)
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "test_event.hxx"
|
||||
|
||||
|
||||
// Set up the tests.
|
||||
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SimgearEventTests, "Simgear unit tests");
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
||||
#include "test_event.hxx"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace {
|
||||
|
||||
SGEventMgr* global_eventManager = nullptr;
|
||||
double global_realTime = 0.0;
|
||||
|
||||
// class which behaves similarly to TimerObj in NasalSys
|
||||
class FakeNasalTimer
|
||||
{
|
||||
public:
|
||||
FakeNasalTimer(string n) : _name(n)
|
||||
{
|
||||
}
|
||||
|
||||
void invoke()
|
||||
{
|
||||
++_invokeCount;
|
||||
|
||||
if (_expectedTime > 0.0) {
|
||||
CPPUNIT_ASSERT_DOUBLES_EQUAL(_expectedTime, global_realTime, 0.1);
|
||||
}
|
||||
|
||||
// rescheudle base don the next time
|
||||
if (!_rescheduleTimes.empty()) {
|
||||
const auto t = _rescheduleTimes.front();
|
||||
_rescheduleTimes.pop_front();
|
||||
|
||||
// TimerObj does this, eugh
|
||||
if (global_eventManager) {
|
||||
global_eventManager->removeTask(_name);
|
||||
start(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void start(double interval)
|
||||
{
|
||||
if (!global_eventManager)
|
||||
return;
|
||||
|
||||
global_eventManager->addTask(_name, [this](){ this->invoke(); },
|
||||
interval, interval /* delay */, false);
|
||||
|
||||
_expectedTime = global_realTime + interval;
|
||||
}
|
||||
|
||||
string _name;
|
||||
deque<double> _rescheduleTimes;
|
||||
int _invokeCount = 0;
|
||||
double _expectedTime = 0.0;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
// Set up function for each test.
|
||||
void SimgearEventTests::setUp()
|
||||
{
|
||||
_eventManager = new SGEventMgr;
|
||||
global_eventManager = _eventManager;
|
||||
_eventManager->init();
|
||||
|
||||
_realTimeProp = new SGPropertyNode{};
|
||||
_eventManager->setRealtimeProperty(_realTimeProp);
|
||||
}
|
||||
|
||||
// Clean up after each test.
|
||||
void SimgearEventTests::tearDown()
|
||||
{
|
||||
global_eventManager = nullptr;
|
||||
}
|
||||
|
||||
void SimgearEventTests::runForTestTime(double totalTime, double updateHz, double simTimeScaling)
|
||||
{
|
||||
const double dt = 1.0 / updateHz;
|
||||
const int updateCount = static_cast<int>(totalTime * updateHz);
|
||||
const double simDt = dt * simTimeScaling;
|
||||
_realTimeProp->setDoubleValue(dt);
|
||||
global_realTime = 0.0;
|
||||
|
||||
for (int c = 0; c < updateCount; ++c) {
|
||||
const double newRealTime = global_realTime + dt;
|
||||
global_realTime = newRealTime;
|
||||
_eventManager->update(simDt);
|
||||
}
|
||||
}
|
||||
|
||||
void SimgearEventTests::testTaskRescheduleDuringRun()
|
||||
{
|
||||
auto t1 = std::make_unique<FakeNasalTimer>("timer_aaa");
|
||||
t1->_rescheduleTimes.resize(10);
|
||||
fill(t1->_rescheduleTimes.begin(), t1->_rescheduleTimes.end(), 0.5);
|
||||
t1->_rescheduleTimes.push_back(30.0); // large final value
|
||||
|
||||
global_realTime = 0.0;
|
||||
t1->start(1.0);
|
||||
runForTestTime(8.0, 20);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(11, t1->_invokeCount);
|
||||
}
|
||||
|
||||
void SimgearEventTests::testTaskRescheduleDuringRun2()
|
||||
{
|
||||
auto t1 = std::make_unique<FakeNasalTimer>("timer_bbb");
|
||||
const std::initializer_list<double> times = {
|
||||
0.02, 0.05, 0.02, 0.1, 0.02, 0.125, 0.05, 0.02};
|
||||
t1->_rescheduleTimes = times;
|
||||
t1->_rescheduleTimes.push_back(30.0); // large final value
|
||||
|
||||
global_realTime = 0.0;
|
||||
t1->start(1.0);
|
||||
|
||||
// running at 60Hz, check we don't miss any
|
||||
runForTestTime(8.0, 60);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(9, t1->_invokeCount);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Edward d'Auvergne
|
||||
*
|
||||
* This file is part of the program FlightGear.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cppunit/TestFixture.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include <simgear/props/props.hxx>
|
||||
#include <simgear/structure/event_mgr.hxx>
|
||||
|
||||
// The unit tests of the simgear property tree.
|
||||
class SimgearEventTests : public CppUnit::TestFixture
|
||||
{
|
||||
// Set up the test suite.
|
||||
CPPUNIT_TEST_SUITE(SimgearEventTests);
|
||||
CPPUNIT_TEST(testTaskRescheduleDuringRun);
|
||||
CPPUNIT_TEST(testTaskRescheduleDuringRun2);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
// Set up function for each test.
|
||||
void setUp();
|
||||
|
||||
// Clean up after each test.
|
||||
void tearDown();
|
||||
|
||||
void runForTestTime(double totalTime, double updateHz, double simTimeScaling = 1.0);
|
||||
|
||||
// The tests.
|
||||
void testTaskRescheduleDuringRun();
|
||||
void testTaskRescheduleDuringRun2();
|
||||
|
||||
private:
|
||||
SGSharedPtr<SGEventMgr> _eventManager;
|
||||
SGPropertyNode_ptr _realTimeProp;
|
||||
SGPropertyNode_ptr _freezeProp;
|
||||
};
|
||||
Reference in New Issue
Block a user