Debian Codegraph @

test/testautomation_timer.c

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