| | |
| | | /* |
| | | Simple DirectMedia Layer |
| | | Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org> |
| | | Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org> |
| | | |
| | | This software is provided 'as-is', without any express or implied |
| | | warranty. In no event will the authors be held liable for any damages |
| | |
| | | #include "SDL_test.h" |
| | | |
| | | /* Assert check message format */ |
| | | const char *SDLTest_AssertCheckFormat = "Assert '%s': %s"; |
| | | #define SDLTEST_ASSERT_CHECK_FORMAT "Assert '%s': %s" |
| | | |
| | | /* Assert summary message format */ |
| | | const char *SDLTest_AssertSummaryFormat = "Assert Summary: Total=%d Passed=%d Failed=%d"; |
| | | #define SDLTEST_ASSERT_SUMMARY_FORMAT "Assert Summary: Total=%d Passed=%d Failed=%d" |
| | | |
| | | /* ! \brief counts the failed asserts */ |
| | | static Uint32 SDLTest_AssertsFailed = 0; |
| | | static int SDLTest_AssertsFailed = 0; |
| | | |
| | | /* ! \brief counts the passed asserts */ |
| | | static Uint32 SDLTest_AssertsPassed = 0; |
| | | static int SDLTest_AssertsPassed = 0; |
| | | |
| | | /* |
| | | * Assert that logs and break execution flow on failures (i.e. for harness errors). |
| | | */ |
| | | void SDLTest_Assert(int assertCondition, const char *assertDescription, ...) |
| | | void SDLTest_Assert(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...) |
| | | { |
| | | va_list list; |
| | | va_list list; |
| | | char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; |
| | | |
| | | /* Print assert description into a buffer */ |
| | |
| | | va_end(list); |
| | | |
| | | /* Log, then assert and break on failure */ |
| | | SDL_assert((SDLTest_AssertCheck(assertCondition, logMessage))); |
| | | SDL_assert((SDLTest_AssertCheck(assertCondition, "%s", logMessage))); |
| | | } |
| | | |
| | | /* |
| | | * Assert that logs but does not break execution flow on failures (i.e. for test cases). |
| | | */ |
| | | int SDLTest_AssertCheck(int assertCondition, const char *assertDescription, ...) |
| | | int SDLTest_AssertCheck(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...) |
| | | { |
| | | va_list list; |
| | | char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; |
| | |
| | | if (assertCondition == ASSERT_FAIL) |
| | | { |
| | | SDLTest_AssertsFailed++; |
| | | SDLTest_LogError(SDLTest_AssertCheckFormat, logMessage, "Failed"); |
| | | SDLTest_LogError(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, "Failed"); |
| | | } |
| | | else |
| | | { |
| | | SDLTest_AssertsPassed++; |
| | | SDLTest_Log(SDLTest_AssertCheckFormat, logMessage, "Passed"); |
| | | SDLTest_Log(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, "Passed"); |
| | | } |
| | | |
| | | return assertCondition; |
| | |
| | | /* |
| | | * Explicitly passing Assert that logs (i.e. for test cases). |
| | | */ |
| | | void SDLTest_AssertPass(const char *assertDescription, ...) |
| | | void SDLTest_AssertPass(SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...) |
| | | { |
| | | va_list list; |
| | | char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; |
| | |
| | | |
| | | /* Log pass message */ |
| | | SDLTest_AssertsPassed++; |
| | | SDLTest_Log(SDLTest_AssertCheckFormat, logMessage, "Pass"); |
| | | SDLTest_Log(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, "Pass"); |
| | | } |
| | | |
| | | /* |
| | |
| | | */ |
| | | void SDLTest_LogAssertSummary() |
| | | { |
| | | Uint32 totalAsserts = SDLTest_AssertsPassed + SDLTest_AssertsFailed; |
| | | int totalAsserts = SDLTest_AssertsPassed + SDLTest_AssertsFailed; |
| | | if (SDLTest_AssertsFailed == 0) |
| | | { |
| | | SDLTest_Log(SDLTest_AssertSummaryFormat, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed); |
| | | SDLTest_Log(SDLTEST_ASSERT_SUMMARY_FORMAT, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed); |
| | | } |
| | | else |
| | | { |
| | | SDLTest_LogError(SDLTest_AssertSummaryFormat, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed); |
| | | SDLTest_LogError(SDLTEST_ASSERT_SUMMARY_FORMAT, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | /* vi: set ts=4 sw=4 expandtab: */ |