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

Latest commit

 

History

History
248 lines (206 loc) · 6.3 KB

File metadata and controls

248 lines (206 loc) · 6.3 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
26
27
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
May 30, 2011
May 30, 2011
28
#include "SDL_test.h"
May 23, 2011
May 23, 2011
29
May 30, 2011
May 30, 2011
30
31
32
//!< Function pointer to a test case function
typedef int (*TestCase)(void *arg);
Jun 1, 2011
Jun 1, 2011
33
34
35
//!< Flag for executing tests in-process
static int execute_inproc = 0;
May 30, 2011
May 30, 2011
36
/*!
Jun 1, 2011
Jun 1, 2011
37
38
* Returns the name for the dynamic library
* which implements the test suite.
May 30, 2011
May 30, 2011
39
*
Jun 1, 2011
Jun 1, 2011
40
41
42
43
44
* (in the future: scans the test/ directory and
* returns the names of the dynamic libraries
* implementing the test suites)
*
* \return Name of the dummy test suite
May 30, 2011
May 30, 2011
45
*/
Jun 1, 2011
Jun 1, 2011
46
47
char *
ScanForTestSuites() {
May 25, 2011
May 25, 2011
48
49
#if defined(linux) || defined( __linux)
char *libName = "tests/libtest.so";
May 26, 2011
May 26, 2011
50
51
#else
char *libName = "tests/libtest.dylib";
May 25, 2011
May 25, 2011
52
#endif
Jun 1, 2011
Jun 1, 2011
53
54
55
return libName;
}
May 25, 2011
May 25, 2011
56
Jun 1, 2011
Jun 1, 2011
57
58
59
60
61
62
63
64
65
66
67
/*!
* Loads test suite which is implemented as dynamic library.
*
* \param test0,330
*
* \return Pointer to loaded test suite, or NULL if library could not be loaded
*/
void *
LoadTestSuite(char *testSuiteName)
{
void *library = SDL_LoadObject(testSuiteName);
68
if(library == NULL) {
Jun 1, 2011
Jun 1, 2011
69
fprintf(stderr, "Loading %s failed\n", testSuiteName);
May 31, 2011
May 31, 2011
70
fprintf(stderr, "%s\n", SDL_GetError());
May 26, 2011
May 26, 2011
73
74
75
return library;
}
May 30, 2011
May 30, 2011
76
77
78
79
/*!
* 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
80
* \return Pointer to array of TestCaseReferences or NULL if function failed
May 30, 2011
May 30, 2011
81
*/
May 30, 2011
May 30, 2011
82
83
84
TestCaseReference **
QueryTestCases(void *library)
{
May 26, 2011
May 26, 2011
85
TestCaseReference **(*suite)(void);
May 26, 2011
May 26, 2011
86
May 30, 2011
May 30, 2011
87
suite = (TestCaseReference **(*)(void)) SDL_LoadFunction(library, "QueryTestSuite");
88
if(suite == NULL) {
May 31, 2011
May 31, 2011
89
90
fprintf(stderr, "Loading QueryTestCaseReferences() failed.\n");
fprintf(stderr, "%s\n", SDL_GetError());
May 26, 2011
May 26, 2011
91
92
}
May 26, 2011
May 26, 2011
93
TestCaseReference **tests = suite();
May 26, 2011
May 26, 2011
94
if(tests == NULL) {
May 31, 2011
May 31, 2011
95
96
fprintf(stderr, "Failed to load test references.\n");
fprintf(stderr, "%s\n", SDL_GetError());
May 26, 2011
May 26, 2011
97
98
99
100
101
}
return tests;
}
May 30, 2011
May 30, 2011
102
103
104
/*!
* Loads test case from a test suite
*
May 30, 2011
May 30, 2011
105
106
* \param suite a test suite
* \param testName Name of the test that is going to be loaded
May 30, 2011
May 30, 2011
107
*
May 31, 2011
May 31, 2011
108
* \return Function Pointer (TestCase) to loaded test case, NULL if function failed
May 30, 2011
May 30, 2011
109
*/
May 30, 2011
May 30, 2011
110
111
112
TestCase
LoadTestCase(void *suite, char *testName)
{
May 30, 2011
May 30, 2011
113
114
TestCase test = (int (*)(void *)) SDL_LoadFunction(suite, testName);
if(test == NULL) {
May 31, 2011
May 31, 2011
115
116
fprintf(stderr, "Loading test failed, tests == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
May 30, 2011
May 30, 2011
117
118
119
120
121
122
}
return test;
}
May 30, 2011
May 30, 2011
123
/*!
May 30, 2011
May 30, 2011
124
125
126
127
* 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
128
*
May 30, 2011
May 30, 2011
129
* \param stat_lock information about the exited child process
May 30, 2011
May 30, 2011
130
*
May 30, 2011
May 30, 2011
131
* \return 0 if test case succeeded, 1 otherwise
May 30, 2011
May 30, 2011
132
*/
May 30, 2011
May 30, 2011
133
134
135
136
int
HandleTestReturnValue(int stat_lock)
{
//! \todo rename to: HandleChildProcessReturnValue?
May 30, 2011
May 30, 2011
137
int returnValue = -1;
May 26, 2011
May 26, 2011
138
May 30, 2011
May 30, 2011
139
140
if(WIFEXITED(stat_lock)) {
returnValue = WEXITSTATUS(stat_lock);
May 26, 2011
May 26, 2011
141
142
} else if(WIFSIGNALED(stat_lock)) {
int signal = WTERMSIG(stat_lock);
May 31, 2011
May 31, 2011
143
fprintf(stderr, "FAILURE: test was aborted due to signal no %d\n", signal);
May 30, 2011
May 30, 2011
144
returnValue = 1;
May 26, 2011
May 26, 2011
145
146
}
May 30, 2011
May 30, 2011
147
return returnValue;
May 26, 2011
May 26, 2011
148
149
}
May 30, 2011
May 30, 2011
150
151
/*!
* Parse command line arguments
Jun 1, 2011
Jun 1, 2011
152
153
154
*
* \param argc Count of command line arguments
* \param argv Array of commond lines arguments
May 30, 2011
May 30, 2011
155
156
157
158
159
160
161
162
*/
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
163
if(SDL_strcmp(arg, "--in-proc") == 0) {
May 30, 2011
May 30, 2011
164
165
execute_inproc = 1;
}
Jun 1, 2011
Jun 1, 2011
166
167
168
169
170
171
172
173
else if(SDL_strcmp(arg, "--help") == 0 || SDL_strcmp(arg, "-h") == 0) {
printf("Usage: ./runner [--in-proc] [--help]\n");
printf("Options:\n");
printf(" --in-proc Executes tests in-process\n");
printf(" --help Print this help.:\n");
exit(0);
}
// \todo print error for unknown option
May 30, 2011
May 30, 2011
174
175
}
}
May 26, 2011
May 26, 2011
176
May 31, 2011
May 31, 2011
177
178
179
180
181
182
/*!
* 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
183
184
185
186
int
main(int argc, char *argv[])
{
ParseOptions(argc, argv);
May 26, 2011
May 26, 2011
187
May 30, 2011
May 30, 2011
188
// print: Testing against SDL version fuu (rev: bar) if verbose == true
May 26, 2011
May 26, 2011
189
190
191
192
193
int failureCount = 0, passCount = 0;
const Uint32 startTicks = SDL_GetTicks();
Jun 1, 2011
Jun 1, 2011
194
195
char *testSuiteName = ScanForTestSuites();
void *suite = LoadTestSuite(testSuiteName);
May 30, 2011
May 30, 2011
196
TestCaseReference **tests = QueryTestCases(suite);
May 26, 2011
May 26, 2011
197
May 26, 2011
May 26, 2011
198
199
200
201
TestCaseReference *reference = NULL;
int counter = 0;
for(reference = tests[counter]; reference; reference = tests[++counter]) {
May 30, 2011
May 30, 2011
202
if(reference->enabled == TEST_DISABLED) {
Jun 1, 2011
Jun 1, 2011
203
printf("Test %s (in %s) disabled. Omitting...\n", reference->name, testSuiteName);
May 26, 2011
May 26, 2011
204
} else {
May 30, 2011
May 30, 2011
205
206
char *testname = reference->name;
Jun 1, 2011
Jun 1, 2011
207
printf("Running %s (in %s):\n", testname, testSuiteName);
May 30, 2011
May 30, 2011
208
May 30, 2011
May 30, 2011
209
210
211
212
int retVal = 1;
if(execute_inproc) {
TestCase test = (TestCase) LoadTestCase(suite, testname);
retVal = test(0x0);
May 30, 2011
May 30, 2011
213
} else {
May 30, 2011
May 30, 2011
214
215
216
217
int childpid = fork();
if(childpid == 0) {
TestCase test = (TestCase) LoadTestCase(suite, testname);
return test(0x0);
May 30, 2011
May 30, 2011
218
} else {
May 30, 2011
May 30, 2011
219
220
221
222
int stat_lock = -1;
int child = wait(&stat_lock);
retVal = HandleTestReturnValue(stat_lock);
May 30, 2011
May 30, 2011
223
}
May 30, 2011
May 30, 2011
225
226
227
if(retVal) {
failureCount++;
Jun 1, 2011
Jun 1, 2011
228
printf("%s (in %s): FAILED\n", testname, testSuiteName);
May 30, 2011
May 30, 2011
229
230
} else {
passCount++;
Jun 1, 2011
Jun 1, 2011
231
printf("%s (in %s): ok\n", testname, testSuiteName);
May 30, 2011
May 30, 2011
232
}
May 26, 2011
May 26, 2011
234
235
printf("\n");
May 30, 2011
May 30, 2011
238
SDL_UnloadObject(suite);
May 23, 2011
May 23, 2011
239
240
241
const Uint32 endTicks = SDL_GetTicks();
May 30, 2011
May 30, 2011
242
printf("Ran %d tests in %0.5f seconds.\n", (passCount + failureCount), (endTicks-startTicks)/1000.0f);
May 23, 2011
May 23, 2011
243
244
245
246
printf("%d tests passed\n", passCount);
printf("%d tests failed\n", failureCount);