From 2b9a6d4951da3fb27ecc1f58b969b5c17bc0f73f Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 25 Nov 2020 18:33:11 +0700 Subject: [PATCH] move common tests code --- untests/vmath_fun_tests.cpp | 19 ++---- untests/vmath_mat_ext_tests.cpp | 60 +----------------- untests/vmath_mat_fun_tests.cpp | 105 +------------------------------ untests/vmath_mat_tests.cpp | 6 +- untests/vmath_tests.hpp | 107 ++++++++++++++++++++++++++++++++ untests/vmath_vec_fun_tests.cpp | 39 +----------- untests/vmath_vec_tests.cpp | 6 +- 7 files changed, 128 insertions(+), 214 deletions(-) create mode 100644 untests/vmath_tests.hpp diff --git a/untests/vmath_fun_tests.cpp b/untests/vmath_fun_tests.cpp index ca23017..11da4ac 100644 --- a/untests/vmath_fun_tests.cpp +++ b/untests/vmath_fun_tests.cpp @@ -9,26 +9,15 @@ #define CATCH_CONFIG_FAST_COMPILE #include +#include "vmath_tests.hpp" + namespace { - template < typename T > - class approx { - public: - explicit constexpr approx(T v) : value_(v) {} - - friend constexpr bool operator==(const T& l, const approx& r) { - return (r.value_ < l + epsilon) - && (l < r.value_ + epsilon); - } - private: - T value_; - static constexpr T epsilon = std::numeric_limits::epsilon() * 100; - }; + using namespace vmath_hpp; + using namespace vmath_tests; } TEST_CASE("vmath/fun") { - using namespace vmath_hpp; - SECTION("Angle and Trigonometry Functions") { STATIC_REQUIRE(radians(degrees(12.13f)) == approx(12.13f)); STATIC_REQUIRE(degrees(radians(12.13f)) == approx(12.13f)); diff --git a/untests/vmath_mat_ext_tests.cpp b/untests/vmath_mat_ext_tests.cpp index d44afa9..0c7b716 100644 --- a/untests/vmath_mat_ext_tests.cpp +++ b/untests/vmath_mat_ext_tests.cpp @@ -9,66 +9,12 @@ #define CATCH_CONFIG_FAST_COMPILE #include +#include "vmath_tests.hpp" + namespace { using namespace vmath_hpp; - - template < typename T > - class approx2 { - public: - constexpr explicit approx2(T v) : value_(v) {} - constexpr explicit approx2(T x, T y) : value_(x, y) {} - - friend constexpr bool operator==(const vec& l, const approx2& r) { - return (r.value_.x < l.x + epsilon) - && (l.x < r.value_.x + epsilon) - && (r.value_.y < l.y + epsilon) - && (l.y < r.value_.y + epsilon); - } - private: - vec value_; - static constexpr T epsilon = std::numeric_limits::epsilon() * 100; - }; - - template < typename T > - class approx3 { - public: - constexpr explicit approx3(T v) : value_(v) {} - constexpr explicit approx3(T x, T y, T z) : value_(x, y, z) {} - - friend constexpr bool operator==(const vec& l, const approx3& r) { - return (r.value_.x < l.x + epsilon) - && (l.x < r.value_.x + epsilon) - && (r.value_.y < l.y + epsilon) - && (l.y < r.value_.y + epsilon) - && (r.value_.z < l.z + epsilon) - && (l.z < r.value_.z + epsilon); - } - private: - vec value_; - static constexpr T epsilon = std::numeric_limits::epsilon() * 100; - }; - - template < typename T > - class approx4 { - public: - constexpr explicit approx4(T v) : value_(v) {} - constexpr explicit approx4(T x, T y, T z, T w) : value_(x, y, z, w) {} - - friend constexpr bool operator==(const vec& l, const approx4& r) { - return (r.value_.x < l.x + epsilon) - && (l.x < r.value_.x + epsilon) - && (r.value_.y < l.y + epsilon) - && (l.y < r.value_.y + epsilon) - && (r.value_.z < l.z + epsilon) - && (l.z < r.value_.z + epsilon) - && (r.value_.w < l.w + epsilon) - && (l.w < r.value_.w + epsilon); - } - private: - vec value_; - static constexpr T epsilon = std::numeric_limits::epsilon() * 100; - }; + using namespace vmath_tests; } TEST_CASE("vmath/mat_ext") { diff --git a/untests/vmath_mat_fun_tests.cpp b/untests/vmath_mat_fun_tests.cpp index 98e482b..068ab4a 100644 --- a/untests/vmath_mat_fun_tests.cpp +++ b/untests/vmath_mat_fun_tests.cpp @@ -10,9 +10,12 @@ #define CATCH_CONFIG_FAST_COMPILE #include +#include "vmath_tests.hpp" + namespace { using namespace vmath_hpp; + using namespace vmath_tests; template < typename T, int Size > constexpr mat generate_frank_matrix() { @@ -30,108 +33,6 @@ namespace } return m; } - - template < typename T > - class approx2 { - public: - constexpr explicit approx2(T v) : value_(v) {} - constexpr explicit approx2(const vec& v) : value_(v) {} - constexpr explicit approx2(T x, T y) : value_(x, y) {} - - friend constexpr bool operator==(const vec& l, const approx2& r) { - return (r.value_.x < l.x + epsilon) - && (l.x < r.value_.x + epsilon) - && (r.value_.y < l.y + epsilon) - && (l.y < r.value_.y + epsilon); - } - private: - vec value_; - static constexpr T epsilon = std::numeric_limits::epsilon() * 100; - }; - - template < typename T > - class approx3 { - public: - constexpr explicit approx3(T v) : value_(v) {} - constexpr explicit approx3(const vec& v) : value_(v) {} - constexpr explicit approx3(T x, T y, T z) : value_(x, y, z) {} - - friend constexpr bool operator==(const vec& l, const approx3& r) { - return (r.value_.x < l.x + epsilon) - && (l.x < r.value_.x + epsilon) - && (r.value_.y < l.y + epsilon) - && (l.y < r.value_.y + epsilon) - && (r.value_.z < l.z + epsilon) - && (l.z < r.value_.z + epsilon); - } - private: - vec value_; - static constexpr T epsilon = std::numeric_limits::epsilon() * 100; - }; - - template < typename T > - class approx4 { - public: - constexpr explicit approx4(T v) : value_(v) {} - constexpr explicit approx4(const vec& v) : value_(v) {} - constexpr explicit approx4(T x, T y, T z, T w) : value_(x, y, z, w) {} - - friend constexpr bool operator==(const vec& l, const approx4& r) { - return (r.value_.x < l.x + epsilon) - && (l.x < r.value_.x + epsilon) - && (r.value_.y < l.y + epsilon) - && (l.y < r.value_.y + epsilon) - && (r.value_.z < l.z + epsilon) - && (l.z < r.value_.z + epsilon) - && (r.value_.w < l.w + epsilon) - && (l.w < r.value_.w + epsilon); - } - private: - vec value_; - static constexpr T epsilon = std::numeric_limits::epsilon() * 100; - }; - - template < typename T > - class approx2x2 { - public: - constexpr explicit approx2x2(const mat& v) : value_(v) {} - - friend constexpr bool operator==(const mat& l, const approx2x2& r) { - return l[0] == approx2(r.value_[0]) - && l[1] == approx2(r.value_[1]); - } - private: - mat value_; - }; - - template < typename T > - class approx3x3 { - public: - constexpr explicit approx3x3(const mat& v) : value_(v) {} - - friend constexpr bool operator==(const mat& l, const approx3x3& r) { - return l[0] == approx3(r.value_[0]) - && l[1] == approx3(r.value_[1]) - && l[2] == approx3(r.value_[2]); - } - private: - mat value_; - }; - - template < typename T > - class approx4x4 { - public: - constexpr explicit approx4x4(const mat& v) : value_(v) {} - - friend constexpr bool operator==(const mat& l, const approx4x4& r) { - return l[0] == approx4(r.value_[0]) - && l[1] == approx4(r.value_[1]) - && l[2] == approx4(r.value_[2]) - && l[3] == approx4(r.value_[3]); - } - private: - mat value_; - }; } TEST_CASE("vmath/mat_fun") { diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index 06860b9..1682855 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -10,13 +10,15 @@ #define CATCH_CONFIG_FAST_COMPILE #include +#include "vmath_tests.hpp" + namespace { + using namespace vmath_hpp; + using namespace vmath_tests; } TEST_CASE("vmath/mat") { - using namespace vmath_hpp; - SECTION("size/sizeof") { STATIC_REQUIRE(mat2i{}.size == 2); STATIC_REQUIRE(mat3i{}.size == 3); diff --git a/untests/vmath_tests.hpp b/untests/vmath_tests.hpp new file mode 100644 index 0000000..5c50b21 --- /dev/null +++ b/untests/vmath_tests.hpp @@ -0,0 +1,107 @@ +/******************************************************************************* + * This file is part of the "https://github.com/blackmatov/vmath.hpp" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#include +#include + +#include + +namespace vmath_tests +{ + using namespace vmath_hpp; + + template < typename T > + inline constexpr T epsilon = std::numeric_limits::epsilon() * 100; + + template < typename T > + struct approx { + T value; + explicit constexpr approx(T v) : value(v) {} + + friend constexpr bool operator==(const T& l, const approx& r) { + return (r.value < l + epsilon) + && (l < r.value + epsilon); + } + }; + + template < typename T > + struct approx2 { + vec value; + constexpr explicit approx2(T v) : value(v) {} + constexpr explicit approx2(T x, T y) : value(x, y) {} + constexpr explicit approx2(const vec& v) : value(v) {} + + friend constexpr bool operator==(const vec& l, const approx2& r) { + return l[0] == approx(r.value[0]) + && l[1] == approx(r.value[1]); + } + }; + + template < typename T > + struct approx3 { + vec value; + constexpr explicit approx3(T v) : value(v) {} + constexpr explicit approx3(T x, T y, T z) : value(x, y, z) {} + constexpr explicit approx3(const vec& v) : value(v) {} + + friend constexpr bool operator==(const vec& l, const approx3& r) { + return l[0] == approx(r.value[0]) + && l[1] == approx(r.value[1]) + && l[2] == approx(r.value[2]); + } + }; + + template < typename T > + struct approx4 { + vec value; + constexpr explicit approx4(T v) : value(v) {} + constexpr explicit approx4(T x, T y, T z, T w) : value(x, y, z, w) {} + constexpr explicit approx4(const vec& v) : value(v) {} + + friend constexpr bool operator==(const vec& l, const approx4& r) { + return l[0] == approx(r.value[0]) + && l[1] == approx(r.value[1]) + && l[2] == approx(r.value[2]) + && l[3] == approx(r.value[3]); + } + }; + + template < typename T > + struct approx2x2 { + mat value; + constexpr explicit approx2x2(const mat& v) : value(v) {} + + friend constexpr bool operator==(const mat& l, const approx2x2& r) { + return l[0] == approx2(r.value[0]) + && l[1] == approx2(r.value[1]); + } + }; + + template < typename T > + struct approx3x3 { + mat value; + constexpr explicit approx3x3(const mat& v) : value(v) {} + + friend constexpr bool operator==(const mat& l, const approx3x3& r) { + return l[0] == approx3(r.value[0]) + && l[1] == approx3(r.value[1]) + && l[2] == approx3(r.value[2]); + } + }; + + template < typename T > + struct approx4x4 { + mat value; + constexpr explicit approx4x4(const mat& v) : value(v) {} + + friend constexpr bool operator==(const mat& l, const approx4x4& r) { + return l[0] == approx4(r.value[0]) + && l[1] == approx4(r.value[1]) + && l[2] == approx4(r.value[2]) + && l[3] == approx4(r.value[3]); + } + }; +} diff --git a/untests/vmath_vec_fun_tests.cpp b/untests/vmath_vec_fun_tests.cpp index 2994c6c..612d01b 100644 --- a/untests/vmath_vec_fun_tests.cpp +++ b/untests/vmath_vec_fun_tests.cpp @@ -9,45 +9,12 @@ #define CATCH_CONFIG_FAST_COMPILE #include +#include "vmath_tests.hpp" + namespace { using namespace vmath_hpp; - - template < typename T > - class approx2 { - public: - constexpr explicit approx2(T v) : value_(v) {} - constexpr explicit approx2(T x, T y) : value_(x, y) {} - - friend constexpr bool operator==(const vec& l, const approx2& r) { - return (r.value_.x < l.x + epsilon) - && (l.x < r.value_.x + epsilon) - && (r.value_.y < l.y + epsilon) - && (l.y < r.value_.y + epsilon); - } - private: - vec value_; - static constexpr T epsilon = std::numeric_limits::epsilon() * 100; - }; - - template < typename T > - class approx3 { - public: - constexpr explicit approx3(T v) : value_(v) {} - constexpr explicit approx3(T x, T y, T z) : value_(x, y, z) {} - - friend constexpr bool operator==(const vec& l, const approx3& r) { - return (r.value_.x < l.x + epsilon) - && (l.x < r.value_.x + epsilon) - && (r.value_.y < l.y + epsilon) - && (l.y < r.value_.y + epsilon) - && (r.value_.z < l.z + epsilon) - && (l.z < r.value_.z + epsilon); - } - private: - vec value_; - static constexpr T epsilon = std::numeric_limits::epsilon() * 100; - }; + using namespace vmath_tests; } TEST_CASE("vmath/vec_fun") { diff --git a/untests/vmath_vec_tests.cpp b/untests/vmath_vec_tests.cpp index 5129d58..3c9f84c 100644 --- a/untests/vmath_vec_tests.cpp +++ b/untests/vmath_vec_tests.cpp @@ -10,13 +10,15 @@ #define CATCH_CONFIG_FAST_COMPILE #include +#include "vmath_tests.hpp" + namespace { + using namespace vmath_hpp; + using namespace vmath_tests; } TEST_CASE("vmath/vec") { - using namespace vmath_hpp; - SECTION("size/sizeof") { STATIC_REQUIRE(vec2i{}.size == 2); STATIC_REQUIRE(vec3i{}.size == 3);