testautomation_time.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * Timer test suite
  3. */
  4. #include "testautomation_suites.h"
  5. #include <SDL3/SDL.h>
  6. #include <SDL3/SDL_test.h>
  7. /* 2000-01-01T16:35:42 UTC */
  8. #define JAN_1_2000_NS SDL_SECONDS_TO_NS(946744542)
  9. /* Test case functions */
  10. /**
  11. * Call to SDL_GetRealtimeClock
  12. */
  13. static int SDLCALL time_getRealtimeClock(void *arg)
  14. {
  15. int result;
  16. SDL_Time ticks;
  17. result = SDL_GetCurrentTime(&ticks);
  18. SDLTest_AssertPass("Call to SDL_GetRealtimeClockTicks()");
  19. SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result);
  20. return TEST_COMPLETED;
  21. }
  22. /**
  23. * Test bidirectional SDL_DateTime conversions.
  24. */
  25. static int SDLCALL time_dateTimeConversion(void *arg)
  26. {
  27. int result;
  28. SDL_Time ticks[2];
  29. SDL_DateTime dt;
  30. ticks[0] = JAN_1_2000_NS;
  31. result = SDL_TimeToDateTime(ticks[0], &dt, false);
  32. SDLTest_AssertPass("Call to SDL_TimeToUTCDateTime()");
  33. SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result);
  34. SDLTest_AssertCheck(dt.year == 2000, "Check year value, expected 2000, got: %i", dt.year);
  35. SDLTest_AssertCheck(dt.month == 1, "Check month value, expected 1, got: %i", dt.month);
  36. SDLTest_AssertCheck(dt.day == 1, "Check day value, expected 1, got: %i", dt.day);
  37. SDLTest_AssertCheck(dt.hour == 16, "Check hour value, expected 16, got: %i", dt.hour);
  38. SDLTest_AssertCheck(dt.minute == 35, "Check hour value, expected 35, got: %i", dt.minute);
  39. SDLTest_AssertCheck(dt.second == 42, "Check hour value, expected 42, got: %i", dt.second);
  40. result = SDL_DateTimeToTime(&dt, &ticks[1]);
  41. SDLTest_AssertPass("Call to SDL_DateTimeToTime()");
  42. SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result);
  43. result = ticks[0] == ticks[1];
  44. SDLTest_AssertCheck(result, "Check that original and converted SDL_Time values match: ticks0 = %" SDL_PRIs64 ", ticks1 = %" SDL_PRIs64, ticks[0], ticks[1]);
  45. /* Local time unknown, so just verify success. */
  46. result = SDL_TimeToDateTime(ticks[0], &dt, true);
  47. SDLTest_AssertPass("Call to SDL_TimeToLocalDateTime()");
  48. SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result);
  49. /* Convert back and verify result. */
  50. result = SDL_DateTimeToTime(&dt, &ticks[1]);
  51. SDLTest_AssertPass("Call to SDL_DateTimeToTime()");
  52. SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result);
  53. result = ticks[0] == ticks[1];
  54. SDLTest_AssertCheck(result, "Check that original and converted SDL_Time values match: ticks0 = %" SDL_PRIs64 ", ticks1 = %" SDL_PRIs64, ticks[0], ticks[1]);
  55. /* Advance the time one day. */
  56. ++dt.day;
  57. if (dt.day > SDL_GetDaysInMonth(dt.year, dt.month)) {
  58. dt.day = 1;
  59. ++dt.month;
  60. }
  61. if (dt.month > 12) {
  62. dt.month = 1;
  63. ++dt.year;
  64. }
  65. result = SDL_DateTimeToTime(&dt, &ticks[1]);
  66. SDLTest_AssertPass("Call to SDL_DateTimeToTime() (one day advanced)");
  67. SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result);
  68. result = (ticks[0] + (Sint64)SDL_SECONDS_TO_NS(86400)) == ticks[1];
  69. SDLTest_AssertCheck(result, "Check that the difference is exactly 86400 seconds, got: %" SDL_PRIs64, (Sint64)SDL_NS_TO_SECONDS(ticks[1] - ticks[0]));
  70. /* Check dates that overflow/underflow an SDL_Time */
  71. dt.year = 2400;
  72. dt.month = 1;
  73. dt.day = 1;
  74. result = SDL_DateTimeToTime(&dt, &ticks[0]);
  75. SDLTest_AssertPass("Call to SDL_DateTimeToTime() (year overflows an SDL_Time)");
  76. SDLTest_AssertCheck(result == false, "Check result value, expected false, got: %i", result);
  77. dt.year = 1601;
  78. result = SDL_DateTimeToTime(&dt, &ticks[0]);
  79. SDLTest_AssertPass("Call to SDL_DateTimeToTime() (year underflows an SDL_Time)");
  80. SDLTest_AssertCheck(result == false, "Check result value, expected false, got: %i", result);
  81. return TEST_COMPLETED;
  82. }
  83. /**
  84. * Test time utility functions.
  85. */
  86. static int SDLCALL time_dateTimeUtilities(void *arg)
  87. {
  88. int result;
  89. /* Leap-year */
  90. result = SDL_GetDaysInMonth(2000, 2);
  91. SDLTest_AssertPass("Call to SDL_GetDaysInMonth(2000, 2)");
  92. SDLTest_AssertCheck(result == 29, "Check result value, expected 29, got: %i", result);
  93. result = SDL_GetDaysInMonth(2001, 2);
  94. SDLTest_AssertPass("Call to SDL_GetDaysInMonth(2001, 2)");
  95. SDLTest_AssertCheck(result == 28, "Check result value, expected 28, got: %i", result);
  96. result = SDL_GetDaysInMonth(2001, 13);
  97. SDLTest_AssertPass("Call to SDL_GetDaysInMonth(2001, 13)");
  98. SDLTest_AssertCheck(result == -1, "Check result value, expected -1, got: %i", result);
  99. result = SDL_GetDaysInMonth(2001, -1);
  100. SDLTest_AssertPass("Call to SDL_GetDaysInMonth(2001, 13)");
  101. SDLTest_AssertCheck(result == -1, "Check result value, expected -1, got: %i", result);
  102. /* 2000-02-29 was a Tuesday */
  103. result = SDL_GetDayOfWeek(2000, 2, 29);
  104. SDLTest_AssertPass("Call to SDL_GetDayOfWeek(2000, 2, 29)");
  105. SDLTest_AssertCheck(result == 2, "Check result value, expected %i, got: %i", 2, result);
  106. /* Nonexistent day */
  107. result = SDL_GetDayOfWeek(2001, 2, 29);
  108. SDLTest_AssertPass("Call to SDL_GetDayOfWeek(2001, 2, 29)");
  109. SDLTest_AssertCheck(result == -1, "Check result value, expected -1, got: %i", result);
  110. result = SDL_GetDayOfYear(2000, 1, 1);
  111. SDLTest_AssertPass("Call to SDL_GetDayOfWeek(2001, 1, 1)");
  112. SDLTest_AssertCheck(result == 0, "Check result value, expected 0, got: %i", result);
  113. /* Leap-year */
  114. result = SDL_GetDayOfYear(2000, 12, 31);
  115. SDLTest_AssertPass("Call to SDL_GetDayOfYear(2000, 12, 31)");
  116. SDLTest_AssertCheck(result == 365, "Check result value, expected 365, got: %i", result);
  117. result = SDL_GetDayOfYear(2001, 12, 31);
  118. SDLTest_AssertPass("Call to SDL_GetDayOfYear(2000, 12, 31)");
  119. SDLTest_AssertCheck(result == 364, "Check result value, expected 364, got: %i", result);
  120. /* Nonexistent day */
  121. result = SDL_GetDayOfYear(2001, 2, 29);
  122. SDLTest_AssertPass("Call to SDL_GetDayOfYear(2001, 2, 29)");
  123. SDLTest_AssertCheck(result == -1, "Check result value, expected -1, got: %i", result);
  124. /* Test Win32 time conversion */
  125. Uint64 wintime = 11644473600LL * 10000000LL; /* The epoch */
  126. SDL_Time ticks = SDL_TimeFromWindows((Uint32)(wintime & 0xFFFFFFFF), (Uint32)(wintime >> 32));
  127. SDLTest_AssertPass("Call to SDL_TimeFromWindows()");
  128. SDLTest_AssertCheck(ticks == 0, "Check result value, expected 0, got: %" SDL_PRIs64, ticks);
  129. /* Out of range times should be clamped instead of rolling over */
  130. wintime = 0;
  131. ticks = SDL_TimeFromWindows((Uint32)(wintime & 0xFFFFFFFF), (Uint32)(wintime >> 32));
  132. SDLTest_AssertPass("Call to SDL_TimeFromWindows()");
  133. SDLTest_AssertCheck(ticks < 0 && ticks >= SDL_MIN_TIME, "Check result value, expected <0 && >=%" SDL_PRIs64 ", got: %" SDL_PRIs64, SDL_MIN_TIME, ticks);
  134. wintime = 0xFFFFFFFFFFFFFFFFULL;
  135. ticks = SDL_TimeFromWindows((Uint32)(wintime & 0xFFFFFFFF), (Uint32)(wintime >> 32));
  136. SDLTest_AssertPass("Call to SDL_TimeFromWindows()");
  137. SDLTest_AssertCheck(ticks > 0 && ticks <= SDL_MAX_TIME, "Check result value, expected >0 && <=%" SDL_PRIs64 ", got: %" SDL_PRIs64, SDL_MAX_TIME, ticks);
  138. /* Test time locale functions */
  139. SDL_DateFormat dateFormat;
  140. SDL_TimeFormat timeFormat;
  141. result = SDL_GetDateTimeLocalePreferences(&dateFormat, &timeFormat);
  142. SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(&dateFormat, &timeFormat)");
  143. SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result);
  144. result = SDL_GetDateTimeLocalePreferences(&dateFormat, NULL);
  145. SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(&dateFormat, NULL)");
  146. SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result);
  147. result = SDL_GetDateTimeLocalePreferences(NULL, &timeFormat);
  148. SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(NULL, &timeFormat)");
  149. SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result);
  150. result = SDL_GetDateTimeLocalePreferences(NULL, NULL);
  151. SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(NULL, NULL)");
  152. SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result);
  153. return TEST_COMPLETED;
  154. }
  155. /* ================= Test References ================== */
  156. /* Time test cases */
  157. static const SDLTest_TestCaseReference timeTest1 = {
  158. time_getRealtimeClock, "time_getRealtimeClock", "Call to SDL_GetRealtimeClockTicks", TEST_ENABLED
  159. };
  160. static const SDLTest_TestCaseReference timeTest2 = {
  161. time_dateTimeConversion, "time_dateTimeConversion", "Call to SDL_TimeToDateTime/SDL_DateTimeToTime", TEST_ENABLED
  162. };
  163. static const SDLTest_TestCaseReference timeTest3 = {
  164. time_dateTimeUtilities, "time_dateTimeUtilities", "Call to SDL_TimeToDateTime/SDL_DateTimeToTime", TEST_ENABLED
  165. };
  166. /* Sequence of Timer test cases */
  167. static const SDLTest_TestCaseReference *timeTests[] = {
  168. &timeTest1, &timeTest2, &timeTest3, NULL
  169. };
  170. /* Time test suite (global) */
  171. SDLTest_TestSuiteReference timeTestSuite = {
  172. "Time",
  173. NULL,
  174. timeTests,
  175. NULL
  176. };