testautomation_timer.c 6.7 KB

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