| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Test program to check the resolution of the SDL timer on the current |
| 14 |
platform |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <stdlib.h> |
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
#include "SDL.h" |
| 21 |
#include "SDL_test.h" |
| 22 |
|
| 23 |
#define DEFAULT_RESOLUTION 1 |
| 24 |
|
| 25 |
static int test_sdl_delay_within_bounds(void) { |
| 26 |
const int testDelay = 100; |
| 27 |
const int marginOfError = 25; |
| 28 |
Uint64 result; |
| 29 |
Uint64 result2; |
| 30 |
Sint64 difference; |
| 31 |
|
| 32 |
SDLTest_ResetAssertSummary(); |
| 33 |
|
| 34 |
/* Get ticks count - should be non-zero by now */ |
| 35 |
result = SDL_GetTicks(); |
| 36 |
SDLTest_AssertPass("Call to SDL_GetTicks()"); |
| 37 |
SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result); |
| 38 |
|
| 39 |
/* Delay a bit longer and measure ticks and verify difference */ |
| 40 |
SDL_Delay(testDelay); |
| 41 |
SDLTest_AssertPass("Call to SDL_Delay(%d)", testDelay); |
| 42 |
result2 = SDL_GetTicks(); |
| 43 |
SDLTest_AssertPass("Call to SDL_GetTicks()"); |
| 44 |
SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result2); |
| 45 |
difference = result2 - result; |
| 46 |
SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %" SDL_PRIu64, testDelay - marginOfError, difference); |
| 47 |
/* Disabled because this might fail on non-interactive systems. */ |
| 48 |
SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %" SDL_PRIu64, testDelay + marginOfError, difference); |
| 49 |
|
| 50 |
return SDLTest_AssertSummaryToTestResult() == TEST_RESULT_PASSED ? 0 : 1; |
| 51 |
} |
| 52 |
|
| 53 |
static int ticks = 0; |
| 54 |
|
| 55 |
static Uint32 SDLCALL |
| 56 |
ticktock(Uint32 interval, void *param) |
| 57 |
{ |
| 58 |
++ticks; |
| 59 |
return interval; |
| 60 |
} |
| 61 |
|
| 62 |
static Uint32 SDLCALL |
| 63 |
callback(Uint32 interval, void *param) |
| 64 |
{ |
| 65 |
int value = (int)(uintptr_t)param; |
| 66 |
SDL_assert( value == 1 || value == 2 || value == 3 ); |
| 67 |
SDL_Log("Timer %" SDL_PRIu32 " : param = %d\n", interval, value); |
| 68 |
return interval; |
| 69 |
} |
| 70 |
|
| 71 |
int main(int argc, char *argv[]) |
| 72 |
{ |
| 73 |
int i; |
| 74 |
int desired = -1; |
| 75 |
SDL_TimerID t1, t2, t3; |
| 76 |
Uint64 start64, now64; |
| 77 |
Uint32 start32, now32; |
| 78 |
Uint64 start, now; |
| 79 |
SDL_bool run_interactive_tests = SDL_FALSE; |
| 80 |
int return_code = 0; |
| 81 |
SDLTest_CommonState *state; |
| 82 |
|
| 83 |
state = SDLTest_CommonCreateState(argv, SDL_INIT_TIMER); |
| 84 |
if (!state) { |
| 85 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError()); |
| 86 |
return 1; |
| 87 |
} |
| 88 |
|
| 89 |
/* Enable standard application logging */ |
| 90 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 91 |
|
| 92 |
/* Parse commandline */ |
| 93 |
for (i = 1; i < argc;) { |
| 94 |
int consumed; |
| 95 |
|
| 96 |
consumed = SDLTest_CommonArg(state, i); |
| 97 |
if (!consumed) { |
| 98 |
if (SDL_strcmp("--interactive", argv[i]) == 0) { |
| 99 |
run_interactive_tests = SDL_TRUE; |
| 100 |
consumed = 1; |
| 101 |
} else if (desired < 0) { |
| 102 |
char *endptr; |
| 103 |
|
| 104 |
desired = SDL_strtoul(argv[i], &endptr, 0); |
| 105 |
if (desired != 0 && endptr != argv[i] && *endptr == '\0') { |
| 106 |
consumed = 1; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
if (consumed <= 0) { |
| 111 |
static const char *options[] = { "[--interactive]", "[interval]", NULL }; |
| 112 |
SDLTest_CommonLogUsage(state, argv[0], options); |
| 113 |
exit(1); |
| 114 |
} |
| 115 |
|
| 116 |
i += consumed; |
| 117 |
} |
| 118 |
|
| 119 |
if (!SDLTest_CommonInit(state)) { |
| 120 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
if (SDL_getenv("SDL_TESTS_QUICK") != NULL) { |
| 125 |
SDL_Log("Not running slower tests"); |
| 126 |
SDL_Quit(); |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
/* Verify SDL_GetTicks* acts monotonically increasing, and not erratic. */ |
| 131 |
SDL_Log("Sanity-checking GetTicks\n"); |
| 132 |
for (i = 0; i < 1000; ++i) { |
| 133 |
start64 = SDL_GetTicks64(); |
| 134 |
start32 = SDL_GetTicks(); |
| 135 |
SDL_Delay(1); |
| 136 |
now64 = SDL_GetTicks64() - start64; |
| 137 |
now32 = SDL_GetTicks() - start32; |
| 138 |
if (now32 > 100 || now64 > 100) { |
| 139 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testtimer.c: Delta time erratic at iter %d. Delay 1ms = %d ms in ticks, %d ms in ticks64\n", i, (int)now32, (int)now64); |
| 140 |
SDL_Quit(); |
| 141 |
return 1; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
if (desired < 0) { |
| 146 |
desired = DEFAULT_RESOLUTION; |
| 147 |
} |
| 148 |
|
| 149 |
/* Start the timer */ |
| 150 |
t1 = SDL_AddTimer(desired, ticktock, NULL); |
| 151 |
|
| 152 |
/* Wait 10 seconds */ |
| 153 |
SDL_Log("Waiting 10 seconds\n"); |
| 154 |
SDL_Delay(10 * 1000); |
| 155 |
|
| 156 |
/* Stop the timer */ |
| 157 |
SDL_RemoveTimer(t1); |
| 158 |
|
| 159 |
/* Print the results */ |
| 160 |
if (ticks) { |
| 161 |
SDL_Log("Timer resolution: desired = %d ms, actual = %f ms\n", |
| 162 |
desired, (double)(10 * 1000) / ticks); |
| 163 |
} |
| 164 |
|
| 165 |
/* Test multiple timers */ |
| 166 |
SDL_Log("Testing multiple timers...\n"); |
| 167 |
t1 = SDL_AddTimer(100, callback, (void *)1); |
| 168 |
if (!t1) { |
| 169 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create timer 1: %s\n", SDL_GetError()); |
| 170 |
return_code = 1; |
| 171 |
} |
| 172 |
t2 = SDL_AddTimer(50, callback, (void *)2); |
| 173 |
if (!t2) { |
| 174 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create timer 2: %s\n", SDL_GetError()); |
| 175 |
return_code = 1; |
| 176 |
} |
| 177 |
t3 = SDL_AddTimer(233, callback, (void *)3); |
| 178 |
if (!t3) { |
| 179 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create timer 3: %s\n", SDL_GetError()); |
| 180 |
return_code = 1; |
| 181 |
} |
| 182 |
|
| 183 |
/* Wait 10 seconds */ |
| 184 |
SDL_Log("Waiting 10 seconds\n"); |
| 185 |
SDL_Delay(10 * 1000); |
| 186 |
|
| 187 |
SDL_Log("Removing timer 1 and waiting 5 more seconds\n"); |
| 188 |
SDL_RemoveTimer(t1); |
| 189 |
|
| 190 |
SDL_Delay(5 * 1000); |
| 191 |
|
| 192 |
SDL_RemoveTimer(t2); |
| 193 |
SDL_RemoveTimer(t3); |
| 194 |
|
| 195 |
start = SDL_GetPerformanceCounter(); |
| 196 |
for (i = 0; i < 1000000; ++i) { |
| 197 |
ticktock(0, NULL); |
| 198 |
} |
| 199 |
now = SDL_GetPerformanceCounter(); |
| 200 |
SDL_Log("1 million iterations of ticktock took %f ms\n", (double)((now - start) * 1000) / SDL_GetPerformanceFrequency()); |
| 201 |
|
| 202 |
SDL_Log("Performance counter frequency: %" SDL_PRIu64 "\n", SDL_GetPerformanceFrequency()); |
| 203 |
start64 = SDL_GetTicks64(); |
| 204 |
start32 = SDL_GetTicks(); |
| 205 |
start = SDL_GetPerformanceCounter(); |
| 206 |
SDL_Delay(1000); |
| 207 |
now = SDL_GetPerformanceCounter(); |
| 208 |
now64 = SDL_GetTicks64(); |
| 209 |
now32 = SDL_GetTicks(); |
| 210 |
SDL_Log("Delay 1 second = %d ms in ticks, %d ms in ticks64, %f ms according to performance counter\n", (int)(now32 - start32), (int)(now64 - start64), (double)((now - start) * 1000) / SDL_GetPerformanceFrequency()); |
| 211 |
|
| 212 |
if (run_interactive_tests) { |
| 213 |
return_code |= test_sdl_delay_within_bounds(); |
| 214 |
} |
| 215 |
SDLTest_CommonQuit(state); |
| 216 |
return return_code; |
| 217 |
} |
| 218 |
|
| 219 |
/* vi: set ts=4 sw=4 expandtab: */ |