From dbe7c901150261af18b4f6b02b64958dc3ca8128 Mon Sep 17 00:00:00 2001 From: Edward d'Auvergne Date: Mon, 11 Jun 2018 11:50:32 +0200 Subject: [PATCH] TestSuite: Shifted the YASim atmosphere test into the CppUnit framework. --- src/FDM/YASim/Atmosphere.cpp | 24 --------- src/FDM/YASim/Atmosphere.hpp | 17 +++--- src/FDM/YASim/CMakeLists.txt | 1 - src/FDM/YASim/yasim-atmotest.cpp | 12 ----- test_suite/CMakeLists.txt | 1 + test_suite/FGTestApi/PrivateAccessorFDM.cxx | 16 ++++++ test_suite/FGTestApi/PrivateAccessorFDM.hxx | 10 ++++ test_suite/unit_tests/FDM/CMakeLists.txt | 2 + test_suite/unit_tests/FDM/TestSuite.cxx | 2 + .../unit_tests/FDM/testYASimAtmosphere.cxx | 44 ++++++++++++++++ .../unit_tests/FDM/testYASimAtmosphere.hxx | 52 +++++++++++++++++++ 11 files changed, 138 insertions(+), 43 deletions(-) delete mode 100644 src/FDM/YASim/yasim-atmotest.cpp create mode 100644 test_suite/unit_tests/FDM/testYASimAtmosphere.cxx create mode 100644 test_suite/unit_tests/FDM/testYASimAtmosphere.hxx diff --git a/src/FDM/YASim/Atmosphere.cpp b/src/FDM/YASim/Atmosphere.cpp index dd1649993..b66c27821 100644 --- a/src/FDM/YASim/Atmosphere.cpp +++ b/src/FDM/YASim/Atmosphere.cpp @@ -202,28 +202,4 @@ int Atmosphere::maxTableIndex() { return (sizeof(data) / (numColumns * sizeof(float))) - 1; } -bool Atmosphere::test() { - bool passed = true; - int rows = maxTableIndex() + 1; - const float maxDeviation = 0.0002f; - - fprintf(stderr, "Atmosphere::test()\n"); - fprintf(stderr, "Columns = %d\n", numColumns); - fprintf(stderr, "Rows = %d\n", rows); - - for (int alt = 0; alt <= maxTableIndex(); alt++) { - float density = calcStdDensity(data[alt][PRESSURE], data[alt][TEMPERATURE]); - float delta = data[alt][DENSITY] - density; - fprintf(stderr, "%d : %f \n", alt, delta); - if (Math::abs(delta) > maxDeviation) { - passed = false; - fprintf(stderr,"FAIL: Deviation above limit of %1.6f\n", maxDeviation); - } - } - if (passed) { - fprintf(stderr,"Deviation below %1.6f for all rows.\n", maxDeviation); - } - return passed; -} - }; // namespace yasim diff --git a/src/FDM/YASim/Atmosphere.hpp b/src/FDM/YASim/Atmosphere.hpp index bcac90728..652de3362 100644 --- a/src/FDM/YASim/Atmosphere.hpp +++ b/src/FDM/YASim/Atmosphere.hpp @@ -1,19 +1,24 @@ #ifndef _ATMOSPHERE_HPP #define _ATMOSPHERE_HPP +namespace FGTestApi { namespace PrivateAccessor { namespace FDM { class Accessor; } } } + namespace yasim { //constexpr int Atmosphere::numColumns {4}; class Atmosphere { + friend class ::FGTestApi::PrivateAccessor::FDM::Accessor; + + static const int numColumns {4}; + +public: enum Column { ALTITUDE, TEMPERATURE, PRESSURE, DENSITY }; - static const int numColumns {4}; -public: void setTemperature(float t) { _temperature = t; } void setPressure(float p) { _pressure = p; } void setDensity(float d) { _density = d; } @@ -46,13 +51,13 @@ public: static void calcStaticAir(float p0, float t0, float d0, float v, float* pOut, float* tOut, float* dOut); void calcStaticAir(float v, float* pOut, float* tOut, float* dOut); - static bool test(); - + + static int maxTableIndex(); + private: static float getRecord(float alt, Atmosphere::Column recNum); static float data[][numColumns]; - static int maxTableIndex(); - + float _temperature = 288.11f; float _pressure = 101325.0f; float _density = 1.22500f; diff --git a/src/FDM/YASim/CMakeLists.txt b/src/FDM/YASim/CMakeLists.txt index 87b38b5e6..73d767697 100644 --- a/src/FDM/YASim/CMakeLists.txt +++ b/src/FDM/YASim/CMakeLists.txt @@ -39,7 +39,6 @@ flightgear_component(YASim "${SOURCES}") add_executable(yasim yasim-test.cpp ${COMMON}) add_executable(yasim-proptest proptest.cpp ${COMMON}) -add_executable(yasim-atmotest yasim-atmotest.cpp Atmosphere.cpp ) target_link_libraries(yasim SimGearCore) target_link_libraries(yasim-proptest SimGearCore) diff --git a/src/FDM/YASim/yasim-atmotest.cpp b/src/FDM/YASim/yasim-atmotest.cpp deleted file mode 100644 index 384cbd522..000000000 --- a/src/FDM/YASim/yasim-atmotest.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "Atmosphere.hpp" - -using namespace yasim; - -int main(int argc, char** argv) -{ - Atmosphere a; - if (a.test()) { - return 0; - } - return 1; -} diff --git a/test_suite/CMakeLists.txt b/test_suite/CMakeLists.txt index e20d6933b..e9b73b336 100644 --- a/test_suite/CMakeLists.txt +++ b/test_suite/CMakeLists.txt @@ -76,6 +76,7 @@ add_test(MktimeUnitTests ${TESTSUITE_OUTPUT_DIR}/run_test_suite --ctest -u Mktim add_test(NasalSysUnitTests ${TESTSUITE_OUTPUT_DIR}/run_test_suite --ctest -u NasalSysTests) add_test(NavaidsUnitTests ${TESTSUITE_OUTPUT_DIR}/run_test_suite --ctest -u NavaidsTests) add_test(PosInitUnitTests ${TESTSUITE_OUTPUT_DIR}/run_test_suite --ctest -u PosInitTests) +add_test(YASimAtmosphereUnitTests ${TESTSUITE_OUTPUT_DIR}/run_test_suite --ctest -u YASimAtmosphereTests) # GUI test suites. diff --git a/test_suite/FGTestApi/PrivateAccessorFDM.cxx b/test_suite/FGTestApi/PrivateAccessorFDM.cxx index 40dd3f431..4491edec5 100644 --- a/test_suite/FGTestApi/PrivateAccessorFDM.cxx +++ b/test_suite/FGTestApi/PrivateAccessorFDM.cxx @@ -3,6 +3,8 @@ #include #include #include +#include + // Access variables from src/FDM/AIWake/AIWakeGroup.hxx. @@ -45,3 +47,17 @@ FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_AIWake_WakeMesh_Gamma(WakeMe { return instance->Gamma; } + + +// Access variables from src/FDM/YASim/Atmosphere.hxx. +float +FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_YASim_Atmosphere_numColumns(std::unique_ptr &instance) const +{ + return instance->numColumns; +} + +float +FGTestApi::PrivateAccessor::FDM::Accessor::read_FDM_YASim_Atmosphere_data(std::unique_ptr &instance, int i, int j) const +{ + return instance->data[i][j]; +} diff --git a/test_suite/FGTestApi/PrivateAccessorFDM.hxx b/test_suite/FGTestApi/PrivateAccessorFDM.hxx index ad08b18df..7fb18158a 100644 --- a/test_suite/FGTestApi/PrivateAccessorFDM.hxx +++ b/test_suite/FGTestApi/PrivateAccessorFDM.hxx @@ -2,6 +2,7 @@ #define _FG_PRIVATE_ACCESSOR_FDM_HXX #include +#include #include #include @@ -15,6 +16,11 @@ class WakeMesh; class AeroElement; typedef SGSharedPtr AeroElement_ptr; +// Forward declaration for: src/FDM/YASim. +namespace yasim { +class Atmosphere; +} + namespace FGTestApi { namespace PrivateAccessor { @@ -34,6 +40,10 @@ public: const std::vector read_FDM_AIWake_WakeMesh_elements(WakeMesh* instance) const; int read_FDM_AIWake_WakeMesh_nelm(WakeMesh* instance) const; double **read_FDM_AIWake_WakeMesh_Gamma(WakeMesh* instance) const; + + // Access variables from src/FDM/YASim/Atmosphere.hxx. + float read_FDM_YASim_Atmosphere_numColumns(std::unique_ptr &instance) const; + float read_FDM_YASim_Atmosphere_data(std::unique_ptr &instance, int i, int j) const; }; } // End of namespace FDM. diff --git a/test_suite/unit_tests/FDM/CMakeLists.txt b/test_suite/unit_tests/FDM/CMakeLists.txt index 2fc3a5d42..487e78473 100644 --- a/test_suite/unit_tests/FDM/CMakeLists.txt +++ b/test_suite/unit_tests/FDM/CMakeLists.txt @@ -3,6 +3,7 @@ set(TESTSUITE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx ${CMAKE_CURRENT_SOURCE_DIR}/test_ls_matrix.cxx ${CMAKE_CURRENT_SOURCE_DIR}/testAeroElement.cxx + ${CMAKE_CURRENT_SOURCE_DIR}/testYASimAtmosphere.cxx PARENT_SCOPE ) @@ -10,5 +11,6 @@ set(TESTSUITE_HEADERS ${TESTSUITE_HEADERS} ${CMAKE_CURRENT_SOURCE_DIR}/test_ls_matrix.hxx ${CMAKE_CURRENT_SOURCE_DIR}/testAeroElement.hxx + ${CMAKE_CURRENT_SOURCE_DIR}/testYASimAtmosphere.hxx PARENT_SCOPE ) diff --git a/test_suite/unit_tests/FDM/TestSuite.cxx b/test_suite/unit_tests/FDM/TestSuite.cxx index 5377f2501..5ae265fd4 100644 --- a/test_suite/unit_tests/FDM/TestSuite.cxx +++ b/test_suite/unit_tests/FDM/TestSuite.cxx @@ -19,8 +19,10 @@ #include "test_ls_matrix.hxx" #include "testAeroElement.hxx" +#include "testYASimAtmosphere.hxx" // Set up the unit tests. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(AeroElementTests, "Unit tests"); CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(LaRCSimMatrixTests, "Unit tests"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(YASimAtmosphereTests, "Unit tests"); diff --git a/test_suite/unit_tests/FDM/testYASimAtmosphere.cxx b/test_suite/unit_tests/FDM/testYASimAtmosphere.cxx new file mode 100644 index 000000000..7f3ed60d0 --- /dev/null +++ b/test_suite/unit_tests/FDM/testYASimAtmosphere.cxx @@ -0,0 +1,44 @@ +#include "test_suite/FGTestApi/PrivateAccessorFDM.hxx" + +#include "testYASimAtmosphere.hxx" + +#include + +#include + + +using namespace yasim; + +void YASimAtmosphereTests::setUp() +{ + a.reset(new Atmosphere()); +} + + +void YASimAtmosphereTests::tearDown() +{ + a.reset(); +} + + +void YASimAtmosphereTests::testAtmosphere() +{ + auto accessor = FGTestApi::PrivateAccessor::FDM::Accessor(); + + int numColumns = accessor.read_FDM_YASim_Atmosphere_numColumns(a); + int maxTableIndex = a->maxTableIndex(); + int rows = maxTableIndex + 1; + const float maxDeviation = 0.0002f; + + SG_LOG(SG_GENERAL, SG_INFO, "Columns = " << numColumns); + SG_LOG(SG_GENERAL, SG_INFO, "Rows = " << rows); + + for (int alt = 0; alt <= maxTableIndex; alt++) { + float density = a->calcStdDensity(accessor.read_FDM_YASim_Atmosphere_data(a, alt, a->PRESSURE), accessor.read_FDM_YASim_Atmosphere_data(a, alt, a->TEMPERATURE)); + float delta = accessor.read_FDM_YASim_Atmosphere_data(a, alt, a->DENSITY) - density; + SG_LOG(SG_GENERAL, SG_INFO, "alt: " << alt << ", delta: " << delta); + if (Math::abs(delta) > maxDeviation) + CPPUNIT_FAIL("Deviation above limit of 0.0002"); + } + SG_LOG(SG_GENERAL, SG_INFO, "Deviation below " << maxDeviation << " for all rows."); +} diff --git a/test_suite/unit_tests/FDM/testYASimAtmosphere.hxx b/test_suite/unit_tests/FDM/testYASimAtmosphere.hxx new file mode 100644 index 000000000..26a80329c --- /dev/null +++ b/test_suite/unit_tests/FDM/testYASimAtmosphere.hxx @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2018 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 . + */ + + +#ifndef _FG_YASIM_ATMOSPHERE_UNIT_TESTS_HXX +#define _FG_YASIM_ATMOSPHERE_UNIT_TESTS_HXX + +#include +#include + +#include + + +// The unit tests. +class YASimAtmosphereTests : public CppUnit::TestFixture +{ + // Set up the test suite. + CPPUNIT_TEST_SUITE(YASimAtmosphereTests); + CPPUNIT_TEST(testAtmosphere); + CPPUNIT_TEST_SUITE_END(); + +public: + // Set up function for each test. + void setUp(); + + // Clean up after each test. + void tearDown(); + + // The tests. + void testAtmosphere(); + + // Data. + std::unique_ptr a; +}; + +#endif // _FG_YASIM_ATMOSPHERE_UNIT_TESTS_HXX