2 * Unit test suite for timer functions
4 * Copyright 2004 Mike McCormack
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.
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.
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
21 #include "wine/test.h"
27 static void test_timer(void)
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" );
38 r
= SetWaitableTimer( handle
, &due
, 0x1f4, NULL
, NULL
, FALSE
);
39 ok( r
, "failed to set timer\n");
41 CloseHandle( handle
);
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" );
47 due
.QuadPart
= -10000;
48 r
= SetWaitableTimer( handle
, &due
, 0x1f4, NULL
, NULL
, FALSE
);
49 ok( r
, "failed to set timer\n");
51 CloseHandle( handle
);
54 #define TICKSPERSEC 10000000
56 static BOOL
adjust_system_time(int sec
)
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
))
70 return SetSystemTime(&st
);
73 static DWORD WINAPI
thread_WaitForSingleObject(void *arg
)
78 event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
79 ok(event
!= NULL
, "CreateEvent failed\n");
81 r
= WaitForSingleObject(event
, 3000);
82 ok(r
== WAIT_TIMEOUT
, "WiatForSingleObject returned %x\n", r
);
84 t
= GetTickCount() - t
;
85 ok(t
> 2000, "t = %d\n", t
);
89 static DWORD WINAPI
thread_Sleep(void *arg
)
91 DWORD t
= GetTickCount();
94 t
= GetTickCount() - t
;
95 ok(t
> 2000, "t = %d\n", t
);
99 static DWORD WINAPI
thread_SleepEx(void *arg
)
101 DWORD t
= GetTickCount();
104 t
= GetTickCount() - t
;
105 ok(t
> 2000, "t = %d\n", t
);
109 static DWORD WINAPI
thread_WaitableTimer_rel(void *arg
)
115 li
.QuadPart
= -3 * TICKSPERSEC
;
117 timer
= CreateWaitableTimerA(NULL
, TRUE
, NULL
);
118 ok(timer
!= NULL
, "CreateWaitableTimer failed\n");
121 r
= SetWaitableTimer(timer
, &li
, 0, NULL
, NULL
, FALSE
);
122 ok(r
, "SetWaitableTimer failed\n");
124 r
= WaitForSingleObject(timer
, INFINITE
);
125 ok(r
== WAIT_OBJECT_0
, "WaitForSingleObject returned %d\n", r
);
127 t
= GetTickCount() - t
;
128 ok(t
> 2000, "t = %d\n", t
);
132 static DWORD WINAPI
thread_WaitableTimer_abs(void *arg
)
139 GetSystemTimeAsFileTime(&ft
);
140 li
.u
.LowPart
= ft
.dwLowDateTime
;
141 li
.u
.HighPart
= ft
.dwHighDateTime
;
142 li
.QuadPart
+= 3 * TICKSPERSEC
;
144 timer
= CreateWaitableTimerA(NULL
, TRUE
, NULL
);
145 ok(timer
!= NULL
, "CreateWaitableTimer failed\n");
148 r
= SetWaitableTimer(timer
, &li
, 0, NULL
, NULL
, FALSE
);
149 ok(r
, "SetWaitableTimer failed\n");
151 r
= WaitForSingleObject(timer
, INFINITE
);
152 ok(r
== WAIT_OBJECT_0
, "WaitForSingleObject returned %d\n", r
);
154 t
= GetTickCount() - t
;
155 ok(t
< 2000, "t = %d\n", t
);
159 static DWORD WINAPI
thread_WaitableTimer_period(void *arg
)
167 timer
= CreateWaitableTimerA(NULL
, FALSE
, NULL
);
168 ok(timer
!= NULL
, "CreateWaitableTimer failed\n");
171 r
= SetWaitableTimer(timer
, &li
, 3000, NULL
, NULL
, FALSE
);
172 ok(r
, "SetWaitableTimer failed\n");
174 r
= WaitForSingleObject(timer
, INFINITE
);
175 ok(r
== WAIT_OBJECT_0
, "WaitForSingleObject returned %d\n", r
);
177 r
= WaitForSingleObject(timer
, INFINITE
);
178 ok(r
== WAIT_OBJECT_0
, "WaitForSingleObject returned %d\n", r
);
180 t
= GetTickCount() - t
;
181 ok(t
> 2000, "t = %d\n", t
);
185 static DWORD WINAPI
thread_SetTimer(void *arg
)
187 DWORD t
= GetTickCount();
191 timer
= SetTimer(NULL
, 0, 3000, NULL
);
192 ok(timer
, "SetTimer failed (%d)\n", GetLastError());
194 while (GetMessageW(&msg
, NULL
, 0, 0))
196 DispatchMessageW(&msg
);
197 if (msg
.message
== WM_TIMER
) break;
200 t
= GetTickCount() - t
;
201 ok(t
> 2000, "t = %d\n", t
);
202 KillTimer(NULL
, timer
);
206 static void test_timeouts(void)
211 if (!adjust_system_time(1))
213 skip("can't adjust system clock (%d)\n", GetLastError());
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");
229 adjust_system_time(10);
231 for (i
=0; i
<ARRAY_SIZE(threads
); i
++)
233 WaitForSingleObject(threads
[i
], INFINITE
);
234 CloseHandle(threads
[i
]);
236 adjust_system_time(-11);