testautomation_timer.c 6.4 KB

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