Got googletest working in sim/tests

This commit is contained in:
Travis Hunter 2023-02-16 10:59:25 -07:00
parent 0c98dc4f7b
commit af4027e54c
16 changed files with 183 additions and 14 deletions

View File

@ -6,8 +6,19 @@ project(
LANGUAGES CXX) LANGUAGES CXX)
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include_directories(${PROJECT_SOURCE_DIR}/src)
set(CMAKE_CXX_EXTENSIONS OFF) 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) set_property(GLOBAL PROPERTY USE_FOLDERS ON)
@ -25,6 +36,7 @@ enable_testing()
add_subdirectory(src/gui) add_subdirectory(src/gui)
add_subdirectory(src/sim) add_subdirectory(src/sim)
add_subdirectory(src/Utils) add_subdirectory(src/sim/tests)
add_subdirectory(src/Utils/math) add_subdirectory(src/utils)
add_subdirectory(src/utils/math)
add_subdirectory(src/model) add_subdirectory(src/model)

View File

@ -1,6 +0,0 @@
find_package(CURL)
add_library(utils
CurlConnection.cpp)
target_link_libraries(utils curl jsoncpp ssl crypto)

View File

@ -1,3 +0,0 @@
add_library(math
Constants.cpp)

View File

@ -4,4 +4,4 @@ add_library(sim
WindModel.cpp WindModel.cpp
USStandardAtmosphere.cpp) USStandardAtmosphere.cpp)
target_include_directories(sim PRIVATE ${PROJECT_SOURCE_DIR}/src) target_link_libraries(sim PRIVATE math)

View File

@ -1,6 +1,9 @@
#include "USStandardAtmosphere.h" #include "USStandardAtmosphere.h"
#include "Utils/math/Constants.h" #include "utils/math/Constants.h"
#include <cmath>
using namespace utils::math;
namespace sim 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 } // namespace sim

View File

@ -2,6 +2,7 @@
#define SIM_USSTANDARDATMOSPHERE_H #define SIM_USSTANDARDATMOSPHERE_H
#include "AtmosphericModel.h" #include "AtmosphericModel.h"
#include "utils/BinMap.h"
namespace sim namespace sim
{ {
@ -12,6 +13,12 @@ public:
USStandardAtmosphere(); USStandardAtmosphere();
virtual ~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 getDensity(double altitude) override;
double getPressure(double altitude) override; double getPressure(double altitude) override;
double getTemperature(double altitude) override; double getTemperature(double altitude) override;

View File

@ -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)

View File

@ -0,0 +1,26 @@
#include "../USStandardAtmosphere.h"
#include <gtest/gtest.h>
#include <iostream>
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();
}

62
src/utils/BinMap.cpp Normal file
View File

@ -0,0 +1,62 @@
#include "BinMap.h"
#include <algorithm>
#include <stdexcept>
#include <sstream>
#include <fmt/core.h>
// TODO: Check on the availability of this in Clang.
// Replace libfmt with format when LLVM libc++ supports it
//#include <format>
namespace utils
{
BinMap::BinMap()
{
}
BinMap::~BinMap()
{
}
void BinMap::insert(std::pair<double, double>& 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

25
src/utils/BinMap.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef UTILS_MATH_BINMAP_H
#define UTILS_MATH_BINMAP_H
#include <vector>
#include <utility>
namespace utils
{
class BinMap
{
public:
BinMap();
~BinMap();
void insert(std::pair<double, double>& toInsert);
double operator[](double key);
private:
std::vector<std::pair<double, double>> bins;
};
} // namespace utils
#endif // UTILS_MATH_BINMAP_H

9
src/utils/CMakeLists.txt Normal file
View File

@ -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)

View File

@ -0,0 +1,2 @@
add_library(math
Constants.cpp)

View File

@ -10,6 +10,7 @@ namespace Constants
constexpr const double g0 = 9.80665; constexpr const double g0 = 9.80665;
constexpr const double airMolarMass = 0.0289644; constexpr const double airMolarMass = 0.0289644;
constexpr const double standardTemperature = 288.15; constexpr const double standardTemperature = 288.15;
constexpr const double standardDensity = 1.2250;
}; };
} // namespace utils::math } // namespace utils::math