testautomation_timer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * Timer test suite
  3. */
  4. #include "SDL.h"
  5. #include "SDL_test.h"
  6. /* Flag indicating if the param should be checked */
  7. int _paramCheck = 0;
  8. /* Userdata value to check */
  9. int _paramValue = 0;
  10. /* Flag indicating that the callback was called */
  11. int _timerCallbackCalled = 0;
  12. /* Fixture */
  13. void
  14. _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
  29. timer_getPerformanceCounter(void *arg)
  30. {
  31. Uint64 result;
  32. result = SDL_GetPerformanceCounter();
  33. SDLTest_AssertPass("Call to SDL_GetPerformanceCounter()");
  34. SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %"SDL_PRIu64, result);
  35. return TEST_COMPLETED;
  36. }
  37. /**
  38. * @brief Call to SDL_GetPerformanceFrequency
  39. */
  40. int
  41. 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. * @brief Call to SDL_Delay and SDL_GetTicks
  51. */
  52. int
  53. timer_delayAndGetTicks(void *arg)
  54. {
  55. const Uint32 testDelay = 100;
  56. const Uint32 marginOfError = 25;
  57. Uint32 result;
  58. Uint32 result2;
  59. Uint32 difference;
  60. /* Zero delay */
  61. SDL_Delay(0);
  62. SDLTest_AssertPass("Call to SDL_Delay(0)");
  63. /* Non-zero delay */
  64. SDL_Delay(1);
  65. SDLTest_AssertPass("Call to SDL_Delay(1)");
  66. SDL_Delay(SDLTest_RandomIntegerInRange(5, 15));
  67. SDLTest_AssertPass("Call to SDL_Delay()");
  68. /* Get ticks count - should be non-zero by now */
  69. result = SDL_GetTicks();
  70. SDLTest_AssertPass("Call to SDL_GetTicks()");
  71. SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %" SDL_PRIu32, result);
  72. /* Delay a bit longer and measure ticks and verify difference */
  73. SDL_Delay(testDelay);
  74. SDLTest_AssertPass("Call to SDL_Delay(%" SDL_PRIu32 ")", testDelay);
  75. result2 = SDL_GetTicks();
  76. SDLTest_AssertPass("Call to SDL_GetTicks()");
  77. SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %" SDL_PRIu32, result2);
  78. difference = result2 - result;
  79. SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%" SDL_PRIu32 ", got: %" SDL_PRIu32, testDelay - marginOfError, difference);
  80. SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%" SDL_PRIu32 ", got: %" SDL_PRIu32, testDelay + marginOfError, difference);
  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
  99. timer_addRemoveTimer(void *arg)
  100. {
  101. SDL_TimerID id;
  102. SDL_bool result;
  103. int param;
  104. /* Reset state */
  105. _paramCheck = 0;
  106. _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: %d", 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 == SDL_TRUE, "Check result value, expected: %i, got: %i", SDL_TRUE, result);
  115. SDLTest_AssertCheck(_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", _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 == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
  120. /* Reset state */
  121. param = SDLTest_RandomIntegerInRange(-1024, 1024);
  122. _paramCheck = 1;
  123. _paramValue = param;
  124. _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: %d", 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 == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
  136. SDLTest_AssertCheck(_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", _timerCallbackCalled);
  137. return TEST_COMPLETED;
  138. }
  139. /* ================= Test References ================== */
  140. /* Timer test cases */
  141. static const SDLTest_TestCaseReference timerTest1 =
  142. { (SDLTest_TestCaseFp)timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED };
  143. static const SDLTest_TestCaseReference timerTest2 =
  144. { (SDLTest_TestCaseFp)timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED };
  145. static const SDLTest_TestCaseReference timerTest3 =
  146. { (SDLTest_TestCaseFp)timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED };
  147. static const SDLTest_TestCaseReference timerTest4 =
  148. { (SDLTest_TestCaseFp)timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED };
  149. /* Sequence of Timer test cases */
  150. static const SDLTest_TestCaseReference *timerTests[] = {
  151. &timerTest1, &timerTest2, &timerTest3, &timerTest4, NULL
  152. };
  153. /* Timer test suite (global) */
  154. SDLTest_TestSuiteReference timerTestSuite = {
  155. "Timer",
  156. _timerSetUp,
  157. timerTests,
  158. NULL
  159. };
  160. /* vi: set ts=4 sw=4 expandtab: */