-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add(tests): adds (wip) helpers and unittest for pose3d_t
- Loading branch information
Showing
5 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
*.out | ||
*.exe | ||
*.asm | ||
*.backup | ||
|
||
# Folder-exclusions | ||
build/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include <catch2/catch.hpp> | ||
#include <math/vec2_t.hpp> | ||
#include <math/vec3_t.hpp> | ||
#include <math/vec4_t.hpp> | ||
#include <math/mat3_t.hpp> | ||
#include <math/quat_t.hpp> | ||
|
||
#include <random> | ||
|
||
namespace math { | ||
|
||
template <typename T, typename V> | ||
class RandomValueBase : public Catch::Generators::IGenerator<V> { | ||
public: | ||
RandomValueBase(T val_range_min, T val_range_max) | ||
: m_Dist(val_range_min, val_range_max), // NOLINT | ||
m_Gen(std::random_device{}()) {} // NOLINT | ||
|
||
auto get() const -> const V& override { return m_Value; } | ||
|
||
protected: | ||
/// Distribution from which to generate random real values | ||
std::uniform_real_distribution<T> m_Dist; | ||
/// The method used to generate random numbers | ||
std::minstd_rand m_Gen; | ||
/// The vector value to be exposed | ||
V m_Value; | ||
}; | ||
|
||
//****************************************************************************// | ||
// Generators for Vector types // | ||
//****************************************************************************// | ||
|
||
template <typename T> | ||
class RandomVec2Generator : public RandomValueBase<T, Vector2<T>> { | ||
public: | ||
RandomVec2Generator(T val_range_min, T val_range_max) | ||
: RandomValueBase<T, Vector2<T>>(val_range_min, val_range_max) {} | ||
|
||
auto next() -> bool override { | ||
this->m_value = {this->m_Dist(this->m_Gen), this->m_Dist(this->m_Gen)}; | ||
return true; | ||
} | ||
}; | ||
|
||
template <typename T> | ||
class RandomVec3Generator : public RandomValueBase<T, Vector3<T>> { | ||
public: | ||
RandomVec3Generator(T val_range_min, T val_range_max) | ||
: RandomValueBase<T, Vector3<T>>(val_range_min, val_range_max) {} | ||
|
||
auto next() -> bool override { | ||
this->m_Value = {this->m_Dist(this->m_Gen), this->m_Dist(this->m_Gen), | ||
this->m_Dist(this->m_Gen)}; | ||
return true; | ||
} | ||
}; | ||
|
||
template <typename T> | ||
class RandomVec4Generator : public RandomValueBase<T, Vector4<T>> { | ||
public: | ||
RandomVec4Generator(T val_range_min, T val_range_max) | ||
: RandomValueBase<T, Vector4<T>>(val_range_min, val_range_max) {} | ||
|
||
auto next() -> bool override { | ||
this->m_Value = {this->m_Dist(this->m_Gen), this->m_Dist(this->m_Gen), | ||
this->m_Dist(this->m_Gen), this->m_Dist(this->m_Gen)}; | ||
return true; | ||
} | ||
}; | ||
|
||
template <typename T> | ||
auto random_vec2(T val_range_min = static_cast<T>(-1.0), | ||
T val_range_max = static_cast<T>(1.0)) | ||
-> Catch::Generators::GeneratorWrapper<Vector2<T>> { | ||
return Catch::Generators::GeneratorWrapper<Vector2<T>>( | ||
Catch::Generators::pf::make_unique<RandomVec2Generator<T>>( | ||
val_range_min, val_range_max)); | ||
} | ||
|
||
template <typename T> | ||
auto random_vec3(T val_range_min = static_cast<T>(-1.0), | ||
T val_range_max = static_cast<T>(1.0)) | ||
-> Catch::Generators::GeneratorWrapper<Vector3<T>> { | ||
return Catch::Generators::GeneratorWrapper<Vector3<T>>( | ||
Catch::Generators::pf::make_unique<RandomVec3Generator<T>>( | ||
val_range_min, val_range_max)); | ||
} | ||
|
||
template <typename T> | ||
auto random_vec4(T val_range_min = static_cast<T>(-1.0), | ||
T val_range_max = static_cast<T>(1.0)) | ||
-> Catch::Generators::GeneratorWrapper<Vector4<T>> { | ||
return Catch::Generators::GeneratorWrapper<Vector4<T>>( | ||
Catch::Generators::pf::make_unique<RandomVec4Generator<T>>( | ||
val_range_min, val_range_max)); | ||
} | ||
|
||
//****************************************************************************// | ||
// Generators for Quaternion type // | ||
//****************************************************************************// | ||
|
||
template <typename T> | ||
class RandomQuaternion : public RandomValueBase<T, Quaternion<T>> { | ||
public: | ||
RandomQuaternion() : RandomValueBase<T, Quaternion<T>>(-1.0, 1.0) {} | ||
|
||
auto next() -> bool override { | ||
this->m_Value = {this->m_Dist(this->m_Gen), this->m_Dist(this->m_Gen), | ||
this->m_Dist(this->m_Gen), this->m_Dist(this->m_Gen)}; | ||
return true; | ||
} | ||
}; | ||
|
||
template <typename T> | ||
auto random_quaternion() -> Catch::Generators::GeneratorWrapper<Quaternion<T>> { | ||
return Catch::Generators::GeneratorWrapper<Quaternion<T>>( | ||
Catch::Generators::pf::make_unique<RandomQuaternion<T>>()); | ||
} | ||
|
||
} // namespace math |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <cmath> | ||
#include <random> | ||
|
||
#include <catch2/catch.hpp> | ||
#include <math/pose3d_t.hpp> | ||
|
||
#include <common_math_generators.hpp> | ||
|
||
#if defined(__clang__) | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wimplicit-float-conversion" | ||
#pragma clang diagnostic ignored "-Wdouble-promotion" | ||
#endif | ||
|
||
// NOLINTNEXTLINE | ||
TEMPLATE_TEST_CASE("Pose3d class (pose3d_t) constructors", | ||
"[pose3d_t][template", ::math::float32_t, | ||
::math::float64_t) { | ||
using T = TestType; | ||
using Pose = ::math::Pose3d<T>; | ||
using Quat = ::math::Quaternion<T>; | ||
using Vec3 = ::math::Vector3<T>; | ||
|
||
SECTION("Default constructor") { | ||
Pose X; // NOLINT | ||
REQUIRE(X.position == Vec3(0.0, 0.0, 0.0)); | ||
REQUIRE(X.orientation == Quat(1.0, 0.0, 0.0, 0.0)); | ||
} | ||
|
||
SECTION("From position(Vec3) and orientation(Quat)") { | ||
auto position = GENERATE(take(10, ::math::random_vec3<T>(-10.0, 10.0))); | ||
auto orientation = GENERATE(take(10, ::math::random_quaternion<T>())); | ||
|
||
Pose X(position, orientation); // NOLINT | ||
// Position should be copied directly to the pose object | ||
REQUIRE(X.position == position); | ||
// Orientation normalized to ensure it represents a rotation | ||
REQUIRE(std::abs(X.orientation.length() - 1.0) < 1e-5); | ||
} | ||
} | ||
|
||
#if defined(__clang__) | ||
#pragma clang diagnostic pop // NOLINT | ||
#endif |