1 /*-------------------------------------------------------------------------
2 * drawElements Utility Library
3 * ----------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Periodic timer test.
22 *//*--------------------------------------------------------------------*/
23
24 #include "deTimerTest.h"
25
26 #include "deTimer.h"
27 #include "deRandom.h"
28 #include "deThread.h"
29
30 #include <stdio.h>
31
timerCallback(void * arg)32 static void timerCallback (void* arg)
33 {
34 volatile int* numCalls = (volatile int*)arg;
35 ++(*numCalls);
36 }
37
deTimer_selfTest(void)38 void deTimer_selfTest (void)
39 {
40 const int numIters = 25;
41 const int minInterval = 1;
42 const int maxInterval = 100;
43 const int intervalSleepMultiplier = 5;
44 int iter;
45 deRandom rnd;
46 deTimer* timer = DE_NULL;
47 volatile int numCalls = 0;
48
49 deRandom_init(&rnd, 6789);
50
51 timer = deTimer_create(timerCallback, (void*)&numCalls);
52 DE_TEST_ASSERT(timer);
53
54 for (iter = 0; iter < numIters; iter++)
55 {
56 deBool isSingle = deRandom_getFloat(&rnd) < 0.25f;
57 int interval = minInterval + (int)(deRandom_getUint32(&rnd) % (deUint32)(maxInterval-minInterval+1));
58 int expectedCalls = isSingle ? 1 : intervalSleepMultiplier;
59 deBool scheduleOk = DE_FALSE;
60
61 printf("Iter %d / %d: %d ms %s timer\n", iter+1, numIters, interval, (isSingle ? "single" : "interval"));
62 numCalls = 0;
63
64 if (isSingle)
65 scheduleOk = deTimer_scheduleSingle(timer, interval);
66 else
67 scheduleOk = deTimer_scheduleInterval(timer, interval);
68
69 DE_TEST_ASSERT(scheduleOk);
70
71 deSleep((deUint32)(interval*intervalSleepMultiplier));
72 deTimer_disable(timer);
73 deSleep((deUint32)interval);
74
75 printf(" timer fired %d times, expected %d\n", numCalls, expectedCalls);
76 DE_TEST_ASSERT(!isSingle || numCalls == 1);
77 }
78
79 deTimer_destroy(timer);
80 }
81