- Started unit tests for the common math functions
This commit is contained in:
@@ -301,7 +301,7 @@ namespace fennec
|
|||||||
/// \param x input value
|
/// \param x input value
|
||||||
template<typename genType>
|
template<typename genType>
|
||||||
constexpr genType abs(genType x)
|
constexpr genType abs(genType x)
|
||||||
{ return std::abs(x); }
|
{ return ::abs(x); }
|
||||||
|
|
||||||
|
|
||||||
// Vector Specializations ----------------------------------------------------------------------------------------------
|
// Vector Specializations ----------------------------------------------------------------------------------------------
|
||||||
@@ -324,7 +324,7 @@ constexpr vector<genType, i...> abs(const vector<genType, i...>& x)
|
|||||||
/// \param x input value
|
/// \param x input value
|
||||||
template<typename genType>
|
template<typename genType>
|
||||||
constexpr genType sign(genType x)
|
constexpr genType sign(genType x)
|
||||||
{ return (x < 0 ? -1 : (x > 0 ? +1 : 0)); }
|
{ return (x < 0 ? -1 : 1) * static_cast<genType>(x != 0); }
|
||||||
|
|
||||||
|
|
||||||
// Vector Specializations ----------------------------------------------------------------------------------------------
|
// Vector Specializations ----------------------------------------------------------------------------------------------
|
||||||
@@ -427,7 +427,7 @@ constexpr vector<genType, i...> round(const vector<genType, i...>& x)
|
|||||||
/// \param x input value
|
/// \param x input value
|
||||||
template<typename genType>
|
template<typename genType>
|
||||||
constexpr genType roundEven(genType x)
|
constexpr genType roundEven(genType x)
|
||||||
{ return x - floor(x) == 0.5f ? floor(x) + (static_cast<int>(floor(x)) % 2) : round(x); }
|
{ return ::roundeven(x); }
|
||||||
|
|
||||||
|
|
||||||
// Vector Specializations ----------------------------------------------------------------------------------------------
|
// Vector Specializations ----------------------------------------------------------------------------------------------
|
||||||
@@ -505,18 +505,18 @@ constexpr vector<genType, i...> fract(const vector<genType, i...>& x)
|
|||||||
/// \param y divisor
|
/// \param y divisor
|
||||||
template<typename genType>
|
template<typename genType>
|
||||||
constexpr genType mod(genType x, genType y)
|
constexpr genType mod(genType x, genType y)
|
||||||
{ return x - y * fennec::floor(x); }
|
{ return x - y * fennec::floor(x / y); }
|
||||||
|
|
||||||
|
|
||||||
// Vector Specializations ----------------------------------------------------------------------------------------------
|
// Vector Specializations ----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
template<typename genType, size_t...i>
|
template<typename genType, size_t...i>
|
||||||
constexpr vector<genType, i...> mod(const vector<genType, i...>& x, genType y)
|
constexpr vector<genType, i...> mod(const vector<genType, i...>& x, genType y)
|
||||||
{ return x - y * fennec::floor(x); }
|
{ return x - y * fennec::floor(x / y); }
|
||||||
|
|
||||||
template<typename genType, size_t...i>
|
template<typename genType, size_t...i>
|
||||||
constexpr vector<genType, i...> mod(const vector<genType, i...>& x, const vector<genType, i...>& y)
|
constexpr vector<genType, i...> mod(const vector<genType, i...>& x, const vector<genType, i...>& y)
|
||||||
{ return x - y * fennec::floor(x); }
|
{ return x - y * fennec::floor(x / y); }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -533,7 +533,7 @@ constexpr vector<genType, i...> mod(const vector<genType, i...>& x, const vector
|
|||||||
/// \param i integral out
|
/// \param i integral out
|
||||||
template<typename genType>
|
template<typename genType>
|
||||||
constexpr genType modf(genType x, genType& i)
|
constexpr genType modf(genType x, genType& i)
|
||||||
{ return ::modf(x, &i); }
|
{ i = fennec::floor(x); return fennec::fract(x); }
|
||||||
|
|
||||||
|
|
||||||
// Vector Specializations ----------------------------------------------------------------------------------------------
|
// Vector Specializations ----------------------------------------------------------------------------------------------
|
||||||
@@ -600,7 +600,7 @@ constexpr vector<genBType, i...> isinf(const vector<genType, i...>& x)
|
|||||||
///
|
///
|
||||||
/// \copydetails fennec::floatBitsToUint(fennec::genFType)
|
/// \copydetails fennec::floatBitsToUint(fennec::genFType)
|
||||||
template<typename genType, typename genIType = int_t> requires(is_floating_point_v<genType> and is_integral_v<genIType> and is_signed_v<genIType> and sizeof(genType) == sizeof(genIType))
|
template<typename genType, typename genIType = int_t> requires(is_floating_point_v<genType> and is_integral_v<genIType> and is_signed_v<genIType> and sizeof(genType) == sizeof(genIType))
|
||||||
constexpr genIType floatBitsToInt(genIType x)
|
constexpr genIType floatBitsToInt(genType x)
|
||||||
{ return fennec::bit_cast<genIType>(x); }
|
{ return fennec::bit_cast<genIType>(x); }
|
||||||
|
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ add_executable(fennec-test main.cpp
|
|||||||
tests/lang/test_conditional_types.h
|
tests/lang/test_conditional_types.h
|
||||||
tests/lang/test_bits.h
|
tests/lang/test_bits.h
|
||||||
tests/lang/test_sequences.h
|
tests/lang/test_sequences.h
|
||||||
|
tests/math/test_common.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(fennec-test PRIVATE
|
target_link_libraries(fennec-test PRIVATE
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#include <fennec/math/common.h>
|
#include <fennec/math/common.h>
|
||||||
#include <fennec/math/matrix.h>
|
#include <fennec/math/matrix.h>
|
||||||
#include <fennec/math/relational.h>
|
#include <fennec/math/relational.h>
|
||||||
|
#include <fennec/math/vector_traits.h>
|
||||||
|
|
||||||
namespace fennec
|
namespace fennec
|
||||||
{
|
{
|
||||||
@@ -76,7 +77,9 @@ inline void __fennec_test_run(const std::string& expression, const ResultT resul
|
|||||||
std::cout << std::boolalpha;
|
std::cout << std::boolalpha;
|
||||||
std::cout << '\t' << expression << " = " << result;
|
std::cout << '\t' << expression << " = " << result;
|
||||||
bool passed = false;
|
bool passed = false;
|
||||||
if constexpr(is_arithmetic_v<ResultT> or is_vector_v<ResultT>)
|
if constexpr(is_arithmetic_v<ResultT>)
|
||||||
|
passed = fennec::abs(expected - result) <= numeric_limits<ResultT>::epsilon();
|
||||||
|
else if constexpr(is_vector_v<ResultT> && not(is_bool_v<typename ResultT::scalar_t>))
|
||||||
passed = fennec::abs(expected - result) <= numeric_limits<ResultT>::epsilon();
|
passed = fennec::abs(expected - result) <= numeric_limits<ResultT>::epsilon();
|
||||||
else
|
else
|
||||||
passed = result == expected;
|
passed = result == expected;
|
||||||
|
|||||||
157
test/tests/math/test_common.h
Normal file
157
test/tests/math/test_common.h
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
// =====================================================================================================================
|
||||||
|
// fennec, a free and open source game engine
|
||||||
|
// Copyright © 2025 Medusa Slockbower
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
// =====================================================================================================================
|
||||||
|
|
||||||
|
#ifndef FENNEC_TEST_MATH_COMMON_H
|
||||||
|
#define FENNEC_TEST_MATH_COMMON_H
|
||||||
|
|
||||||
|
#include "../../test.h"
|
||||||
|
|
||||||
|
namespace fennec
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace test
|
||||||
|
{
|
||||||
|
|
||||||
|
inline void fennec_test_math_common()
|
||||||
|
{
|
||||||
|
fennec_test_section("Sign Functions");
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::abs( 1), 1);
|
||||||
|
fennec_test_run(fennec::abs(-1), 1);
|
||||||
|
fennec_test_run(fennec::abs( 1.0f), 1.0f);
|
||||||
|
fennec_test_run(fennec::abs(-1.0f), 1.0f);
|
||||||
|
fennec_test_run(fennec::abs( 1.0), 1.0);
|
||||||
|
fennec_test_run(fennec::abs(-1.0), 1.0);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::sign(vec2( 1, -2)), vec2( 1, -1));
|
||||||
|
fennec_test_run(fennec::sign(vec3(-1, 2, -3)), vec3(-1, 1, -1));
|
||||||
|
fennec_test_run(fennec::sign(vec4( 1, -2, 3, -4)), vec4( 1, -1, 1, -1));
|
||||||
|
|
||||||
|
fennec_test_spacer(2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fennec_test_section("Rounding Functions");
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::ceil(2.4f), 3.0f);
|
||||||
|
fennec_test_run(fennec::ceil(2.5f), 3.0f);
|
||||||
|
fennec_test_run(fennec::ceil(2.6f), 3.0f);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::ceil(-2.4f), -2.0f);
|
||||||
|
fennec_test_run(fennec::ceil(-2.5f), -2.0f);
|
||||||
|
fennec_test_run(fennec::ceil(-2.6f), -2.0f);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::floor(2.4f), 2.0f);
|
||||||
|
fennec_test_run(fennec::floor(2.5f), 2.0f);
|
||||||
|
fennec_test_run(fennec::floor(2.6f), 2.0f);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::floor(-2.4f), -3.0f);
|
||||||
|
fennec_test_run(fennec::floor(-2.5f), -3.0f);
|
||||||
|
fennec_test_run(fennec::floor(-2.6f), -3.0f);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::round(2.4f), 2.0f);
|
||||||
|
fennec_test_run(fennec::round(2.5f), 3.0f);
|
||||||
|
fennec_test_run(fennec::round(2.6f), 3.0f);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::roundEven(2.4f), 2.0f);
|
||||||
|
fennec_test_run(fennec::roundEven(2.5f), 2.0f);
|
||||||
|
fennec_test_run(fennec::roundEven(2.6f), 3.0f);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::trunc(2.4f), 2.0f);
|
||||||
|
fennec_test_run(fennec::trunc(2.5f), 2.0f);
|
||||||
|
fennec_test_run(fennec::trunc(2.6f), 2.0f);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::trunc(-2.4f), -2.0f);
|
||||||
|
fennec_test_run(fennec::trunc(-2.5f), -2.0f);
|
||||||
|
fennec_test_run(fennec::trunc(-2.6f), -2.0f);
|
||||||
|
|
||||||
|
fennec_test_spacer(2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fennec_test_section("Floating Point Functions");
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::fract(2.4f), 0.4f);
|
||||||
|
fennec_test_run(fennec::fract(2.5f), 0.5f);
|
||||||
|
fennec_test_run(fennec::fract(2.6f), 0.6f);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::mod(2.4f, 2.0f), 0.4f);
|
||||||
|
fennec_test_run(fennec::mod(2.5f, 2.0f), 0.5f);
|
||||||
|
fennec_test_run(fennec::mod(2.6f, 2.0f), 0.6f);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run([]() -> float { float i; return fennec::modf(2.4f, i) + i; }(), 2.4f);
|
||||||
|
fennec_test_run([]() -> float { float i; return fennec::modf(2.5f, i) + i; }(), 2.5f);
|
||||||
|
fennec_test_run([]() -> float { float i; return fennec::modf(2.6f, i) + i; }(), 2.6f);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::isnan(0.0f), false);
|
||||||
|
fennec_test_run(fennec::isnan(fennec::numeric_limits<float>::quiet_NaN()), true);
|
||||||
|
fennec_test_run(fennec::isnan(fennec::numeric_limits<float>::signaling_NaN()), true);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::isinf( 0.0f), false);
|
||||||
|
fennec_test_run(fennec::isinf( fennec::numeric_limits<float>::infinity()), true);
|
||||||
|
fennec_test_run(fennec::isinf(-fennec::numeric_limits<float>::infinity()), true);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::floatBitsToInt( 1.0f), static_cast<int>(0x3f800000u));
|
||||||
|
fennec_test_run(fennec::floatBitsToInt(-1.0f), static_cast<int>(0xbf800000u));
|
||||||
|
fennec_test_run(fennec::floatBitsToUint( 1.0f), 0x3f800000u);
|
||||||
|
fennec_test_run(fennec::floatBitsToUint(-1.0f), 0xbf800000u);
|
||||||
|
|
||||||
|
fennec_test_spacer(1);
|
||||||
|
|
||||||
|
fennec_test_run(fennec::intBitsToFloat(static_cast<int>(0x3f800000u)), 1.0f);
|
||||||
|
fennec_test_run(fennec::intBitsToFloat(static_cast<int>(0xbf800000u)), -1.0f);
|
||||||
|
fennec_test_run(fennec::uintBitsToFloat(0x3f800000u), 1.0f);
|
||||||
|
fennec_test_run(fennec::uintBitsToFloat(0xbf800000u), -1.0f);
|
||||||
|
|
||||||
|
fennec_test_spacer(2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // FENNEC_TEST_MATH_COMMON_H
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
#ifndef FENNEC_TEST_MATH_H
|
#ifndef FENNEC_TEST_MATH_H
|
||||||
#define FENNEC_TEST_MATH_H
|
#define FENNEC_TEST_MATH_H
|
||||||
|
|
||||||
|
#include "math/test_common.h"
|
||||||
#include "math/test_scalar.h"
|
#include "math/test_scalar.h"
|
||||||
#include "math/test_vector.h"
|
#include "math/test_vector.h"
|
||||||
#include "math/test_matrix.h"
|
#include "math/test_matrix.h"
|
||||||
@@ -47,6 +48,10 @@ inline void fennec_test_math()
|
|||||||
fennec_test_math_matrix();
|
fennec_test_math_matrix();
|
||||||
fennec_test_spacer(3);
|
fennec_test_spacer(3);
|
||||||
|
|
||||||
|
fennec_test_subheader("common tests");
|
||||||
|
fennec_test_spacer(2);
|
||||||
|
fennec_test_math_common();
|
||||||
|
fennec_test_spacer(3);
|
||||||
|
|
||||||
fennec_test_subheader("geometric tests");
|
fennec_test_subheader("geometric tests");
|
||||||
fennec_test_spacer(2);
|
fennec_test_spacer(2);
|
||||||
|
|||||||
Reference in New Issue
Block a user