timer.c 6.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Test winmm timer
 *
 * Copyright (c) 2005 Robert Reif
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "wine/test.h"
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "mmsystem.h"
#define NOBITMAP
#include "mmreg.h"

#include "winmm_test.h"

36
static TIMECAPS tc;
37

38
static void test_timeGetDevCaps(void)
39 40 41 42
{
   MMRESULT rc;

    rc = timeGetDevCaps(&tc, 0);
43 44 45
    ok(rc == TIMERR_NOCANDO || rc == MMSYSERR_INVALPARAM,
       "timeGetDevCaps() returned %s, should have returned TIMERR_NOCANDO "
       "or MMSYSERR_INVALPARAM\n", mmsys_error(rc));
46 47

    rc = timeGetDevCaps(0, sizeof(tc));
48 49 50
    ok(rc == TIMERR_NOCANDO || rc == TIMERR_STRUCT,
       "timeGetDevCaps() returned %s, should have returned TIMERR_NOCANDO "
       "or TIMERR_STRUCT\n", mmsys_error(rc));
51 52

    rc = timeGetDevCaps(0, 0);
53 54 55
    ok(rc == TIMERR_NOCANDO || rc == MMSYSERR_INVALPARAM,
       "timeGetDevCaps() returned %s, should have returned TIMERR_NOCANDO "
       "or MMSYSERR_INVALPARAM\n", mmsys_error(rc));
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    rc = timeGetDevCaps(&tc, sizeof(tc));
    ok(rc == TIMERR_NOERROR, "timeGetDevCaps() returned %s, "
       "should have returned TIMERR_NOERROR\n", mmsys_error(rc));

    if (rc == TIMERR_NOERROR)
        trace("wPeriodMin = %u, wPeriodMax = %u\n",
              tc.wPeriodMin, tc.wPeriodMax);
}

#define NUM_SAMPLES    100

static DWORD count = 0;
static DWORD times[NUM_SAMPLES];

71 72
static void CALLBACK testTimeProc(UINT uID, UINT uMsg, DWORD_PTR dwUser,
                                  DWORD_PTR dw1, DWORD_PTR dw2)
73 74 75 76 77
{
    if (count < NUM_SAMPLES)
        times[count++] = timeGetTime();
}

78
static void test_timer(UINT period, UINT resolution)
79 80 81 82 83 84 85 86 87 88 89 90 91 92
{
    MMRESULT rc;
    UINT i, id, delta;
    DWORD dwMin = 0xffffffff, dwMax = 0;
    double sum = 0.0;
    double deviation = 0.0;

    count = 0;

    for (i = 0; i < NUM_SAMPLES; i++)
        times[i] = 0;

    rc = timeBeginPeriod(period);
    ok(rc == TIMERR_NOERROR, "timeBeginPeriod(%u) returned %s, "
Robert Reif's avatar
Robert Reif committed
93
       "should have returned TIMERR_NOERROR\n", period, mmsys_error(rc));
94 95 96 97 98
    if (rc != TIMERR_NOERROR)
        return;

    id = timeSetEvent(period, resolution, testTimeProc, 0, TIME_PERIODIC);
    ok(id != 0, "timeSetEvent(%u, %u, %p, 0, TIME_PERIODIC) returned %d, "
Robert Reif's avatar
Robert Reif committed
99
       "should have returned id > 0\n", period, resolution, testTimeProc, id);
100 101 102 103 104 105 106
    if (id == 0)
        return;

    Sleep((NUM_SAMPLES * period) + (2 * period));

    rc = timeEndPeriod(period);
    ok(rc == TIMERR_NOERROR, "timeEndPeriod(%u) returned %s, "
Robert Reif's avatar
Robert Reif committed
107
       "should have returned TIMERR_NOERROR\n", period, mmsys_error(rc));
108 109 110 111 112
    if (rc != TIMERR_NOERROR)
        return;

    rc = timeKillEvent(id);
    ok(rc == TIMERR_NOERROR, "timeKillEvent(%u) returned %s, "
Robert Reif's avatar
Robert Reif committed
113
       "should have returned TIMERR_NOERROR\n", id, mmsys_error(rc));
114 115 116 117 118 119 120 121

    trace("period = %u, resolution = %u\n", period, resolution);

    for (i = 0; i < count; i++)
    {
        if (i == 0)
        {
            if (winetest_debug > 1)
122
                trace("time[%d] = %u\n", i, times[i]);
123 124 125 126 127 128
        }
        else
        {
            delta = times[i] - times[i - 1];

            if (winetest_debug > 1)
129
                trace("time[%d] = %u delta = %d\n", i, times[i], delta);
130 131

            sum += delta;
Robert Reif's avatar
Robert Reif committed
132
            deviation += ((delta - period) * (delta - period));
133 134 135 136 137 138 139 140 141

            if (delta < dwMin)
                dwMin = delta;

            if (delta > dwMax)
                dwMax = delta;
        }
    }

142
    trace("min = %u, max = %u, average = %f, standard deviation = %f\n",
143 144 145
          dwMin, dwMax, sum / (count - 1), sqrt(deviation / (count - 2)));
}

Mike McCormack's avatar
Mike McCormack committed
146
static const char * get_priority(int priority)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
{
    static char     tmp[32];
#define STR(x) case x: return #x
    switch(priority) {
    STR(THREAD_PRIORITY_LOWEST);
    STR(THREAD_PRIORITY_BELOW_NORMAL);
    STR(THREAD_PRIORITY_NORMAL);
    STR(THREAD_PRIORITY_HIGHEST);
    STR(THREAD_PRIORITY_ABOVE_NORMAL);
    STR(THREAD_PRIORITY_TIME_CRITICAL);
    STR(THREAD_PRIORITY_IDLE);
    }
    sprintf(tmp, "UNKNOWN(%d)", priority);
    return tmp;
}

static int priority = 0;
static BOOL fired = FALSE;

166 167
static void CALLBACK priorityTimeProc(UINT uID, UINT uMsg, DWORD_PTR dwUser,
                                      DWORD_PTR dw1, DWORD_PTR dw2)
168 169
{
    priority = GetThreadPriority(GetCurrentThread());
170
    ok(priority!=THREAD_PRIORITY_ERROR_RETURN, "GetThreadPriority() failed, GetLastError() = %u\n", GetLastError());
171 172 173
    fired = TRUE;
}

Mike McCormack's avatar
Mike McCormack committed
174
static void test_priority(void)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
{
    UINT id;

    id = timeSetEvent(100, 100, priorityTimeProc, 0, TIME_ONESHOT);
    ok(id != 0, "timeSetEvent(100, 100, %p, 0, TIME_ONESHOT) returned %d, "
       "should have returned id > 0\n", priorityTimeProc, id);
    if (id == 0)
        return;

    Sleep(200);

    ok(fired == TRUE, "Callback not called\n");
    if (fired)
    {
        ok(priority == THREAD_PRIORITY_TIME_CRITICAL,
           "thread priority is %s, should be THREAD_PRIORITY_TIME_CRITICAL\n",
           get_priority(priority));
    }
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
START_TEST(timer)
{
    test_timeGetDevCaps();

    if (tc.wPeriodMin <= 1) {
        test_timer(1, 0);
        test_timer(1, 1);
    }

    if (tc.wPeriodMin <= 10) {
        test_timer(10, 0);
        test_timer(10, 1);
        test_timer(10, 10);
    }

    if (tc.wPeriodMin <= 20) {
        test_timer(20, 0);
        test_timer(20, 1);
        test_timer(20, 10);
        test_timer(20, 20);
    }
216 217

    test_priority();
218
}