diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/test/SDL_test_assert.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/test/SDL_test_assert.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/test/SDL_test_assert.c | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/test/SDL_test_assert.c b/contrib/SDL-3.2.8/src/test/SDL_test_assert.c new file mode 100644 index 0000000..970e986 --- /dev/null +++ b/contrib/SDL-3.2.8/src/test/SDL_test_assert.c | |||
| @@ -0,0 +1,157 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 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 | |||
| 29 | /* Enable to have color in logs */ | ||
| 30 | #if 1 | ||
| 31 | #define COLOR_RED "\033[0;31m" | ||
| 32 | #define COLOR_GREEN "\033[0;32m" | ||
| 33 | #define COLOR_YELLOW "\033[0;93m" | ||
| 34 | #define COLOR_BLUE "\033[0;94m" | ||
| 35 | #define COLOR_END "\033[0m" | ||
| 36 | #else | ||
| 37 | #define COLOR_RED "" | ||
| 38 | #define COLOR_GREEN "" | ||
| 39 | #define COLOR_BLUE "" | ||
| 40 | #define COLOR_YELLOW "" | ||
| 41 | #define COLOR_END "" | ||
| 42 | #endif | ||
| 43 | |||
| 44 | /* Assert check message format */ | ||
| 45 | #define SDLTEST_ASSERT_CHECK_FORMAT "Assert '%s': %s" | ||
| 46 | |||
| 47 | /* Assert summary message format */ | ||
| 48 | #define SDLTEST_ASSERT_SUMMARY_FORMAT "Assert Summary: Total=%d " COLOR_GREEN "Passed=%d" COLOR_END " " COLOR_RED "Failed=%d" COLOR_END | ||
| 49 | #define SDLTEST_ASSERT_SUMMARY_FORMAT_OK "Assert Summary: Total=%d " COLOR_GREEN "Passed=%d" COLOR_END " " COLOR_GREEN "Failed=%d" COLOR_END | ||
| 50 | |||
| 51 | /* ! counts the failed asserts */ | ||
| 52 | static int SDLTest_AssertsFailed = 0; | ||
| 53 | |||
| 54 | /* ! counts the passed asserts */ | ||
| 55 | static int SDLTest_AssertsPassed = 0; | ||
| 56 | |||
| 57 | /* | ||
| 58 | * Assert that logs and break execution flow on failures (i.e. for harness errors). | ||
| 59 | */ | ||
| 60 | void SDLTest_Assert(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...) | ||
| 61 | { | ||
| 62 | va_list list; | ||
| 63 | char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; | ||
| 64 | |||
| 65 | /* Print assert description into a buffer */ | ||
| 66 | SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH); | ||
| 67 | va_start(list, assertDescription); | ||
| 68 | (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list); | ||
| 69 | va_end(list); | ||
| 70 | |||
| 71 | /* Log, then assert and break on failure */ | ||
| 72 | SDL_assert((SDLTest_AssertCheck(assertCondition, "%s", logMessage))); | ||
| 73 | } | ||
| 74 | |||
| 75 | /* | ||
| 76 | * Assert that logs but does not break execution flow on failures (i.e. for test cases). | ||
| 77 | */ | ||
| 78 | int SDLTest_AssertCheck(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...) | ||
| 79 | { | ||
| 80 | va_list list; | ||
| 81 | char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; | ||
| 82 | |||
| 83 | /* Print assert description into a buffer */ | ||
| 84 | SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH); | ||
| 85 | va_start(list, assertDescription); | ||
| 86 | (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list); | ||
| 87 | va_end(list); | ||
| 88 | |||
| 89 | /* Log pass or fail message */ | ||
| 90 | if (assertCondition == ASSERT_FAIL) { | ||
| 91 | SDLTest_AssertsFailed++; | ||
| 92 | SDLTest_LogError(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, COLOR_RED "Failed" COLOR_END); | ||
| 93 | } else { | ||
| 94 | SDLTest_AssertsPassed++; | ||
| 95 | SDLTest_Log(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, COLOR_GREEN "Passed" COLOR_END); | ||
| 96 | } | ||
| 97 | |||
| 98 | return assertCondition; | ||
| 99 | } | ||
| 100 | |||
| 101 | /* | ||
| 102 | * Explicitly passing Assert that logs (i.e. for test cases). | ||
| 103 | */ | ||
| 104 | void SDLTest_AssertPass(SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...) | ||
| 105 | { | ||
| 106 | va_list list; | ||
| 107 | char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH]; | ||
| 108 | |||
| 109 | /* Print assert description into a buffer */ | ||
| 110 | SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH); | ||
| 111 | va_start(list, assertDescription); | ||
| 112 | (void)SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list); | ||
| 113 | va_end(list); | ||
| 114 | |||
| 115 | /* Log pass message */ | ||
| 116 | SDLTest_AssertsPassed++; | ||
| 117 | SDLTest_Log(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, COLOR_GREEN "Passed" COLOR_END); | ||
| 118 | } | ||
| 119 | |||
| 120 | /* | ||
| 121 | * Resets the assert summary counters to zero. | ||
| 122 | */ | ||
| 123 | void SDLTest_ResetAssertSummary(void) | ||
| 124 | { | ||
| 125 | SDLTest_AssertsPassed = 0; | ||
| 126 | SDLTest_AssertsFailed = 0; | ||
| 127 | } | ||
| 128 | |||
| 129 | /* | ||
| 130 | * Logs summary of all assertions (total, pass, fail) since last reset | ||
| 131 | * as INFO (failed==0) or ERROR (failed > 0). | ||
| 132 | */ | ||
| 133 | void SDLTest_LogAssertSummary(void) | ||
| 134 | { | ||
| 135 | int totalAsserts = SDLTest_AssertsPassed + SDLTest_AssertsFailed; | ||
| 136 | if (SDLTest_AssertsFailed == 0) { | ||
| 137 | SDLTest_Log(SDLTEST_ASSERT_SUMMARY_FORMAT_OK, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed); | ||
| 138 | } else { | ||
| 139 | SDLTest_LogError(SDLTEST_ASSERT_SUMMARY_FORMAT, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | /* | ||
| 144 | * Converts the current assert state into a test result | ||
| 145 | */ | ||
| 146 | int SDLTest_AssertSummaryToTestResult(void) | ||
| 147 | { | ||
| 148 | if (SDLTest_AssertsFailed > 0) { | ||
| 149 | return TEST_RESULT_FAILED; | ||
| 150 | } else { | ||
| 151 | if (SDLTest_AssertsPassed > 0) { | ||
| 152 | return TEST_RESULT_PASSED; | ||
| 153 | } else { | ||
| 154 | return TEST_RESULT_NO_ASSERT; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
