🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.4 KiB

  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. SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%" SDL_PRIu32 ", got: %" SDL_PRIu32, testDelay + marginOfError, difference);
  78. return TEST_COMPLETED;
  79. }
  80. /* Test callback */
  81. Uint32 SDLCALL _timerTestCallback(Uint32 interval, void *param)
  82. {
  83. _timerCallbackCalled = 1;
  84. if (_paramCheck != 0) {
  85. SDLTest_AssertCheck(param != NULL, "Check param pointer, expected: non-NULL, got: %s", (param != NULL) ? "non-NULL" : "NULL");
  86. if (param != NULL) {
  87. SDLTest_AssertCheck(*(int *)param == _paramValue, "Check param value, expected: %i, got: %i", _paramValue, *(int *)param);
  88. }
  89. }
  90. return 0;
  91. }
  92. /**
  93. * @brief Call to SDL_AddTimer and SDL_RemoveTimer
  94. */
  95. int timer_addRemoveTimer(void *arg)
  96. {
  97. SDL_TimerID id;
  98. SDL_bool result;
  99. int param;
  100. /* Reset state */
  101. _paramCheck = 0;
  102. _timerCallbackCalled = 0;
  103. /* Set timer with a long delay */
  104. id = SDL_AddTimer(10000, _timerTestCallback, NULL);
  105. SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)");
  106. SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
  107. /* Remove timer again and check that callback was not called */
  108. result = SDL_RemoveTimer(id);
  109. SDLTest_AssertPass("Call to SDL_RemoveTimer()");
  110. SDLTest_AssertCheck(result == SDL_TRUE, "Check result value, expected: %i, got: %i", SDL_TRUE, result);
  111. SDLTest_AssertCheck(_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", _timerCallbackCalled);
  112. /* Try to remove timer again (should be a NOOP) */
  113. result = SDL_RemoveTimer(id);
  114. SDLTest_AssertPass("Call to SDL_RemoveTimer()");
  115. SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
  116. /* Reset state */
  117. param = SDLTest_RandomIntegerInRange(-1024, 1024);
  118. _paramCheck = 1;
  119. _paramValue = param;
  120. _timerCallbackCalled = 0;
  121. /* Set timer with a short delay */
  122. id = SDL_AddTimer(10, _timerTestCallback, (void *)&param);
  123. SDLTest_AssertPass("Call to SDL_AddTimer(10, param)");
  124. SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
  125. /* Wait to let timer trigger callback */
  126. SDL_Delay(100);
  127. SDLTest_AssertPass("Call to SDL_Delay(100)");
  128. /* Remove timer again and check that callback was called */
  129. result = SDL_RemoveTimer(id);
  130. SDLTest_AssertPass("Call to SDL_RemoveTimer()");
  131. SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
  132. SDLTest_AssertCheck(_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", _timerCallbackCalled);
  133. return TEST_COMPLETED;
  134. }
  135. /* ================= Test References ================== */
  136. /* Timer test cases */
  137. static const SDLTest_TestCaseReference timerTest1 = {
  138. (SDLTest_TestCaseFp)timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED
  139. };
  140. static const SDLTest_TestCaseReference timerTest2 = {
  141. (SDLTest_TestCaseFp)timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED
  142. };
  143. static const SDLTest_TestCaseReference timerTest3 = {
  144. (SDLTest_TestCaseFp)timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED
  145. };
  146. static const SDLTest_TestCaseReference timerTest4 = {
  147. (SDLTest_TestCaseFp)timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED
  148. };
  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. };