Atlas - testautomation_timer.c

Home / ext / SDL / test Lines: 1 | Size: 6571 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/** 2 * Timer test suite 3 */ 4#include <SDL3/SDL.h> 5#include <SDL3/SDL_test.h> 6#include "testautomation_suites.h" 7 8#ifndef SDL_PLATFORM_EMSCRIPTEN 9 10/* Flag indicating if the param should be checked */ 11static int g_paramCheck = 0; 12 13/* Userdata value to check */ 14static int g_paramValue = 0; 15 16/* Flag indicating that the callback was called */ 17static int g_timerCallbackCalled = 0; 18 19#endif 20 21/* Fixture */ 22 23static void SDLCALL timerSetUp(void **arg) 24{ 25} 26 27/* Test case functions */ 28 29/** 30 * Call to SDL_GetPerformanceCounter 31 */ 32static int SDLCALL timer_getPerformanceCounter(void *arg) 33{ 34 Uint64 result; 35 36 result = SDL_GetPerformanceCounter(); 37 SDLTest_AssertPass("Call to SDL_GetPerformanceCounter()"); 38 SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result); 39 40 return TEST_COMPLETED; 41} 42 43/** 44 * Call to SDL_GetPerformanceFrequency 45 */ 46static int SDLCALL timer_getPerformanceFrequency(void *arg) 47{ 48 Uint64 result; 49 50 result = SDL_GetPerformanceFrequency(); 51 SDLTest_AssertPass("Call to SDL_GetPerformanceFrequency()"); 52 SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result); 53 54 return TEST_COMPLETED; 55} 56 57/** 58 * Call to SDL_Delay and SDL_GetTicks 59 */ 60static int SDLCALL timer_delayAndGetTicks(void *arg) 61{ 62 const int testDelay = 100; 63 const int marginOfError = 25; 64 Uint64 result; 65 Uint64 result2; 66 Sint64 difference; 67 68 /* Zero delay */ 69 SDL_Delay(0); 70 SDLTest_AssertPass("Call to SDL_Delay(0)"); 71 72 /* Non-zero delay */ 73 SDL_Delay(1); 74 SDLTest_AssertPass("Call to SDL_Delay(1)"); 75 76 SDL_Delay(SDLTest_RandomIntegerInRange(5, 15)); 77 SDLTest_AssertPass("Call to SDL_Delay()"); 78 79 /* Get ticks count - should be non-zero by now */ 80 result = SDL_GetTicks(); 81 SDLTest_AssertPass("Call to SDL_GetTicks()"); 82 SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result); 83 84 /* Delay a bit longer and measure ticks and verify difference */ 85 SDL_Delay(testDelay); 86 SDLTest_AssertPass("Call to SDL_Delay(%d)", testDelay); 87 result2 = SDL_GetTicks(); 88 SDLTest_AssertPass("Call to SDL_GetTicks()"); 89 SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result2); 90 difference = result2 - result; 91 SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %" SDL_PRIu64, testDelay - marginOfError, difference); 92#if 0 93 /* Disabled because this might fail on non-interactive systems. Moved to testtimer. */ 94 SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %" SDL_PRIu64, testDelay + marginOfError, difference); 95#endif 96 97 return TEST_COMPLETED; 98} 99 100#ifndef SDL_PLATFORM_EMSCRIPTEN 101 102/* Test callback */ 103static Uint32 SDLCALL timerTestCallback(void *param, SDL_TimerID timerID, Uint32 interval) 104{ 105 g_timerCallbackCalled = 1; 106 107 if (g_paramCheck != 0) { 108 SDLTest_AssertCheck(param != NULL, "Check param pointer, expected: non-NULL, got: %s", (param != NULL) ? "non-NULL" : "NULL"); 109 if (param != NULL) { 110 SDLTest_AssertCheck(*(int *)param == g_paramValue, "Check param value, expected: %i, got: %i", g_paramValue, *(int *)param); 111 } 112 } 113 114 return 0; 115} 116 117#endif 118 119/** 120 * Call to SDL_AddTimer and SDL_RemoveTimer 121 */ 122static int SDLCALL timer_addRemoveTimer(void *arg) 123{ 124#ifdef SDL_PLATFORM_EMSCRIPTEN 125 SDLTest_Log("Timer callbacks on Emscripten require a main loop to handle events"); 126 return TEST_SKIPPED; 127#else 128 SDL_TimerID id; 129 int result; 130 int param; 131 132 /* Reset state */ 133 g_paramCheck = 0; 134 g_timerCallbackCalled = 0; 135 136 /* Set timer with a long delay */ 137 id = SDL_AddTimer(10000, timerTestCallback, NULL); 138 SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)"); 139 SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %" SDL_PRIu32, id); 140 141 /* Remove timer again and check that callback was not called */ 142 result = SDL_RemoveTimer(id); 143 SDLTest_AssertPass("Call to SDL_RemoveTimer()"); 144 SDLTest_AssertCheck(result == true, "Check result value, expected: true, got: %i", result); 145 SDLTest_AssertCheck(g_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", g_timerCallbackCalled); 146 147 /* Try to remove timer again (should be a NOOP) */ 148 result = SDL_RemoveTimer(id); 149 SDLTest_AssertPass("Call to SDL_RemoveTimer()"); 150 SDLTest_AssertCheck(result == false, "Check result value, expected: false, got: %i", result); 151 152 /* Reset state */ 153 param = SDLTest_RandomIntegerInRange(-1024, 1024); 154 g_paramCheck = 1; 155 g_paramValue = param; 156 g_timerCallbackCalled = 0; 157 158 /* Set timer with a short delay */ 159 id = SDL_AddTimer(10, timerTestCallback, (void *)&param); 160 SDLTest_AssertPass("Call to SDL_AddTimer(10, param)"); 161 SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %" SDL_PRIu32, id); 162 163 /* Wait to let timer trigger callback */ 164 SDL_Delay(100); 165 SDLTest_AssertPass("Call to SDL_Delay(100)"); 166 167 /* Remove timer again and check that callback was called */ 168 result = SDL_RemoveTimer(id); 169 SDLTest_AssertPass("Call to SDL_RemoveTimer()"); 170 SDLTest_AssertCheck(result == false, "Check result value, expected: false, got: %i", result); 171 SDLTest_AssertCheck(g_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", g_timerCallbackCalled); 172 173 return TEST_COMPLETED; 174#endif 175} 176 177/* ================= Test References ================== */ 178 179/* Timer test cases */ 180static const SDLTest_TestCaseReference timerTest1 = { 181 timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED 182}; 183 184static const SDLTest_TestCaseReference timerTest2 = { 185 timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED 186}; 187 188static const SDLTest_TestCaseReference timerTest3 = { 189 timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED 190}; 191 192static const SDLTest_TestCaseReference timerTest4 = { 193 timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED 194}; 195 196/* Sequence of Timer test cases */ 197static const SDLTest_TestCaseReference *timerTests[] = { 198 &timerTest1, &timerTest2, &timerTest3, &timerTest4, NULL 199}; 200 201/* Timer test suite (global) */ 202SDLTest_TestSuiteReference timerTestSuite = { 203 "Timer", 204 timerSetUp, 205 timerTests, 206 NULL 207}; 208
[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.