1 /*-------------------------------------------------------------------------
2 * drawElements Utility Library
3 * ----------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Periodic timer test.
22 *//*--------------------------------------------------------------------*/
24 #include "deTimerTest.h"
32 static void timerCallback (void* arg)
34 volatile int* numCalls = (volatile int*)arg;
38 void deTimer_selfTest (void)
40 const int numIters = 25;
41 const int minInterval = 1;
42 const int maxInterval = 100;
43 const int intervalSleepMultiplier = 5;
46 deTimer* timer = DE_NULL;
47 volatile int numCalls = 0;
49 deRandom_init(&rnd, 6789);
51 timer = deTimer_create(timerCallback, (void*)&numCalls);
52 DE_TEST_ASSERT(timer);
54 for (iter = 0; iter < numIters; iter++)
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;
61 printf("Iter %d / %d: %d ms %s timer\n", iter+1, numIters, interval, (isSingle ? "single" : "interval"));
65 scheduleOk = deTimer_scheduleSingle(timer, interval);
67 scheduleOk = deTimer_scheduleInterval(timer, interval);
69 DE_TEST_ASSERT(scheduleOk);
71 deSleep((deUint32)(interval*intervalSleepMultiplier));
72 deTimer_disable(timer);
73 deSleep((deUint32)interval);
75 printf(" timer fired %d times, expected %d\n", numCalls, expectedCalls);
76 DE_TEST_ASSERT(!isSingle || numCalls == 1);
79 deTimer_destroy(timer);