- Finished unit tests for core math library

- Adjusted formatting
This commit is contained in:
2025-07-02 10:17:35 -04:00
parent 516d9f4977
commit 9010650ceb
16 changed files with 446 additions and 259 deletions

View File

@@ -17,6 +17,8 @@ add_executable(fennec-test main.cpp
tests/lang/test_sequences.h
tests/math/test_common.h
tests/math/test_exponential.h
tests/math/test_relational.h
tests/math/test_trigonometric.h
)
target_link_libraries(fennec-test PRIVATE

View File

@@ -18,12 +18,12 @@
#include <iostream>
#include "test.h"
#include "tests/test_lang.h"
#include "tests/test_math.h"
#include "tests/test_memory.h"
#include "test.h"
int main(int, char **)
{

View File

@@ -35,6 +35,7 @@ namespace fennec
namespace test
{
// Helper for printing vectors
template<typename ScalarT, size_t...IndicesV>
inline std::ostream& operator<<(std::ostream& os, const vector<ScalarT, IndicesV...>& v)
{
@@ -44,15 +45,7 @@ inline std::ostream& operator<<(std::ostream& os, const vector<ScalarT, IndicesV
return os;
}
template<typename ScalarT, size_t...IndicesV>
inline std::ostream& operator<<(std::ostream& os, vector<ScalarT, IndicesV...>&& v)
{
os << "< ";
((os << v[IndicesV] << " "), ...);
os << ">";
return os;
}
// Helper for printing matrices
template<typename ScalarT, size_t RowsV, size_t...ColIndicesV>
inline std::ostream& operator<<(std::ostream& os, const matrix<ScalarT, RowsV, ColIndicesV...>& m)
{
@@ -65,30 +58,41 @@ inline std::ostream& operator<<(std::ostream& os, const matrix<ScalarT, RowsV, C
return os;
}
template<typename ScalarT, size_t...IndicesV>
inline bool operator<=(const vector<ScalarT, IndicesV...>& lhs, const vector<ScalarT, IndicesV...>& rhs)
{
return fennec::all(fennec::lessThanEqual(lhs, rhs));
}
// Core test function
template<typename ResultT>
inline void __fennec_test_run(const std::string& expression, const ResultT result, const ResultT expected)
{
// Print out the expression as a string and the resulting value
std::cout << std::boolalpha;
std::cout << '\t' << expression << " = " << result;
bool passed = false;
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();
else
passed = result == expected;
bool passed = false;
// Check scalar arithmetic values
if constexpr(is_arithmetic_v<ResultT>)
{
passed = result == expected;
passed |= fennec::abs(expected - result) <= numeric_limits<ResultT>::epsilon();
}
// Check non-boolean vector values
else if constexpr(is_vector_v<ResultT> && not(is_bool_v<typename ResultT::scalar_t>))
{
passed = result == expected;
passed |= fennec::all(fennec::lessThanEqual(fennec::abs(expected - result), ResultT(numeric_limits<typename ResultT::scalar_t>::epsilon())));
}
// Base Case, simple comparison
else
{
passed = result == expected;
}
// Print out expected when failed
if (not passed)
std::cout << ", expected " << expected;
std::cout << std::endl;
// Assert to halt and get some debug info
assert(passed);
}

View File

@@ -40,6 +40,8 @@ inline void fennec_test_math_common()
fennec_test_run(fennec::abs( 1.0), 1.0);
fennec_test_run(fennec::abs(-1.0), 1.0);
fennec_test_run(fennec::abs(vec2(1.0f, -1.0f)), vec2(1.0f));
fennec_test_spacer(1);
fennec_test_run(fennec::sign(vec2( 1, -2)), vec2( 1, -1));
@@ -165,14 +167,12 @@ inline void fennec_test_math_common()
fennec_test_spacer(1);
fennec_test_run(min(2.0f, 1.0f), 1.0f);
fennec_test_run(min(-fennec::numeric_limits<float>::infinity(), 0.0f), -fennec::numeric_limits<float>::infinity());
fennec_test_run(min( fennec::numeric_limits<float>::quiet_NaN(), -1.0f), -1.0f);
fennec_test_run(min(-fennec::numeric_limits<float>::infinity(), 0.0f), -fennec::numeric_limits<float>::infinity());
fennec_test_spacer(1);
fennec_test_run(max(2.0f, 1.0f), 2.0f);
fennec_test_run(max(-fennec::numeric_limits<float>::infinity(), 0.0f), 0.0f);
fennec_test_run(max( fennec::numeric_limits<float>::quiet_NaN(), -1.0f), -1.0f);
fennec_test_run(max(-fennec::numeric_limits<float>::infinity(), 0.0f), 0.0f);
fennec_test_spacer(1);

View File

@@ -32,8 +32,6 @@ namespace test
inline void fennec_test_math_exponential()
{
fennec_test_spacer(1);
fennec_test_run(fennec::pow(1.0f, 2.0f), 1.0f);
fennec_test_run(fennec::pow(2.0f, 0.0f), 1.0f);
fennec_test_run(fennec::pow(2.0f, 1.0f), 2.0f);

View File

@@ -68,6 +68,18 @@ inline void fennec_test_math_geometric()
fennec_test_run(fennec::normalize(vec3(1, 1, 1)), vec3(sqrt(3.0f) / 3.0f, sqrt(3.0f) / 3.0f, sqrt(3.0f) / 3.0f));
fennec_test_run(fennec::normalize(vec4(1, 1, 1, 1)), vec4(0.5f, 0.5f, 0.5f, 0.5f));
fennec_test_spacer(1);
fennec_test_run(fennec::faceforward(vec3(1, 0, 0), vec3(-1, 0, 0), vec3(1, 0, 0)), vec3(-1, 0, 0));
fennec_test_run(fennec::faceforward(vec3(1, 0, 0), vec3(-1, 0, 0), vec3(-1, 0, 0)), vec3( 1, 0, 0));
fennec_test_spacer(1);
fennec_test_run(fennec::reflect(vec2(1, -1), vec2(0, 1)), vec2(1, 1));
fennec_test_run(fennec::refract(vec2(1, -1), vec2(0, 1), 1/1.33f), vec2(0.7518797, -1));
fennec_test_spacer(2);
}
}

View File

@@ -120,7 +120,7 @@ inline void fennec_test_math_matrix()
fennec_test_header("matrix equality operators");
fennec_test_section("matrix equality operators");
fennec_test_spacer(1);
@@ -138,7 +138,7 @@ inline void fennec_test_math_matrix()
fennec_test_header("matrix constructors");
fennec_test_section("matrix constructors");
fennec_test_spacer(1);
@@ -156,7 +156,7 @@ inline void fennec_test_math_matrix()
fennec_test_header("matrix-scalar operators");
fennec_test_section("matrix-scalar operators");
fennec_test_spacer(1);
@@ -169,7 +169,7 @@ inline void fennec_test_math_matrix()
fennec_test_header("matrix-vector operators");
fennec_test_section("matrix-vector operators");
fennec_test_spacer(1);
@@ -187,7 +187,7 @@ inline void fennec_test_math_matrix()
fennec_test_header("matrix-matrix operators");
fennec_test_section("matrix-matrix operators");
fennec_test_spacer(1);
@@ -315,11 +315,13 @@ inline void fennec_test_math_matrix()
fennec_test_section("inverse");
fennec_test_spacer(2);
fennec_test_spacer(1);
fennec_test_run(fennec::inverse(mat2(1, 2, 3, 4)), mat2(-2, 1, 1.5, -0.5));
fennec_test_run(fennec::inverse(mat3(1, 2, 3, 0, 1, 4, 5, 6, 0)), mat3(-24, 18, 5, 20, -15, -4, -5, 4, 1));
fennec_test_run(fennec::inverse(mat4(2, 1, -3, 4, -1, 0, 2, 5, 3, 2, 1, 0, 4, -2, 3, 1)), (1.0f / 414.0f) * mat4(36, -39, 33, 51, -18, 31, 133, -83, -72, 55, 49, 13, 36, 53, -13, 5));
fennec_test_spacer(2);
}
}

View File

@@ -0,0 +1,56 @@
// =====================================================================================================================
// 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_RELATIONAL_H
#define FENNEC_TEST_MATH_RELATIONAL_H
#include "../../test.h"
namespace fennec
{
namespace test
{
inline void fennec_test_math_relational()
{
fennec_test_run(fennec::lessThan(vec3(3, 2, 1), vec3(1, 2, 3)), bvec3(false, false, true));
fennec_test_run(fennec::lessThanEqual(vec3(3, 2, 1), vec3(1, 2, 3)), bvec3(false, true, true));
fennec_test_spacer(1);
fennec_test_run(fennec::greaterThan(vec3(3, 2, 1), vec3(1, 2, 3)), bvec3(true, false, false));
fennec_test_run(fennec::greaterThanEqual(vec3(3, 2, 1), vec3(1, 2, 3)), bvec3(true, true, false));
fennec_test_spacer(1);
fennec_test_run(fennec::equal(vec3(3, 2, 1), vec3(1, 2, 3)), bvec3(false, true, false));
fennec_test_run(fennec::notEqual(vec3(3, 2, 1), vec3(1, 2, 3)), bvec3(true, false, true));
fennec_test_spacer(1);
fennec_test_run(not(bvec3(true, false, true)), bvec3(false, true, false));
fennec_test_run(fennec::all(bvec3(true, true, true)), true);
fennec_test_spacer(2);
}
}
}
#endif // FENNEC_TEST_MATH_RELATIONAL_H

View File

@@ -0,0 +1,126 @@
// =====================================================================================================================
// 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_TRIGONOMETRIC_H
#define FENNEC_TEST_MATH_TRIGONOMETRIC_H
#include <fennec/math/trigonometric.h>
#include <fennec/math/ext/constants.h>
#include "../../test.h"
namespace fennec
{
namespace test
{
inline void fennec_test_math_trigonometric()
{
fennec_test_section("angle conversions");
fennec_test_spacer(1);
fennec_test_run(fennec::radians( 45.0f), fennec::quarter_pi<float>());
fennec_test_run(fennec::radians( 90.0f), fennec::half_pi<float>());
fennec_test_run(fennec::radians(180.0f), fennec::pi<float>());
fennec_test_run(fennec::radians(270.0f), fennec::three_halves_pi<float>());
fennec_test_run(fennec::radians(360.0f), fennec::two_pi<float>());
fennec_test_spacer(1);
fennec_test_run(fennec::degrees(fennec::quarter_pi<float>()), 45.0f);
fennec_test_run(fennec::degrees(fennec::half_pi<float>()), 90.0f);
fennec_test_run(fennec::degrees(fennec::pi<float>()), 180.0f);
fennec_test_run(fennec::degrees(fennec::three_halves_pi<float>()), 270.0f);
fennec_test_run(fennec::degrees(fennec::two_pi<float>()), 360.0f);
fennec_test_spacer(2);
fennec_test_section("trig functions");
fennec_test_spacer(1);
fennec_test_run(fennec::sin(fennec::sixth_pi<float>()), one_half<float>());
fennec_test_run(fennec::cos(fennec::sixth_pi<float>()), sqrt_three<float>() / 2.0f);
fennec_test_run(fennec::tan(fennec::sixth_pi<float>()), sqrt_three<float>() / 3.0f);
fennec_test_spacer(1);
fennec_test_run(fennec::asin(one_half<float>()), fennec::sixth_pi<float>());
fennec_test_run(fennec::acos(sqrt_three<float>() / 2.0f), fennec::sixth_pi<float>());
fennec_test_run(fennec::atan(sqrt_three<float>() / 3.0f), fennec::sixth_pi<float>());
fennec_test_spacer(2);
fennec_test_run(fennec::sin(fennec::quarter_pi<float>()), sqrt_two<float>() / 2.0f);
fennec_test_run(fennec::cos(fennec::quarter_pi<float>()), sqrt_two<float>() / 2.0f);
fennec_test_run(fennec::tan(fennec::quarter_pi<float>()), 1.0f);
fennec_test_spacer(1);
fennec_test_run(fennec::asin(sqrt_two<float>() / 2.0f), fennec::quarter_pi<float>());
fennec_test_run(fennec::acos(sqrt_two<float>() / 2.0f), fennec::quarter_pi<float>());
fennec_test_run(fennec::atan(1.0f), fennec::quarter_pi<float>());
fennec_test_spacer(2);
fennec_test_section("hyperbolic functions");
fennec_test_spacer(1);
fennec_test_run(fennec::sinh(0.0f), 0.0f);
fennec_test_run(fennec::cosh(0.0f), 1.0f);
fennec_test_run(fennec::tanh(0.0f), 0.0f);
fennec_test_spacer(1);
fennec_test_run(fennec::asinh(0.0f), 0.0f);
fennec_test_run(fennec::acosh(1.0f), 0.0f);
fennec_test_run(fennec::atanh(0.0f), 0.0f);
fennec_test_spacer(2);
fennec_test_run(fennec::sinh(1.0f), (fennec::e<float>() - fennec::one_over_e<float>()) / 2.0f);
fennec_test_run(fennec::cosh(1.0f), (fennec::e<float>() + fennec::one_over_e<float>()) / 2.0f);
fennec_test_run(fennec::tanh(1.0f), ((fennec::e_raised_two<float>() - 1) / (fennec::e_raised_two<float>() + 1)));
fennec_test_spacer(1);
fennec_test_run(fennec::asinh((fennec::e<float>() - fennec::one_over_e<float>()) / 2.0f), 1.0f);
fennec_test_run(fennec::acosh((fennec::e<float>() + fennec::one_over_e<float>()) / 2.0f), 1.0f);
fennec_test_run(fennec::atanh(((fennec::e_raised_two<float>() - 1) / (fennec::e_raised_two<float>() + 1))), 1.0f);
fennec_test_spacer(2);
}
}
}
#endif // FENNEC_TEST_MATH_TRIGONOMETRIC_H

View File

@@ -26,6 +26,8 @@
#include "math/test_common.h"
#include "math/test_exponential.h"
#include "math/test_geometric.h"
#include "math/test_relational.h"
#include "math/test_trigonometric.h"
namespace fennec
{
@@ -64,6 +66,16 @@ inline void fennec_test_math()
fennec_test_spacer(2);
fennec_test_math_geometric();
fennec_test_spacer(3);
fennec_test_subheader("relational tests");
fennec_test_spacer(2);
fennec_test_math_relational();
fennec_test_spacer(3);
fennec_test_subheader("trigonometric tests");
fennec_test_spacer(2);
fennec_test_math_trigonometric();
fennec_test_spacer(3);
}
}