Skip to content

Commit

Permalink
feat: add tests for profile::models::Tabulated (#339)
Browse files Browse the repository at this point in the history
* refactor!: remove profile state and use trajectory state

* feat: fix tests

* feat: add tests for Flight::Profile::Models::Tabulated

* feat: fix tests

* feat: fix tests

* fix: tests

* Apply suggestions from code review

Co-authored-by: Antoine Paletta <98616558+apaletta3@users.noreply.github.com>

* feat: address remaining feedback

---------

Co-authored-by: Vishwa Shah <vishwa@users-MacBook-Pro.local>
Co-authored-by: Antoine Paletta <98616558+apaletta3@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 14, 2024
1 parent 6b65f18 commit 7cb2eff
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Tabulated : public virtual Model

bool operator==(const Tabulated& aTabulatedModel) const;

bool operator!=(const Tabulated& aTabulatedModel) const;

friend std::ostream& operator<<(std::ostream& anOutputStream, const Tabulated& aTabulatedModel);

virtual bool isDefined() const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ bool Tabulated::operator==(const Tabulated& aTabulatedModel) const
return this->states_ == aTabulatedModel.states_;
}

bool Tabulated::operator!=(const Tabulated& aTabulatedModel) const
{
return !((*this) == aTabulatedModel);
}

std::ostream& operator<<(std::ostream& anOutputStream, const Tabulated& aTabulatedModel)
{
aTabulatedModel.print(anOutputStream);
Expand Down
40 changes: 39 additions & 1 deletion test/Global.test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@

#include <gtest/gtest.h>

#define EXPECT_VECTORS_ALMOST_EQUAL(v1, v2, tolerance) \
do \
{ \
if ((v1).size() != (v2).size()) \
{ \
ADD_FAILURE() << "Vectors have different sizes."; \
} \
else \
{ \
for (int i = 0; i < (v1).size(); ++i) \
{ \
if (std::abs((v1)(i) - (v2)(i)) > (tolerance)) \
{ \
ADD_FAILURE() << "Vectors differ at index " << i << ". Values: " << (v1)(i) << " vs. " << (v2)(i); \
} \
} \
} \
} while (0)

#define ASSERT_VECTORS_ALMOST_EQUAL(v1, v2, tolerance) \
do \
{ \
if ((v1).size() != (v2).size()) \
{ \
FAIL() << "Vectors have different sizes."; \
} \
else \
{ \
for (int i = 0; i < (v1).size(); ++i) \
{ \
if (std::abs((v1)(i) - (v2)(i)) > (tolerance)) \
{ \
FAIL() << "Vectors differ at index " << i << ". Values: " << (v1)(i) << " vs. " << (v2)(i); \
} \
} \
} \
} while (0)

namespace ostk
{
namespace astrodynamics
Expand All @@ -13,7 +51,7 @@ namespace global

extern std::string someGlobalString;

}
} // namespace global
} // namespace test
} // namespace astrodynamics
} // namespace ostk
198 changes: 198 additions & 0 deletions test/OpenSpaceToolkit/Astrodynamics/Flight/Profile/Tabulated.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/// Apache License 2.0

#include <OpenSpaceToolkit/Astrodynamics/Flight/Profile/Model/Tabulated.hpp>
#include <OpenSpaceToolkit/Astrodynamics/Trajectory/State.hpp>

#include <Global.test.hpp>

using ostk::core::container::Array;
using ostk::core::container::String;

using ostk::mathematics::object::Vector3d;
using ostk::mathematics::object::VectorXd;
using ostk::mathematics::geometry::d3::transformation::rotation::Quaternion;

using ostk::physics::time::Interval;
using ostk::physics::time::Instant;
using ostk::physics::time::DateTime;
using ostk::physics::time::Scale;
using ostk::physics::coordinate::Position;
using ostk::physics::coordinate::Velocity;
using ostk::physics::coordinate::Frame;

using ostk::astrodynamics::trajectory::State;
using ostk::astrodynamics::flight::profile::model::Tabulated;

class OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated : public ::testing::Test
{
protected:
void SetUp() override
{
{
const Instant instant = Instant::DateTime(DateTime(2024, 1, 29, 0, 0, 0), Scale::UTC);
const Position position =
Position::Meters({755972.142139276024, -3390511.949699319433, 5955672.751532567665}, Frame::GCRF());
const Velocity velocity =
Velocity::MetersPerSecond({-563.764594800880, -6619.592151780337, -3685.668514834143}, Frame::GCRF());
const Quaternion attitude = {
-0.638160707740, -0.163520830523, 0.726693549038, 0.194751982966, Quaternion::Format::XYZS
};
const Vector3d angularVelocity = {0.0, 0.0, 0.0};
states_.add({instant, position, velocity, attitude, angularVelocity, Frame::GCRF()});
}

{
const Instant instant = Instant::DateTime(DateTime(2024, 1, 29, 0, 0, 30), Scale::UTC);
const Position position =
Position::Meters({738650.069144404028, -3587215.226667015813, 5841866.765430007130}, Frame::GCRF());
const Velocity velocity =
Velocity::MetersPerSecond({-590.931279522597, -6492.770161294917, -3900.690931064452}, Frame::GCRF());
const Quaternion attitude = {
-0.635371915567, -0.174049521049, 0.723374654124, 0.206732684649, Quaternion::Format::XYZS
};
const Vector3d angularVelocity = {0.0, 0.0, 0.0};
states_.add({instant, position, velocity, attitude, angularVelocity, Frame::GCRF()});
}

tabulated_ = {states_};
}

Array<State> states_ = Array<State>::Empty();
Tabulated tabulated_ = {states_};
};

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated, Constructor)
{
{
EXPECT_NO_THROW(Tabulated tabulated(states_););
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated, EqualToOperator)
{
{
EXPECT_TRUE(tabulated_ == tabulated_);
}

{
const Array<State> statesSubset = {states_[0]};
const Tabulated tabulatedSubset = {statesSubset};
EXPECT_FALSE(tabulated_ == tabulatedSubset);
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated, NotEqualToOperator)
{
{
EXPECT_FALSE(tabulated_ != tabulated_);
}

{
const Array<State> statesSubset = {states_[0]};
const Tabulated tabulatedSubset = {statesSubset};
EXPECT_TRUE(tabulated_ != tabulatedSubset);
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated, StreamOperator)
{
{
testing::internal::CaptureStdout();

EXPECT_NO_THROW(std::cout << tabulated_ << std::endl);

EXPECT_FALSE(testing::internal::GetCapturedStdout().empty());
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated, Print)
{
testing::internal::CaptureStdout();

EXPECT_NO_THROW(tabulated_.print(std::cout, true));
EXPECT_NO_THROW(tabulated_.print(std::cout, false));

EXPECT_FALSE(testing::internal::GetCapturedStdout().empty());
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated, IsDefined)
{
{
EXPECT_TRUE(tabulated_.isDefined());
}

{
const Tabulated tabulated = {{}};
EXPECT_FALSE(tabulated.isDefined());
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated, Getters)
{
{
const Interval interval = tabulated_.getInterval();
EXPECT_EQ(interval.getStart(), states_.accessFirst().accessInstant());
EXPECT_EQ(interval.getEnd(), states_.accessLast().accessInstant());
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated, CalculateStateAt)
{
{
const Instant instant = Instant::DateTime(DateTime(2024, 1, 29, 0, 0, 15), Scale::UTC);

const State state = tabulated_.calculateStateAt(instant);

EXPECT_EQ(state.getInstant(), instant);

const Vector3d positionCoordinates = {747311.105641840026, -3488863.588183167391, 5898769.758481287397};
EXPECT_VECTORS_ALMOST_EQUAL(state.getPosition().accessCoordinates(), positionCoordinates, 1e-10);

const Vector3d velocityCoordinates = {-577.347937161739, -6556.181156537627, -3793.179722949298};
EXPECT_VECTORS_ALMOST_EQUAL(state.getVelocity().accessCoordinates(), velocityCoordinates, 1e-10);

VectorXd attitudeVector(4);
attitudeVector << -0.636788056981, -0.168790939736, 0.725058861220, 0.200749189082;
EXPECT_VECTORS_ALMOST_EQUAL(state.getAttitude().toVector(Quaternion::Format::XYZS), attitudeVector, 1e-10);

const Vector3d angularVelocity = {0.0, 0.0, 0.0};
EXPECT_VECTORS_ALMOST_EQUAL(state.getAngularVelocity(), angularVelocity, 1e-10);
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated, getAxesAt)
{
// undefined
{
const Tabulated undefinedTabulated = {{}};
EXPECT_THROW(
undefinedTabulated.getAxesAt(states_.accessFirst().accessInstant()), ostk::core::error::runtime::Undefined
);
}

// instant undefined
{
const Instant instant = Instant::Undefined();
EXPECT_THROW(tabulated_.getAxesAt(instant), ostk::core::error::runtime::Undefined);
}

// not yet implemented
{
EXPECT_THROW(
tabulated_.getAxesAt(states_.accessFirst().accessInstant()), ostk::core::error::runtime::ToBeImplemented
);
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile_Models_Tabulated, getBodyFrame)
{
// instant undefined
{
const String frameName = String::Empty();
EXPECT_THROW(tabulated_.getBodyFrame(frameName), ostk::core::error::runtime::Undefined);
}

{
EXPECT_THROW(tabulated_.getBodyFrame("Body Frame Name"), ostk::core::error::runtime::ToBeImplemented);
}
}

0 comments on commit 7cb2eff

Please sign in to comment.