Atlas - testautomation_time.c

Home / ext / SDL / test Lines: 1 | Size: 9269 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/** 2 * Timer test suite 3 */ 4#include "testautomation_suites.h" 5#include <SDL3/SDL.h> 6#include <SDL3/SDL_test.h> 7 8/* 2000-01-01T16:35:42 UTC */ 9#define JAN_1_2000_NS SDL_SECONDS_TO_NS(946744542) 10 11/* Test case functions */ 12 13/** 14 * Call to SDL_GetRealtimeClock 15 */ 16static int SDLCALL time_getRealtimeClock(void *arg) 17{ 18 int result; 19 SDL_Time ticks; 20 21 result = SDL_GetCurrentTime(&ticks); 22 SDLTest_AssertPass("Call to SDL_GetRealtimeClockTicks()"); 23 SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result); 24 25 return TEST_COMPLETED; 26} 27 28/** 29 * Test bidirectional SDL_DateTime conversions. 30 */ 31static int SDLCALL time_dateTimeConversion(void *arg) 32{ 33 int result; 34 SDL_Time ticks[2]; 35 SDL_DateTime dt; 36 37 ticks[0] = JAN_1_2000_NS; 38 39 result = SDL_TimeToDateTime(ticks[0], &dt, false); 40 SDLTest_AssertPass("Call to SDL_TimeToUTCDateTime()"); 41 SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result); 42 SDLTest_AssertCheck(dt.year == 2000, "Check year value, expected 2000, got: %i", dt.year); 43 SDLTest_AssertCheck(dt.month == 1, "Check month value, expected 1, got: %i", dt.month); 44 SDLTest_AssertCheck(dt.day == 1, "Check day value, expected 1, got: %i", dt.day); 45 SDLTest_AssertCheck(dt.hour == 16, "Check hour value, expected 16, got: %i", dt.hour); 46 SDLTest_AssertCheck(dt.minute == 35, "Check hour value, expected 35, got: %i", dt.minute); 47 SDLTest_AssertCheck(dt.second == 42, "Check hour value, expected 42, got: %i", dt.second); 48 49 result = SDL_DateTimeToTime(&dt, &ticks[1]); 50 SDLTest_AssertPass("Call to SDL_DateTimeToTime()"); 51 SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result); 52 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 56 /* Local time unknown, so just verify success. */ 57 result = SDL_TimeToDateTime(ticks[0], &dt, true); 58 SDLTest_AssertPass("Call to SDL_TimeToLocalDateTime()"); 59 SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result); 60 61 /* Convert back and verify result. */ 62 result = SDL_DateTimeToTime(&dt, &ticks[1]); 63 SDLTest_AssertPass("Call to SDL_DateTimeToTime()"); 64 SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result); 65 66 result = ticks[0] == ticks[1]; 67 SDLTest_AssertCheck(result, "Check that original and converted SDL_Time values match: ticks0 = %" SDL_PRIs64 ", ticks1 = %" SDL_PRIs64, ticks[0], ticks[1]); 68 69 /* Advance the time one day. */ 70 ++dt.day; 71 if (dt.day > SDL_GetDaysInMonth(dt.year, dt.month)) { 72 dt.day = 1; 73 ++dt.month; 74 } 75 if (dt.month > 12) { 76 dt.month = 1; 77 ++dt.year; 78 } 79 80 result = SDL_DateTimeToTime(&dt, &ticks[1]); 81 SDLTest_AssertPass("Call to SDL_DateTimeToTime() (one day advanced)"); 82 SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result); 83 84 result = (ticks[0] + (Sint64)SDL_SECONDS_TO_NS(86400)) == ticks[1]; 85 SDLTest_AssertCheck(result, "Check that the difference is exactly 86400 seconds, got: %" SDL_PRIs64, (Sint64)SDL_NS_TO_SECONDS(ticks[1] - ticks[0])); 86 87 /* Check dates that overflow/underflow an SDL_Time */ 88 dt.year = 2400; 89 dt.month = 1; 90 dt.day = 1; 91 result = SDL_DateTimeToTime(&dt, &ticks[0]); 92 SDLTest_AssertPass("Call to SDL_DateTimeToTime() (year overflows an SDL_Time)"); 93 SDLTest_AssertCheck(result == false, "Check result value, expected false, got: %i", result); 94 95 dt.year = 1601; 96 result = SDL_DateTimeToTime(&dt, &ticks[0]); 97 SDLTest_AssertPass("Call to SDL_DateTimeToTime() (year underflows an SDL_Time)"); 98 SDLTest_AssertCheck(result == false, "Check result value, expected false, got: %i", result); 99 100 return TEST_COMPLETED; 101} 102 103/** 104 * Test time utility functions. 105 */ 106static int SDLCALL time_dateTimeUtilities(void *arg) 107{ 108 int result; 109 110 /* Leap-year */ 111 result = SDL_GetDaysInMonth(2000, 2); 112 SDLTest_AssertPass("Call to SDL_GetDaysInMonth(2000, 2)"); 113 SDLTest_AssertCheck(result == 29, "Check result value, expected 29, got: %i", result); 114 115 result = SDL_GetDaysInMonth(2001, 2); 116 SDLTest_AssertPass("Call to SDL_GetDaysInMonth(2001, 2)"); 117 SDLTest_AssertCheck(result == 28, "Check result value, expected 28, got: %i", result); 118 119 result = SDL_GetDaysInMonth(2001, 13); 120 SDLTest_AssertPass("Call to SDL_GetDaysInMonth(2001, 13)"); 121 SDLTest_AssertCheck(result == -1, "Check result value, expected -1, got: %i", result); 122 123 result = SDL_GetDaysInMonth(2001, -1); 124 SDLTest_AssertPass("Call to SDL_GetDaysInMonth(2001, 13)"); 125 SDLTest_AssertCheck(result == -1, "Check result value, expected -1, got: %i", result); 126 127 /* 2000-02-29 was a Tuesday */ 128 result = SDL_GetDayOfWeek(2000, 2, 29); 129 SDLTest_AssertPass("Call to SDL_GetDayOfWeek(2000, 2, 29)"); 130 SDLTest_AssertCheck(result == 2, "Check result value, expected %i, got: %i", 2, result); 131 132 /* Nonexistent day */ 133 result = SDL_GetDayOfWeek(2001, 2, 29); 134 SDLTest_AssertPass("Call to SDL_GetDayOfWeek(2001, 2, 29)"); 135 SDLTest_AssertCheck(result == -1, "Check result value, expected -1, got: %i", result); 136 137 result = SDL_GetDayOfYear(2000, 1, 1); 138 SDLTest_AssertPass("Call to SDL_GetDayOfWeek(2001, 1, 1)"); 139 SDLTest_AssertCheck(result == 0, "Check result value, expected 0, got: %i", result); 140 141 /* Leap-year */ 142 result = SDL_GetDayOfYear(2000, 12, 31); 143 SDLTest_AssertPass("Call to SDL_GetDayOfYear(2000, 12, 31)"); 144 SDLTest_AssertCheck(result == 365, "Check result value, expected 365, got: %i", result); 145 146 result = SDL_GetDayOfYear(2001, 12, 31); 147 SDLTest_AssertPass("Call to SDL_GetDayOfYear(2000, 12, 31)"); 148 SDLTest_AssertCheck(result == 364, "Check result value, expected 364, got: %i", result); 149 150 /* Nonexistent day */ 151 result = SDL_GetDayOfYear(2001, 2, 29); 152 SDLTest_AssertPass("Call to SDL_GetDayOfYear(2001, 2, 29)"); 153 SDLTest_AssertCheck(result == -1, "Check result value, expected -1, got: %i", result); 154 155 /* Test Win32 time conversion */ 156 Uint64 wintime = 11644473600LL * 10000000LL; /* The epoch */ 157 SDL_Time ticks = SDL_TimeFromWindows((Uint32)(wintime & 0xFFFFFFFF), (Uint32)(wintime >> 32)); 158 SDLTest_AssertPass("Call to SDL_TimeFromWindows()"); 159 SDLTest_AssertCheck(ticks == 0, "Check result value, expected 0, got: %" SDL_PRIs64, ticks); 160 161 /* Out of range times should be clamped instead of rolling over */ 162 wintime = 0; 163 ticks = SDL_TimeFromWindows((Uint32)(wintime & 0xFFFFFFFF), (Uint32)(wintime >> 32)); 164 SDLTest_AssertPass("Call to SDL_TimeFromWindows()"); 165 SDLTest_AssertCheck(ticks < 0 && ticks >= SDL_MIN_TIME, "Check result value, expected <0 && >=%" SDL_PRIs64 ", got: %" SDL_PRIs64, SDL_MIN_TIME, ticks); 166 167 wintime = 0xFFFFFFFFFFFFFFFFULL; 168 ticks = SDL_TimeFromWindows((Uint32)(wintime & 0xFFFFFFFF), (Uint32)(wintime >> 32)); 169 SDLTest_AssertPass("Call to SDL_TimeFromWindows()"); 170 SDLTest_AssertCheck(ticks > 0 && ticks <= SDL_MAX_TIME, "Check result value, expected >0 && <=%" SDL_PRIs64 ", got: %" SDL_PRIs64, SDL_MAX_TIME, ticks); 171 172 /* Test time locale functions */ 173 SDL_DateFormat dateFormat; 174 SDL_TimeFormat timeFormat; 175 176 result = SDL_GetDateTimeLocalePreferences(&dateFormat, &timeFormat); 177 SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(&dateFormat, &timeFormat)"); 178 SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result); 179 180 result = SDL_GetDateTimeLocalePreferences(&dateFormat, NULL); 181 SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(&dateFormat, NULL)"); 182 SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result); 183 184 result = SDL_GetDateTimeLocalePreferences(NULL, &timeFormat); 185 SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(NULL, &timeFormat)"); 186 SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result); 187 188 result = SDL_GetDateTimeLocalePreferences(NULL, NULL); 189 SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(NULL, NULL)"); 190 SDLTest_AssertCheck(result == true, "Check result value, expected true, got: %i", result); 191 192 return TEST_COMPLETED; 193} 194 195/* ================= Test References ================== */ 196 197/* Time test cases */ 198static const SDLTest_TestCaseReference timeTest1 = { 199 time_getRealtimeClock, "time_getRealtimeClock", "Call to SDL_GetRealtimeClockTicks", TEST_ENABLED 200}; 201 202static const SDLTest_TestCaseReference timeTest2 = { 203 time_dateTimeConversion, "time_dateTimeConversion", "Call to SDL_TimeToDateTime/SDL_DateTimeToTime", TEST_ENABLED 204}; 205 206static const SDLTest_TestCaseReference timeTest3 = { 207 time_dateTimeUtilities, "time_dateTimeUtilities", "Call to SDL_TimeToDateTime/SDL_DateTimeToTime", TEST_ENABLED 208}; 209 210/* Sequence of Timer test cases */ 211static const SDLTest_TestCaseReference *timeTests[] = { 212 &timeTest1, &timeTest2, &timeTest3, NULL 213}; 214 215/* Time test suite (global) */ 216SDLTest_TestSuiteReference timeTestSuite = { 217 "Time", 218 NULL, 219 timeTests, 220 NULL 221}; 222
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.