Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
201 lines (160 loc) · 6.18 KB

testautomation_timer.c

File metadata and controls

201 lines (160 loc) · 6.18 KB
 
Jan 14, 2013
Jan 14, 2013
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Timer test suite
*/
#include <stdio.h>
#include "SDL.h"
#include "SDL_test.h"
/* Flag indicating if the param should be checked */
int _paramCheck = 0;
/* Userdata value to check */
int _paramValue = 0;
/* Flag indicating that the callback was called */
int _timerCallbackCalled = 0;
/* Fixture */
void
_timerSetUp(void *arg)
{
May 18, 2013
May 18, 2013
24
25
/* Start SDL timer subsystem */
int ret = SDL_InitSubSystem( SDL_INIT_TIMER );
Jan 14, 2013
Jan 14, 2013
26
SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_TIMER)");
May 18, 2013
May 18, 2013
27
28
SDLTest_AssertCheck(ret==0, "Check result from SDL_InitSubSystem(SDL_INIT_TIMER)");
if (ret != 0) {
Jan 14, 2013
Jan 14, 2013
29
30
31
32
33
34
35
36
37
38
39
40
41
SDLTest_LogError("%s", SDL_GetError());
}
}
/* Test case functions */
/**
* @brief Call to SDL_GetPerformanceCounter
*/
int
timer_getPerformanceCounter(void *arg)
{
Uint64 result;
May 18, 2013
May 18, 2013
42
Jan 14, 2013
Jan 14, 2013
43
44
45
result = SDL_GetPerformanceCounter();
SDLTest_AssertPass("Call to SDL_GetPerformanceCounter()");
SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
May 18, 2013
May 18, 2013
46
Jan 14, 2013
Jan 14, 2013
47
48
49
50
51
52
53
54
55
56
return TEST_COMPLETED;
}
/**
* @brief Call to SDL_GetPerformanceFrequency
*/
int
timer_getPerformanceFrequency(void *arg)
{
Uint64 result;
May 18, 2013
May 18, 2013
57
Jan 14, 2013
Jan 14, 2013
58
59
60
result = SDL_GetPerformanceFrequency();
SDLTest_AssertPass("Call to SDL_GetPerformanceFrequency()");
SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
May 18, 2013
May 18, 2013
61
Jan 14, 2013
Jan 14, 2013
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
return TEST_COMPLETED;
}
/**
* @brief Call to SDL_Delay and SDL_GetTicks
*/
int
timer_delayAndGetTicks(void *arg)
{
const Uint32 testDelay = 100;
const Uint32 marginOfError = 25;
Uint32 result;
Uint32 result2;
Uint32 difference;
May 18, 2013
May 18, 2013
77
/* Zero delay */
Jan 14, 2013
Jan 14, 2013
78
79
SDL_Delay(0);
SDLTest_AssertPass("Call to SDL_Delay(0)");
May 18, 2013
May 18, 2013
80
Jan 14, 2013
Jan 14, 2013
81
82
83
84
85
86
/* Non-zero delay */
SDL_Delay(1);
SDLTest_AssertPass("Call to SDL_Delay(1)");
SDL_Delay(SDLTest_RandomIntegerInRange(5, 15));
SDLTest_AssertPass("Call to SDL_Delay()");
May 18, 2013
May 18, 2013
87
88
/* Get ticks count - should be non-zero by now */
Jan 14, 2013
Jan 14, 2013
89
90
91
92
result = SDL_GetTicks();
SDLTest_AssertPass("Call to SDL_GetTicks()");
SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %d", result);
May 5, 2013
May 5, 2013
93
/* Delay a bit longer and measure ticks and verify difference */
Jan 14, 2013
Jan 14, 2013
94
95
96
97
98
99
100
101
SDL_Delay(testDelay);
SDLTest_AssertPass("Call to SDL_Delay(%d)", testDelay);
result2 = SDL_GetTicks();
SDLTest_AssertPass("Call to SDL_GetTicks()");
SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %d", result2);
difference = result2 - result;
SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %d", testDelay - marginOfError, difference);
SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %d", testDelay + marginOfError, difference);
May 18, 2013
May 18, 2013
102
Jan 14, 2013
Jan 14, 2013
103
104
105
106
107
108
109
return TEST_COMPLETED;
}
/* Test callback */
Uint32 _timerTestCallback(Uint32 interval, void *param)
{
_timerCallbackCalled = 1;
May 18, 2013
May 18, 2013
110
Jan 14, 2013
Jan 14, 2013
111
112
113
114
115
116
if (_paramCheck != 0) {
SDLTest_AssertCheck(param != NULL, "Check param pointer, expected: non-NULL, got: %s", (param != NULL) ? "non-NULL" : "NULL");
if (param != NULL) {
SDLTest_AssertCheck(*(int *)param == _paramValue, "Check param value, expected: %i, got: %i", _paramValue, *(int *)param);
}
}
May 18, 2013
May 18, 2013
117
Jan 14, 2013
Jan 14, 2013
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
return 0;
}
/**
* @brief Call to SDL_AddTimer and SDL_RemoveTimer
*/
int
timer_addRemoveTimer(void *arg)
{
SDL_TimerID id;
SDL_bool result;
int param;
/* Reset state */
_paramCheck = 0;
_timerCallbackCalled = 0;
May 18, 2013
May 18, 2013
134
135
/* Set timer with a long delay */
Jan 14, 2013
Jan 14, 2013
136
137
138
id = SDL_AddTimer(10000, _timerTestCallback, NULL);
SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)");
SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
May 18, 2013
May 18, 2013
139
Jan 14, 2013
Jan 14, 2013
140
141
142
143
144
145
/* Remove timer again and check that callback was not called */
result = SDL_RemoveTimer(id);
SDLTest_AssertPass("Call to SDL_RemoveTimer()");
SDLTest_AssertCheck(result == SDL_TRUE, "Check result value, expected: %i, got: %i", SDL_TRUE, result);
SDLTest_AssertCheck(_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", _timerCallbackCalled);
May 5, 2013
May 5, 2013
146
/* Try to remove timer again (should be a NOOP) */
Jan 14, 2013
Jan 14, 2013
147
148
149
150
151
152
153
154
155
156
result = SDL_RemoveTimer(id);
SDLTest_AssertPass("Call to SDL_RemoveTimer()");
SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
/* Reset state */
param = SDLTest_RandomIntegerInRange(-1024, 1024);
_paramCheck = 1;
_paramValue = param;
_timerCallbackCalled = 0;
May 18, 2013
May 18, 2013
157
/* Set timer with a short delay */
Jan 14, 2013
Jan 14, 2013
158
159
160
id = SDL_AddTimer(10, _timerTestCallback, (void *)&param);
SDLTest_AssertPass("Call to SDL_AddTimer(10, param)");
SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
May 18, 2013
May 18, 2013
161
Jan 14, 2013
Jan 14, 2013
162
163
164
/* Wait to let timer trigger callback */
SDL_Delay(100);
SDLTest_AssertPass("Call to SDL_Delay(100)");
May 18, 2013
May 18, 2013
165
Jan 14, 2013
Jan 14, 2013
166
167
168
169
170
/* Remove timer again and check that callback was called */
result = SDL_RemoveTimer(id);
SDLTest_AssertPass("Call to SDL_RemoveTimer()");
SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
SDLTest_AssertCheck(_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", _timerCallbackCalled);
May 18, 2013
May 18, 2013
171
Jan 14, 2013
Jan 14, 2013
172
173
174
175
176
177
178
return TEST_COMPLETED;
}
/* ================= Test References ================== */
/* Timer test cases */
static const SDLTest_TestCaseReference timerTest1 =
May 18, 2013
May 18, 2013
179
{ (SDLTest_TestCaseFp)timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED };
Jan 14, 2013
Jan 14, 2013
180
181
static const SDLTest_TestCaseReference timerTest2 =
May 18, 2013
May 18, 2013
182
{ (SDLTest_TestCaseFp)timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED };
Jan 14, 2013
Jan 14, 2013
183
184
static const SDLTest_TestCaseReference timerTest3 =
May 18, 2013
May 18, 2013
185
{ (SDLTest_TestCaseFp)timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED };
Jan 14, 2013
Jan 14, 2013
186
187
static const SDLTest_TestCaseReference timerTest4 =
May 18, 2013
May 18, 2013
188
{ (SDLTest_TestCaseFp)timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED };
Jan 14, 2013
Jan 14, 2013
189
190
191
/* Sequence of Timer test cases */
static const SDLTest_TestCaseReference *timerTests[] = {
May 18, 2013
May 18, 2013
192
&timerTest1, &timerTest2, &timerTest3, &timerTest4, NULL
Jan 14, 2013
Jan 14, 2013
193
194
195
196
};
/* Timer test suite (global) */
SDLTest_TestSuiteReference timerTestSuite = {
May 18, 2013
May 18, 2013
197
198
199
200
"Timer",
_timerSetUp,
timerTests,
NULL
Jan 14, 2013
Jan 14, 2013
201
};