SDL  2.0
testautomation_timer.c
Go to the documentation of this file.
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 */
18 
19 /* Fixture */
20 
21 void
22 _timerSetUp(void *arg)
23 {
24  /* Start SDL timer subsystem */
25  int ret = SDL_InitSubSystem( SDL_INIT_TIMER );
26  SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_TIMER)");
27  SDLTest_AssertCheck(ret==0, "Check result from SDL_InitSubSystem(SDL_INIT_TIMER)");
28  if (ret != 0) {
30  }
31 }
32 
33 /* Test case functions */
34 
35 /**
36  * @brief Call to SDL_GetPerformanceCounter
37  */
38 int
40 {
41  Uint64 result;
42 
44  SDLTest_AssertPass("Call to SDL_GetPerformanceCounter()");
45  SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %"SDL_PRIu64, result);
46 
47  return TEST_COMPLETED;
48 }
49 
50 /**
51  * @brief Call to SDL_GetPerformanceFrequency
52  */
53 int
55 {
56  Uint64 result;
57 
59  SDLTest_AssertPass("Call to SDL_GetPerformanceFrequency()");
60  SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %"SDL_PRIu64, result);
61 
62  return TEST_COMPLETED;
63 }
64 
65 /**
66  * @brief Call to SDL_Delay and SDL_GetTicks
67  */
68 int
70 {
71  const Uint32 testDelay = 100;
72  const Uint32 marginOfError = 25;
73  Uint32 result;
74  Uint32 result2;
75  Uint32 difference;
76 
77  /* Zero delay */
78  SDL_Delay(0);
79  SDLTest_AssertPass("Call to SDL_Delay(0)");
80 
81  /* Non-zero delay */
82  SDL_Delay(1);
83  SDLTest_AssertPass("Call to SDL_Delay(1)");
84 
86  SDLTest_AssertPass("Call to SDL_Delay()");
87 
88  /* Get ticks count - should be non-zero by now */
89  result = SDL_GetTicks();
90  SDLTest_AssertPass("Call to SDL_GetTicks()");
91  SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %d", result);
92 
93  /* Delay a bit longer and measure ticks and verify difference */
94  SDL_Delay(testDelay);
95  SDLTest_AssertPass("Call to SDL_Delay(%d)", testDelay);
96  result2 = SDL_GetTicks();
97  SDLTest_AssertPass("Call to SDL_GetTicks()");
98  SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %d", result2);
99  difference = result2 - result;
100  SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %d", testDelay - marginOfError, difference);
101  SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %d", testDelay + marginOfError, difference);
102 
103  return TEST_COMPLETED;
104 }
105 
106 /* Test callback */
108 {
110 
111  if (_paramCheck != 0) {
112  SDLTest_AssertCheck(param != NULL, "Check param pointer, expected: non-NULL, got: %s", (param != NULL) ? "non-NULL" : "NULL");
113  if (param != NULL) {
114  SDLTest_AssertCheck(*(int *)param == _paramValue, "Check param value, expected: %i, got: %i", _paramValue, *(int *)param);
115  }
116  }
117 
118  return 0;
119 }
120 
121 /**
122  * @brief Call to SDL_AddTimer and SDL_RemoveTimer
123  */
124 int
126 {
127  SDL_TimerID id;
129  int param;
130 
131  /* Reset state */
132  _paramCheck = 0;
134 
135  /* Set timer with a long delay */
136  id = SDL_AddTimer(10000, _timerTestCallback, NULL);
137  SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)");
138  SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
139 
140  /* Remove timer again and check that callback was not called */
141  result = SDL_RemoveTimer(id);
142  SDLTest_AssertPass("Call to SDL_RemoveTimer()");
143  SDLTest_AssertCheck(result == SDL_TRUE, "Check result value, expected: %i, got: %i", SDL_TRUE, result);
144  SDLTest_AssertCheck(_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", _timerCallbackCalled);
145 
146  /* Try to remove timer again (should be a NOOP) */
147  result = SDL_RemoveTimer(id);
148  SDLTest_AssertPass("Call to SDL_RemoveTimer()");
149  SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
150 
151  /* Reset state */
152  param = SDLTest_RandomIntegerInRange(-1024, 1024);
153  _paramCheck = 1;
154  _paramValue = param;
156 
157  /* Set timer with a short delay */
158  id = SDL_AddTimer(10, _timerTestCallback, (void *)&param);
159  SDLTest_AssertPass("Call to SDL_AddTimer(10, param)");
160  SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
161 
162  /* Wait to let timer trigger callback */
163  SDL_Delay(100);
164  SDLTest_AssertPass("Call to SDL_Delay(100)");
165 
166  /* Remove timer again and check that callback was called */
167  result = SDL_RemoveTimer(id);
168  SDLTest_AssertPass("Call to SDL_RemoveTimer()");
169  SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
170  SDLTest_AssertCheck(_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", _timerCallbackCalled);
171 
172  return TEST_COMPLETED;
173 }
174 
175 /* ================= Test References ================== */
176 
177 /* Timer test cases */
179  { (SDLTest_TestCaseFp)timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED };
180 
182  { (SDLTest_TestCaseFp)timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED };
183 
185  { (SDLTest_TestCaseFp)timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED };
186 
188  { (SDLTest_TestCaseFp)timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED };
189 
190 /* Sequence of Timer test cases */
193 };
194 
195 /* Timer test suite (global) */
197  "Timer",
198  _timerSetUp,
199  timerTests,
200  NULL
201 };
SDL.h
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_test.h
_timerSetUp
void _timerSetUp(void *arg)
Definition: testautomation_timer.c:22
NULL
#define NULL
Definition: begin_code.h:167
SDL_GetPerformanceFrequency
Uint64 SDL_GetPerformanceFrequency(void)
Get the count per second of the high resolution counter.
timerTest4
static const SDLTest_TestCaseReference timerTest4
Definition: testautomation_timer.c:187
_timerCallbackCalled
int _timerCallbackCalled
Definition: testautomation_timer.c:17
timerTest3
static const SDLTest_TestCaseReference timerTest3
Definition: testautomation_timer.c:184
SDLCALL
#define SDLCALL
Definition: SDL_internal.h:49
SDL_TimerID
int SDL_TimerID
Definition: SDL_timer.h:86
SDL_PRIu64
#define SDL_PRIu64
Definition: SDL_stdinc.h:238
TEST_ENABLED
#define TEST_ENABLED
Definition: SDL_test_harness.h:47
_timerTestCallback
Uint32 _timerTestCallback(Uint32 interval, void *param)
Definition: testautomation_timer.c:107
timerTest2
static const SDLTest_TestCaseReference timerTest2
Definition: testautomation_timer.c:181
SDL_InitSubSystem
#define SDL_InitSubSystem
Definition: SDL_dynapi_overrides.h:55
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9435
SDL_AddTimer
#define SDL_AddTimer
Definition: SDL_dynapi_overrides.h:487
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDLTest_TestCaseFp
int(* SDLTest_TestCaseFp)(void *arg)
Definition: SDL_test_harness.h:67
SDLTest_AssertPass
void SDLTest_AssertPass(SDL_PRINTF_FORMAT_STRING const char *assertDescription,...) SDL_PRINTF_VARARG_FUNC(1)
Explicitly pass without checking an assertion condition. Updates assertion counter.
Definition: SDL_test_assert.c:94
SDLTest_AssertCheck
int SDLTest_AssertCheck(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription,...) SDL_PRINTF_VARARG_FUNC(2)
Assert for test cases that logs but does not break execution flow on failures. Updates assertion coun...
Definition: SDL_test_assert.c:65
SDL_GetTicks
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
SDLTest_TestCaseReference
Definition: SDL_test_harness.h:75
timer_getPerformanceFrequency
int timer_getPerformanceFrequency(void *arg)
Call to SDL_GetPerformanceFrequency.
Definition: testautomation_timer.c:54
TEST_COMPLETED
#define TEST_COMPLETED
Definition: SDL_test_harness.h:53
param
GLfloat param
Definition: SDL_opengl_glext.h:373
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
SDL_GetPerformanceCounter
#define SDL_GetPerformanceCounter
Definition: SDL_dynapi_overrides.h:484
timer_delayAndGetTicks
int timer_delayAndGetTicks(void *arg)
Call to SDL_Delay and SDL_GetTicks.
Definition: testautomation_timer.c:69
id
GLuint id
Definition: SDL_opengl_glext.h:531
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
timerTestSuite
SDLTest_TestSuiteReference timerTestSuite
Definition: testautomation_timer.c:196
SDL_INIT_TIMER
#define SDL_INIT_TIMER
Definition: SDL.h:78
Uint64
uint64_t Uint64
Definition: SDL_stdinc.h:216
SDLTest_LogError
void SDLTest_LogError(SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(1)
Prints given message with a timestamp in the TEST category and the ERROR priority.
Definition: SDL_test_log.c:103
_paramCheck
int _paramCheck
Definition: testautomation_timer.c:11
timer_addRemoveTimer
int timer_addRemoveTimer(void *arg)
Call to SDL_AddTimer and SDL_RemoveTimer.
Definition: testautomation_timer.c:125
timer_getPerformanceCounter
int timer_getPerformanceCounter(void *arg)
Call to SDL_GetPerformanceCounter.
Definition: testautomation_timer.c:39
SDLTest_TestSuiteReference
Definition: SDL_test_harness.h:89
_paramValue
int _paramValue
Definition: testautomation_timer.c:14
timerTests
static const SDLTest_TestCaseReference * timerTests[]
Definition: testautomation_timer.c:191
SDL_RemoveTimer
#define SDL_RemoveTimer
Definition: SDL_dynapi_overrides.h:488
timerTest1
static const SDLTest_TestCaseReference timerTest1
Definition: testautomation_timer.c:178
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161
SDLTest_RandomIntegerInRange
Sint32 SDLTest_RandomIntegerInRange(Sint32 min, Sint32 max)
Definition: SDL_test_fuzzer.c:163