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