SDL: Move check for SDL_Delay upper bounds to testtimer

From f45761908aa88c61cf7f2b7cb2e663abee06eed0 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Tue, 29 Aug 2023 00:03:05 +0200
Subject: [PATCH] Move check for SDL_Delay upper bounds to testtimer

---
 test/CMakeLists.txt         |  2 +-
 test/testautomation_timer.c |  3 +++
 test/testtimer.c            | 43 ++++++++++++++++++++++++++++++++++---
 3 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 870d9eec2e76..70069a547c81 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -315,7 +315,7 @@ add_sdl_test_executable(testshape NEEDS_RESOURCES SOURCES testshape.c)
 add_sdl_test_executable(testsprite NEEDS_RESOURCES TESTUTILS SOURCES testsprite.c)
 add_sdl_test_executable(testspriteminimal SOURCES testspriteminimal.c ${icon_bmp_header})
 add_sdl_test_executable(teststreaming NEEDS_RESOURCES TESTUTILS SOURCES teststreaming.c)
-add_sdl_test_executable(testtimer NONINTERACTIVE NONINTERACTIVE_TIMEOUT 60 SOURCES testtimer.c)
+add_sdl_test_executable(testtimer NONINTERACTIVE NONINTERACTIVE_ARGS --no-interactive NONINTERACTIVE_TIMEOUT 60 SOURCES testtimer.c)
 add_sdl_test_executable(testurl SOURCES testurl.c)
 add_sdl_test_executable(testver NONINTERACTIVE SOURCES testver.c)
 add_sdl_test_executable(testviewport NEEDS_RESOURCES TESTUTILS SOURCES testviewport.c)
diff --git a/test/testautomation_timer.c b/test/testautomation_timer.c
index 8f37e35763b5..f2b320395212 100644
--- a/test/testautomation_timer.c
+++ b/test/testautomation_timer.c
@@ -92,7 +92,10 @@ static int timer_delayAndGetTicks(void *arg)
     SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result2);
     difference = result2 - result;
     SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %" SDL_PRIu64, testDelay - marginOfError, difference);
+#if 0
+    /* Disabled because this might fail on non-interactive systems. Moved to testtimer. */
     SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %" SDL_PRIu64, testDelay + marginOfError, difference);
+#endif
 
     return TEST_COMPLETED;
 }
diff --git a/test/testtimer.c b/test/testtimer.c
index 278d30b017ad..76c806b54147 100644
--- a/test/testtimer.c
+++ b/test/testtimer.c
@@ -19,6 +19,34 @@
 
 #define DEFAULT_RESOLUTION 1
 
+static int test_sdl_delay_within_bounds(void) {
+    const int testDelay = 100;
+    const int marginOfError = 25;
+    Uint64 result;
+    Uint64 result2;
+    Sint64 difference;
+
+    SDLTest_ResetAssertSummary();
+
+    /* Get ticks count - should be non-zero by now */
+    result = SDL_GetTicks();
+    SDLTest_AssertPass("Call to SDL_GetTicks()");
+    SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result);
+
+    /* Delay a bit longer and measure ticks and verify difference */
+    SDL_Delay(testDelay);
+    SDLTest_AssertPass("Call to SDL_Delay(%d)", testDelay);
+    result2 = SDL_GetTicks();
+    SDLTest_AssertPass("Call to SDL_GetTicks()");
+    SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result2);
+    difference = result2 - result;
+    SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %" SDL_PRIu64, testDelay - marginOfError, difference);
+    /* Disabled because this might fail on non-interactive systems. */
+    SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %" SDL_PRIu64, testDelay + marginOfError, difference);
+
+    return SDLTest_AssertSummaryToTestResult() == TEST_RESULT_PASSED ? 0 : 1;
+}
+
 static int ticks = 0;
 
 static Uint32 SDLCALL
@@ -43,6 +71,8 @@ int main(int argc, char *argv[])
     Uint64 start, now;
     Uint64 start_perf, now_perf;
     SDLTest_CommonState  *state;
+    SDL_bool run_interactive_tests = SDL_TRUE;
+    int return_code = 0;
 
     /* Initialize test framework */
     state = SDLTest_CommonCreateState(argv, 0);
@@ -59,7 +89,10 @@ int main(int argc, char *argv[])
 
         consumed = SDLTest_CommonArg(state, i);
         if (!consumed) {
-            if (desired < 0) {
+            if (SDL_strcmp(argv[i], "--no-interactive") == 0) {
+                run_interactive_tests = SDL_FALSE;
+                consumed = 1;
+            } else if (desired < 0) {
                 char *endptr;
 
                 desired = SDL_strtoul(argv[i], &endptr, 0);
@@ -69,7 +102,7 @@ int main(int argc, char *argv[])
             }
         }
         if (consumed <= 0) {
-            static const char *options[] = { "[interval]", NULL };
+            static const char *options[] = { "[--no-interactive]", "[interval]", NULL };
             SDLTest_CommonLogUsage(state, argv[0], options);
             return 1;
         }
@@ -162,7 +195,11 @@ int main(int argc, char *argv[])
     now = SDL_GetTicks();
     SDL_Log("Delay 1 second = %d ms in ticks, %f ms according to performance counter\n", (int)(now - start), (double)((now_perf - start_perf) * 1000) / SDL_GetPerformanceFrequency());
 
+    if (run_interactive_tests) {
+        return_code = test_sdl_delay_within_bounds();
+    }
+
     SDLTest_CommonDestroyState(state);
     SDL_Quit();
-    return 0;
+    return return_code;
 }