115 lines
3.7 KiB
C++
115 lines
3.7 KiB
C++
// =====================================================================================================================
|
|
// fennec-test, a program to execute unit tests for fennec
|
|
// 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_H
|
|
#define FENNEC_TEST_H
|
|
|
|
#include <iostream>
|
|
#include <ostream>
|
|
#include <string>
|
|
|
|
#include <fennec/lang/limits.h>
|
|
#include <fennec/math/common.h>
|
|
#include <fennec/math/matrix.h>
|
|
#include <fennec/math/relational.h>
|
|
#include <fennec/math/vector_traits.h>
|
|
|
|
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)
|
|
{
|
|
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)
|
|
{
|
|
os << "[ ";
|
|
((os << m[ColIndicesV] << " "), ...);
|
|
//[m]<size_t...i>(std::ostream& os, index_sequence<i...>) {
|
|
// ((os << fennec::row(m, i) << " "), ...);
|
|
//}(os, make_index_sequence<RowsV>{});
|
|
os << "]";
|
|
return os;
|
|
}
|
|
|
|
// 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;
|
|
|
|
// 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);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#define fennec_test_run(Expression, Expected) __fennec_test_run(#Expression, Expression, Expected)
|
|
|
|
#define fennec_test_header(Header) std::cout << std::string(80, '=') << std::endl \
|
|
<< (Header) << std::endl \
|
|
<< std::string(80, '=') << std::endl
|
|
|
|
#define fennec_test_subheader(Header) std::cout << (Header) << ' ' << std::string(80 - sizeof(Header), '=') << std::endl
|
|
#define fennec_test_section(Section) std::cout << (Section) << ' ' << std::string(80 - sizeof(Section), '-') << std::endl
|
|
|
|
#define fennec_test_spacer(Count) std::cout << std::string(Count, std::cout.widen('\n'))
|
|
|
|
#endif // FENNEC_TEST_H
|