From 74579fbadf0f89154cba5d9157a57f59fcda8f70 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 26 Aug 2019 21:38:34 +0200 Subject: Improved testing framework. (#4376) --- src/Globals.h | 62 ++++++++++++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/Globals.h b/src/Globals.h index e37f6dc60..890394e2e 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -296,61 +296,49 @@ template class SizeChecker; /** Faster than (int)floorf((float)x / (float)div) */ #define FAST_FLOOR_DIV(x, div) (((x) - (((x) < 0) ? ((div) - 1) : 0)) / (div)) -// Own version of assert() that writes failed assertions to the log for review +// Own version of ASSERT() that plays nicely with the testing framework #ifdef TEST_GLOBALS class cAssertFailure { - }; - - #ifdef _WIN32 - #if (defined(_MSC_VER) && defined(_DEBUG)) - #define DBG_BREAK _CrtDbgBreak() - #else - #define DBG_BREAK - #endif - #define REPORT_ERROR(FMT, ...) \ - { \ - AString msg = Printf(FMT, __VA_ARGS__); \ - puts(msg.c_str()); \ - fflush(stdout); \ - OutputDebugStringA(msg.c_str()); \ - DBG_BREAK; \ + AString mExpression; + AString mFileName; + int mLineNumber; + + public: + cAssertFailure(const AString & aExpression, const AString & aFileName, int aLineNumber): + mExpression(aExpression), + mFileName(aFileName), + mLineNumber(aLineNumber) + { } - #else - #define REPORT_ERROR(FMT, ...) \ - { \ - AString msg = Printf(FMT, __VA_ARGS__); \ - puts(msg.c_str()); \ - fflush(stdout); \ - } - #endif + + const AString & expression() const { return mExpression; } + const AString & fileName() const { return mFileName; } + int lineNumber() const { return mLineNumber; } + }; #ifdef _DEBUG - #define ASSERT(x) do { if (!(x)) { throw cAssertFailure();} } while (0) - #define testassert(x) do { if (!(x)) { REPORT_ERROR("Test failure: %s, file %s, line %d\n", #x, __FILE__, __LINE__); exit(1); } } while (0) - #define CheckAsserts(x) do { try {x} catch (cAssertFailure) { break; } REPORT_ERROR("Test failure: assert didn't fire for %s, file %s, line %d\n", #x, __FILE__, __LINE__); exit(1); } while (0) + #define ASSERT(x) do { if (!(x)) { throw cAssertFailure(#x, __FILE__, __LINE__);} } while (0) #else #define ASSERT(...) - #define testassert(...) - #define CheckAsserts(...) LOG("Assert checking is disabled in Release-mode executables (file %s, line %d)", __FILE__, __LINE__) #endif -#else + // Pretty much the same as ASSERT() but stays in Release builds + #define VERIFY(x) (!!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), exit(1), 0)) + +#else // TEST_GLOBALS + #ifdef _DEBUG #define ASSERT(x) ( !!(x) || ( LOGERROR("Assertion failed: %s, file %s, line %i", #x, __FILE__, __LINE__), PrintStackTrace(), assert(0), 0)) #else #define ASSERT(x) #endif -#endif -// Pretty much the same as ASSERT() but stays in Release builds -#define VERIFY( x) ( !!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), PrintStackTrace(), exit(1), 0)) + // Pretty much the same as ASSERT() but stays in Release builds + #define VERIFY(x) (!!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), PrintStackTrace(), exit(1), 0)) -// Same as assert but in all Self test builds -#ifdef SELF_TEST - #define assert_test(x) ( !!(x) || (assert(!#x), exit(1), 0)) -#endif +#endif // else TEST_GLOBALS /** Use to mark code that should be impossible to reach. */ #define UNREACHABLE(x) do { FLOGERROR("Hit unreachable code: {0}, file {1}, line {2}", #x, __FILE__, __LINE__); PrintStackTrace(); std::terminate(); } while (false) -- cgit v1.2.3