From af4027e54c01105f99845a6e99b218ddb002c3ed Mon Sep 17 00:00:00 2001 From: Travis Hunter Date: Thu, 16 Feb 2023 10:59:25 -0700 Subject: [PATCH] Got googletest working in sim/tests --- CMakeLists.txt | 18 +++++- src/Utils/CMakeLists.txt | 6 -- src/Utils/math/CMakeLists.txt | 3 - src/sim/CMakeLists.txt | 2 +- src/sim/USStandardAtmosphere.cpp | 17 +++++- src/sim/USStandardAtmosphere.h | 7 +++ src/sim/tests/CMakeLists.txt | 19 +++++++ src/sim/tests/USStandardAtmosphereTests.cpp | 26 +++++++++ src/utils/BinMap.cpp | 62 +++++++++++++++++++++ src/utils/BinMap.h | 25 +++++++++ src/utils/CMakeLists.txt | 9 +++ src/{Utils => utils}/CurlConnection.cpp | 0 src/{Utils => utils}/CurlConnection.h | 0 src/utils/math/CMakeLists.txt | 2 + src/{Utils => utils}/math/Constants.cpp | 0 src/{Utils => utils}/math/Constants.h | 1 + 16 files changed, 183 insertions(+), 14 deletions(-) delete mode 100644 src/Utils/CMakeLists.txt delete mode 100644 src/Utils/math/CMakeLists.txt create mode 100644 src/sim/tests/CMakeLists.txt create mode 100644 src/sim/tests/USStandardAtmosphereTests.cpp create mode 100644 src/utils/BinMap.cpp create mode 100644 src/utils/BinMap.h create mode 100644 src/utils/CMakeLists.txt rename src/{Utils => utils}/CurlConnection.cpp (100%) rename src/{Utils => utils}/CurlConnection.h (100%) create mode 100644 src/utils/math/CMakeLists.txt rename src/{Utils => utils}/math/Constants.cpp (100%) rename src/{Utils => utils}/math/Constants.h (79%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c3824ff..f3ee03f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,19 @@ project( LANGUAGES CXX) if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + include_directories(${PROJECT_SOURCE_DIR}/src) set(CMAKE_CXX_EXTENSIONS OFF) - set(CMAKE_CXX_STANDARD 17) + set(CMAKE_CXX_STANDARD 20) + + # Need to set specific options for specific compilers here. + if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + # No special flags yet + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + # No special flags yet + + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + # Not sure... Anyone that uses MSVC can help here + endif() set_property(GLOBAL PROPERTY USE_FOLDERS ON) @@ -25,6 +36,7 @@ enable_testing() add_subdirectory(src/gui) add_subdirectory(src/sim) -add_subdirectory(src/Utils) -add_subdirectory(src/Utils/math) +add_subdirectory(src/sim/tests) +add_subdirectory(src/utils) +add_subdirectory(src/utils/math) add_subdirectory(src/model) diff --git a/src/Utils/CMakeLists.txt b/src/Utils/CMakeLists.txt deleted file mode 100644 index 880bbe8..0000000 --- a/src/Utils/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -find_package(CURL) - -add_library(utils - CurlConnection.cpp) - -target_link_libraries(utils curl jsoncpp ssl crypto) diff --git a/src/Utils/math/CMakeLists.txt b/src/Utils/math/CMakeLists.txt deleted file mode 100644 index 6156c19..0000000 --- a/src/Utils/math/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -add_library(math - Constants.cpp) - diff --git a/src/sim/CMakeLists.txt b/src/sim/CMakeLists.txt index 7706fbf..f28a4ec 100644 --- a/src/sim/CMakeLists.txt +++ b/src/sim/CMakeLists.txt @@ -4,4 +4,4 @@ add_library(sim WindModel.cpp USStandardAtmosphere.cpp) -target_include_directories(sim PRIVATE ${PROJECT_SOURCE_DIR}/src) +target_link_libraries(sim PRIVATE math) diff --git a/src/sim/USStandardAtmosphere.cpp b/src/sim/USStandardAtmosphere.cpp index c87a52a..b83a96a 100644 --- a/src/sim/USStandardAtmosphere.cpp +++ b/src/sim/USStandardAtmosphere.cpp @@ -1,6 +1,9 @@ #include "USStandardAtmosphere.h" -#include "Utils/math/Constants.h" +#include "utils/math/Constants.h" +#include + +using namespace utils::math; namespace sim { @@ -15,5 +18,17 @@ USStandardAtmosphere::~USStandardAtmosphere() } +double USStandardAtmosphere::getDensity(double altitude) +{ + return Constants::standardDensity * std::exp((-Constants::g0 * Constants::airMolarMass * altitude) / (Constants::Rstar * Constants::standardTemperature)); +} +double USStandardAtmosphere::getTemperature(double altitude) +{ + return 0.0; +} +double USStandardAtmosphere::getPressure(double altitude) +{ + return 0.0; +} } // namespace sim \ No newline at end of file diff --git a/src/sim/USStandardAtmosphere.h b/src/sim/USStandardAtmosphere.h index 172b42b..b35eb58 100644 --- a/src/sim/USStandardAtmosphere.h +++ b/src/sim/USStandardAtmosphere.h @@ -2,6 +2,7 @@ #define SIM_USSTANDARDATMOSPHERE_H #include "AtmosphericModel.h" +#include "utils/BinMap.h" namespace sim { @@ -12,6 +13,12 @@ public: USStandardAtmosphere(); virtual ~USStandardAtmosphere(); + /** + * @brief Get the density of the air at a given altitude above mean sea level + * + * @param altitude the altitude above sea level + * @return the density in kg/m^3 + */ double getDensity(double altitude) override; double getPressure(double altitude) override; double getTemperature(double altitude) override; diff --git a/src/sim/tests/CMakeLists.txt b/src/sim/tests/CMakeLists.txt new file mode 100644 index 0000000..45db129 --- /dev/null +++ b/src/sim/tests/CMakeLists.txt @@ -0,0 +1,19 @@ +include(FetchContent) + +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.11.0 +) + +FetchContent_MakeAvailable(googletest) +add_library(GTest::GTest INTERFACE IMPORTED) +target_link_libraries(GTest::GTest INTERFACE gtest_main) + +add_executable(usstandardatmosphere_test USStandardAtmosphereTests.cpp) +target_link_libraries(usstandardatmosphere_test + PRIVATE + GTest::GTest + sim) + +add_test(sim_tests usstandardatmosphere_test) \ No newline at end of file diff --git a/src/sim/tests/USStandardAtmosphereTests.cpp b/src/sim/tests/USStandardAtmosphereTests.cpp new file mode 100644 index 0000000..1a6117d --- /dev/null +++ b/src/sim/tests/USStandardAtmosphereTests.cpp @@ -0,0 +1,26 @@ +#include "../USStandardAtmosphere.h" +#include + +#include + +TEST(USStandardAtmosphereTests, DensityTests) +{ + sim::USStandardAtmosphere atm; + double seaLevelDensity = 1.2250; + double testDensity = atm.getDensity(0.0); + ASSERT_EQ(seaLevelDensity, testDensity); + + // Test some other values + for(int i = 1; i <= 10; ++i) + { + double altitude = 1000.0 * i; + std::cout << altitude << ": " << atm.getDensity(altitude) << std::endl; + } + +} + +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/src/utils/BinMap.cpp b/src/utils/BinMap.cpp new file mode 100644 index 0000000..e8e79ec --- /dev/null +++ b/src/utils/BinMap.cpp @@ -0,0 +1,62 @@ +#include "BinMap.h" + +#include + +#include +#include + +#include + +// TODO: Check on the availability of this in Clang. +// Replace libfmt with format when LLVM libc++ supports it +//#include + +namespace utils +{ + +BinMap::BinMap() +{ + +} + +BinMap::~BinMap() +{ + +} + +void BinMap::insert(std::pair& toInsert) +{ + bins.push_back(toInsert); + std::sort(bins.begin(), bins.end(), + [](const auto& a, const auto& b){ return a.first < b.first; }); +} + +double BinMap::operator[](double key) +{ + auto iter = bins.begin(); + // If the key is less than the lowest bin value, then it is out of range + // This should be an error. It's also possible to interpret this as simply + // the lowest bin, but I think that invites a logic error so we'll throw + // instead + if(key < iter->first) + { + throw std::out_of_range( + fmt::format("{} less than lower bound {} of BinMap", key, iter->first)); + } + // Increment it and start searching If we reach the end without finding an existing key + // greater than our search term, then we've just hit the last bin and return that + iter++; + double retVal = bins.end()->second; + while(iter != bins.end()) + { + if(key < iter->first) + { + retVal = std::prev(iter)->second; + break; + } + iter++; + } + return retVal; +} + +} // namespace utils \ No newline at end of file diff --git a/src/utils/BinMap.h b/src/utils/BinMap.h new file mode 100644 index 0000000..035e5a9 --- /dev/null +++ b/src/utils/BinMap.h @@ -0,0 +1,25 @@ +#ifndef UTILS_MATH_BINMAP_H +#define UTILS_MATH_BINMAP_H + +#include +#include + +namespace utils +{ +class BinMap +{ +public: + BinMap(); + ~BinMap(); + + void insert(std::pair& toInsert); + double operator[](double key); + +private: + std::vector> bins; + +}; + +} // namespace utils + +#endif // UTILS_MATH_BINMAP_H \ No newline at end of file diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt new file mode 100644 index 0000000..7c8c7ea --- /dev/null +++ b/src/utils/CMakeLists.txt @@ -0,0 +1,9 @@ +find_package(CURL) +find_package(fmt) + +add_library(utils + BinMap.cpp + CurlConnection.cpp) + +# Also including the header-only fmt library +target_link_libraries(utils PRIVATE curl jsoncpp ssl crypto fmt::fmt-header-only) diff --git a/src/Utils/CurlConnection.cpp b/src/utils/CurlConnection.cpp similarity index 100% rename from src/Utils/CurlConnection.cpp rename to src/utils/CurlConnection.cpp diff --git a/src/Utils/CurlConnection.h b/src/utils/CurlConnection.h similarity index 100% rename from src/Utils/CurlConnection.h rename to src/utils/CurlConnection.h diff --git a/src/utils/math/CMakeLists.txt b/src/utils/math/CMakeLists.txt new file mode 100644 index 0000000..23c2f13 --- /dev/null +++ b/src/utils/math/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(math + Constants.cpp) \ No newline at end of file diff --git a/src/Utils/math/Constants.cpp b/src/utils/math/Constants.cpp similarity index 100% rename from src/Utils/math/Constants.cpp rename to src/utils/math/Constants.cpp diff --git a/src/Utils/math/Constants.h b/src/utils/math/Constants.h similarity index 79% rename from src/Utils/math/Constants.h rename to src/utils/math/Constants.h index e92fd1c..6340c3c 100644 --- a/src/Utils/math/Constants.h +++ b/src/utils/math/Constants.h @@ -10,6 +10,7 @@ namespace Constants constexpr const double g0 = 9.80665; constexpr const double airMolarMass = 0.0289644; constexpr const double standardTemperature = 288.15; + constexpr const double standardDensity = 1.2250; }; } // namespace utils::math