Skip to content

Commit

Permalink
(chore): refactoring tests for mat2 type (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpumacay committed Jul 21, 2023
1 parent 7f188bc commit 2850c1b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 43 deletions.
12 changes: 11 additions & 1 deletion tests/cpp/common_math_helpers.hpp
Expand Up @@ -2,9 +2,10 @@
#include <math/vec2_t.hpp>
#include <math/vec3_t.hpp>
#include <math/vec4_t.hpp>
#include <math/mat2_t.hpp>
#include <math/mat3_t.hpp>
#include <math/quat_t.hpp>
#include <math/mat4_t.hpp>
#include <math/quat_t.hpp>

// NOLINTNEXTLINE
#define gen_random_value(T, range_min, range_max, Nsamples) \
Expand Down Expand Up @@ -42,4 +43,13 @@ constexpr auto func_all_close(const ::math::Vector4<T>& vec, T x, T y, T z, T w,
func_value_close(vec.w(), w, eps);
}

template <typename T>
constexpr auto func_all_close(const ::math::Matrix2<T>& mat, T x00, T x01,
T x10, T x11, T eps) -> bool {
return func_value_close<T>(mat(0, 0), x00, eps) &&
func_value_close<T>(mat(0, 1), x01, eps) &&
func_value_close<T>(mat(1, 0), x10, eps) &&
func_value_close<T>(mat(1, 1), x11, eps);
}

} // namespace math
119 changes: 77 additions & 42 deletions tests/cpp/test_mat2_type.cpp
@@ -1,6 +1,9 @@
#include <catch2/catch.hpp>
#include <math/mat2_t.hpp>

#include "./common_math_helpers.hpp"
#include "./common_math_generators.hpp"

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-float-conversion"
Expand All @@ -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<Type>(RANGE_MIN), \
static_cast<Type>(RANGE_MAX))))

template <typename T>
constexpr auto FuncClose(T a, T b, T eps) -> bool {
return ((a - b) < eps) && ((a - b) > -eps);
}

template <typename T>
auto FuncAllClose(const math::Matrix2<T>& mat, T x00, T x01, T x10, T x11)
-> bool {
constexpr T EPSILON = static_cast<T>(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<T>;
using Vector2 = math::Vector2<T>;
using Matrix2 = ::math::Matrix2<T>;

constexpr T EPSILON = static_cast<T>(USER_EPSILON);
constexpr T RANGE_MIN = static_cast<T>(USER_RANGE_MIN);
constexpr T RANGE_MAX = static_cast<T>(USER_RANGE_MAX);

// Checking all exposed constructors
SECTION("Default constructor") {
Matrix2 mat;
REQUIRE(FuncAllClose<T>(mat, 0.0, 0.0, 0.0, 0.0));
// Default constructor creates zero-initialized matrix
// clang-format off
REQUIRE(::math::func_all_close<T>(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<T>(mat, x00, x01, x10, x11));
// clang-format off
REQUIRE(::math::func_all_close<T>(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<T>(mat, x00, 0.0, 0.0, x11));
// clang-format off
REQUIRE(::math::func_all_close<T>(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<T>(RANGE_MIN, RANGE_MAX)));
auto col1 = GENERATE(
take(NUM_SAMPLES, ::math::random_vec2<T>(RANGE_MIN, RANGE_MAX)));

Matrix2 mat(col0, col1);
REQUIRE(FuncAllClose<T>(mat, x00, x01, x10, x11));
// clang-format off
REQUIRE(::math::func_all_close<T>(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<T>(mat(0, 0), 1.0, EPSILON));
REQUIRE(::math::func_value_close<T>(mat(1, 0), 2.0, EPSILON));
REQUIRE(::math::func_value_close<T>(mat(0, 1), 3.0, EPSILON));
REQUIRE(::math::func_value_close<T>(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<T>(col0, 1.0, 3.0, EPSILON));
REQUIRE(::math::func_all_close<T>(col1, 2.0, 4.0, EPSILON));
}
}

Expand Down

0 comments on commit 2850c1b

Please sign in to comment.