From 2850c1b5c78a9312b98202b4fb53cd697735c177 Mon Sep 17 00:00:00 2001 From: gregor Date: Fri, 21 Jul 2023 16:24:11 -0500 Subject: [PATCH] (chore): refactoring tests for mat2 type (wip) --- tests/cpp/common_math_helpers.hpp | 12 ++- tests/cpp/test_mat2_type.cpp | 119 +++++++++++++++++++----------- 2 files changed, 88 insertions(+), 43 deletions(-) diff --git a/tests/cpp/common_math_helpers.hpp b/tests/cpp/common_math_helpers.hpp index 20622c0..8327f06 100644 --- a/tests/cpp/common_math_helpers.hpp +++ b/tests/cpp/common_math_helpers.hpp @@ -2,9 +2,10 @@ #include #include #include +#include #include -#include #include +#include // NOLINTNEXTLINE #define gen_random_value(T, range_min, range_max, Nsamples) \ @@ -42,4 +43,13 @@ constexpr auto func_all_close(const ::math::Vector4& vec, T x, T y, T z, T w, func_value_close(vec.w(), w, eps); } +template +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) && + func_value_close(mat(1, 0), x10, eps) && + func_value_close(mat(1, 1), x11, eps); +} + } // namespace math diff --git a/tests/cpp/test_mat2_type.cpp b/tests/cpp/test_mat2_type.cpp index 123e92f..44c9022 100644 --- a/tests/cpp/test_mat2_type.cpp +++ b/tests/cpp/test_mat2_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,67 +17,99 @@ #pragma warning(disable : 4305) #endif -constexpr double RANGE_MIN = -10.0; -constexpr double RANGE_MAX = 10.0; - -// NOLINTNEXTLINE -#define GenRandomValue(Type, Nsamples) \ - 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); -} - -template -auto FuncAllClose(const math::Matrix2& mat, T x00, T x01, T x10, T x11) - -> bool { - constexpr T EPSILON = static_cast(math::EPS); - return FuncClose(mat(0, 0), x00, EPSILON) && - FuncClose(mat(0, 1), x01, EPSILON) && - FuncClose(mat(1, 0), x10, EPSILON) && - FuncClose(mat(1, 1), x11, EPSILON); -} +constexpr double USER_RANGE_MIN = -10.0; +constexpr double USER_RANGE_MAX = 10.0; +constexpr double USER_EPSILON = 1e-5; // NOLINTNEXTLINE TEMPLATE_TEST_CASE("Matrix4 class (mat2_t) constructors", "[mat2_t][template]", - math::float32_t, math::float64_t) { + ::math::float32_t, ::math::float64_t) { using T = TestType; - using Matrix2 = math::Matrix2; - using Vector2 = math::Vector2; + using Matrix2 = ::math::Matrix2; + + 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") { Matrix2 mat; - REQUIRE(FuncAllClose(mat, 0.0, 0.0, 0.0, 0.0)); + // Default constructor creates zero-initialized matrix + // clang-format off + REQUIRE(::math::func_all_close(mat, + 0.0, 0.0, + 0.0, 0.0, EPSILON)); + // clang-format on } + SECTION("From all matrix entries") { - auto x00 = GenRandomValue(T, 8); - auto x01 = GenRandomValue(T, 8); - auto x10 = GenRandomValue(T, 8); - auto x11 = GenRandomValue(T, 8); + constexpr auto NUM_SAMPLES = 4; + auto x00 = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES); + auto x01 = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES); + auto x10 = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES); + auto x11 = gen_random_value(T, RANGE_MIN, RANGE_MAX, NUM_SAMPLES); Matrix2 mat(x00, x01, x10, x11); - REQUIRE(FuncAllClose(mat, x00, x01, x10, x11)); + // clang-format off + REQUIRE(::math::func_all_close(mat, + x00, x01, + x10, x11, EPSILON)); + // clang-format on } + SECTION("From diagonal entries") { - auto x00 = GenRandomValue(T, 8); - auto x11 = 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); Matrix2 mat(x00, x11); - REQUIRE(FuncAllClose(mat, x00, 0.0, 0.0, x11)); + // clang-format off + REQUIRE(::math::func_all_close(mat, + x00, 0.0, + 0.0, x11, EPSILON)); + // clang-format on } + SECTION("From column vectors") { - auto x00 = GenRandomValue(T, 8); - auto x01 = GenRandomValue(T, 8); - auto x10 = GenRandomValue(T, 8); - auto x11 = GenRandomValue(T, 8); - Vector2 col0(x00, x10); - Vector2 col1(x01, x11); + constexpr auto NUM_SAMPLES = 8; + auto col0 = GENERATE( + take(NUM_SAMPLES, ::math::random_vec2(RANGE_MIN, RANGE_MAX))); + auto col1 = GENERATE( + take(NUM_SAMPLES, ::math::random_vec2(RANGE_MIN, RANGE_MAX))); Matrix2 mat(col0, col1); - REQUIRE(FuncAllClose(mat, x00, x01, x10, x11)); + // clang-format off + REQUIRE(::math::func_all_close(mat, + col0.x(), col1.x(), + col0.y(), col1.y(), EPSILON)); + // clang-format on + } + + SECTION("Accessor (i, j) returns scalar entry") { + Matrix2 mat; + // Update entries in first column + mat(0, 0) = 1.0; + mat(1, 0) = 2.0; + // Update entries in second column + mat(0, 1) = 3.0; + mat(1, 1) = 4.0; + // Make sure all entries are set accordingly + REQUIRE(::math::func_value_close(mat(0, 0), 1.0, EPSILON)); + REQUIRE(::math::func_value_close(mat(1, 0), 2.0, EPSILON)); + REQUIRE(::math::func_value_close(mat(0, 1), 3.0, EPSILON)); + REQUIRE(::math::func_value_close(mat(1, 1), 4.0, EPSILON)); + } + + SECTION("Accessor [idx] returns column at index 'idx'") { + // clang-format off + Matrix2 mat( + 1.0, 2.0, + 3.0, 4.0); + // clang-format on + auto col0 = mat[0]; + auto col1 = mat[1]; + // Make sure we got the right column vectors from the matrix + REQUIRE(::math::func_all_close(col0, 1.0, 3.0, EPSILON)); + REQUIRE(::math::func_all_close(col1, 2.0, 4.0, EPSILON)); } }