fennec/test/test.h
Medusa Slockbower 4d8466851c - Added More Documentation
- Added some more notes to the planning doc regarding shared libraries
 - Started adding unit tests for the C++ lang library.
2025-06-19 15:16:29 -04:00

104 lines
3.3 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/relational.h>
namespace fennec
{
namespace test
{
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;
}
template<typename ScalarT, size_t...IndicesV>
inline std::ostream& operator<<(std::ostream& os, vector<ScalarT, IndicesV...>&& v)
{
os << "< ";
((os << v[IndicesV] << " "), ...);
os << ">";
return os;
}
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] << " "), ...);
os << " ]";
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));
}
template<typename ResultT>
inline void __fennec_test_run(const std::string& expression, const ResultT result, const ResultT expected)
{
std::cout << std::boolalpha;
std::cout << '\t' << expression << " = " << result;
bool passed = false;
if constexpr(is_arithmetic_v<ResultT> or is_vector_v<ResultT>)
passed = fennec::abs(expected - result) <= numeric_limits<ResultT>::epsilon();
else
passed = result == expected;
if (not passed)
std::cout << ", expected " << expected;
std::cout << std::endl;
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