testautomation_timer.c 6.4 KB

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