Skip to content

Commit

Permalink
add(tests): adds (wip) helpers and unittest for pose3d_t
Browse files Browse the repository at this point in the history
  • Loading branch information
wpumacay committed Mar 22, 2023
1 parent 0bab3b8 commit d09376e
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.out
*.exe
*.asm
*.backup

# Folder-exclusions
build/
Expand Down
5 changes: 3 additions & 2 deletions tests/cpp/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ Checks: >
modernize-*,
performance-*,
readability-*,
-cppcoreguidelines-avoid-magic-numbers,
-readability-redundant-access-specifiers,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-avoid-magic-numbers,
-readability-magic-numbers,
-readability-function-cognitive-complexity,
-readability-identifier-length,
-readability-function-cognitive-complexity,
WarningsAsErrors: ''
FormatStyle: none
Expand Down
2 changes: 2 additions & 0 deletions tests/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ add_executable(
${CMAKE_CURRENT_SOURCE_DIR}/test_quat_type.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_quat_operations.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_euler_type.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_pose3d_type.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_mat2_functions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_mat3_functions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_mat4_functions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_utils_spherical_coordinates.cpp)
target_link_libraries(MathCppTests PRIVATE math::math Catch2::Catch2)
target_include_directories(MathCppTests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
# Discover tets and pick an integer as the random seed
catch_discover_tests(MathCppTests)
121 changes: 121 additions & 0 deletions tests/cpp/common_math_generators.hpp
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
44 changes: 44 additions & 0 deletions tests/cpp/test_pose3d_type.cpp
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

0 comments on commit d09376e

Please sign in to comment.