9#include <kernel/backend.hpp>
10#include <kernel/util/type_traits.hpp>
12#include <kernel/util/math.hpp>
24#define CHECK_INTERNAL(was_ok, message)\
26 throw FEAT::TestSystem::TestFailedException(__func__, __FILE__, __LINE__, message);
44 :
public std::exception
71 const char*
const function,
72 const char*
const file,
85 virtual const char*
what() const noexcept
override
111 TestList::_delete(*
_ptr);
122#ifdef FEAT_COMPILER_MICROSOFT
123# pragma warning(push)
124# pragma warning(disable: 4640)
130#ifdef FEAT_COMPILER_MICROSOFT
137 static void _delete(
TestList *
const ptr)
164 if (
nullptr == *instance_ptr)
172 if (
nullptr == *instance_ptr)
177 return *instance_ptr;
266 virtual void check(
const char *
const function,
const char *
const file,
267 const long line,
bool was_ok,
const String & message)
const
278 virtual void run()
const = 0;
297 virtual String get_preferred_backend_name()
const
303 template<
typename DT_>
306 return Math::pow(Math::eps<DT_>(), DT_(0.7));
310 inline double tol<double>()
312 return Math::pow(Math::eps<double>(), 0.7);
316 inline float tol<float>()
318 return Math::pow(Math::eps<float>(), 0.7F);
321#ifdef FEAT_HAVE_QUADMATH
323 inline __float128 tol<__float128>()
325 return Math::pow(Math::eps<__float128>(), __float128(0.7));
329#ifdef FEAT_HAVE_HALFMATH
331 inline Half tol<Half>()
337 template<
typename DT_>
340 return Math::pow(Math::eps<DT_>(), DT_(0.4));
344 inline double relaxed_tol<double>()
346 return Math::pow(Math::eps<double>(), 0.4);
350 inline float relaxed_tol<float>()
352 return Math::pow(Math::eps<float>(), 0.3F);
355#ifdef FEAT_HAVE_QUADMATH
357 inline __float128 relaxed_tol<__float128>()
359 return Math::pow(Math::eps<__float128>(), __float128(0.4));
363#ifdef FEAT_HAVE_HALFMATH
365 inline Half relaxed_tol<Half>()
374#define TEST_CHECK_EQUAL(a, b) \
376 CHECK_INTERNAL((a)==(b), this->_id + "\n" + "Expected '" #a "' to equal \n'" + FEAT::stringify(b) + "'\nbut got\n'" + FEAT::stringify(a) + "'")\
380#define TEST_CHECK_NOT_EQUAL(a, b) \
382 CHECK_INTERNAL(!((a)==(b)), this->_id + "\n" + "Expected '" #a "' that is'" + FEAT::stringify(a) + "' to equal not '" + FEAT::stringify(b) + "'")\
386#define TEST_CHECK_IN_RANGE(x, a, b) \
388 CHECK_INTERNAL(((a) <= (x)) && ((x) <= (b)), this->_id + "\n" + "Expected '" #x "' that is'" + FEAT::stringify(x) + "' to be in range [" + FEAT::stringify(a) + "," + FEAT::stringify(b) + "]")\
392#define TEST_CHECK_STRINGIFY_EQUAL(a, b) \
394 String s_a(FEAT::stringify(a)); \
395 String s_b(FEAT::stringify(b)); \
396 CHECK_INTERNAL(s_a == s_b, this->_id + "\n" + "Expected '" #a "' to equal '" + s_b + "'\nbut got\n'" + s_a + "'")\
400#define TEST_CHECK(a) \
402 CHECK_INTERNAL(a, this->_id + "\n" + "Check '" #a "' failed") \
406#define TEST_CHECK_MSG(a,msg) \
408 CHECK_INTERNAL(a, this->_id + "\n" + (msg))\
412#define TEST_CHECK_THROWS(a, b) \
417 this->check(__func__, __FILE__, __LINE__, false, \
418 this->_id + "\n" + "Expected exception of type '" #b "' not thrown"); \
422 } catch (const FEAT::TestSystem::TestFailedException &) { \
424 } catch (const std::exception & test_e) { \
425 throw FEAT::TestSystem::TestFailedException(__func__, __FILE__, __LINE__, \
426 "Test threw unexpected exception "+ FEAT::stringify(test_e.what()) + \
427 " inside a TEST_CHECK_THROWS block"); \
429 throw FEAT::TestSystem::TestFailedException(__func__, __FILE__, __LINE__, \
430 "Test threw unexpected unknown exception inside a TEST_CHECK_THROWS block"); \
435#define TEST_CHECK_EQUAL_WITHIN_EPS(a, b, eps) \
437 CHECK_INTERNAL(((a) <= ((b) + (eps))) && ((b) <= ((a) + (eps))), \
438 this->_id + "\n" + "Expected '|" #a " - " #b \
439 "|' <= '" + FEAT::stringify(eps) + "' but was '" + \
440 FEAT::stringify((a) < (b) ? (b) - (a) : (a) - (b)) + "'" \
441 + ", with " #a "=" + FEAT::stringify(a) + " and " #b "=" + FEAT::stringify(b))\
445#define TEST_CHECK_RELATIVE(a, b, eps) \
447 CHECK_INTERNAL(Math::abs((a) - (b)) <= (eps)*Math::abs(b), \
448 this->_id + "\n" + "Expected '|" #a " - " #b \
449 "|' <= '" + FEAT::stringify(eps) + "*|" #b "|' = '" + \
450 FEAT::stringify((eps)*Math::abs(b)) + "' but was '" + \
451 FEAT::stringify(Math::abs((a) - (b))) + "'" \
452 + ", with " #a "=" + FEAT::stringify(a) + " and " #b "=" + FEAT::stringify(b))\
456#define TEST_CHECK_LESS_THAN(a, b) \
458 CHECK_INTERNAL((a) < (b), this->_id + "\n" + "Expected '" #a "' to be less than \n'" + FEAT::stringify(b) + "'\nbut got\n'" + FEAT::stringify(a) + "'")\
462#define TEST(pre, test, post) \
467 } catch (const FEAT::TestSystem::TestFailedException & test_e) { \
String class implementation.
exception thrown by the check method in UnitTest
virtual const char * what() const noexcept override
description
const String _message
failure message
virtual ~TestFailedException() noexcept
DTOR.
TestFailedException(const char *const function, const char *const file, const long line, const String &message)
CTOR.
TestList **const _ptr
Pointer to our managed object.
~DeleteOnDestruction()
Destructor.
DeleteOnDestruction(TestList **const ptr)
Constructor.
list of all instantiated tests
static TestList * instance()
Returns the instance.
std::list< UnitTest * > _tests
internal STL list representation of TestList
static TestList ** _instance_ptr()
Returns a pointer to our instance pointer.
Iterator begin_tests()
returns an iterator to the front of the TestList
std::list< UnitTest * >::iterator Iterator
TestList forward iterator.
void register_test(UnitTest *const test)
adds a test to the TestList
size_t size()
returns the size of the TestList
Iterator erase(Iterator i)
removes iterator target from the TestList
Iterator end_tests()
returns an iterator beyond the last element of the TestList
virtual const String id() const
returns our id string
String _index_name
index type description String
virtual String get_index_name()
returns our target platform
const String _id
test description String
UnitTest(const String &id_in, const String &datatype_name="none", const String &index_name="none", PreferredBackend preferred_backend=PreferredBackend::generic)
CTOR.
virtual void run() const =0
runs the test case
virtual void check(const char *const function, const char *const file, const long line, bool was_ok, const String &message) const
utility method used bei TEST_CHECK_THROWS
virtual String get_datatype_name()
returns our target platform
String _datatype_name
precision description String
PreferredBackend _preferred_backend
preferred compute intensive backend
T_ pow(T_ x, T_ y)
Returns x raised to the power of y.
PreferredBackend
The backend that shall be used in all compute heavy calculations.
String stringify(const T_ &item)
Converts an item into a String.
__half Half
Half data type.