forked from jnordgren/radixtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AceUnit.c
464 lines (444 loc) · 13.6 KB
/
AceUnit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
235
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* Copyright (c) 2007, Christian Hujer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the AceUnit developers nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** Main module of AceUnit.
* Contains the runner.
* @author <a href="mailto:[email protected]">Christian Hujer</a>
* @file AceUnit.c
*/
#include "AceUnit.h"
#include "AceUnitLogging.h"
#include "AceUnitData.h"
#ifdef ACEUNIT_CODE_INCLUDE
#include ACEUNIT_CODE_INCLUDE
#endif
/** Records an error.
* @param fixtureId Id of the fixture that contained the test case with the failed assertion.
* @param assertionId Id of the assertion that failed.
*/
void recordError(const FixtureId_t fixtureId, const AssertionId_t assertionId) {
runnerData->recentError = &runnerData->recentErrorData;
runnerData->recentError->fixtureId = fixtureId;
runnerData->recentError->assertionId = assertionId;
runnerData->recentError->testId = runnerData->currentTestId;
}
#ifdef ACEUNIT_SUITES
/** Returns if the specified test id is contained in a list of test ids.
*
* @note Always returns \c true if \p tests contains the value #ALL_TESTS.
*
* @param tests Tests in which to look for \p testId, terminated by #NO_TEST.
* @param testId Test id for which to look.
* @return Whether \p testId is contained in \p tests or not.
* @retval true if \p testId is contained in \p tests.
* @retval true if \p tests contains #ALL_TESTS.
* @retval false otherwise
*/
static bool containsTests(const AceTestId_t *tests, const AceTestId_t test) {
for (;NO_TEST != *tests; tests++) {
if ((test == *tests) || (ALL_TESTS == *tests)) {
return true;
}
}
return false;
}
#endif
/** The Logger. */
extern TestLogger_t *globalLogger;
/* Define the PRE/POST macros as empty macros in case they are undefined to make further source code more readable. */
#ifndef ACEUNIT_PRE_BEFORECLASS
#define ACEUNIT_PRE_BEFORECLASS
#endif
#ifndef ACEUNIT_POST_BEFORECLASS
#define ACEUNIT_POST_BEFORECLASS
#endif
#ifndef ACEUNIT_PRE_BEFORE
#define ACEUNIT_PRE_BEFORE
#endif
#ifndef ACEUNIT_POST_BEFORE
#define ACEUNIT_POST_BEFORE
#endif
#ifndef ACEUNIT_PRE_TEST
#define ACEUNIT_PRE_TEST
#endif
#ifndef ACEUNIT_POST_TEST
#define ACEUNIT_POST_TEST
#endif
#ifndef ACEUNIT_PRE_AFTER
#define ACEUNIT_PRE_AFTER
#endif
#ifndef ACEUNIT_POST_AFTER
#define ACEUNIT_POST_AFTER
#endif
#ifndef ACEUNIT_PRE_AFTERCLASS
#define ACEUNIT_PRE_AFTERCLASS
#endif
#ifndef ACEUNIT_POST_AFTERCLASS
#define ACEUNIT_POST_AFTERCLASS
#endif
/** Runs all test cases from a test fixture.
* @param fixture Test fixture to run.
* @param group Group to run.
*/
void runFixture(const TestFixture_t *const fixture
#ifdef ACEUNIT_GROUP
, AceGroupId_t group
#endif
) {
#define invokeAll(X) for (secondary = fixture->X; NULL != *secondary; secondary++) {\
(*secondary)();\
}
#define globalLog(X, Y) if ((NULL != globalLogger) && (NULL != globalLogger->X)) {\
globalLogger->X(Y);\
}
const testMethod_t *secondary; /* beforeClass, before, after, afterClass */
const testMethod_t *testCase;
const TestCaseId_t *testId;
#ifdef ACEUNIT_LOOP
const aceunit_loop_t *loopMax;
aceunit_loop_t currentLoop;
#endif
#ifdef ACEUNIT_GROUP
const AceGroupId_t *groups;
#endif
bool ranBeforeClass = false;
#ifdef ACEUNIT_LOG_FIXTURE
globalLog(fixtureStarted, fixture->id);
#endif
for (
testCase = fixture->testCase,
#ifdef ACEUNIT_LOOP
loopMax = fixture->loops,
#endif
#ifdef ACEUNIT_GROUP
groups = fixture->groups,
#endif
testId = fixture->testIds;
NULL != *testCase;
testCase++,
#ifdef ACEUNIT_LOOP
loopMax++,
#endif
#ifdef ACEUNIT_GROUP
groups++,
#endif
testId++
) {
#ifdef ACEUNIT_GROUP
if (*groups == group) {
#endif
if (!ranBeforeClass) {
ACEUNIT_PRE_BEFORECLASS
invokeAll(beforeClass);
ACEUNIT_POST_BEFORECLASS
ranBeforeClass = true;
}
runnerData->currentTestId = *testId;
#ifdef ACEUNIT_LOG_TESTCASE
globalLog(testCaseStarted, runnerData->currentTestId);
#endif
ACEUNIT_PRE_BEFORE
invokeAll(before);
ACEUNIT_POST_BEFORE
runnerData->recentError = NULL;
ACEUNIT_PRE_TEST
#if ACEUNIT_ASSERTION_STYLE == ACEUNIT_ASSERTION_STYLE_LONGJMP
if (0 == setjmp(runnerData->jmpBuf)) {
#endif
#ifdef ACEUNIT_LOOP
for (currentLoop = 0; (currentLoop < *loopMax) && (NULL == runnerData->recentError); currentLoop++) {
#endif
(*testCase)();
#ifdef ACEUNIT_LOOP
}
#endif
#if ACEUNIT_ASSERTION_STYLE == ACEUNIT_ASSERTION_STYLE_LONGJMP
}
#endif
ACEUNIT_POST_TEST
runnerData->testCaseCount++;
if (NULL != runnerData->recentError) {
globalLog(testCaseFailed, runnerData->recentError);
runnerData->testCaseFailureCount++;
}
ACEUNIT_PRE_AFTER
invokeAll(after);
ACEUNIT_POST_AFTER
#ifdef ACEUNIT_LOG_TESTCASE
globalLog(testCaseEnded, runnerData->currentTestId);
#endif
runnerData->currentTestId = TestCaseId_NULL;
#ifdef ACEUNIT_GROUP
}
#endif
}
ACEUNIT_PRE_AFTERCLASS
invokeAll(afterClass);
ACEUNIT_POST_AFTERCLASS
#ifdef ACEUNIT_LOG_FIXTURE
globalLog(fixtureEnded, fixture->id);
#endif
#undef invokeAll
#undef globalLog
}
/** Runs all test cases from the supplied fixtures.
* @param fixtures Test fixtures to run (NULL terminated).
*/
void runFixtures(const TestFixture_t *const fixtures[]
#ifdef ACEUNIT_GROUP
, AceGroupId_t group
#endif
) {
while (NULL != *fixtures) {
runFixture(*fixtures
#ifdef ACEUNIT_GROUP
, group
#endif
);
fixtures++;
}
}
#ifdef ACEUNIT_SUITES
/** Runs the specified test cases from a test fixture.
* @param fixture Test fixture to run.
* @param tests Tests to run.
* @param group Group to run.
*/
void runFixtureIfSpecified(const TestFixture_t *const fixture, const AceTestId_t *const tests
#ifdef ACEUNIT_GROUP
, AceGroupId_t group
#endif
) {
#define invokeAll(X) for (secondary = fixture->X; NULL != *secondary; secondary++) {\
(*secondary)();\
}
#define globalLog(X, Y) if ((NULL != globalLogger) && (NULL != globalLogger->X)) {\
globalLogger->X(Y);\
}
const testMethod_t *secondary; /* beforeClass, before, after, afterClass */
const testMethod_t *testCase;
const TestCaseId_t *testId;
#ifdef ACEUNIT_LOOP
const aceunit_loop_t *loopMax;
aceunit_loop_t currentLoop;
#endif
#ifdef ACEUNIT_GROUP
const AceGroupId_t *groups;
#endif
bool ranBeforeClass = false;
#ifdef ACEUNIT_LOG_FIXTURE
globalLog(fixtureStarted, fixture->id);
#endif
for (
testCase = fixture->testCase,
#ifdef ACEUNIT_LOOP
loopMax = fixture->loops,
#endif
#ifdef ACEUNIT_GROUP
groups = fixture->groups,
#endif
testId = fixture->testIds;
NULL != *testCase;
testCase++,
#ifdef ACEUNIT_LOOP
loopMax++,
#endif
#ifdef ACEUNIT_GROUP
groups++,
#endif
testId++
) {
if (containsTests(tests, *testId)) {
#ifdef ACEUNIT_GROUP
if (*groups == group) {
#endif
if (!ranBeforeClass) {
ACEUNIT_PRE_BEFORECLASS
invokeAll(beforeClass);
ACEUNIT_POST_BEFORECLASS
ranBeforeClass = true;
}
runnerData->currentTestId = *testId;
#ifdef ACEUNIT_LOG_TESTCASE
globalLog(testCaseStarted, runnerData->currentTestId);
#endif
ACEUNIT_PRE_BEFORE
invokeAll(before);
ACEUNIT_POST_BEFORE
runnerData->recentError = NULL;
ACEUNIT_PRE_TEST
#if ACEUNIT_ASSERTION_STYLE == ACEUNIT_ASSERTION_STYLE_LONGJMP
if (0 == setjmp(runnerData->jmpBuf)) {
#endif
#ifdef ACEUNIT_LOOP
for (currentLoop = 0; (currentLoop < *loopMax) && (NULL == runnerData->recentError); currentLoop++) {
#endif
(*testCase)();
#ifdef ACEUNIT_LOOP
}
#endif
#if ACEUNIT_ASSERTION_STYLE == ACEUNIT_ASSERTION_STYLE_LONGJMP
}
#endif
ACEUNIT_POST_TEST
runnerData->testCaseCount++;
if (NULL != runnerData->recentError) {
globalLog(testCaseFailed, runnerData->recentError);
runnerData->testCaseFailureCount++;
}
ACEUNIT_PRE_AFTER
invokeAll(after);
ACEUNIT_POST_AFTER
#ifdef ACEUNIT_LOG_TESTCASE
globalLog(testCaseEnded, runnerData->currentTestId);
#endif
runnerData->currentTestId = TestCaseId_NULL;
#ifdef ACEUNIT_GROUP
}
#endif
}
}
ACEUNIT_PRE_AFTERCLASS
invokeAll(afterClass);
ACEUNIT_POST_AFTERCLASS
#ifdef ACEUNIT_LOG_FIXTURE
globalLog(fixtureEnded, fixture->id);
#endif
#undef invokeAll
#undef globalLog
}
/** Runs all test cases from the supplied suite.
* @param suite Test suite to run.
* @param group Group to run.
*/
void runSuite(const TestSuite_t *const suite
#ifdef ACEUNIT_GROUP
, AceGroupId_t group
#endif
) {
#define globalLog(X, Y) if ((NULL != globalLogger) && (NULL != globalLogger->X)) {\
globalLogger->X(Y);\
}
const TestSuite_t *const *suites = suite->suites;
#ifdef ACEUNIT_LOG_SUITE
globalLog(suiteStarted, suite->id);
#endif
if (suites != NULL) {
/* this is a not a Fixture */
for (; NULL != *suites; suites++) {
runSuite(*suites
#ifdef ACEUNIT_GROUP
, group
#endif
);
}
} else {
/* this is a Fixture */
runFixture((TestFixture_t *) suite
#ifdef ACEUNIT_GROUP
, group
#endif
);
}
#ifdef ACEUNIT_LOG_SUITE
globalLog(suiteEnded, suite->id);
#endif
#undef globalLog
}
#endif
#ifdef ACEUNIT_SUITES
/** Runs all test cases from the supplied test suites.
* @param suites Test suites to run (NULL terminated).
*/
void runSuites(const TestSuite_t *const suites[]
#ifdef ACEUNIT_GROUP
, AceGroupId_t group
#endif
) {
while (NULL != *suites) {
runSuite(*suites
#ifdef ACEUNIT_GROUP
, group
#endif
);
suites++;
}
}
/** This always is the first suite. */
extern TestSuite_t suite1;
/** Runs the specified tests.
*
* @param suite Suite or fixture at which the test runner currently is at.
* @param tests Tests to run, terminated by #NO_TEST.
* @param group Group to run.
*/
void runTestsIfSpecified(const TestSuite_t *const suite, const AceTestId_t *const tests
#ifdef ACEUNIT_GROUP
, AceUnitGroupid_t group
#endif
) {
if (containsTests(tests, suite->id)) {
runSuite(suite);
} else {
const TestSuite_t *const *suites = suite->suites;
if (suites != NULL) {
/* this is not a Fixture. */
for (; NULL != *suites; suites++) {
runTestsIfSpecified(*suites, tests
#ifdef ACEUNIT_GROUP
,group
#endif
);
}
} else {
/** this is a Fixture */
runFixtureIfSpecified((TestFixture_t *) suite, tests
#ifdef ACEUNIT_GROUP
, group
#endif
);
}
}
}
/** Runs the specified tests.
* All tests (test cases and suites - fixtures as well as packages) share the same number space.
* With other words, the number for a test case is unique (within where you run AceUnit.jar).
* That means you can specify test suites, fixtures or tests.
*
* @note Each test will be executed only zero or one times, even if \p tests contains duplicates.
* Duplicates may be explicit or implicit.
* Explicit duplicates means the same test id is specified more than once.
* Implicit duplicates means the same test id is specified indirectly twice.
* Indirect specification of a test id is not via its test id but via its fixture or suite id.
*
* @param tests tests to run, terminated with zero.
*/
void runTests(const AceTestId_t *const tests) {
runTestsIfSpecified(&suite1, tests);
}
#endif
/* TODO: Add method to run specified test cases from a test fixture.
* This should be done by a linear search through the list of test cases. */