VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstTimeSpec.cpp@ 13068

Last change on this file since 13068 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.4 KB
Line 
1/* $Id: tstTimeSpec.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
2/** @file
3 * IPRT - RTTimeSpec and PRTTIME tests.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/time.h>
35#include <iprt/stream.h>
36#include <iprt/string.h>
37
38
39/**
40 * Format the time into a string using a static buffer.
41 */
42char *ToString(PRTTIME pTime)
43{
44 static char szBuf[128];
45 RTStrPrintf(szBuf, sizeof(szBuf), "%04d-%02d-%02dT%02u:%02u:%02u.%09u [YD%u WD%u UO%d F%#x]",
46 pTime->i32Year,
47 pTime->u8Month,
48 pTime->u8MonthDay,
49 pTime->u8Hour,
50 pTime->u8Minute,
51 pTime->u8Second,
52 pTime->u32Nanosecond,
53 pTime->u16YearDay,
54 pTime->u8WeekDay,
55 pTime->offUTC,
56 pTime->fFlags);
57 return szBuf;
58}
59
60#define CHECK_NZ(expr) do { if (!(expr)) { RTPrintf("tstTimeSpec: FAILURE at line %d: %#x\n", __LINE__, #expr); return 1; } } while (0)
61
62#define TEST_NS(ns) do {\
63 CHECK_NZ(RTTimeExplode(&T1, RTTimeSpecSetNano(&Ts1, ns))); \
64 RTPrintf("tstTimeSpec: %RI64 ns - %s\n", ns, ToString(&T1)); \
65 CHECK_NZ(RTTimeImplode(&Ts2, &T1)); \
66 if (!RTTimeSpecIsEqual(&Ts2, &Ts1)) \
67 { \
68 RTPrintf("tstTimeSpec: FAILURE - %RI64 != %RI64\n", RTTimeSpecGetNano(&Ts2), RTTimeSpecGetNano(&Ts1)); \
69 RTPrintf(" line no %d\n", __LINE__); \
70 cErrors++; \
71 } \
72 } while (0)
73
74#define TEST_SEC(sec) do {\
75 CHECK_NZ(RTTimeExplode(&T1, RTTimeSpecSetSeconds(&Ts1, sec))); \
76 RTPrintf("tstTimeSpec: %RI64 sec - %s\n", sec, ToString(&T1)); \
77 CHECK_NZ(RTTimeImplode(&Ts2, &T1)); \
78 if (!RTTimeSpecIsEqual(&Ts2, &Ts1)) \
79 { \
80 RTPrintf("tstTimeSpec: FAILURE - %RI64 != %RI64\n", RTTimeSpecGetNano(&Ts2), RTTimeSpecGetNano(&Ts1)); \
81 RTPrintf(" line no %d\n", __LINE__); \
82 cErrors++; \
83 } \
84 } while (0)
85
86#define CHECK_TIME(pTime, _i32Year, _u8Month, _u8MonthDay, _u8Hour, _u8Minute, _u8Second, _u32Nanosecond, _u16YearDay, _u8WeekDay, _offUTC, _fFlags)\
87 do { \
88 if ( (pTime)->i32Year != (_i32Year) \
89 || (pTime)->u8Month != (_u8Month) \
90 || (pTime)->u8WeekDay != (_u8WeekDay) \
91 || (pTime)->u16YearDay != (_u16YearDay) \
92 || (pTime)->u8MonthDay != (_u8MonthDay) \
93 || (pTime)->u8Hour != (_u8Hour) \
94 || (pTime)->u8Minute != (_u8Minute) \
95 || (pTime)->u8Second != (_u8Second) \
96 || (pTime)->u32Nanosecond != (_u32Nanosecond) \
97 || (pTime)->offUTC != (_offUTC) \
98 || (pTime)->fFlags != (_fFlags) \
99 ) \
100 { \
101 RTPrintf("tstTimeSpec: FAILURE - %s\n" \
102 " != %04d-%02d-%02dT%02u-%02u-%02u.%09u [YD%u WD%u UO%d F%#x]\n", \
103 ToString(pTime), (_i32Year), (_u8Month), (_u8MonthDay), (_u8Hour), (_u8Minute), \
104 (_u8Second), (_u32Nanosecond), (_u16YearDay), (_u8WeekDay), (_offUTC), (_fFlags)); \
105 RTPrintf(" line no %d\n", __LINE__); \
106 cErrors++; \
107 } \
108 else \
109 RTPrintf(" => %s\n", ToString(pTime)); \
110 } while (0)
111
112#define SET_TIME(pTime, _i32Year, _u8Month, _u8MonthDay, _u8Hour, _u8Minute, _u8Second, _u32Nanosecond, _u16YearDay, _u8WeekDay, _offUTC, _fFlags)\
113 do { \
114 (pTime)->i32Year = (_i32Year); \
115 (pTime)->u8Month = (_u8Month); \
116 (pTime)->u8WeekDay = (_u8WeekDay); \
117 (pTime)->u16YearDay = (_u16YearDay); \
118 (pTime)->u8MonthDay = (_u8MonthDay); \
119 (pTime)->u8Hour = (_u8Hour); \
120 (pTime)->u8Minute = (_u8Minute); \
121 (pTime)->u8Second = (_u8Second); \
122 (pTime)->u32Nanosecond = (_u32Nanosecond); \
123 (pTime)->offUTC = (_offUTC); \
124 (pTime)->fFlags = (_fFlags); \
125 RTPrintf("tstTimeSpec: %s\n", ToString(pTime)); \
126 } while (0)
127
128
129int main()
130{
131 unsigned cErrors = 0;
132 RTTIMESPEC Now;
133 RTTIMESPEC Ts1;
134 RTTIMESPEC Ts2;
135 RTTIME T1;
136 RTTIME T2;
137
138 /*
139 * Simple test with current time.
140 */
141 CHECK_NZ(RTTimeNow(&Now));
142 CHECK_NZ(RTTimeExplode(&T1, &Now));
143 RTPrintf("tstTimeSpec: %RI64 ns - %s\n", RTTimeSpecGetNano(&Now), ToString(&T1));
144 CHECK_NZ(RTTimeImplode(&Ts1, &T1));
145 if (!RTTimeSpecIsEqual(&Ts1, &Now))
146 {
147 RTPrintf("tstTimeSpec: FAILURE - %RI64 != %RI64\n", RTTimeSpecGetNano(&Ts1), RTTimeSpecGetNano(&Now));
148 cErrors++;
149 }
150
151 /*
152 * Simple test with current local time.
153 */
154 CHECK_NZ(RTTimeLocalNow(&Now));
155 CHECK_NZ(RTTimeExplode(&T1, &Now));
156 RTPrintf("tstTimeSpec: %RI64 ns - %s\n", RTTimeSpecGetNano(&Now), ToString(&T1));
157 CHECK_NZ(RTTimeImplode(&Ts1, &T1));
158 if (!RTTimeSpecIsEqual(&Ts1, &Now))
159 {
160 RTPrintf("tstTimeSpec: FAILURE - %RI64 != %RI64\n", RTTimeSpecGetNano(&Ts1), RTTimeSpecGetNano(&Now));
161 cErrors++;
162 }
163
164 /*
165 * Some simple tests with fixed dates (just checking for smoke).
166 */
167 TEST_NS(INT64_C(0));
168 CHECK_TIME(&T1, 1970,01,01, 00,00,00, 0, 1, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
169 TEST_NS(INT64_C(86400000000000));
170 CHECK_TIME(&T1, 1970,01,02, 00,00,00, 0, 2, 4, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
171
172 TEST_NS(INT64_C(1));
173 CHECK_TIME(&T1, 1970,01,01, 00,00,00, 1, 1, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
174 TEST_NS(INT64_C(-1));
175 CHECK_TIME(&T1, 1969,12,31, 23,59,59,999999999, 365, 2, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
176
177 /*
178 * Test the limits.
179 */
180 TEST_NS(INT64_MAX);
181 TEST_NS(INT64_MIN);
182 TEST_SEC(1095379198);
183 CHECK_TIME(&T1, 2004, 9,16, 23,59,58, 0, 260, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
184 TEST_SEC(1095379199);
185 CHECK_TIME(&T1, 2004, 9,16, 23,59,59, 0, 260, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
186 TEST_SEC(1095379200);
187 CHECK_TIME(&T1, 2004, 9,17, 00,00,00, 0, 261, 4, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
188 TEST_SEC(1095379201);
189 CHECK_TIME(&T1, 2004, 9,17, 00,00,01, 0, 261, 4, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
190
191
192 /*
193 * Test normalization (UTC).
194 */
195 /* simple */
196 CHECK_NZ(RTTimeNow(&Now));
197 CHECK_NZ(RTTimeExplode(&T1, &Now));
198 T2 = T1;
199 CHECK_NZ(RTTimeNormalize(&T1));
200 if (memcmp(&T1, &T2, sizeof(T1)))
201 {
202 RTPrintf("tstTimeSpec: FAILURE - simple normalization failed\n");
203 cErrors++;
204 }
205 CHECK_NZ(RTTimeImplode(&Ts1, &T1));
206 CHECK_NZ(RTTimeSpecIsEqual(&Ts1, &Now));
207
208 /* a few partial dates. */
209 memset(&T1, 0, sizeof(T1));
210 SET_TIME( &T1, 1970,01,01, 00,00,00, 0, 0, 0, 0, 0);
211 CHECK_NZ(RTTimeNormalize(&T1));
212 CHECK_TIME(&T1, 1970,01,01, 00,00,00, 0, 1, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
213
214 SET_TIME( &T1, 1970,00,00, 00,00,00, 1, 1, 0, 0, 0);
215 CHECK_NZ(RTTimeNormalize(&T1));
216 CHECK_TIME(&T1, 1970,01,01, 00,00,00, 1, 1, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
217
218 SET_TIME( &T1, 2007,12,06, 02,15,23, 1, 0, 0, 0, 0);
219 CHECK_NZ(RTTimeNormalize(&T1));
220 CHECK_TIME(&T1, 2007,12,06, 02,15,23, 1, 340, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
221
222 SET_TIME( &T1, 1968,01,30, 00,19,24, 5, 0, 0, 0, 0);
223 CHECK_NZ(RTTimeNormalize(&T1));
224 CHECK_TIME(&T1, 1968,01,30, 00,19,24, 5, 30, 1, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
225
226 SET_TIME( &T1, 1969,01,31, 00, 9, 2, 7, 0, 0, 0, 0);
227 CHECK_NZ(RTTimeNormalize(&T1));
228 CHECK_TIME(&T1, 1969,01,31, 00, 9, 2, 7, 31, 4, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
229
230 SET_TIME( &T1, 1969,03,31, 00, 9, 2, 7, 0, 0, 0, 0);
231 CHECK_NZ(RTTimeNormalize(&T1));
232 CHECK_TIME(&T1, 1969,03,31, 00, 9, 2, 7, 90, 0, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
233
234 SET_TIME( &T1, 1969,12,31, 00,00,00, 9, 0, 0, 0, 0);
235 CHECK_NZ(RTTimeNormalize(&T1));
236 CHECK_TIME(&T1, 1969,12,31, 00,00,00, 9, 365, 2, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
237
238 SET_TIME( &T1, 1969,12,30, 00,00,00, 30, 0, 0, 0, 0);
239 CHECK_NZ(RTTimeNormalize(&T1));
240 CHECK_TIME(&T1, 1969,12,30, 00,00,00, 30, 364, 1, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
241
242 SET_TIME( &T1, 1969,00,00, 00,00,00, 30, 363, 0, 0, 0);
243 CHECK_NZ(RTTimeNormalize(&T1));
244 CHECK_TIME(&T1, 1969,12,29, 00,00,00, 30, 363, 0, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
245
246 SET_TIME( &T1, 1969,00,00, 00,00,00, 30, 362, 6, 0, 0);
247 CHECK_NZ(RTTimeNormalize(&T1));
248 CHECK_TIME(&T1, 1969,12,28, 00,00,00, 30, 362, 6, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
249
250 SET_TIME( &T1, 1969,12,27, 00,00,00, 30, 0, 5, 0, 0);
251 CHECK_NZ(RTTimeNormalize(&T1));
252 CHECK_TIME(&T1, 1969,12,27, 00,00,00, 30, 361, 5, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
253
254 SET_TIME( &T1, 1969,00,00, 00,00,00, 30, 360, 0, 0, 0);
255 CHECK_NZ(RTTimeNormalize(&T1));
256 CHECK_TIME(&T1, 1969,12,26, 00,00,00, 30, 360, 4, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
257
258 SET_TIME( &T1, 1969,12,25, 00,00,00, 12, 0, 0, 0, 0);
259 CHECK_NZ(RTTimeNormalize(&T1));
260 CHECK_TIME(&T1, 1969,12,25, 00,00,00, 12, 359, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
261
262 SET_TIME( &T1, 1969,12,24, 00,00,00, 16, 0, 0, 0, 0);
263 CHECK_NZ(RTTimeNormalize(&T1));
264 CHECK_TIME(&T1, 1969,12,24, 00,00,00, 16, 358, 2, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
265
266 /* outside the year table range */
267 SET_TIME( &T1, 1200,01,30, 00,00,00, 2, 0, 0, 0, 0);
268 CHECK_NZ(RTTimeNormalize(&T1));
269 CHECK_TIME(&T1, 1200,01,30, 00,00,00, 2, 30, 6, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
270
271 SET_TIME( &T1, 2555,11,29, 00,00,00, 2, 0, 0, 0, 0);
272 CHECK_NZ(RTTimeNormalize(&T1));
273 CHECK_TIME(&T1, 2555,11,29, 00,00,00, 2, 333, 5, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
274
275 SET_TIME( &T1, 2555,00,00, 00,00,00, 3, 333, 0, 0, 0);
276 CHECK_NZ(RTTimeNormalize(&T1));
277 CHECK_TIME(&T1, 2555,11,29, 00,00,00, 3, 333, 5, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
278
279 /* time overflow */
280 SET_TIME( &T1, 1969,12,30, 255,255,255, UINT32_MAX, 364, 0, 0, 0);
281 CHECK_NZ(RTTimeNormalize(&T1));
282 CHECK_TIME(&T1, 1970,01, 9, 19,19,19,294967295, 9, 4, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
283
284 /* date overflow */
285 SET_TIME( &T1, 2007,11,36, 02,15,23, 1, 0, 0, 0, 0);
286 CHECK_NZ(RTTimeNormalize(&T1));
287 CHECK_TIME(&T1, 2007,12,06, 02,15,23, 1, 340, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
288
289 SET_TIME( &T1, 2007,10,67, 02,15,23, 1, 0, 0, 0, 0);
290 CHECK_NZ(RTTimeNormalize(&T1));
291 CHECK_TIME(&T1, 2007,12,06, 02,15,23, 1, 340, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
292
293 SET_TIME( &T1, 2007,10,98, 02,15,23, 1, 0, 0, 0, 0);
294 CHECK_NZ(RTTimeNormalize(&T1));
295 CHECK_TIME(&T1, 2008,01,06, 02,15,23, 1, 6, 6, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
296
297 SET_TIME( &T1, 2006,24,06, 02,15,23, 1, 0, 0, 0, 0);
298 CHECK_NZ(RTTimeNormalize(&T1));
299 CHECK_TIME(&T1, 2007,12,06, 02,15,23, 1, 340, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
300
301 SET_TIME( &T1, 2003,60,37, 02,15,23, 1, 0, 0, 0, 0);
302 CHECK_NZ(RTTimeNormalize(&T1));
303 CHECK_TIME(&T1, 2008,01,06, 02,15,23, 1, 6, 6, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
304
305 SET_TIME( &T1, 2003,00,00, 02,15,23, 1,1801, 0, 0, 0);
306 CHECK_NZ(RTTimeNormalize(&T1));
307 CHECK_TIME(&T1, 2007,12,06, 02,15,23, 1, 340, 3, 0, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
308
309 /*
310 * Summary
311 */
312 if (!cErrors)
313 RTPrintf("tstTimeSpec: SUCCESS\n");
314 else
315 RTPrintf("tstTimeSpec: FAILURE - %d errors\n", cErrors);
316 return cErrors ? 1 : 0;
317}
318
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette