- Finished unit tests for core math library
- Adjusted formatting
This commit is contained in:
48
test/test.h
48
test/test.h
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user