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

Latest commit

 

History

History
331 lines (272 loc) · 6.3 KB

File metadata and controls

331 lines (272 loc) · 6.3 KB
 
1
2
3
4
5
6
7
8
9
10
/*
* Common code for automated test suite.
*
* Written by Edgar Simo "bobbens"
*
* Released under Public Domain.
*/
#include "SDL_at.h"
Nov 18, 2009
Nov 18, 2009
11
#include "SDL_stdinc.h"
Nov 20, 2009
Nov 20, 2009
12
#include "SDL_error.h"
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h> /* printf/fprintf */
#include <stdarg.h> /* va_list */
/*
* Internal usage SDL_AT variables.
*/
static char *at_suite_msg = NULL; /**< Testsuite message. */
static char *at_test_msg = NULL; /**< Testcase message. */
static int at_success = 0; /**< Number of successful testcases. */
static int at_failure = 0; /**< Number of failed testcases. */
/*
* Global properties.
*/
static int at_verbose = 0; /**< Verbosity. */
static int at_quiet = 0; /**< Quietness. */
/*
* Prototypes.
*/
static void SDL_ATcleanup (void);
static void SDL_ATendWith( int success );
static void SDL_ATassertFailed( const char *msg );
/**
* @brief Cleans up the automated testsuite state.
*/
static void SDL_ATcleanup (void)
{
if (at_suite_msg != NULL)
Nov 18, 2009
Nov 18, 2009
48
SDL_free(at_suite_msg);
49
50
at_suite_msg = NULL;
if (at_test_msg != NULL)
Nov 18, 2009
Nov 18, 2009
51
SDL_free(at_test_msg);
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
at_test_msg = NULL;
at_success = 0;
at_failure = 0;
}
/**
* @brief Begin testsuite.
*/
void SDL_ATinit( const char *suite )
{
/* Do not open twice. */
if (at_suite_msg) {
SDL_ATprintErr( "AT suite '%s' not closed before opening suite '%s'\n",
at_suite_msg, suite );
}
/* Must have a name. */
if (suite == NULL) {
SDL_ATprintErr( "AT testsuite does not have a name.\n");
}
SDL_ATcleanup();
Nov 18, 2009
Nov 18, 2009
73
at_suite_msg = SDL_strdup(suite);
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Verbose message. */
SDL_ATprintVerbose( 2, "--+---> Started Test Suite '%s'\n", at_suite_msg );
}
/**
* @brief Finish testsuite.
*/
int SDL_ATfinish (void)
{
int failed;
/* Make sure initialized. */
if (at_suite_msg == NULL) {
SDL_ATprintErr("Ended testcase without initializing.\n");
return 1;
}
/* Finished without closing testcase. */
if (at_test_msg) {
SDL_ATprintErr( "AT suite '%s' finished without closing testcase '%s'\n",
at_suite_msg, at_test_msg );
}
/* Verbose message. */
SDL_ATprintVerbose( 2, "<-+---- Finished Test Suite '%s'\n", at_suite_msg );
/* Display message if verbose on failed. */
failed = at_failure;
if (at_failure > 0) {
SDL_ATprintErr( "%s : Failed %d out of %d testcases!\n",
at_suite_msg, at_failure, at_failure+at_success );
}
else {
SDL_ATprint( "%s : All tests successful (%d)\n",
at_suite_msg, at_success );
}
/* Clean up. */
SDL_ATcleanup();
/* Return failed. */
return failed;
}
/**
* @brief Sets a property.
*/
void SDL_ATseti( int property, int value )
{
switch (property) {
case SDL_AT_VERBOSE:
at_verbose = value;
break;
case SDL_AT_QUIET:
at_quiet = value;
break;
}
}
/**
* @brief Gets a property.
*/
void SDL_ATgeti( int property, int *value )
{
switch (property) {
case SDL_AT_VERBOSE:
*value = at_verbose;
break;
case SDL_AT_QUIET:
*value = at_quiet;
break;
}
}
/**
* @brief Begin testcase.
*/
void SDL_ATbegin( const char *testcase )
{
/* Do not open twice. */
if (at_test_msg) {
SDL_ATprintErr( "AT testcase '%s' not closed before opening testcase '%s'\n",
at_test_msg, testcase );
}
/* Must have a name. */
if (testcase == NULL) {
SDL_ATprintErr( "AT testcase does not have a name.\n");
}
Nov 18, 2009
Nov 18, 2009
169
at_test_msg = SDL_strdup(testcase);
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Verbose message. */
SDL_ATprintVerbose( 2, " +---> StartedTest Case '%s'\n", testcase );
}
/**
* @brief Ends the testcase with a succes or failure.
*/
static void SDL_ATendWith( int success )
{
/* Make sure initialized. */
if (at_test_msg == NULL) {
SDL_ATprintErr("Ended testcase without initializing.\n");
return;
}
/* Mark as success or failure. */
if (success)
at_success++;
else
at_failure++;
/* Verbose message. */
SDL_ATprintVerbose( 2, " +---- Finished Test Case '%s'\n", at_test_msg );
/* Clean up. */
if (at_test_msg != NULL)
Nov 18, 2009
Nov 18, 2009
198
SDL_free(at_test_msg);
199
200
201
202
203
204
205
206
207
208
209
210
211
212
at_test_msg = NULL;
}
/**
* @brief Display failed assert message.
*/
static void SDL_ATassertFailed( const char *msg )
{
/* Print. */
SDL_ATprintErr( "Assert Failed!\n" );
SDL_ATprintErr( " %s\n", msg );
SDL_ATprintErr( " Test Case '%s'\n", at_test_msg );
SDL_ATprintErr( " Test Suite '%s'\n", at_suite_msg );
Nov 20, 2009
Nov 20, 2009
213
SDL_ATprintErr( " Last SDL error '%s'\n", SDL_GetError() );
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* End. */
SDL_ATendWith(0);
}
/**
* @brief Testcase test.
*/
int SDL_ATassert( const char *msg, int condition )
{
/* Condition failed. */
if (!condition) {
/* Failed message. */
SDL_ATassertFailed(msg);
}
return !condition;
}
/**
* @brief Testcase test.
*/
int SDL_ATvassert( int condition, const char *msg, ... )
{
va_list args;
char buf[256];
/* Condition failed. */
if (!condition) {
/* Get message. */
va_start( args, msg );
Nov 22, 2009
Nov 22, 2009
245
SDL_vsnprintf( buf, sizeof(buf), msg, args );
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
va_end( args );
/* Failed message. */
SDL_ATassertFailed( buf );
}
return !condition;
}
/**
* @brief End testcase.
*/
void SDL_ATend (void)
{
SDL_ATendWith(1);
}
/**
* @brief Displays an error.
*/
int SDL_ATprintErr( const char *msg, ... )
{
va_list ap;
int ret;
/* Make sure there is something to print. */
if (msg == NULL)
return 0;
else {
va_start(ap, msg);
ret = vfprintf( stderr, msg, ap );
va_end(ap);
}
return ret;
}
Nov 22, 2009
Nov 22, 2009
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* @brief Displays a message.
*/
int SDL_ATprint( const char *msg, ... )
{
va_list ap;
int ret;
/* Only print if not quiet. */
if (at_quiet)
return 0;
/* Make sure there is something to print. */
if (msg == NULL)
return 0;
else {
va_start(ap, msg);
ret = vfprintf( stdout, msg, ap );
va_end(ap);
}
return ret;
}
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* @brief Displays a verbose message.
*/
int SDL_ATprintVerbose( int level, const char *msg, ... )
{
va_list ap;
int ret;
/* Only print if not quiet. */
if (at_quiet || (at_verbose < level))
return 0;
/* Make sure there is something to print. */
if (msg == NULL)
return 0;
else {
va_start(ap, msg);
ret = vfprintf( stdout, msg, ap );
va_end(ap);
}
return ret;
}