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

Latest commit

 

History

History
913 lines (735 loc) · 24.5 KB

File metadata and controls

913 lines (735 loc) · 24.5 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
#include <string.h>
Jun 7, 2011
Jun 7, 2011
27
#include <dirent.h>
Jun 5, 2011
Jun 5, 2011
28
29
30
#include <sys/types.h>
Jul 4, 2011
Jul 4, 2011
31
32
#include "config.h"
May 30, 2011
May 30, 2011
33
#include "SDL_test.h"
Jul 6, 2011
Jul 6, 2011
34
35
36
#include "plain_logger.h"
#include "xml_logger.h"
Jun 26, 2011
Jun 26, 2011
37
#include "logger.h"
Jun 9, 2011
Jun 9, 2011
38
May 30, 2011
May 30, 2011
39
//!< Function pointer to a test case function
Jun 9, 2011
Jun 9, 2011
40
typedef void (*TestCaseFp)(void *arg);
Jun 4, 2011
Jun 4, 2011
41
//!< Function pointer to a test case init function
Jul 11, 2011
Jul 11, 2011
42
typedef void (*InitTestInvironmentFp)(void);
Jun 4, 2011
Jun 4, 2011
43
//!< Function pointer to a test case quit function
Jul 11, 2011
Jul 11, 2011
44
45
46
47
48
typedef int (*QuitTestInvironmentFp)(void);
//!< Function pointer to a test case set up function
typedef void (*TestCaseSetUpFp)(void *arg);
//!< Function pointer to a test case tear down function
typedef void (*TestCaseTearDownFp)(void *arg);
Jun 9, 2011
Jun 9, 2011
49
May 30, 2011
May 30, 2011
50
Jun 1, 2011
Jun 1, 2011
51
52
//!< Flag for executing tests in-process
static int execute_inproc = 0;
Jun 13, 2011
Jun 13, 2011
53
54
//!< Flag for only printing out the test names
static int only_print_tests = 0;
Jun 5, 2011
Jun 5, 2011
55
56
57
58
//!< 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;
Jun 9, 2011
Jun 9, 2011
59
60
//!< Flag for executing only tests that contain certain string in their name
static int only_tests_with_string = 0;
Jun 26, 2011
Jun 26, 2011
61
62
//!< Flag for enabling XML logging
static int xml_enabled = 0;
Jun 30, 2011
Jun 30, 2011
63
64
//! Flag for enabling user-supplied style sheet for XML test report
static int custom_xsl_enabled = 0;
Jul 1, 2011
Jul 1, 2011
65
66
//! Flag for disabling xsl-style from xml report
static int xsl_enabled = 0;
Jun 9, 2011
Jun 9, 2011
67
Jun 5, 2011
Jun 5, 2011
68
Jun 8, 2011
Jun 8, 2011
69
//!< Size of the test and suite name buffers
Jun 7, 2011
Jun 7, 2011
70
#define NAME_BUFFER_SIZE 1024
Jun 5, 2011
Jun 5, 2011
71
72
73
74
//!< 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
75
Jun 9, 2011
Jun 9, 2011
76
77
78
//!< substring of test case name
char testcase_name_substring[NAME_BUFFER_SIZE];
Jun 30, 2011
Jun 30, 2011
79
80
81
//! Name for user-supplied XSL style sheet name
char xsl_stylesheet_name[NAME_BUFFER_SIZE];
Jun 8, 2011
Jun 8, 2011
82
83
84
//! Default directory of the test suites
#define DEFAULT_TEST_DIRECTORY "tests/"
Jun 9, 2011
Jun 9, 2011
85
Jun 7, 2011
Jun 7, 2011
86
/*!
Jun 9, 2011
Jun 9, 2011
87
88
* Holds information about test suite such as it's name
* and pointer to dynamic library. Implemented as linked list.
Jun 7, 2011
Jun 7, 2011
89
90
*/
typedef struct TestSuiteReference {
Jun 9, 2011
Jun 9, 2011
91
char *name; //!< test suite name
Jul 1, 2011
Jul 1, 2011
92
char *directoryPath; //!< test suites path (eg. tests/libtestsuite)
Jun 9, 2011
Jun 9, 2011
93
void *library; //!< pointer to shared/dynamic library implementing the suite
Jun 9, 2011
Jun 9, 2011
94
Jun 7, 2011
Jun 7, 2011
95
96
97
struct TestSuiteReference *next; //!< Pointer to next item in the list
} TestSuiteReference;
Jun 9, 2011
Jun 9, 2011
98
99
100
101
102
103
/*!
* Holds information about the tests that will be executed.
*
* Implemented as linked list.
*/
Jun 9, 2011
Jun 9, 2011
104
105
106
107
typedef struct TestCaseItem {
char *testName;
char *suiteName;
Jun 26, 2011
Jun 26, 2011
108
109
110
111
char *description;
long requirements;
long timeout;
Jul 11, 2011
Jul 11, 2011
112
113
InitTestInvironmentFp initTestEnvironment;
TestCaseSetUpFp testSetUp;
Jun 9, 2011
Jun 9, 2011
114
TestCaseFp testCase;
Jul 11, 2011
Jul 11, 2011
115
116
TestCaseTearDownFp testTearDown;
QuitTestInvironmentFp quitTestEnvironment;
Jun 9, 2011
Jun 9, 2011
117
118
struct TestCaseItem *next;
Jun 9, 2011
Jun 9, 2011
119
120
121
122
123
} TestCase;
/*! Some function prototypes. Add the rest of functions and move to runner.h */
TestCaseFp LoadTestCaseFunction(void *suite, char *testName);
Jul 11, 2011
Jul 11, 2011
124
125
InitTestInvironmentFp LoadInitTestInvironmentFunction(void *suite);
QuitTestInvironmentFp LoadQuitTestInvironmentFunction(void *suite);
Jun 9, 2011
Jun 9, 2011
126
TestCaseReference **QueryTestCaseReferences(void *library);
Jul 11, 2011
Jul 11, 2011
127
128
129
TestCaseSetUpFp LoadTestSetUpFunction(void *suite);
TestCaseTearDownFp LoadTestTearDownFunction(void *suite);
Jun 9, 2011
Jun 9, 2011
130
Jul 6, 2011
Jul 6, 2011
131
132
133
134
135
136
137
138
139
140
141
142
/*! Pointers to selected logger implementation */
RunStartedFp RunStarted = NULL;
RunEndedFp RunEnded = NULL;
SuiteStartedFp SuiteStarted = NULL;
SuiteEndedFp SuiteEnded = NULL;
TestStartedFp TestStarted = NULL;
TestEndedFp TestEnded = NULL;
AssertFp Assert = NULL;
AssertWithValuesFp AssertWithValues = NULL;
AssertSummaryFp AssertSummary = NULL;
LogFp Log = NULL;
Jun 4, 2011
Jun 4, 2011
143
Jun 9, 2011
Jun 9, 2011
144
145
146
147
148
149
/*!
* Goes through the previously loaded test suites and
* loads test cases from them. Test cases are filtered
* during the process. Function will only return the
* test cases which aren't filtered out.
*
Jun 9, 2011
Jun 9, 2011
150
* \param suites previously loaded test suites
Jun 9, 2011
Jun 9, 2011
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
*
* \return Test cases that survived filtering process.
*/
TestCase *
LoadTestCases(TestSuiteReference *suites)
{
TestCase *testCases = NULL;
TestSuiteReference *suiteReference = NULL;
for(suiteReference = suites; suiteReference; suiteReference = suiteReference->next) {
TestCaseReference **tests = QueryTestCaseReferences(suiteReference->library);
TestCaseReference *testReference = NULL;
int counter = 0;
for(testReference = tests[counter]; testReference; testReference = tests[++counter]) {
void *suite = suiteReference->library;
// Load test case functions
Jul 11, 2011
Jul 11, 2011
170
171
172
173
174
175
176
InitTestInvironmentFp initTestEnvironment = LoadInitTestInvironmentFunction(suiteReference->library);
QuitTestInvironmentFp quitTestEnvironment = LoadQuitTestInvironmentFunction(suiteReference->library);
TestCaseSetUpFp testSetUp = LoadTestSetUpFunction(suiteReference->library);
TestCaseTearDownFp testTearDown = LoadTestTearDownFunction(suiteReference->library);
TestCaseFp testCase = LoadTestCaseFunction(suiteReference->library, testReference->name);
Jun 9, 2011
Jun 9, 2011
177
178
179
180
181
182
// Do the filtering
if(FilterTestCase(testReference)) {
TestCase *item = SDL_malloc(sizeof(TestCase));
memset(item, 0, sizeof(TestCase));
Jul 11, 2011
Jul 11, 2011
183
184
185
186
187
188
item->initTestEnvironment = initTestEnvironment;
item->quitTestEnvironment = quitTestEnvironment;
item->testSetUp = testSetUp;
item->testTearDown = testTearDown;
Jun 9, 2011
Jun 9, 2011
189
190
item->testCase = testCase;
Jun 26, 2011
Jun 26, 2011
191
// copy suite name
Jun 27, 2011
Jun 27, 2011
192
int length = SDL_strlen(suiteReference->name) + 1;
Jun 9, 2011
Jun 9, 2011
193
item->suiteName = SDL_malloc(length);
Jun 27, 2011
Jun 27, 2011
194
strncpy(item->suiteName, suiteReference->name, length);
Jun 9, 2011
Jun 9, 2011
195
Jun 26, 2011
Jun 26, 2011
196
// copy test name
Jun 27, 2011
Jun 27, 2011
197
length = SDL_strlen(testReference->name) + 1;
Jun 9, 2011
Jun 9, 2011
198
item->testName = SDL_malloc(length);
Jun 27, 2011
Jun 27, 2011
199
strncpy(item->testName, testReference->name, length);
Jun 9, 2011
Jun 9, 2011
200
Jun 26, 2011
Jun 26, 2011
201
// copy test description
Jun 27, 2011
Jun 27, 2011
202
length = SDL_strlen(testReference->description) + 1;
Jun 26, 2011
Jun 26, 2011
203
item->description = SDL_malloc(length);
Jun 27, 2011
Jun 27, 2011
204
strncpy(item->description, testReference->description, length);
Jun 26, 2011
Jun 26, 2011
205
206
207
208
item->requirements = testReference->requirements;
item->timeout = testReference->timeout;
Jun 9, 2011
Jun 9, 2011
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// prepend the list
item->next = testCases;
testCases = item;
//printf("Added test: %s\n", testReference->name);
}
}
}
return testCases;
}
/*!
* Unloads the given TestCases. Frees all the resources
* allocated for test cases.
*
* \param testCases Test cases to be deallocated
*/
void
UnloadTestCases(TestCase *testCases)
{
TestCase *ref = testCases;
while(ref) {
SDL_free(ref->testName);
SDL_free(ref->suiteName);
Jun 26, 2011
Jun 26, 2011
235
SDL_free(ref->description);
Jun 9, 2011
Jun 9, 2011
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
TestCase *temp = ref->next;
SDL_free(ref);
ref = temp;
}
testCases = NULL;
}
/*!
* Filters a test case based on its properties in TestCaseReference and user
* preference.
*
* \return Non-zero means test will be added to execution list, zero means opposite
*/
int
FilterTestCase(TestCaseReference *testReference)
{
int retVal = 1;
if(testReference->enabled == TEST_DISABLED) {
retVal = 0;
}
Jun 9, 2011
Jun 9, 2011
261
262
263
264
265
266
267
268
if(only_selected_test) {
if(SDL_strncmp(testReference->name, selected_test_name, NAME_BUFFER_SIZE) == 0) {
retVal = 1;
} else {
retVal = 0;
}
}
Jun 9, 2011
Jun 9, 2011
269
270
271
272
273
274
275
276
277
278
279
280
if(only_tests_with_string) {
if(strstr(testReference->name, testcase_name_substring) != NULL) {
retVal = 1;
} else {
retVal = 0;
}
}
return retVal;
}
Jun 30, 2011
Jun 30, 2011
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*!
* Scans the tests/ directory and returns the names
* of the dynamic libraries implementing the test suites.
*
* Note: currently function assumes that test suites names
* are in following format: libtestsuite.dylib or libtestsuite.so.
*
* Note: if only_selected_suite flags is non-zero, only the selected
* test will be loaded.
*
* \param directoryName Name of the directory which will be scanned
* \param extension What file extension is used with dynamic objects
*
* \return Pointer to TestSuiteReference which holds all the info about suites
*/
TestSuiteReference *
ScanForTestSuites(char *directoryName, char *extension)
{
typedef struct dirent Entry;
DIR *directory = opendir(directoryName);
TestSuiteReference *suites = NULL;
Entry *entry = NULL;
if(!directory) {
Jul 11, 2011
Jul 11, 2011
306
307
308
fprintf(stderr, "Failed to open test suite directory: %s\n", directoryName);
perror("Error message");
exit(1);
Jun 30, 2011
Jun 30, 2011
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
}
while(entry = readdir(directory)) {
if(strlen(entry->d_name) > 2) { // discards . and ..
const char *delimiters = ".";
char *name = strtok(entry->d_name, delimiters);
char *ext = strtok(NULL, delimiters);
// filter out all other suites but the selected test suite
int ok = 1;
if(only_selected_suite) {
ok = SDL_strncmp(selected_suite_name, name, NAME_BUFFER_SIZE) == 0;
}
if(ok && SDL_strcmp(ext, extension) == 0) {
// create test suite reference
TestSuiteReference *reference = (TestSuiteReference *) SDL_malloc(sizeof(TestSuiteReference));
memset(reference, 0, sizeof(TestSuiteReference));
Jul 1, 2011
Jul 1, 2011
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
const int dirSize = SDL_strlen(directoryName);
const int extSize = SDL_strlen(ext);
const int nameSize = SDL_strlen(name) + 1;
// copy the name
reference->name = SDL_malloc(nameSize * sizeof(char));
if(reference->name == NULL) {
SDL_free(reference);
return NULL;
}
SDL_snprintf(reference->name, nameSize, "%s", name);
// copy the directory path
const int dpSize = dirSize + nameSize + 1 + extSize + 1;
reference->directoryPath = SDL_malloc(dpSize * sizeof(char));
if(reference->directoryPath == NULL) {
SDL_free(reference->name);
SDL_free(reference);
return NULL;
}
SDL_snprintf(reference->directoryPath, dpSize, "%s%s.%s",
directoryName, name, ext);
Jun 30, 2011
Jun 30, 2011
352
353
354
355
356
357
358
359
360
361
362
363
364
reference->next = suites;
suites = reference;
}
}
}
closedir(directory);
return suites;
}
Jun 1, 2011
Jun 1, 2011
365
366
367
/*!
* Loads test suite which is implemented as dynamic library.
*
Jul 1, 2011
Jul 1, 2011
368
* \param suite Reference to test suite that'll be loaded
Jun 1, 2011
Jun 1, 2011
369
370
371
372
*
* \return Pointer to loaded test suite, or NULL if library could not be loaded
*/
void *
Jul 1, 2011
Jul 1, 2011
373
LoadTestSuite(const TestSuiteReference *suite)
Jun 1, 2011
Jun 1, 2011
374
{
Jul 1, 2011
Jul 1, 2011
375
void *library = SDL_LoadObject(suite->directoryPath);
376
if(library == NULL) {
Jul 1, 2011
Jul 1, 2011
377
fprintf(stderr, "Loading %s failed\n", suite->name);
May 31, 2011
May 31, 2011
378
fprintf(stderr, "%s\n", SDL_GetError());
May 26, 2011
May 26, 2011
381
382
383
return library;
}
Jun 5, 2011
Jun 5, 2011
384
Jun 9, 2011
Jun 9, 2011
385
386
387
388
389
390
391
392
393
394
/*!
* Goes through all the given TestSuiteReferences
* and loads the dynamic libraries. Updates the suites
* parameter on-the-fly and returns it.
*
* \param suites Suites that will be loaded
*
* \return Updated TestSuiteReferences with pointer to loaded libraries
*/
TestSuiteReference *
Jul 1, 2011
Jul 1, 2011
395
LoadTestSuites(TestSuiteReference *suites)
Jun 9, 2011
Jun 9, 2011
396
397
398
{
TestSuiteReference *reference = NULL;
for(reference = suites; reference; reference = reference->next) {
Jul 1, 2011
Jul 1, 2011
399
reference->library = LoadTestSuite(reference);
Jun 9, 2011
Jun 9, 2011
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
}
return suites;
}
/*!
* Unloads the given TestSuiteReferences. Frees all
* the allocated resources including the dynamic libraries.
*
* \param suites TestSuiteReferences for deallocation process
*/
void
UnloadTestSuites(TestSuiteReference *suites)
{
TestSuiteReference *ref = suites;
while(ref) {
SDL_free(ref->name);
Jul 1, 2011
Jul 1, 2011
418
SDL_free(ref->directoryPath);
Jun 9, 2011
Jun 9, 2011
419
420
421
422
423
424
425
426
427
428
429
SDL_UnloadObject(ref->library);
TestSuiteReference *temp = ref->next;
SDL_free(ref);
ref = temp;
}
suites = NULL;
}
May 30, 2011
May 30, 2011
430
431
432
433
/*!
* 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
434
* \return Pointer to array of TestCaseReferences or NULL if function failed
May 30, 2011
May 30, 2011
435
*/
May 30, 2011
May 30, 2011
436
TestCaseReference **
Jun 9, 2011
Jun 9, 2011
437
QueryTestCaseReferences(void *library)
May 30, 2011
May 30, 2011
438
{
Jun 9, 2011
Jun 9, 2011
439
TestCaseReference **(*suite)(void);
May 26, 2011
May 26, 2011
440
Jun 9, 2011
Jun 9, 2011
441
442
443
444
445
suite = (TestCaseReference **(*)(void)) SDL_LoadFunction(library, "QueryTestSuite");
if(suite == NULL) {
fprintf(stderr, "Loading QueryTestCaseReferences() failed.\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
May 26, 2011
May 26, 2011
446
Jun 9, 2011
Jun 9, 2011
447
448
449
450
451
TestCaseReference **tests = suite();
if(tests == NULL) {
fprintf(stderr, "Failed to load test references.\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
May 26, 2011
May 26, 2011
452
Jun 9, 2011
Jun 9, 2011
453
return tests;
May 26, 2011
May 26, 2011
454
455
}
Jun 4, 2011
Jun 4, 2011
456
May 30, 2011
May 30, 2011
457
458
459
/*!
* Loads test case from a test suite
*
May 30, 2011
May 30, 2011
460
461
* \param suite a test suite
* \param testName Name of the test that is going to be loaded
May 30, 2011
May 30, 2011
462
*
May 31, 2011
May 31, 2011
463
* \return Function Pointer (TestCase) to loaded test case, NULL if function failed
May 30, 2011
May 30, 2011
464
*/
Jun 9, 2011
Jun 9, 2011
465
466
TestCaseFp
LoadTestCaseFunction(void *suite, char *testName)
May 30, 2011
May 30, 2011
467
{
Jun 9, 2011
Jun 9, 2011
468
TestCaseFp test = (TestCaseFp) SDL_LoadFunction(suite, testName);
May 30, 2011
May 30, 2011
469
if(test == NULL) {
May 31, 2011
May 31, 2011
470
471
fprintf(stderr, "Loading test failed, tests == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
May 30, 2011
May 30, 2011
472
473
474
475
476
}
return test;
}
Jun 5, 2011
Jun 5, 2011
477
Jun 4, 2011
Jun 4, 2011
478
/*!
Jul 11, 2011
Jul 11, 2011
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
* Loads function that sets up a fixture for a test case. Note: if there's
* no SetUp function present in the suite the function will return NULL.
*
* \param suite Used test suite
*
* \return Function pointer to test case's set up function
*/
TestCaseSetUpFp
LoadTestSetUpFunction(void *suite) {
TestCaseSetUpFp testSetUp = (TestCaseSetUpFp) SDL_LoadFunction(suite, "SetUp");
if(testSetUp == NULL) {
fprintf(stderr, "Loading SetUp function failed, testSetUp == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
return testSetUp;
}
/*!
* Loads function that tears down a fixture for a test case. Note: if there's
* no TearDown function present in the suite the function will return NULL.
*
* \param suite Used test suite
*
* \return Function pointer to test case's tear down function
*/
TestCaseTearDownFp
LoadTestTearDownFunction(void *suite) {
TestCaseTearDownFp testTearDown = (TestCaseTearDownFp) SDL_LoadFunction(suite, "TearDown");
if(testTearDown == NULL) {
fprintf(stderr, "Loading TearDown function failed, testTearDown == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
return testTearDown;
}
/*!
* Loads function that initialises the test environment for
* a test case in the given suite.
Jun 4, 2011
Jun 4, 2011
521
522
523
*
* \param suite Used test suite
*
Jul 11, 2011
Jul 11, 2011
524
* \return Function pointer (InitTestInvironmentFp) which points to loaded init function. NULL if function fails.
Jun 4, 2011
Jun 4, 2011
525
*/
Jul 11, 2011
Jul 11, 2011
526
527
528
529
530
InitTestInvironmentFp
LoadInitTestInvironmentFunction(void *suite) {
InitTestInvironmentFp testEnvInit = (InitTestInvironmentFp) SDL_LoadFunction(suite, "_InitTestEnvironment");
if(testEnvInit == NULL) {
fprintf(stderr, "Loading _InitTestInvironment function failed, testEnvInit == NULL\n");
Jun 4, 2011
Jun 4, 2011
531
532
533
fprintf(stderr, "%s\n", SDL_GetError());
}
Jul 11, 2011
Jul 11, 2011
534
return testEnvInit;
Jun 4, 2011
Jun 4, 2011
535
536
}
Jun 5, 2011
Jun 5, 2011
537
Jun 4, 2011
Jun 4, 2011
538
/*!
Jul 11, 2011
Jul 11, 2011
539
540
* Loads function that deinitialises the test environment (and returns
* the test case's result) created for the test case in the given suite.
Jun 4, 2011
Jun 4, 2011
541
542
543
*
* \param suite Used test suite
*
Jul 11, 2011
Jul 11, 2011
544
* \return Function pointer (QuitTestInvironmentFp) which points to loaded init function. NULL if function fails.
Jun 4, 2011
Jun 4, 2011
545
*/
Jul 11, 2011
Jul 11, 2011
546
547
548
549
550
QuitTestInvironmentFp
LoadQuitTestInvironmentFunction(void *suite) {
QuitTestInvironmentFp testEnvQuit = (QuitTestInvironmentFp) SDL_LoadFunction(suite, "_QuitTestEnvironment");
if(testEnvQuit == NULL) {
fprintf(stderr, "Loading _QuitTestEnvironment function failed, testEnvQuit == NULL\n");
Jun 4, 2011
Jun 4, 2011
551
552
553
fprintf(stderr, "%s\n", SDL_GetError());
}
Jul 11, 2011
Jul 11, 2011
554
return testEnvQuit;
Jun 4, 2011
Jun 4, 2011
555
}
May 30, 2011
May 30, 2011
556
Jun 5, 2011
Jun 5, 2011
557
May 30, 2011
May 30, 2011
558
/*!
May 30, 2011
May 30, 2011
559
560
561
562
* 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
563
*
May 30, 2011
May 30, 2011
564
* \param stat_lock information about the exited child process
May 30, 2011
May 30, 2011
565
*
May 30, 2011
May 30, 2011
566
* \return 0 if test case succeeded, 1 otherwise
May 30, 2011
May 30, 2011
567
*/
May 30, 2011
May 30, 2011
568
int
Jun 9, 2011
Jun 9, 2011
569
HandleChildProcessReturnValue(int stat_lock)
May 30, 2011
May 30, 2011
570
{
May 30, 2011
May 30, 2011
571
int returnValue = -1;
May 26, 2011
May 26, 2011
572
May 30, 2011
May 30, 2011
573
574
if(WIFEXITED(stat_lock)) {
returnValue = WEXITSTATUS(stat_lock);
May 26, 2011
May 26, 2011
575
576
} else if(WIFSIGNALED(stat_lock)) {
int signal = WTERMSIG(stat_lock);
Jul 10, 2011
Jul 10, 2011
577
578
// \todo add this to logger (add signal number)
Log("FAILURE: test was aborted due to signal\n", time(0));
May 30, 2011
May 30, 2011
579
returnValue = 1;
May 26, 2011
May 26, 2011
580
581
}
May 30, 2011
May 30, 2011
582
return returnValue;
May 26, 2011
May 26, 2011
583
584
}
Jun 5, 2011
Jun 5, 2011
585
Jun 4, 2011
Jun 4, 2011
586
587
588
589
/*!
* Executes a test case. Loads the test, executes it and
* returns the tests return value to the caller.
*
Jun 9, 2011
Jun 9, 2011
590
* \param testItem The test case that will be executed
Jun 4, 2011
Jun 4, 2011
591
592
593
* \return The return value of the test. Zero means success, non-zero failure.
*/
int
Jun 9, 2011
Jun 9, 2011
594
ExecuteTest(TestCase *testItem) {
Jun 4, 2011
Jun 4, 2011
595
596
int retVal = 1;
if(execute_inproc) {
Jul 11, 2011
Jul 11, 2011
597
598
599
600
601
testItem->initTestEnvironment();
if(testItem->testSetUp) {
testItem->testSetUp(0x0);
}
Jun 4, 2011
Jun 4, 2011
602
Jun 9, 2011
Jun 9, 2011
603
testItem->testCase(0x0);
Jun 4, 2011
Jun 4, 2011
604
Jul 11, 2011
Jul 11, 2011
605
606
607
608
609
if(testItem->testTearDown) {
testItem->testTearDown(0x0);
}
retVal = testItem->quitTestEnvironment();
Jun 4, 2011
Jun 4, 2011
610
611
612
} else {
int childpid = fork();
if(childpid == 0) {
Jul 11, 2011
Jul 11, 2011
613
614
615
616
617
testItem->initTestEnvironment();
if(testItem->testSetUp) {
testItem->testSetUp(0x0);
}
Jun 4, 2011
Jun 4, 2011
618
Jun 9, 2011
Jun 9, 2011
619
testItem->testCase(0x0);
Jun 4, 2011
Jun 4, 2011
620
Jul 11, 2011
Jul 11, 2011
621
622
623
624
625
626
627
// note: if test case is is aborted by some signal
// then TearDown function won't be called
if(testItem->testTearDown) {
testItem->testTearDown(0x0);
}
exit(testItem->quitTestEnvironment());
Jun 4, 2011
Jun 4, 2011
628
629
630
631
} else {
int stat_lock = -1;
int child = wait(&stat_lock);
Jun 9, 2011
Jun 9, 2011
632
retVal = HandleChildProcessReturnValue(stat_lock);
Jun 4, 2011
Jun 4, 2011
633
634
635
636
637
638
639
}
}
return retVal;
}
Jun 4, 2011
Jun 4, 2011
640
641
642
/*!
* Prints usage information
*/
Jun 6, 2011
Jun 6, 2011
643
void
Jul 4, 2011
Jul 4, 2011
644
PrintUsage() {
Jun 9, 2011
Jun 9, 2011
645
printf("Usage: ./runner [--in-proc] [--suite SUITE] [--test TEST]\n");
Jul 3, 2011
Jul 3, 2011
646
printf(" [--name-contains SUBSTR] [--show-tests]\n");
Jul 3, 2011
Jul 3, 2011
647
printf(" [--xml] [--xsl [STYLESHEET]] [--help]\n");
Jun 4, 2011
Jun 4, 2011
648
printf("Options:\n");
Jun 9, 2011
Jun 9, 2011
649
printf(" --in-proc Executes tests in-process\n");
Jun 13, 2011
Jun 13, 2011
650
printf(" --show-tests Prints out all the executable tests\n");
Jul 1, 2011
Jul 1, 2011
651
printf(" --xml Enables XML logger\n");
Jul 3, 2011
Jul 3, 2011
652
653
654
printf(" --xsl [STYLESHEET] Adds XSL stylesheet to the XML test reports for\n");
printf(" browser viewing. Optionally uses the specified XSL\n");
printf(" file or URL instead of the default one\n");
Jun 9, 2011
Jun 9, 2011
655
656
657
658
printf(" -t --test TEST Executes only tests with given name\n");
printf(" -ts --name-contains SUBSTR Executes only tests that have given\n");
printf(" substring in test name\n");
printf(" -s --suite SUITE Executes only the given test suite\n");
Jun 5, 2011
Jun 5, 2011
659
Jun 9, 2011
Jun 9, 2011
660
printf(" -h --help Print this help\n");
Jun 4, 2011
Jun 4, 2011
661
662
}
Jun 5, 2011
Jun 5, 2011
663
May 30, 2011
May 30, 2011
664
665
/*!
* Parse command line arguments
Jun 1, 2011
Jun 1, 2011
666
667
668
*
* \param argc Count of command line arguments
* \param argv Array of commond lines arguments
May 30, 2011
May 30, 2011
669
670
671
672
673
674
675
676
*/
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
677
if(SDL_strcmp(arg, "--in-proc") == 0) {
May 30, 2011
May 30, 2011
678
679
execute_inproc = 1;
}
Jun 13, 2011
Jun 13, 2011
680
681
else if(SDL_strcmp(arg, "--show-tests") == 0) {
only_print_tests = 1;
Jun 5, 2011
Jun 5, 2011
682
}
Jun 26, 2011
Jun 26, 2011
683
684
685
else if(SDL_strcmp(arg, "--xml") == 0) {
xml_enabled = 1;
}
Jun 5, 2011
Jun 5, 2011
686
687
else if(SDL_strcmp(arg, "--test") == 0 || SDL_strcmp(arg, "-t") == 0) {
only_selected_test = 1;
Jun 5, 2011
Jun 5, 2011
688
689
690
691
692
693
char *testName = NULL;
if( (i + 1) < argc) {
testName = argv[++i];
} else {
printf("runner: test name is missing\n");
Jul 4, 2011
Jul 4, 2011
694
PrintUsage();
Jun 5, 2011
Jun 5, 2011
695
696
exit(1);
}
Jun 5, 2011
Jun 5, 2011
697
Jun 5, 2011
Jun 5, 2011
698
memset(selected_test_name, 0, NAME_BUFFER_SIZE);
Jun 5, 2011
Jun 5, 2011
699
700
strcpy(selected_test_name, testName);
}
Jun 30, 2011
Jun 30, 2011
701
else if(SDL_strcmp(arg, "--xsl") == 0) {
Jul 1, 2011
Jul 1, 2011
702
xsl_enabled = 1;
Jun 30, 2011
Jun 30, 2011
703
704
if( (i + 1) < argc) {
Jul 1, 2011
Jul 1, 2011
705
706
707
char *stylesheet = argv[++i];
if(stylesheet[0] != '-') {
custom_xsl_enabled = 1;
Jun 30, 2011
Jun 30, 2011
708
Jul 1, 2011
Jul 1, 2011
709
710
711
712
memset(xsl_stylesheet_name, 0, NAME_BUFFER_SIZE);
strncpy(xsl_stylesheet_name, stylesheet, NAME_BUFFER_SIZE);
}
}
Jun 30, 2011
Jun 30, 2011
713
}
Jun 9, 2011
Jun 9, 2011
714
else if(SDL_strcmp(arg, "--name-contains") == 0 || SDL_strcmp(arg, "-ts") == 0) {
Jun 9, 2011
Jun 9, 2011
715
716
717
718
719
720
721
only_tests_with_string = 1;
char *substring = NULL;
if( (i + 1) < argc) {
substring = argv[++i];
} else {
printf("runner: substring of test name is missing\n");
Jul 4, 2011
Jul 4, 2011
722
PrintUsage();
Jun 9, 2011
Jun 9, 2011
723
724
725
726
727
728
exit(1);
}
memset(testcase_name_substring, 0, NAME_BUFFER_SIZE);
strcpy(testcase_name_substring, substring);
}
Jun 5, 2011
Jun 5, 2011
729
730
731
else if(SDL_strcmp(arg, "--suite") == 0 || SDL_strcmp(arg, "-s") == 0) {
only_selected_suite = 1;
Jun 5, 2011
Jun 5, 2011
732
733
734
735
736
char *suiteName = NULL;
if( (i + 1) < argc) {
suiteName = argv[++i];
} else {
printf("runner: suite name is missing\n");
Jul 4, 2011
Jul 4, 2011
737
PrintUsage();
Jun 5, 2011
Jun 5, 2011
738
739
740
741
exit(1);
}
memset(selected_suite_name, 0, NAME_BUFFER_SIZE);
Jun 5, 2011
Jun 5, 2011
742
743
strcpy(selected_suite_name, suiteName);
}
Jul 4, 2011
Jul 4, 2011
744
745
746
747
else if(SDL_strcmp(arg, "--version") == 0) {
fprintf(stdout, "SDL test harness (version %s)\n", PACKAGE_VERSION);
exit(0);
}
Jun 13, 2011
Jun 13, 2011
748
else if(SDL_strcmp(arg, "--help") == 0 || SDL_strcmp(arg, "-h") == 0) {
Jul 4, 2011
Jul 4, 2011
749
PrintUsage();
Jun 13, 2011
Jun 13, 2011
750
751
exit(0);
}
Jun 5, 2011
Jun 5, 2011
752
else {
Jun 4, 2011
Jun 4, 2011
753
printf("runner: unknown command '%s'\n", arg);
Jul 4, 2011
Jul 4, 2011
754
PrintUsage();
Jun 1, 2011
Jun 1, 2011
755
756
exit(0);
}
May 30, 2011
May 30, 2011
757
758
}
}
May 26, 2011
May 26, 2011
759
Jun 5, 2011
Jun 5, 2011
760
May 31, 2011
May 31, 2011
761
762
763
764
765
766
/*!
* 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
767
768
769
770
int
main(int argc, char *argv[])
{
ParseOptions(argc, argv);
May 26, 2011
May 26, 2011
771
May 30, 2011
May 30, 2011
772
// print: Testing against SDL version fuu (rev: bar) if verbose == true
May 26, 2011
May 26, 2011
773
Jun 27, 2011
Jun 27, 2011
774
775
int totalTestfailureCount = 0, totalTestPassCount = 0;
int testFailureCount = 0, testPassCount = 0, testSkipCount = 0;
Jun 6, 2011
Jun 6, 2011
776
777
char *testSuiteName = NULL;
int suiteCounter = 0;
May 26, 2011
May 26, 2011
778
Jun 8, 2011
Jun 8, 2011
779
780
781
782
783
#if defined(linux) || defined( __linux)
char *extension = "so";
#else
char *extension = "dylib";
#endif
Jun 30, 2011
Jun 30, 2011
784
Jul 4, 2011
Jul 4, 2011
785
void *loggerData = NULL;
Jun 26, 2011
Jun 26, 2011
786
if(xml_enabled) {
Jul 6, 2011
Jul 6, 2011
787
788
789
790
791
792
793
794
795
796
797
798
799
800
RunStarted = XMLRunStarted;
RunEnded = XMLRunEnded;
SuiteStarted = XMLSuiteStarted;
SuiteEnded = XMLSuiteEnded;
TestStarted = XMLTestStarted;
TestEnded = XMLTestEnded;
Assert = XMLAssert;
AssertWithValues = XMLAssertWithValues;
AssertSummary = XMLAssertSummary;
Log = XMLLog;
Jun 30, 2011
Jun 30, 2011
801
Jul 1, 2011
Jul 1, 2011
802
803
804
805
806
807
808
809
810
char *sheet = NULL;
if(xsl_enabled) {
sheet = "style.xsl"; // default style sheet;
}
if(custom_xsl_enabled) {
sheet = xsl_stylesheet_name;
}
Jul 4, 2011
Jul 4, 2011
811
loggerData = sheet;
Jun 26, 2011
Jun 26, 2011
812
} else {
Jul 6, 2011
Jul 6, 2011
813
814
815
816
817
818
819
820
821
822
823
824
825
826
RunStarted = PlainRunStarted;
RunEnded = PlainRunEnded;
SuiteStarted = PlainSuiteStarted;
SuiteEnded = PlainSuiteEnded;
TestStarted = PlainTestStarted;
TestEnded = PlainTestEnded;
Assert = PlainAssert;
AssertWithValues = PlainAssertWithValues;
AssertSummary = PlainAssertSummary;
Log = PlainLog;
Jun 26, 2011
Jun 26, 2011
827
}
Jun 8, 2011
Jun 8, 2011
828
May 26, 2011
May 26, 2011
829
const Uint32 startTicks = SDL_GetTicks();
Jun 9, 2011
Jun 9, 2011
830
Jun 8, 2011
Jun 8, 2011
831
TestSuiteReference *suites = ScanForTestSuites(DEFAULT_TEST_DIRECTORY, extension);
Jul 1, 2011
Jul 1, 2011
832
suites = LoadTestSuites(suites);
Jun 8, 2011
Jun 8, 2011
833
Jun 9, 2011
Jun 9, 2011
834
TestCase *testCases = LoadTestCases(suites);
Jun 5, 2011
Jun 5, 2011
835
Jun 13, 2011
Jun 13, 2011
836
837
838
839
840
841
842
843
844
845
// if --show-tests option is given, only print tests and exit
if(only_print_tests) {
TestCase *testItem = NULL;
for(testItem = testCases; testItem; testItem = testItem->next) {
printf("%s (in %s)\n", testItem->testName, testItem->suiteName);
}
return 0;
}
Jul 4, 2011
Jul 4, 2011
846
847
848
RunStarted(argc, argv, time(0), loggerData);
Jun 26, 2011
Jun 26, 2011
849
850
char *currentSuiteName = NULL;
Jun 27, 2011
Jun 27, 2011
851
852
int suiteStartTime = SDL_GetTicks();
Jun 9, 2011
Jun 9, 2011
853
TestCase *testItem = NULL;
Jun 9, 2011
Jun 9, 2011
854
for(testItem = testCases; testItem; testItem = testItem->next) {
Jun 26, 2011
Jun 26, 2011
855
856
if(currentSuiteName == NULL) {
currentSuiteName = testItem->suiteName;
Jun 27, 2011
Jun 27, 2011
857
SuiteStarted(currentSuiteName, time(0));
Jun 27, 2011
Jun 27, 2011
858
859
860
861
862
863
testFailureCount = testPassCount = 0;
suiteCounter++;
}
else if(strncmp(currentSuiteName, testItem->suiteName, NAME_BUFFER_SIZE) != 0) {
Jun 27, 2011
Jun 27, 2011
864
865
866
867
const double suiteRuntime = (SDL_GetTicks() - suiteStartTime) / 1000.0f;
SuiteEnded(testPassCount, testFailureCount, testSkipCount, time(0),
suiteRuntime);
Jun 27, 2011
Jun 27, 2011
868
Jun 29, 2011
Jun 29, 2011
869
870
suiteStartTime = SDL_GetTicks();
Jun 27, 2011
Jun 27, 2011
871
currentSuiteName = testItem->suiteName;
Jun 27, 2011
Jun 27, 2011
872
SuiteStarted(currentSuiteName, time(0));
Jun 27, 2011
Jun 27, 2011
873
874
875
876
testFailureCount = testPassCount = 0;
suiteCounter++;
Jun 26, 2011
Jun 26, 2011
877
878
879
}
TestStarted(testItem->testName, testItem->suiteName,
Jun 27, 2011
Jun 27, 2011
880
881
882
testItem->description, time(0));
const Uint32 testTimeStart = SDL_GetTicks();
Jun 26, 2011
Jun 26, 2011
883
Jun 9, 2011
Jun 9, 2011
884
885
int retVal = ExecuteTest(testItem);
if(retVal) {
Jun 27, 2011
Jun 27, 2011
886
887
totalTestfailureCount++;
testFailureCount++;
Jun 9, 2011
Jun 9, 2011
888
} else {
Jun 27, 2011
Jun 27, 2011
889
890
totalTestPassCount++;
testPassCount++;
Jun 8, 2011
Jun 8, 2011
892
Jun 27, 2011
Jun 27, 2011
893
894
895
const double testTotalRuntime = (SDL_GetTicks() - testTimeStart) / 1000.0f;
TestEnded(testItem->testName, testItem->suiteName, retVal, time(0), testTotalRuntime);
May 23, 2011
May 23, 2011
897
Jun 27, 2011
Jun 27, 2011
898
if(currentSuiteName) {
Jun 27, 2011
Jun 27, 2011
899
900
SuiteEnded(testPassCount, testFailureCount, testSkipCount, time(0),
(SDL_GetTicks() - suiteStartTime) / 1000.0f);
Jun 27, 2011
Jun 27, 2011
901
}
Jun 27, 2011
Jun 27, 2011
902
Jun 9, 2011
Jun 9, 2011
903
904
905
UnloadTestCases(testCases);
UnloadTestSuites(suites);
May 23, 2011
May 23, 2011
906
const Uint32 endTicks = SDL_GetTicks();
Jun 27, 2011
Jun 27, 2011
907
const double totalRunTime = (endTicks - startTicks) / 1000.0f;
May 23, 2011
May 23, 2011
908
Jun 27, 2011
Jun 27, 2011
909
RunEnded(totalTestPassCount + totalTestfailureCount, suiteCounter,
Jun 27, 2011
Jun 27, 2011
910
totalTestPassCount, totalTestfailureCount, time(0), totalRunTime);
May 23, 2011
May 23, 2011
911
Jun 30, 2011
Jun 30, 2011
912
return (totalTestfailureCount ? 1 : 0);