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

Latest commit

 

History

History
221 lines (179 loc) · 5.45 KB

File metadata and controls

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