From c2741b175f0a4cdc2d7fd6cdb62447bf6eb3b181 Mon Sep 17 00:00:00 2001 From: gregor Date: Fri, 21 Jul 2023 21:07:17 -0500 Subject: [PATCH] (chore): updating mat3 tests (WIP) --- tests/cpp/common_math_helpers.hpp | 50 +++++++++- tests/cpp/test_mat3_type.cpp | 156 +++++++++++++----------------- 2 files changed, 118 insertions(+), 88 deletions(-) diff --git a/tests/cpp/common_math_helpers.hpp b/tests/cpp/common_math_helpers.hpp index 8327f06..d37dc08 100644 --- a/tests/cpp/common_math_helpers.hpp +++ b/tests/cpp/common_math_helpers.hpp @@ -43,8 +43,10 @@ constexpr auto func_all_close(const ::math::Vector4& vec, T x, T y, T z, T w, func_value_close(vec.w(), w, eps); } +// clang-format off template -constexpr auto func_all_close(const ::math::Matrix2& mat, T x00, T x01, +constexpr auto func_all_close(const ::math::Matrix2& mat, + T x00, T x01, T x10, T x11, T eps) -> bool { return func_value_close(mat(0, 0), x00, eps) && func_value_close(mat(0, 1), x01, eps) && @@ -52,4 +54,50 @@ constexpr auto func_all_close(const ::math::Matrix2& mat, T x00, T x01, func_value_close(mat(1, 1), x11, eps); } +template +constexpr auto func_all_close(const ::math::Matrix3& mat, + T x00, T x01, T x02, + T x10, T x11, T x12, + T x20, T x21, T x22, T eps) -> bool { + return func_value_close(mat(0, 0), x00, eps) && + func_value_close(mat(0, 1), x01, eps) && + func_value_close(mat(0, 2), x02, eps) && + + func_value_close(mat(1, 0), x10, eps) && + func_value_close(mat(1, 1), x11, eps) && + func_value_close(mat(1, 2), x12, eps) && + + func_value_close(mat(2, 0), x20, eps) && + func_value_close(mat(2, 1), x21, eps) && + func_value_close(mat(2, 2), x22, eps); +} + +template +constexpr auto func_all_close(const ::math::Matrix4& mat, + T x00, T x01, T x02, T x03, + T x10, T x11, T x12, T x13, + T x20, T x21, T x22, T x23, + T x30, T x31, T x32, T x33, T eps) -> bool { + return func_value_close(mat(0, 0), x00, eps) && + func_value_close(mat(0, 1), x01, eps) && + func_value_close(mat(0, 2), x02, eps) && + func_value_close(mat(0, 3), x03, eps) && + + func_value_close(mat(1, 0), x10, eps) && + func_value_close(mat(1, 1), x11, eps) && + func_value_close(mat(1, 2), x12, eps) && + func_value_close(mat(1, 3), x13, eps) && + + func_value_close(mat(2, 0), x20, eps) && + func_value_close(mat(2, 1), x21, eps) && + func_value_close(mat(2, 2), x22, eps) && + func_value_close(mat(2, 3), x23, eps) && + + func_value_close(mat(3, 0), x30, eps) && + func_value_close(mat(3, 1), x31, eps) && + func_value_close(mat(3, 2), x32, eps) && + func_value_close(mat(3, 3), x33, eps); +} +// clang-format on + } // namespace math diff --git a/tests/cpp/test_mat3_type.cpp b/tests/cpp/test_mat3_type.cpp index 5107591..a99097e 100644 --- a/tests/cpp/test_mat3_type.cpp +++ b/tests/cpp/test_mat3_type.cpp @@ -1,6 +1,9 @@ #include #include +#include "./common_math_helpers.hpp" +#include "./common_math_generators.hpp" + #if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wimplicit-float-conversion" @@ -14,94 +17,69 @@ #pragma warning(disable : 4305) #endif -constexpr double RANGE_MIN = -100.0; -constexpr double RANGE_MAX = 100.0; - -// NOLINTNEXTLINE -#define GenRandomValue(Type, Nsamples) \ - GENERATE(take(Nsamples, random(static_cast(RANGE_MIN), \ - static_cast(RANGE_MAX)))) - -// NOLINTNEXTLINE -#define GenRandomInRange(Type, Nsamples, range_min, range_max) \ - GENERATE(take(Nsamples, random(static_cast(range_min), \ - static_cast(range_max)))) - -template -constexpr auto FuncClose(T a, T b, T eps) -> bool { - return ((a - b) < eps) && ((a - b) > -eps); -} - -// clang-format off -template -auto FuncAllClose(const math::Matrix3& mat, - T x00, T x01, T x02, - T x10, T x11, T x12, - T x20, T x21, T x22) -> bool { - constexpr T EPSILON = static_cast(math::EPS); - - return FuncClose(mat(0, 0), x00, EPSILON) && - FuncClose(mat(0, 1), x01, EPSILON) && - FuncClose(mat(0, 2), x02, EPSILON) && - FuncClose(mat(1, 0), x10, EPSILON) && - FuncClose(mat(1, 1), x11, EPSILON) && - FuncClose(mat(1, 2), x12, EPSILON) && - FuncClose(mat(2, 0), x20, EPSILON) && - FuncClose(mat(2, 1), x21, EPSILON) && - FuncClose(mat(2, 2), x22, EPSILON); -} -// clang-format on +constexpr double USER_RANGE_MIN = -100.0; +constexpr double USER_RANGE_MAX = 100.0; +constexpr double USER_EPSILON = 1e-5; // NOLINTNEXTLINE TEMPLATE_TEST_CASE("Matrix3 class (mat3_t) constructors", "[mat3_t][template]", - math::float32_t, math::float64_t) { + ::math::float32_t, ::math::float64_t) { using T = TestType; - using Vec3 = math::Vector3; - using Euler = math::Euler; - using Mat3 = math::Matrix3; - using Mat4 = math::Matrix4; - using Quat = math::Quaternion; + using Vec3 = ::math::Vector3; + using Euler = ::math::Euler; + using Mat3 = ::math::Matrix3; + using Mat4 = ::math::Matrix4; + using Quat = ::math::Quaternion; + + constexpr T EPSILON = static_cast(USER_EPSILON); + constexpr T RANGE_MIN = static_cast(USER_RANGE_MIN); + constexpr T RANGE_MAX = static_cast(USER_RANGE_MAX); // Checking all exposed constructors SECTION("Default constructor") { Mat3 mat; // clang-format off - REQUIRE(FuncAllClose(mat, - 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0)); + REQUIRE(::math::func_all_close(mat, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, EPSILON)); // clang-format on } + SECTION("From all matrix entries") { // clang-format off Mat3 mat(1.0, 2.0, 3.0, - 4.0, 5.0, 6.0, - 7.0, 8.0, 9.0); + 4.0, 5.0, 6.0, + 7.0, 8.0, 9.0); - REQUIRE(FuncAllClose(mat, - 1.0, 2.0, 3.0, - 4.0, 5.0, 6.0, - 7.0, 8.0, 9.0)); + REQUIRE(::math::func_all_close(mat, + 1.0, 2.0, 3.0, + 4.0, 5.0, 6.0, + 7.0, 8.0, 9.0, EPSILON)); // clang-format on } + SECTION("From diagonal entries") { - auto x00 = GenRandomValue(T, 8); - auto x11 = GenRandomValue(T, 8); - auto x22 = GenRandomValue(T, 8); + constexpr auto NUM_SAMPLES = 8; + auto x00 = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES); + auto x11 = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES); + auto x22 = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES); Mat3 mat(x00, x11, x22); // clang-format off - REQUIRE(FuncAllClose(mat, - x00, 0.0, 0.0, - 0.0, x11, 0.0, - 0.0, 0.0, x22)); + REQUIRE(::math::func_all_close(mat, + x00, 0.0, 0.0, + 0.0, x11, 0.0, + 0.0, 0.0, x22, EPSILON)); // clang-format on } + SECTION("From column vectors") { - auto x00 = GenRandomValue(T, 8); - auto x11 = GenRandomValue(T, 8); - auto x22 = GenRandomValue(T, 8); + constexpr auto NUM_SAMPLES = 8; + auto x00 = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES); + auto x11 = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES); + auto x22 = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES); Vec3 col0 = {x00, 2.0, 3.0}; Vec3 col1 = {4.0, x11, 6.0}; @@ -110,26 +88,28 @@ TEMPLATE_TEST_CASE("Matrix3 class (mat3_t) constructors", "[mat3_t][template]", Mat3 mat(col0, col1, col2); // clang-format off - REQUIRE(FuncAllClose(mat, - x00, 4.0, 7.0, - 2.0, x11, 8.0, - 3.0, 6.0, x22)); + REQUIRE(::math::func_all_close(mat, + x00, 4.0, 7.0, + 2.0, x11, 8.0, + 3.0, 6.0, x22, EPSILON)); // clang-format on } + SECTION("From quaternion") { - // NOLINTNEXTLINE - Quat q(1.0F, 0.0F, 0.0F, 0.0F); + Quat q(1.0, 0.0, 0.0, 0.0); Mat3 mat(q); // clang-format off - REQUIRE(FuncAllClose(mat, + REQUIRE(::math::func_all_close(mat, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 1.0)); + 0.0, 0.0, 1.0, EPSILON)); // clang-format on } + SECTION("From Euler angles") { - auto theta = GenRandomInRange(T, 10, -::math::PI, ::math::PI); + constexpr auto NUM_SAMPLES = 8; + auto theta = gen_random_value(T, -::math::PI, ::math::PI, NUM_SAMPLES); auto cos_t = std::cos(theta); auto sin_t = std::sin(theta); @@ -141,24 +121,26 @@ TEMPLATE_TEST_CASE("Matrix3 class (mat3_t) constructors", "[mat3_t][template]", Mat3 mat_z(e_z); // clang-format off - REQUIRE(FuncAllClose(mat_x, - 1.0, 0.0 , 0.0 , + REQUIRE(::math::func_all_close(mat_x, + 1.0, 0.0 , 0.0 , 0.0, cos_t, -sin_t, - 0.0, sin_t, cos_t)); + 0.0, sin_t, cos_t, EPSILON)); - REQUIRE(FuncAllClose(mat_y, + REQUIRE(::math::func_all_close(mat_y, cos_t , 0.0, sin_t , 0.0 , 1.0, 0.0 , - -sin_t , 0.0, cos_t)); + -sin_t , 0.0, cos_t , EPSILON)); - REQUIRE(FuncAllClose(mat_z, + REQUIRE(::math::func_all_close(mat_z, cos_t , -sin_t , 0.0, sin_t , cos_t , 0.0, - 0.0 , 0.0 , 1.0)); + 0.0 , 0.0 , 1.0, EPSILON)); // clang-format on } + SECTION("From 4x4 transform matrix") { - auto theta = GenRandomInRange(T, 10, -::math::PI, ::math::PI); + constexpr auto NUM_SAMPLES = 8; + auto theta = gen_random_value(T, -::math::PI, ::math::PI, NUM_SAMPLES); auto cos_t = std::cos(theta); auto sin_t = std::sin(theta); @@ -170,20 +152,20 @@ TEMPLATE_TEST_CASE("Matrix3 class (mat3_t) constructors", "[mat3_t][template]", Mat3 mat_z(tf_z); // clang-format off - REQUIRE(FuncAllClose(mat_x, - 1.0, 0.0 , 0.0 , + REQUIRE(::math::func_all_close(mat_x, + 1.0, 0.0 , 0.0 , 0.0, cos_t, -sin_t, - 0.0, sin_t, cos_t)); + 0.0, sin_t, cos_t, EPSILON)); - REQUIRE(FuncAllClose(mat_y, + REQUIRE(::math::func_all_close(mat_y, cos_t , 0.0, sin_t , 0.0 , 1.0, 0.0 , - -sin_t , 0.0, cos_t)); + -sin_t , 0.0, cos_t , EPSILON)); - REQUIRE(FuncAllClose(mat_z, + REQUIRE(::math::func_all_close(mat_z, cos_t , -sin_t , 0.0, sin_t , cos_t , 0.0, - 0.0 , 0.0 , 1.0)); + 0.0 , 0.0 , 1.0, EPSILON)); // clang-format on } }