ReactOS  r76032
time.c
Go to the documentation of this file.
1 /*
2  * Unit test suite for ntdll time functions
3  *
4  * Copyright 2004 Rein Klazes
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "ntdll_test.h"
22 
23 #define TICKSPERSEC 10000000
24 #define TICKSPERMSEC 10000
25 #define SECSPERDAY 86400
26 
27 static VOID (WINAPI *pRtlTimeToTimeFields)( const LARGE_INTEGER *liTime, PTIME_FIELDS TimeFields) ;
28 static VOID (WINAPI *pRtlTimeFieldsToTime)( PTIME_FIELDS TimeFields, PLARGE_INTEGER Time) ;
29 
30 static const int MonthLengths[2][12] =
31 {
32  { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
33  { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
34 };
35 
36 static inline BOOL IsLeapYear(int Year)
37 {
38  return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
39 }
40 
41 /* start time of the tests */
42 static TIME_FIELDS tftest = {1889,12,31,23,59,59,0,0};
43 
44 static void test_pRtlTimeToTimeFields(void)
45 {
46  LARGE_INTEGER litime , liresult;
47  TIME_FIELDS tfresult;
48  int i=0;
49  litime.QuadPart = ((ULONGLONG)0x0144017a << 32) | 0xf0b0a980;
50  while( tftest.Year < 2110 ) {
51  /* test at the last second of the month */
52  pRtlTimeToTimeFields( &litime, &tfresult);
53  ok( tfresult.Year == tftest.Year && tfresult.Month == tftest.Month &&
54  tfresult.Day == tftest.Day && tfresult.Hour == tftest.Hour &&
55  tfresult.Minute == tftest.Minute && tfresult.Second == tftest.Second,
56  "#%d expected: %d-%d-%d %d:%d:%d got: %d-%d-%d %d:%d:%d\n", ++i,
57  tftest.Year, tftest.Month, tftest.Day,
58  tftest.Hour, tftest.Minute,tftest.Second,
59  tfresult.Year, tfresult.Month, tfresult.Day,
60  tfresult.Hour, tfresult.Minute, tfresult.Second);
61  /* test the inverse */
62  pRtlTimeFieldsToTime( &tfresult, &liresult);
63  ok( liresult.QuadPart == litime.QuadPart," TimeFieldsToTime failed on %d-%d-%d %d:%d:%d. Error is %d ticks\n",
64  tfresult.Year, tfresult.Month, tfresult.Day,
65  tfresult.Hour, tfresult.Minute, tfresult.Second,
66  (int) (liresult.QuadPart - litime.QuadPart) );
67  /* one second later is beginning of next month */
68  litime.QuadPart += TICKSPERSEC ;
69  pRtlTimeToTimeFields( &litime, &tfresult);
70  ok( tfresult.Year == tftest.Year + (tftest.Month ==12) &&
71  tfresult.Month == tftest.Month % 12 + 1 &&
72  tfresult.Day == 1 && tfresult.Hour == 0 &&
73  tfresult.Minute == 0 && tfresult.Second == 0,
74  "#%d expected: %d-%d-%d %d:%d:%d got: %d-%d-%d %d:%d:%d\n", ++i,
75  tftest.Year + (tftest.Month ==12),
76  tftest.Month % 12 + 1, 1, 0, 0, 0,
77  tfresult.Year, tfresult.Month, tfresult.Day,
78  tfresult.Hour, tfresult.Minute, tfresult.Second);
79  /* test the inverse */
80  pRtlTimeFieldsToTime( &tfresult, &liresult);
81  ok( liresult.QuadPart == litime.QuadPart," TimeFieldsToTime failed on %d-%d-%d %d:%d:%d. Error is %d ticks\n",
82  tfresult.Year, tfresult.Month, tfresult.Day,
83  tfresult.Hour, tfresult.Minute, tfresult.Second,
84  (int) (liresult.QuadPart - litime.QuadPart) );
85  /* advance to the end of the month */
86  litime.QuadPart -= TICKSPERSEC ;
87  if( tftest.Month == 12) {
88  tftest.Month = 1;
89  tftest.Year += 1;
90  } else
91  tftest.Month += 1;
92  tftest.Day = MonthLengths[IsLeapYear(tftest.Year)][tftest.Month - 1];
93  litime.QuadPart += (LONGLONG) tftest.Day * TICKSPERSEC * SECSPERDAY;
94  }
95 }
96 
98 {
99  HMODULE mod = GetModuleHandleA("ntdll.dll");
100  pRtlTimeToTimeFields = (void *)GetProcAddress(mod,"RtlTimeToTimeFields");
101  pRtlTimeFieldsToTime = (void *)GetProcAddress(mod,"RtlTimeFieldsToTime");
102  if (pRtlTimeToTimeFields && pRtlTimeFieldsToTime)
104  else
105  win_skip("Required time conversion functions are not available\n");
106 }
__u16 time
Definition: mkdosfs.c:366
START_TEST(time)
Definition: time.c:43
GLenum GLclampf GLint i
Definition: glfuncs.h:14
static VOID(WINAPI *pRtlTimeToTimeFields)(const LARGE_INTEGER *liTime
#define TICKSPERSEC
Definition: time.c:23
int64_t LONGLONG
Definition: typedefs.h:66
unsigned int BOOL
Definition: ntddk_ex.h:94
uint64_t ULONGLONG
Definition: typedefs.h:65
static const int MonthLengths[2][12]
Definition: time.c:30
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:818
#define SECSPERDAY
Definition: time.c:25
#define WINAPI
Definition: msvc.h:20
static BOOL IsLeapYear(int Year)
Definition: time.c:36
static TIME_FIELDS tftest
Definition: time.c:42
static void test_pRtlTimeToTimeFields(void)
Definition: time.c:44
HANDLE HMODULE
Definition: typedefs.h:75
#define GetProcAddress(x, y)
Definition: compat.h:410
#define ok(value,...)
Definition: CImage.cpp:33
static PTIME_FIELDS TimeFields
Definition: time.c:27
#define win_skip
Definition: test.h:141
static PLARGE_INTEGER Time
Definition: time.c:28
LONGLONG QuadPart
Definition: typedefs.h:112
static int mod
Definition: i386-dis.c:1273