Skip to content

Commit

Permalink
Added Unit test macros
Browse files Browse the repository at this point in the history
CHECK_FLOAT_SAME
CHECK_FLOAT_DIFFERENT
  • Loading branch information
John Wellbelove committed Aug 9, 2024
1 parent 3f018ee commit 344f1b2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/UnitTest++/CheckMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/UnitTest++/Checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 344f1b2

Please sign in to comment.