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

Latest commit

 

History

History
437 lines (357 loc) · 11.4 KB

File metadata and controls

437 lines (357 loc) · 11.4 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
Copyright (C) 2011 Markus Kauppila <markus.kauppila@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
May 23, 2011
May 23, 2011
21
#include "SDL/SDL.h"
22
23
24
25
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Jun 5, 2011
Jun 5, 2011
26
27
#include <string.h>
28
29
#include <sys/types.h>
May 30, 2011
May 30, 2011
30
#include "SDL_test.h"
May 23, 2011
May 23, 2011
31
May 30, 2011
May 30, 2011
32
//!< Function pointer to a test case function
Jun 4, 2011
Jun 4, 2011
33
34
35
36
37
typedef void (*TestCase)(void *arg);
//!< Function pointer to a test case init function
typedef void (*TestCaseInit)(void);
//!< Function pointer to a test case quit function
typedef int (*TestCaseQuit)(void);
May 30, 2011
May 30, 2011
38
Jun 1, 2011
Jun 1, 2011
39
40
41
//!< Flag for executing tests in-process
static int execute_inproc = 0;
Jun 5, 2011
Jun 5, 2011
42
43
44
45
46
47
48
49
50
51
52
//!< Flag for executing only test with selected name
static int only_selected_test = 0;
//!< Flag for executing only the selected test suite
static int only_selected_suite = 0;
//<! Size of the test and suite name buffers
#define NAME_BUFFER_SIZE 256
//!< Name of the selected test
char selected_test_name[NAME_BUFFER_SIZE];
//!< Name of the selected suite
char selected_suite_name[NAME_BUFFER_SIZE];
Jun 4, 2011
Jun 4, 2011
53
54
55
56
57
58
59
60
61
//!< Temporary array to hold test suite names
#if defined(linux) || defined( __linux)
char *testSuites[] = { "tests/libtest.so", "tests/libtestrect.so", NULL};
#else
char *testSuites[] = { "tests/libtest.dylib", "tests/libtestrect.dylib", NULL};
#endif
May 30, 2011
May 30, 2011
62
/*!
Jun 1, 2011
Jun 1, 2011
63
64
* Returns the name for the dynamic library
* which implements the test suite.
May 30, 2011
May 30, 2011
65
*
Jun 1, 2011
Jun 1, 2011
66
67
68
69
* (in the future: scans the test/ directory and
* returns the names of the dynamic libraries
* implementing the test suites)
*
Jun 4, 2011
Jun 4, 2011
70
* \return Array of test suite names
May 30, 2011
May 30, 2011
71
*/
Jun 4, 2011
Jun 4, 2011
72
char **
Jun 1, 2011
Jun 1, 2011
73
ScanForTestSuites() {
Jun 4, 2011
Jun 4, 2011
74
return testSuites;
Jun 1, 2011
Jun 1, 2011
75
76
}
May 25, 2011
May 25, 2011
77
Jun 1, 2011
Jun 1, 2011
78
79
80
/*!
* Loads test suite which is implemented as dynamic library.
*
Jun 4, 2011
Jun 4, 2011
81
* \param testSuiteName Name of the test suite which will be loaded
Jun 1, 2011
Jun 1, 2011
82
83
84
85
86
87
88
*
* \return Pointer to loaded test suite, or NULL if library could not be loaded
*/
void *
LoadTestSuite(char *testSuiteName)
{
void *library = SDL_LoadObject(testSuiteName);
89
if(library == NULL) {
Jun 1, 2011
Jun 1, 2011
90
fprintf(stderr, "Loading %s failed\n", testSuiteName);
May 31, 2011
May 31, 2011
91
fprintf(stderr, "%s\n", SDL_GetError());
May 26, 2011
May 26, 2011
94
95
96
return library;
}
Jun 5, 2011
Jun 5, 2011
97
May 30, 2011
May 30, 2011
98
99
100
101
/*!
* Loads the test case references from the given test suite.
* \param library Previously loaded dynamic library AKA test suite
May 31, 2011
May 31, 2011
102
* \return Pointer to array of TestCaseReferences or NULL if function failed
May 30, 2011
May 30, 2011
103
*/
May 30, 2011
May 30, 2011
104
105
106
TestCaseReference **
QueryTestCases(void *library)
{
May 26, 2011
May 26, 2011
107
TestCaseReference **(*suite)(void);
May 26, 2011
May 26, 2011
108
May 30, 2011
May 30, 2011
109
suite = (TestCaseReference **(*)(void)) SDL_LoadFunction(library, "QueryTestSuite");
110
if(suite == NULL) {
May 31, 2011
May 31, 2011
111
112
fprintf(stderr, "Loading QueryTestCaseReferences() failed.\n");
fprintf(stderr, "%s\n", SDL_GetError());
May 26, 2011
May 26, 2011
113
114
}
May 26, 2011
May 26, 2011
115
TestCaseReference **tests = suite();
May 26, 2011
May 26, 2011
116
if(tests == NULL) {
May 31, 2011
May 31, 2011
117
118
fprintf(stderr, "Failed to load test references.\n");
fprintf(stderr, "%s\n", SDL_GetError());
May 26, 2011
May 26, 2011
119
120
121
122
123
}
return tests;
}
Jun 4, 2011
Jun 4, 2011
124
May 30, 2011
May 30, 2011
125
126
127
/*!
* Loads test case from a test suite
*
May 30, 2011
May 30, 2011
128
129
* \param suite a test suite
* \param testName Name of the test that is going to be loaded
May 30, 2011
May 30, 2011
130
*
May 31, 2011
May 31, 2011
131
* \return Function Pointer (TestCase) to loaded test case, NULL if function failed
May 30, 2011
May 30, 2011
132
*/
May 30, 2011
May 30, 2011
133
134
135
TestCase
LoadTestCase(void *suite, char *testName)
{
Jun 4, 2011
Jun 4, 2011
136
TestCase test = (TestCase) SDL_LoadFunction(suite, testName);
May 30, 2011
May 30, 2011
137
if(test == NULL) {
May 31, 2011
May 31, 2011
138
139
fprintf(stderr, "Loading test failed, tests == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
May 30, 2011
May 30, 2011
140
141
142
143
144
}
return test;
}
Jun 5, 2011
Jun 5, 2011
145
Jun 4, 2011
Jun 4, 2011
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*!
* Loads function that initialises the test case from the
* given test suite.
*
* \param suite Used test suite
*
* \return Function pointer (TestCaseInit) which points to loaded init function. NULL if function fails.
*/
TestCaseInit
LoadTestCaseInit(void *suite) {
TestCaseInit testCaseInit = (TestCaseInit) SDL_LoadFunction(suite, "_TestCaseInit");
if(testCaseInit == NULL) {
fprintf(stderr, "Loading TestCaseInit function failed, testCaseInit == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
return testCaseInit;
}
Jun 5, 2011
Jun 5, 2011
165
Jun 4, 2011
Jun 4, 2011
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*!
* Loads function that deinitialises the executed test case from the
* given test suite.
*
* \param suite Used test suite
*
* \return Function pointer (TestCaseInit) which points to loaded init function. NULL if function fails.
*/
TestCaseQuit
LoadTestCaseQuit(void *suite) {
TestCaseQuit testCaseQuit = (TestCaseQuit) SDL_LoadFunction(suite, "_TestCaseQuit");
if(testCaseQuit == NULL) {
fprintf(stderr, "Loading TestCaseQuit function failed, testCaseQuit == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
return testCaseQuit;
}
May 30, 2011
May 30, 2011
184
Jun 5, 2011
Jun 5, 2011
185
May 30, 2011
May 30, 2011
186
/*!
May 30, 2011
May 30, 2011
187
188
189
190
* If using out-of-proc execution of tests. This function
* will handle the return value of the child process
* and interprets it to the runner. Also prints warnings
* if child was aborted by a signela.
May 30, 2011
May 30, 2011
191
*
May 30, 2011
May 30, 2011
192
* \param stat_lock information about the exited child process
May 30, 2011
May 30, 2011
193
*
May 30, 2011
May 30, 2011
194
* \return 0 if test case succeeded, 1 otherwise
May 30, 2011
May 30, 2011
195
*/
May 30, 2011
May 30, 2011
196
197
198
199
int
HandleTestReturnValue(int stat_lock)
{
//! \todo rename to: HandleChildProcessReturnValue?
May 30, 2011
May 30, 2011
200
int returnValue = -1;
May 26, 2011
May 26, 2011
201
May 30, 2011
May 30, 2011
202
203
if(WIFEXITED(stat_lock)) {
returnValue = WEXITSTATUS(stat_lock);
May 26, 2011
May 26, 2011
204
205
} else if(WIFSIGNALED(stat_lock)) {
int signal = WTERMSIG(stat_lock);
May 31, 2011
May 31, 2011
206
fprintf(stderr, "FAILURE: test was aborted due to signal no %d\n", signal);
May 30, 2011
May 30, 2011
207
returnValue = 1;
May 26, 2011
May 26, 2011
208
209
}
May 30, 2011
May 30, 2011
210
return returnValue;
May 26, 2011
May 26, 2011
211
212
}
Jun 5, 2011
Jun 5, 2011
213
Jun 4, 2011
Jun 4, 2011
214
215
216
217
218
219
220
221
222
223
224
225
/*!
* Executes a test case. Loads the test, executes it and
* returns the tests return value to the caller.
*
* \param suite The suite from which the test will be loaded
* \param testReference TestCaseReference of the test under execution
* \return The return value of the test. Zero means success, non-zero failure.
*/
int
ExecuteTest(void *suite, TestCaseReference *testReference) {
TestCaseInit testCaseInit = LoadTestCaseInit(suite);
TestCaseQuit testCaseQuit = LoadTestCaseQuit(suite);
Jun 5, 2011
Jun 5, 2011
226
TestCase test = (TestCase) LoadTestCase(suite, testReference->name);
Jun 4, 2011
Jun 4, 2011
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
int retVal = 1;
if(execute_inproc) {
testCaseInit();
test(0x0);
retVal = testCaseQuit();
} else {
int childpid = fork();
if(childpid == 0) {
testCaseInit();
test(0x0);
exit(testCaseQuit());
} else {
int stat_lock = -1;
int child = wait(&stat_lock);
retVal = HandleTestReturnValue(stat_lock);
}
}
return retVal;
}
Jun 4, 2011
Jun 4, 2011
255
256
257
258
/*!
* Prints usage information
*/
void printUsage() {
Jun 5, 2011
Jun 5, 2011
259
printf("Usage: ./runner [--in-proc] [--suite SUITE] [--test TEST] [--help]\n");
Jun 4, 2011
Jun 4, 2011
260
printf("Options:\n");
Jun 5, 2011
Jun 5, 2011
261
262
263
264
265
printf(" --in-proc Executes tests in-process\n");
printf(" -t --test TEST Executes only tests with given name\n");
printf(" -s --suite SUITE Executes only the given test suite\n");
printf(" -h --help Print this help\n");
Jun 4, 2011
Jun 4, 2011
266
267
}
Jun 5, 2011
Jun 5, 2011
268
May 30, 2011
May 30, 2011
269
270
/*!
* Parse command line arguments
Jun 1, 2011
Jun 1, 2011
271
272
273
*
* \param argc Count of command line arguments
* \param argv Array of commond lines arguments
May 30, 2011
May 30, 2011
274
275
276
277
278
279
280
281
*/
void
ParseOptions(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; ++i) {
const char *arg = argv[i];
Jun 1, 2011
Jun 1, 2011
282
if(SDL_strcmp(arg, "--in-proc") == 0) {
May 30, 2011
May 30, 2011
283
284
execute_inproc = 1;
}
Jun 1, 2011
Jun 1, 2011
285
else if(SDL_strcmp(arg, "--help") == 0 || SDL_strcmp(arg, "-h") == 0) {
Jun 4, 2011
Jun 4, 2011
286
287
printUsage();
exit(0);
Jun 5, 2011
Jun 5, 2011
288
289
290
}
else if(SDL_strcmp(arg, "--test") == 0 || SDL_strcmp(arg, "-t") == 0) {
only_selected_test = 1;
Jun 5, 2011
Jun 5, 2011
291
292
293
294
295
296
297
298
299
char *testName = NULL;
if( (i + 1) < argc) {
testName = argv[++i];
} else {
printf("runner: test name is missing\n");
printUsage();
exit(1);
}
Jun 5, 2011
Jun 5, 2011
300
Jun 5, 2011
Jun 5, 2011
301
memset(selected_test_name, 0, NAME_BUFFER_SIZE);
Jun 5, 2011
Jun 5, 2011
302
303
304
305
306
strcpy(selected_test_name, testName);
}
else if(SDL_strcmp(arg, "--suite") == 0 || SDL_strcmp(arg, "-s") == 0) {
only_selected_suite = 1;
Jun 5, 2011
Jun 5, 2011
307
308
309
310
311
312
313
314
315
316
char *suiteName = NULL;
if( (i + 1) < argc) {
suiteName = argv[++i];
} else {
printf("runner: suite name is missing\n");
printUsage();
exit(1);
}
memset(selected_suite_name, 0, NAME_BUFFER_SIZE);
Jun 5, 2011
Jun 5, 2011
317
318
319
strcpy(selected_suite_name, suiteName);
}
else {
Jun 4, 2011
Jun 4, 2011
320
321
printf("runner: unknown command '%s'\n", arg);
printUsage();
Jun 1, 2011
Jun 1, 2011
322
323
exit(0);
}
May 30, 2011
May 30, 2011
324
325
}
}
May 26, 2011
May 26, 2011
326
Jun 5, 2011
Jun 5, 2011
327
Jun 5, 2011
Jun 5, 2011
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*!
* Tests if the given test suite is selected for execution.
* If only_selected_suite flag is zero, then all the suites are
* automatically selected. If the flags is non-zero, only the suite
* which matches the selected suite is selected.
*
* \param testSuiteName Name of the test suite
*
* \return 1 if given suite is selected, otherwise 0
*/
int
SuiteIsSelected(char *testSuiteName) {
int retVal = 1;
if(only_selected_suite) {
// extract the suite name. Rips the tests/ and file suffix from the suite name
char buffer[NAME_BUFFER_SIZE];
int len = strlen(testSuiteName);
//! \todo Fix this, it's rather horrible way to do it
#define DIR_NAME_LENGTH 6
#if defined(linux) || defined( __linux)
#define FILE_EXT_LENGTH 3
#else
#define FILE_EXT_LENGTH 6
#endif
int length = len - DIR_NAME_LENGTH - FILE_EXT_LENGTH;
memset(buffer, 0, NAME_BUFFER_SIZE);
Jun 5, 2011
Jun 5, 2011
357
memcpy(buffer, testSuiteName + DIR_NAME_LENGTH, length);
Jun 5, 2011
Jun 5, 2011
358
359
360
361
362
363
364
365
retVal = SDL_strncmp(selected_suite_name, buffer, NAME_BUFFER_SIZE) == 0;
}
return retVal;
}
May 31, 2011
May 31, 2011
366
367
368
369
370
371
/*!
* Entry point for test runner
*
* \param argc Count of command line arguments
* \param argv Array of commond lines arguments
*/
May 30, 2011
May 30, 2011
372
373
374
375
int
main(int argc, char *argv[])
{
ParseOptions(argc, argv);
May 26, 2011
May 26, 2011
376
May 30, 2011
May 30, 2011
377
// print: Testing against SDL version fuu (rev: bar) if verbose == true
May 26, 2011
May 26, 2011
378
379
380
381
382
int failureCount = 0, passCount = 0;
const Uint32 startTicks = SDL_GetTicks();
Jun 4, 2011
Jun 4, 2011
383
char **testSuiteNames = ScanForTestSuites();
May 26, 2011
May 26, 2011
384
Jun 4, 2011
Jun 4, 2011
385
386
387
char *testSuiteName = NULL;
int suiteCounter = 0;
for(testSuiteName = testSuiteNames[suiteCounter]; testSuiteName; testSuiteName = testSuiteNames[++suiteCounter]) {
Jun 5, 2011
Jun 5, 2011
388
389
390
// if the current suite isn't selected, go to next suite
if(SuiteIsSelected(testSuiteName) == 0) {
continue;
Jun 5, 2011
Jun 5, 2011
391
392
}
Jun 4, 2011
Jun 4, 2011
393
394
void *suite = LoadTestSuite(testSuiteName);
TestCaseReference **tests = QueryTestCases(suite);
Jun 4, 2011
Jun 4, 2011
395
Jun 4, 2011
Jun 4, 2011
396
397
398
TestCaseReference *reference = NULL;
int counter = 0;
for(reference = tests[counter]; reference; reference = tests[++counter]) {
Jun 5, 2011
Jun 5, 2011
399
400
if(only_selected_test && SDL_strncmp(selected_test_name, reference->name, NAME_BUFFER_SIZE) != 0) {
continue;
Jun 5, 2011
Jun 5, 2011
401
402
}
Jun 4, 2011
Jun 4, 2011
403
404
if(reference->enabled == TEST_DISABLED) {
printf("Test %s (in %s) disabled. Omitting...\n", reference->name, testSuiteName);
May 30, 2011
May 30, 2011
405
} else {
Jun 4, 2011
Jun 4, 2011
406
printf("Executing %s (in %s):\n", reference->name, testSuiteName);
Jun 4, 2011
Jun 4, 2011
407
Jun 4, 2011
Jun 4, 2011
408
int retVal = ExecuteTest(suite, reference);
Jun 4, 2011
Jun 4, 2011
409
Jun 4, 2011
Jun 4, 2011
410
411
412
413
414
415
416
if(retVal) {
failureCount++;
if(retVal == 2) {
printf("%s (in %s): FAILED -> No asserts\n", reference->name, testSuiteName);
} else {
printf("%s (in %s): FAILED\n", reference->name, testSuiteName);
}
May 30, 2011
May 30, 2011
417
} else {
Jun 4, 2011
Jun 4, 2011
418
419
passCount++;
printf("%s (in %s): ok\n", reference->name, testSuiteName);
May 30, 2011
May 30, 2011
420
}
May 30, 2011
May 30, 2011
422
Jun 4, 2011
Jun 4, 2011
423
printf("\n");
May 26, 2011
May 26, 2011
425
Jun 4, 2011
Jun 4, 2011
426
SDL_UnloadObject(suite);
May 23, 2011
May 23, 2011
428
429
430
const Uint32 endTicks = SDL_GetTicks();
May 30, 2011
May 30, 2011
431
printf("Ran %d tests in %0.5f seconds.\n", (passCount + failureCount), (endTicks-startTicks)/1000.0f);
May 23, 2011
May 23, 2011
432
433
434
435
printf("%d tests passed\n", passCount);
printf("%d tests failed\n", failureCount);