testautomation_timer.c 6.5 KB

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