testautomation_timer.c 6.2 KB

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