Reactos
1/*
2 * Unit test suite for timer functions
3 *
4 * Copyright 2004 Mike McCormack
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 "wine/test.h"
22#include "winbase.h"
23#include "winternl.h"
24#include "winuser.h"
25
26
27static void test_timer(void)
28{
29 HANDLE handle;
30 BOOL r;
31 LARGE_INTEGER due;
32
33 /* try once with a positive number */
34 handle = CreateWaitableTimerA( NULL, 0, NULL );
35 ok( handle != NULL, "failed to create waitable timer with no name\n" );
36
37 due.QuadPart = 10000;
38 r = SetWaitableTimer( handle, &due, 0x1f4, NULL, NULL, FALSE );
39 ok( r, "failed to set timer\n");
40
41 CloseHandle( handle );
42
43 /* try once with a negative number */
44 handle = CreateWaitableTimerA( NULL, 0, NULL );
45 ok( handle != NULL, "failed to create waitable timer with no name\n" );
46
47 due.QuadPart = -10000;
48 r = SetWaitableTimer( handle, &due, 0x1f4, NULL, NULL, FALSE );
49 ok( r, "failed to set timer\n");
50
51 CloseHandle( handle );
52}
53
54#define TICKSPERSEC 10000000
55
56static BOOL adjust_system_time(int sec)
57{
58 ULARGE_INTEGER uli;
59 SYSTEMTIME st;
60 FILETIME ft;
61
62 GetSystemTimeAsFileTime(&ft);
63 uli.u.LowPart = ft.dwLowDateTime;
64 uli.u.HighPart = ft.dwHighDateTime;
65 uli.QuadPart += (LONGLONG)sec * TICKSPERSEC;
66 ft.dwLowDateTime = uli.u.LowPart;
67 ft.dwHighDateTime = uli.u.HighPart;
68 if (!FileTimeToSystemTime(&ft, &st))
69 return FALSE;
70 return SetSystemTime(&st);
71}
72
73static DWORD WINAPI thread_WaitForSingleObject(void *arg)
74{
75 HANDLE event;
76 DWORD t, r;
77
78 event = CreateEventW(NULL, FALSE, FALSE, NULL);
79 ok(event != NULL, "CreateEvent failed\n");
80 t = GetTickCount();
81 r = WaitForSingleObject(event, 3000);
82 ok(r == WAIT_TIMEOUT, "WiatForSingleObject returned %lx\n", r);
83 CloseHandle(event);
84 t = GetTickCount() - t;
85 ok(t > 2000, "t = %ld\n", t);
86 return 0;
87}
88
89static DWORD WINAPI thread_Sleep(void *arg)
90{
91 DWORD t = GetTickCount();
92
93 Sleep(3000);
94 t = GetTickCount() - t;
95 ok(t > 2000, "t = %ld\n", t);
96 return 0;
97}
98
99static DWORD WINAPI thread_SleepEx(void *arg)
100{
101 DWORD t = GetTickCount();
102
103 SleepEx(3000, TRUE);
104 t = GetTickCount() - t;
105 ok(t > 2000, "t = %ld\n", t);
106 return 0;
107}
108
109static DWORD WINAPI thread_WaitableTimer_rel(void *arg)
110{
111 LARGE_INTEGER li;
112 HANDLE timer;
113 DWORD t, r;
114
115 li.QuadPart = -3 * TICKSPERSEC;
116
117 timer = CreateWaitableTimerA(NULL, TRUE, NULL);
118 ok(timer != NULL, "CreateWaitableTimer failed\n");
119
120 t = GetTickCount();
121 r = SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE);
122 ok(r, "SetWaitableTimer failed\n");
123
124 r = WaitForSingleObject(timer, INFINITE);
125 ok(r == WAIT_OBJECT_0, "WaitForSingleObject returned %ld\n", r);
126 CloseHandle(timer);
127 t = GetTickCount() - t;
128 ok(t > 2000, "t = %ld\n", t);
129 return 0;
130}
131
132static DWORD WINAPI thread_WaitableTimer_abs(void *arg)
133{
134 LARGE_INTEGER li;
135 HANDLE timer;
136 FILETIME ft;
137 DWORD t, r;
138
139 GetSystemTimeAsFileTime(&ft);
140 li.u.LowPart = ft.dwLowDateTime;
141 li.u.HighPart = ft.dwHighDateTime;
142 li.QuadPart += 3 * TICKSPERSEC;
143
144 timer = CreateWaitableTimerA(NULL, TRUE, NULL);
145 ok(timer != NULL, "CreateWaitableTimer failed\n");
146
147 t = GetTickCount();
148 r = SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE);
149 ok(r, "SetWaitableTimer failed\n");
150
151 r = WaitForSingleObject(timer, INFINITE);
152 ok(r == WAIT_OBJECT_0, "WaitForSingleObject returned %ld\n", r);
153 CloseHandle(timer);
154 t = GetTickCount() - t;
155 ok(t < 2000, "t = %ld\n", t);
156 return 0;
157}
158
159static DWORD WINAPI thread_WaitableTimer_period(void *arg)
160{
161 LARGE_INTEGER li;
162 HANDLE timer;
163 DWORD t, r;
164
165 li.QuadPart = -1;
166
167 timer = CreateWaitableTimerA(NULL, FALSE, NULL);
168 ok(timer != NULL, "CreateWaitableTimer failed\n");
169
170 t = GetTickCount();
171 r = SetWaitableTimer(timer, &li, 3000, NULL, NULL, FALSE);
172 ok(r, "SetWaitableTimer failed\n");
173
174 r = WaitForSingleObject(timer, INFINITE);
175 ok(r == WAIT_OBJECT_0, "WaitForSingleObject returned %ld\n", r);
176
177 r = WaitForSingleObject(timer, INFINITE);
178 ok(r == WAIT_OBJECT_0, "WaitForSingleObject returned %ld\n", r);
179 CloseHandle(timer);
180 t = GetTickCount() - t;
181 ok(t > 2000, "t = %ld\n", t);
182 return 0;
183}
184
185static DWORD WINAPI thread_SetTimer(void *arg)
186{
187 DWORD t = GetTickCount();
188 UINT_PTR timer;
189 MSG msg;
190
191 timer = SetTimer(NULL, 0, 3000, NULL);
192 ok(timer, "SetTimer failed (%ld)\n", GetLastError());
193
194 while (GetMessageW(&msg, NULL, 0, 0))
195 {
196 DispatchMessageW(&msg);
197 if (msg.message == WM_TIMER) break;
198 }
199
200 t = GetTickCount() - t;
201 ok(t > 2000, "t = %ld\n", t);
202 KillTimer(NULL, timer);
203 return 0;
204}
205
206static void test_timeouts(void)
207{
208 HANDLE threads[7];
209 DWORD i;
210
211 if (!adjust_system_time(1))
212 {
213 skip("can't adjust system clock (%ld)\n", GetLastError());
214 return;
215 }
216
217
218 threads[0] = CreateThread(NULL, 0, thread_WaitForSingleObject, NULL, 0, NULL);
219 threads[1] = CreateThread(NULL, 0, thread_Sleep, NULL, 0, NULL);
220 threads[2] = CreateThread(NULL, 0, thread_SleepEx, NULL, 0, NULL);
221 threads[3] = CreateThread(NULL, 0, thread_WaitableTimer_rel, NULL, 0, NULL);
222 threads[4] = CreateThread(NULL, 0, thread_WaitableTimer_abs, NULL, 0, NULL);
223 threads[5] = CreateThread(NULL, 0, thread_WaitableTimer_period, NULL, 0, NULL);
224 threads[6] = CreateThread(NULL, 0, thread_SetTimer, NULL, 0, NULL);
225 for(i=0; i<ARRAY_SIZE(threads); i++)
226 ok(threads[i] != NULL, "CreateThread failed\n");
227
228 Sleep(500);
229 adjust_system_time(10);
230
231 for (i=0; i<ARRAY_SIZE(threads); i++)
232 {
233 WaitForSingleObject(threads[i], INFINITE);
234 CloseHandle(threads[i]);
235 }
236 adjust_system_time(-11);
237}
238
239START_TEST(timer)
240{
241 test_timer();
242 test_timeouts();
243}