Added Unit test macros

CHECK_FLOAT_SAME
CHECK_FLOAT_DIFFERENT
This commit is contained in:
John Wellbelove 2024-08-09 21:36:15 +01:00
parent 3f018ee1a7
commit 344f1b2387
2 changed files with 67 additions and 0 deletions

View File

@ -160,6 +160,48 @@
}) \
UNITTEST_MULTILINE_MACRO_END
#define UNITTEST_CHECK_FLOAT_SAME(expected, actual) \
UNITTEST_MULTILINE_MACRO_BEGIN \
UNITTEST_IMPL_TRY \
({ \
UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, 0, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
}) \
UNITTEST_IMPL_RETHROW (UnitTest::RequiredCheckException) \
UNITTEST_IMPL_CATCH (std::exception, exc, \
{ \
UnitTest::MemoryOutStream UnitTest_message; \
UnitTest_message << "Unhandled exception (" << exc.what() << ") in CHECK_FLOAT_SAME(" #expected ", " #actual ")"; \
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
UnitTest_message.GetText()); \
}) \
UNITTEST_IMPL_CATCH_ALL \
({ \
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
"Unhandled exception in CHECK_FLOAT_SAME(" #expected ", " #actual ")"); \
}) \
UNITTEST_MULTILINE_MACRO_END
#define UNITTEST_CHECK_FLOAT_DIFFERENT(expected, actual) \
UNITTEST_MULTILINE_MACRO_BEGIN \
UNITTEST_IMPL_TRY \
({ \
UnitTest::CheckNotClose(*UnitTest::CurrentTest::Results(), expected, actual, 0, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
}) \
UNITTEST_IMPL_RETHROW (UnitTest::RequiredCheckException) \
UNITTEST_IMPL_CATCH (std::exception, exc, \
{ \
UnitTest::MemoryOutStream UnitTest_message; \
UnitTest_message << "Unhandled exception (" << exc.what() << ") in CHECK_FLOAT_DIFFERENT(" #expected ", " #actual ")"; \
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
UnitTest_message.GetText()); \
}) \
UNITTEST_IMPL_CATCH_ALL \
({ \
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
"Unhandled exception in CHECK_FLOAT_DIFFERENT(" #expected ", " #actual ")"); \
}) \
UNITTEST_MULTILINE_MACRO_END
#define UNITTEST_CHECK_ARRAY_EQUAL(expected, actual, count) \
UNITTEST_MULTILINE_MACRO_BEGIN \
UNITTEST_IMPL_TRY \
@ -272,6 +314,18 @@
#define CHECK_CLOSE(expected, actual, tolerance) UNITTEST_CHECK_CLOSE((expected), (actual), (tolerance))
#endif
#ifdef CHECK_FLOAT_SAME
#error CHECK_FLOAT_SAME already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK_FLOAT_SAME instead
#else
#define CHECK_FLOAT_SAME(expected, actual) UNITTEST_CHECK_FLOAT_SAME((expected), (actual))
#endif
#ifdef CHECK_FLOAT_DIFFERENT
#error CHECK_FLOAT_DIFFERENT already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK_FLOAT_DIFFERENT instead
#else
#define CHECK_FLOAT_DIFFERENT(expected, actual) UNITTEST_CHECK_FLOAT_DIFFERENT((expected), (actual))
#endif
#ifdef CHECK_ARRAY_EQUAL
#error CHECK_ARRAY_EQUAL already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_CHECK_ARRAY_EQUAL instead
#else

View File

@ -265,6 +265,19 @@ namespace UnitTest
}
}
template< typename Expected, typename Actual, typename Tolerance >
void CheckNotClose(TestResults& results, Expected const& expected, Actual const& actual, Tolerance const& tolerance,
TestDetails const& details)
{
if (AreClose(expected, actual, tolerance))
{
UnitTest::MemoryOutStream stream;
stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;
results.OnTestFailure(details, stream.GetText());
}
}
template< typename Expected, typename Actual >
void CheckArrayEqual(TestResults& results, Expected const& expected, Actual const& actual,
size_t const count, TestDetails const& details)