Fix timer tests to pass on windows 98.
[wine/gsoc_dplay.git] / dlls / winmm / tests / timer.c
blob170b0e0047fdba2a24367722df7ad74d3b484759
1 /*
2 * Test winmm timer
4 * Copyright (c) 2005 Robert Reif
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <math.h>
26 #include "wine/test.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "mmsystem.h"
31 #define NOBITMAP
32 #include "mmreg.h"
34 #include "winmm_test.h"
36 static TIMECAPS tc;
38 static void test_timeGetDevCaps()
40 MMRESULT rc;
42 rc = timeGetDevCaps(&tc, 0);
43 ok(rc == TIMERR_NOCANDO || rc == MMSYSERR_INVALPARAM,
44 "timeGetDevCaps() returned %s, should have returned TIMERR_NOCANDO "
45 "or MMSYSERR_INVALPARAM\n", mmsys_error(rc));
47 rc = timeGetDevCaps(0, sizeof(tc));
48 ok(rc == TIMERR_NOCANDO || rc == TIMERR_STRUCT,
49 "timeGetDevCaps() returned %s, should have returned TIMERR_NOCANDO "
50 "or TIMERR_STRUCT\n", mmsys_error(rc));
52 rc = timeGetDevCaps(0, 0);
53 ok(rc == TIMERR_NOCANDO || rc == MMSYSERR_INVALPARAM,
54 "timeGetDevCaps() returned %s, should have returned TIMERR_NOCANDO "
55 "or MMSYSERR_INVALPARAM\n", mmsys_error(rc));
57 rc = timeGetDevCaps(&tc, sizeof(tc));
58 ok(rc == TIMERR_NOERROR, "timeGetDevCaps() returned %s, "
59 "should have returned TIMERR_NOERROR\n", mmsys_error(rc));
61 if (rc == TIMERR_NOERROR)
62 trace("wPeriodMin = %u, wPeriodMax = %u\n",
63 tc.wPeriodMin, tc.wPeriodMax);
66 #define NUM_SAMPLES 100
68 static DWORD count = 0;
69 static DWORD times[NUM_SAMPLES];
71 static void CALLBACK testTimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
73 if (count < NUM_SAMPLES)
74 times[count++] = timeGetTime();
77 static void test_timer(UINT period, UINT resolution)
79 MMRESULT rc;
80 UINT i, id, delta;
81 DWORD dwMin = 0xffffffff, dwMax = 0;
82 double sum = 0.0;
83 double deviation = 0.0;
85 count = 0;
87 for (i = 0; i < NUM_SAMPLES; i++)
88 times[i] = 0;
90 rc = timeBeginPeriod(period);
91 ok(rc == TIMERR_NOERROR, "timeBeginPeriod(%u) returned %s, "
92 "should have returned TIMERR_NOERROR\n", period, mmsys_error(rc));
93 if (rc != TIMERR_NOERROR)
94 return;
96 id = timeSetEvent(period, resolution, testTimeProc, 0, TIME_PERIODIC);
97 ok(id != 0, "timeSetEvent(%u, %u, %p, 0, TIME_PERIODIC) returned %d, "
98 "should have returned id > 0\n", period, resolution, testTimeProc, id);
99 if (id == 0)
100 return;
102 Sleep((NUM_SAMPLES * period) + (2 * period));
104 rc = timeEndPeriod(period);
105 ok(rc == TIMERR_NOERROR, "timeEndPeriod(%u) returned %s, "
106 "should have returned TIMERR_NOERROR\n", period, mmsys_error(rc));
107 if (rc != TIMERR_NOERROR)
108 return;
110 rc = timeKillEvent(id);
111 ok(rc == TIMERR_NOERROR, "timeKillEvent(%u) returned %s, "
112 "should have returned TIMERR_NOERROR\n", id, mmsys_error(rc));
114 trace("period = %u, resolution = %u\n", period, resolution);
116 for (i = 0; i < count; i++)
118 if (i == 0)
120 if (winetest_debug > 1)
121 trace("time[%d] = %lu\n", i, times[i]);
123 else
125 delta = times[i] - times[i - 1];
127 if (winetest_debug > 1)
128 trace("time[%d] = %lu delta = %d\n", i, times[i], delta);
130 sum += delta;
131 deviation += ((delta - period) * (delta - period));
133 if (delta < dwMin)
134 dwMin = delta;
136 if (delta > dwMax)
137 dwMax = delta;
141 trace("min = %lu, max = %lu, average = %f, standard deviation = %f\n",
142 dwMin, dwMax, sum / (count - 1), sqrt(deviation / (count - 2)));
145 START_TEST(timer)
147 test_timeGetDevCaps();
149 if (tc.wPeriodMin <= 1) {
150 test_timer(1, 0);
151 test_timer(1, 1);
154 if (tc.wPeriodMin <= 10) {
155 test_timer(10, 0);
156 test_timer(10, 1);
157 test_timer(10, 10);
160 if (tc.wPeriodMin <= 20) {
161 test_timer(20, 0);
162 test_timer(20, 1);
163 test_timer(20, 10);
164 test_timer(20, 20);