Skip to content

Commit

Permalink
(tests): tidying up unittests for vec2_t
Browse files Browse the repository at this point in the history
  • Loading branch information
wpumacay committed Jul 18, 2023
1 parent 5b6edc1 commit 1019fcb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 40 deletions.
28 changes: 28 additions & 0 deletions tests/cpp/common_math_helpers.hpp
@@ -0,0 +1,28 @@
#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 <math/mat4_t.hpp>

// NOLINTNEXTLINE
#define gen_random_value(T, range_min, range_max, Nsamples) \
GENERATE(take(Nsamples, random(static_cast<T>(range_min), \
static_cast<T>(range_max))))

namespace math {

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

template <typename T>
constexpr auto func_all_close(const ::math::Vector2<T>& vec, T x, T y, T eps)
-> bool {
return func_value_close(vec.x(), x, eps) &&
func_value_close(vec.y(), y, eps);
}

} // namespace math
74 changes: 34 additions & 40 deletions tests/cpp/test_vec2_type.cpp
@@ -1,6 +1,8 @@
#include <catch2/catch.hpp>
#include <math/vec2_t.hpp>

#include "./common_math_helpers.hpp"

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-float-conversion"
Expand All @@ -14,79 +16,71 @@
#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::Vector2<T>& vec, T x, T y) -> bool {
constexpr T EPSILON = static_cast<T>(math::EPS);
return FuncClose(vec.x(), x, EPSILON) && FuncClose(vec.y(), y, 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("Vector2 class (vec2_t) type", "[vec2_t][template]",
math::float32_t, math::float64_t) {
using T = TestType;
using Vector2 = math::Vector2<T>;

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

SECTION("Get/Set using .x(), .y()") {
auto val_x = gen_random_value(T, RANGE_MIN, RANGE_MAX, 10);
auto val_y = gen_random_value(T, RANGE_MIN, RANGE_MAX, 10);

Vector2 v;
v.x() = val_x;
v.y() = val_y;

REQUIRE(::math::func_value_close<T>(v.x(), val_x, EPSILON));
REQUIRE(::math::func_value_close<T>(v.y(), val_y, EPSILON));
}

SECTION("Default constructor") {
Vector2 v;
REQUIRE(FuncAllClose<T>(v, 0.0, 0.0));
// Default constructor initializes the vector entries to zeros
REQUIRE(::math::func_all_close<T>(v, 0.0, 0.0, EPSILON));
}

SECTION("From single scalar argument") {
auto val_x = GenRandomValue(T, 100);
auto val_x = gen_random_value(T, RANGE_MIN, RANGE_MAX, 10);

Vector2 v(val_x);
REQUIRE(FuncAllClose<T>(v, val_x, val_x));
// The given argument is copied for the second entry
REQUIRE(::math::func_all_close<T>(v, val_x, val_x, EPSILON));
}

SECTION(
"From two scalar arguments, from initializer_list, or using "
"comma-initializer") {
auto val_x = GenRandomValue(T, 10);
auto val_y = GenRandomValue(T, 10);
auto val_x = gen_random_value(T, RANGE_MIN, RANGE_MAX, 10);
auto val_y = gen_random_value(T, RANGE_MIN, RANGE_MAX, 10);

Vector2 v_1(val_x, val_y);
Vector2 v_2 = {val_x, val_y};
Vector2 v_3;
// cppcheck-suppress constStatement
v_3 << val_x, val_y;

REQUIRE(FuncAllClose<T>(v_1, val_x, val_y));
REQUIRE(FuncAllClose<T>(v_2, val_x, val_y));
REQUIRE(FuncAllClose<T>(v_3, val_x, val_y));
}

SECTION("Accessors .x(), .y()") {
auto val_x = GenRandomValue(T, 10);
auto val_y = GenRandomValue(T, 10);

Vector2 v;
v.x() = val_x;
v.y() = val_y;

REQUIRE(FuncAllClose<T>(v, val_x, val_y));
REQUIRE(::math::func_all_close<T>(v_1, val_x, val_y, EPSILON));
REQUIRE(::math::func_all_close<T>(v_2, val_x, val_y, EPSILON));
REQUIRE(::math::func_all_close<T>(v_3, val_x, val_y, EPSILON));
}

SECTION("Accessors [index]") {
auto val_x = GenRandomValue(T, 10);
auto val_y = GenRandomValue(T, 10);
auto val_x = gen_random_value(T, RANGE_MIN, RANGE_MAX, 10);
auto val_y = gen_random_value(T, RANGE_MIN, RANGE_MAX, 10);

Vector2 v;
v[0] = val_x;
v[1] = val_y;
REQUIRE(FuncAllClose<T>(v, val_x, val_y));
REQUIRE(::math::func_all_close<T>(v, val_x, val_y, EPSILON));
}
}

Expand Down

0 comments on commit 1019fcb

Please sign in to comment.