From 9517b5b68227ae6cf3ef6f6cf216b46e35121a6b Mon Sep 17 00:00:00 2001 From: Travis Hunter Date: Fri, 20 Oct 2023 17:03:52 -0600 Subject: [PATCH] Added first model tests. Super basic Part test --- model/CMakeLists.txt | 3 ++ model/Part.cpp | 26 ++++++++++++- model/Part.h | 11 +++++- model/Stage.h | 10 ++++- model/tests/CMakeLists.txt | 15 +++++++ model/tests/PartTests.cpp | 80 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 model/tests/CMakeLists.txt create mode 100644 model/tests/PartTests.cpp diff --git a/model/CMakeLists.txt b/model/CMakeLists.txt index c943867..d3a8f14 100644 --- a/model/CMakeLists.txt +++ b/model/CMakeLists.txt @@ -16,3 +16,6 @@ add_library(model target_link_libraries(model PRIVATE utils) + +# Unit tests +add_subdirectory(tests) diff --git a/model/Part.cpp b/model/Part.cpp index be05350..8cae619 100644 --- a/model/Part.cpp +++ b/model/Part.cpp @@ -4,8 +4,30 @@ namespace model { -Part::Part() -{} +Part::Part(const std::string& n, + const Matrix3& I, + double m, + const Vector3& centerMass, + double l, + double inRadTop, + double outRadTop, + double inRadBottom, + double outRadBottom) + : parent(nullptr), + name(n), + inertiaTensor(I), + compositeInertiaTensor(I), + mass(m), + compositeMass(m), + cm(centerMass), + length(l), + innerRadiusTop(inRadTop), + outerRadiusTop(outRadTop), + innerRadiusBottom(inRadBottom), + outerRadiusBottom(outRadBottom), + needsRecomputing(false), + childParts() +{ } Part::~Part() {} diff --git a/model/Part.h b/model/Part.h index 7926c21..03da6bf 100644 --- a/model/Part.h +++ b/model/Part.h @@ -21,7 +21,16 @@ namespace model class Part { public: - Part(); + Part(const std::string& name, + const Matrix3& I, + double m, + const Vector3& centerMass, + double length, + double inRadTop, + double outRadTop, + double inRadBottom, + double outRadBottom); + virtual ~Part(); Part(const Part&); diff --git a/model/Stage.h b/model/Stage.h index c3df69f..f215674 100644 --- a/model/Stage.h +++ b/model/Stage.h @@ -5,6 +5,7 @@ // C headers // C++ headers #include +#include // 3rd party headers /// \endcond @@ -37,7 +38,12 @@ public: virtual double getMass(double t) override { - return topPart.getCompositeMass(t) + mm.getMass(t); + if(topPart) + { + return topPart->getCompositeMass(t) + mm.getMass(t); + } + else + return 0.0; } virtual double getDragCoefficient() override { return 1.0 ;} @@ -58,7 +64,7 @@ public: private: std::string name; - Part topPart; + std::shared_ptr topPart; model::MotorModel mm; Vector3 motorModelPosition; // position of motor cg w.r.t. the stage c.g. diff --git a/model/tests/CMakeLists.txt b/model/tests/CMakeLists.txt new file mode 100644 index 0000000..5ff53a7 --- /dev/null +++ b/model/tests/CMakeLists.txt @@ -0,0 +1,15 @@ +enable_testing() + +add_executable(model_tests + PartTests.cpp +) + +target_link_libraries(model_tests PRIVATE + model + utils + GTest::gtest_main +) + +include(GoogleTest) +gtest_discover_tests(model_tests) + diff --git a/model/tests/PartTests.cpp b/model/tests/PartTests.cpp new file mode 100644 index 0000000..f985d3a --- /dev/null +++ b/model/tests/PartTests.cpp @@ -0,0 +1,80 @@ +#include + +#include "model/Part.h" + +class PartTest : public testing::Test +{ +protected: + // Per-test-suite set-up. + // Called before the first test in this test suite. + // Can be omitted if not needed. + static void SetUpTestSuite() + { + //shared_resource_ = new ...; + + // If `shared_resource_` is **not deleted** in `TearDownTestSuite()`, + // reallocation should be prevented because `SetUpTestSuite()` may be called + // in subclasses of FooTest and lead to memory leak. + // + // if (shared_resource_ == nullptr) { + // shared_resource_ = new ...; + // } + } + + // Per-test-suite tear-down. + // Called after the last test in this test suite. + // Can be omitted if not needed. + static void TearDownTestSuite() + { + //delete shared_resource_; + //shared_resource_ = nullptr; + } + + // You can define per-test set-up logic as usual. + void SetUp() override { } + + // You can define per-test tear-down logic as usual. + void TearDown() override { } + + // Some expensive resource shared by all tests. + //static T* shared_resource_; +}; + +//T* FooTest::shared_resource_ = nullptr; + +TEST(PartTest, CreationTests) +{ + Matrix3 inertia; + inertia << 1, 0, 0, + 0, 1, 0, + 0, 0, 1; + Vector3 cm{1, 0, 0}; + model::Part testPart("testPart", + inertia, + 1.0, + cm, + 2.0, + 1.0, + 1.1, + 1.1, + 1.0); + + Matrix3 inertia2; + inertia2 << 1, 0, 0, + 0, 1, 0, + 0, 0, 1; + Vector3 cm2{1, 0, 0}; + model::Part testPart2("testPart2", + inertia2, + 1.0, + cm2, + 2.0, + 1.0, + 1.1, + 1.1, + 1.0); + Vector3 R{2.0, 2.0, 2.0}; + testPart.addChildPart(testPart2, R); + + +} \ No newline at end of file