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