Atlas - SDL_test_assert.c
Home / ext / SDL / src / test Lines: 1 | Size: 4889 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22/* 23 24 Used by the test framework and test cases. 25 26*/ 27#include <SDL3/SDL_test.h> 28#include "SDL_test_internal.h" 29 30/* ! counts the failed asserts */ 31static int SDLTest_AssertsFailed = 0; 32 33/* ! counts the passed asserts */ 34static int SDLTest_AssertsPassed = 0; 35 36static void SDLTest_LogAssertMessage(bool success, const char *assertion) 37{ 38 SDL_LogPriority priority; 39 const char *color; 40 const char *message; 41 42 if (success) { 43 priority = SDL_LOG_PRIORITY_INFO; 44 color = COLOR_GREEN; 45 message = "Passed"; 46 } else { 47 priority = SDL_LOG_PRIORITY_ERROR; 48 color = COLOR_RED; 49 message = "Failed"; 50 } 51 SDLTest_LogMessage(priority, "Assert '%s': %s%s%s", assertion, color, message, COLOR_END); 52} 53 54/* 55 * Assert that logs and break execution flow on failures (i.e. for harness errors). 56 */ 57void SDLTest_Assert(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...) 58{ 59 va_list list; 60 char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; 61 62 /* Print assert description into a buffer */ 63 SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH); 64 va_start(list, assertDescription); 65 (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list); 66 va_end(list); 67 68 /* Log, then assert and break on failure */ 69 SDL_assert((SDLTest_AssertCheck(assertCondition, "%s", logMessage))); 70} 71 72/* 73 * Assert that logs but does not break execution flow on failures (i.e. for test cases). 74 */ 75int SDLTest_AssertCheck(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...) 76{ 77 va_list list; 78 char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; 79 80 /* Print assert description into a buffer */ 81 SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH); 82 va_start(list, assertDescription); 83 (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list); 84 va_end(list); 85 86 /* Log pass or fail message */ 87 if (assertCondition == ASSERT_FAIL) { 88 SDLTest_AssertsFailed++; 89 SDLTest_LogAssertMessage(false, logMessage); 90 } else { 91 SDLTest_AssertsPassed++; 92 SDLTest_LogAssertMessage(true, logMessage); 93 } 94 95 return assertCondition; 96} 97 98/* 99 * Explicitly passing Assert that logs (i.e. for test cases). 100 */ 101void SDLTest_AssertPass(SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...) 102{ 103 va_list list; 104 char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; 105 106 /* Print assert description into a buffer */ 107 SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH); 108 va_start(list, assertDescription); 109 (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list); 110 va_end(list); 111 112 /* Log pass message */ 113 SDLTest_AssertsPassed++; 114 SDLTest_LogAssertMessage(true, logMessage); 115} 116 117/* 118 * Resets the assert summary counters to zero. 119 */ 120void SDLTest_ResetAssertSummary(void) 121{ 122 SDLTest_AssertsPassed = 0; 123 SDLTest_AssertsFailed = 0; 124} 125 126/* 127 * Logs summary of all assertions (total, pass, fail) since last reset 128 * as INFO (failed==0) or ERROR (failed > 0). 129 */ 130void SDLTest_LogAssertSummary(void) 131{ 132 int totalAsserts = SDLTest_AssertsPassed + SDLTest_AssertsFailed; 133 bool success = SDLTest_AssertsFailed == 0; 134 135 SDLTest_LogMessage(success ? SDL_LOG_PRIORITY_INFO : SDL_LOG_PRIORITY_ERROR, 136 "Assert Summary: Total=%d " "%s" "Passed=%d" "%s" " " "%s" "Failed=%d" "%s", 137 totalAsserts, COLOR_GREEN, SDLTest_AssertsPassed, COLOR_END, 138 success ? COLOR_GREEN : COLOR_RED, SDLTest_AssertsFailed, COLOR_END); 139} 140 141/* 142 * Converts the current assert state into a test result 143 */ 144int SDLTest_AssertSummaryToTestResult(void) 145{ 146 if (SDLTest_AssertsFailed > 0) { 147 return TEST_RESULT_FAILED; 148 } else { 149 if (SDLTest_AssertsPassed > 0) { 150 return TEST_RESULT_PASSED; 151 } else { 152 return TEST_RESULT_NO_ASSERT; 153 } 154 } 155} 156[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.