VirtualBox

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

Last change on this file since 1507 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/* $Id: tstTimeSpec.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - RTTimeSpec and PRTTIME tests.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include <iprt/time.h>
26#include <iprt/stream.h>
27#include <iprt/string.h>
28
29
30/**
31 * Format the time into a string using a static buffer.
32 */
33char *ToString(PRTTIME pTime)
34{
35 static char szBuf[128];
36 RTStrPrintf(szBuf, sizeof(szBuf), "%04d-%02d-%02dT%02u-%02u-%02u.%09u [YD%u WD%u F%#x]",
37 pTime->i32Year,
38 pTime->u8Month,
39 pTime->u8MonthDay,
40 pTime->u8Hour,
41 pTime->u8Minute,
42 pTime->u8Second,
43 pTime->u32Nanosecond,
44 pTime->u16YearDay,
45 pTime->u8WeekDay,
46 pTime->fFlags);
47 return szBuf;
48}
49
50#define CHECK_NZ(expr) do { if (!(expr)) { RTPrintf("tstTimeSpec: FAILURE at line %d: %#x\n", __LINE__, #expr); return 1; } } while (0)
51
52#define TEST_NS(ns) do {\
53 CHECK_NZ(RTTimeExplode(&T1, RTTimeSpecSetNano(&Ts1, ns))); \
54 RTPrintf("tstTimeSpec: %RI64 ns - %s\n", ns, ToString(&T1)); \
55 CHECK_NZ(RTTimeImplode(&Ts2, &T1)); \
56 if (!RTTimeSpecIsEqual(&Ts2, &Ts1)) \
57 { \
58 RTPrintf("tstTimeSpec: FAILURE - %RI64 != %RI64\n", RTTimeSpecGetNano(&Ts2), RTTimeSpecGetNano(&Ts1)); \
59 return 1; \
60 } \
61 } while (0)
62
63#define TEST_SEC(sec) do {\
64 CHECK_NZ(RTTimeExplode(&T1, RTTimeSpecSetSeconds(&Ts1, sec))); \
65 RTPrintf("tstTimeSpec: %RI64 sec - %s\n", sec, ToString(&T1)); \
66 CHECK_NZ(RTTimeImplode(&Ts2, &T1)); \
67 if (!RTTimeSpecIsEqual(&Ts2, &Ts1)) \
68 { \
69 RTPrintf("tstTimeSpec: FAILURE - %RI64 != %RI64\n", RTTimeSpecGetNano(&Ts2), RTTimeSpecGetNano(&Ts1)); \
70 return 1; \
71 } \
72 } while (0)
73
74#define CHECK_TIME(pTime, _i32Year, _u8Month, _u8MonthDay, _u8Hour, _u8Minute, _u8Second, _u32Nanosecond, _u16YearDay, _u8WeekDay, _fFlags)\
75 do { \
76 if ( (pTime)->i32Year != (_i32Year) \
77 || (pTime)->u8Month != (_u8Month) \
78 || (pTime)->u8WeekDay != (_u8WeekDay) \
79 || (pTime)->u16YearDay != (_u16YearDay) \
80 || (pTime)->u8MonthDay != (_u8MonthDay) \
81 || (pTime)->u8Hour != (_u8Hour) \
82 || (pTime)->u8Minute != (_u8Minute) \
83 || (pTime)->u8Second != (_u8Second) \
84 || (pTime)->u32Nanosecond != (_u32Nanosecond) \
85 || (pTime)->fFlags != (_fFlags) \
86 ) \
87 { \
88 RTPrintf("tstTimeSpec: FAILURE - %s\n" \
89 " != %04d-%02d-%02dT%02u-%02u-%02u.%09u [YD%u WD%u F%#x]\n", \
90 ToString(pTime), (_i32Year), (_u8Month), (_u8MonthDay), (_u8Hour), (_u8Minute), \
91 (_u8Second), (_u32Nanosecond), (_u16YearDay), (_u8WeekDay), (_fFlags)); \
92 return 1; \
93 } \
94 } while (0)
95
96
97int main()
98{
99 RTTIMESPEC Now;
100 RTTIMESPEC Ts1;
101 RTTIMESPEC Ts2;
102 RTTIME T1;
103 //RTTIME T2;
104
105 /*
106 * Simple test with current time.
107 */
108 CHECK_NZ(RTTimeNow(&Now));
109 CHECK_NZ(RTTimeExplode(&T1, &Now));
110 RTPrintf("tstTimeSpec: %RI64 ns - %s\n", RTTimeSpecGetNano(&Now), ToString(&T1));
111 CHECK_NZ(RTTimeImplode(&Ts1, &T1));
112 if (!RTTimeSpecIsEqual(&Ts1, &Now))
113 {
114 RTPrintf("tstTimeSpec: FAILURE - %RI64 != %RI64\n", RTTimeSpecGetNano(&Ts1), RTTimeSpecGetNano(&Now));
115 return 1;
116 }
117
118 /*
119 * Some simple tests with fixed dates (just checking for smoke).
120 */
121 TEST_NS(INT64_C(0));
122 CHECK_TIME(&T1, 1970,01,01, 00,00,00, 0, 1, 3, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
123 TEST_NS(INT64_C(86400000000000));
124 CHECK_TIME(&T1, 1970,01,02, 00,00,00, 0, 2, 4, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
125
126 TEST_NS(INT64_C(1));
127 CHECK_TIME(&T1, 1970,01,01, 00,00,00, 1, 1, 3, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
128 TEST_NS(INT64_C(-1));
129 CHECK_TIME(&T1, 1969,12,31, 23,59,59,999999999, 365, 2, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_COMMON_YEAR);
130
131 /*
132 * Test the limits.
133 */
134 TEST_NS(INT64_MAX);
135 TEST_NS(INT64_MIN);
136 TEST_SEC(1095379198);
137 CHECK_TIME(&T1, 2004, 9,16, 23,59,58, 0, 260, 3, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
138 TEST_SEC(1095379199);
139 CHECK_TIME(&T1, 2004, 9,16, 23,59,59, 0, 260, 3, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
140 TEST_SEC(1095379200);
141 CHECK_TIME(&T1, 2004, 9,17, 00,00,00, 0, 261, 4, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
142 TEST_SEC(1095379201);
143 CHECK_TIME(&T1, 2004, 9,17, 00,00,01, 0, 261, 4, RTTIME_FLAGS_TYPE_UTC | RTTIME_FLAGS_LEAP_YEAR);
144 RTPrintf("tstTimeSpec: SUCCESS\n");
145 return 0;
146}
147
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