Apache HTTPD
abts.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "abts.h"
18#include "abts_tests.h"
19#include "testutil.h"
20
21#define ABTS_STAT_SIZE 6
22static char status[ABTS_STAT_SIZE] = {'|', '/', '-', '|', '\\', '-'};
23static int curr_char;
24static int verbose = 0;
25static int exclude = 0;
26static int quiet = 0;
27static int list_tests = 0;
28
29const char **testlist = NULL;
30
31static int find_test_name(const char *testname) {
32 int i;
33 for (i = 0; testlist[i] != NULL; i++) {
34 if (!strcmp(testlist[i], testname)) {
35 return 1;
36 }
37 }
38 return 0;
39}
40
41/* Determine if the test should be run at all */
42static int should_test_run(const char *testname) {
43 int found = 0;
44 if (list_tests == 1) {
45 return 0;
46 }
47 if (testlist == NULL) {
48 return 1;
49 }
51 if ((found && !exclude) || (!found && exclude)) {
52 return 1;
53 }
54 return 0;
55}
56
57static void reset_status(void)
58{
59 curr_char = 0;
60}
61
62static void update_status(void)
63{
64 if (!quiet) {
66 fprintf(stdout, "\b%c", status[curr_char]);
68 }
69}
70
71static void end_suite(abts_suite *suite)
72{
73 if (suite != NULL) {
74 sub_suite *last = suite->tail;
75 if (!quiet) {
76 fprintf(stdout, "\b");
78 }
79 if (last->skipped > 0) {
80 fprintf(stdout, "SKIPPED %d of %d\n", last->skipped, last->num_test);
82 }
83 if (last->failed == 0) {
84 fprintf(stdout, "SUCCESS\n");
86 }
87 else {
88 fprintf(stdout, "FAILED %d of %d\n", last->failed, last->num_test);
90 }
91 }
92}
93
95{
97 char *p;
98 const char *suite_name;
99 curr_char = 0;
100
101 /* Only end the suite if we actually ran it */
102 if (suite && suite->tail &&!suite->tail->not_run) {
103 end_suite(suite);
104 }
105
106 subsuite = malloc(sizeof(*subsuite));
107 subsuite->num_test = 0;
108 subsuite->failed = 0;
109 subsuite->skipped = 0;
110 subsuite->next = NULL;
111 /* suite_name_full may be an absolute path depending on __FILE__
112 * expansion */
114 if (!suite_name) {
116 }
117 if (suite_name) {
118 suite_name++;
119 } else {
121 }
122 p = strrchr(suite_name, '.');
123 if (p) {
124 subsuite->name = memcpy(calloc(p - suite_name + 1, 1),
126 }
127 else {
128 subsuite->name = strdup(suite_name);
129 }
130
131 if (list_tests) {
132 fprintf(stdout, "%s\n", subsuite->name);
133 }
134
135 subsuite->not_run = 0;
136
137 if (suite == NULL) {
138 suite = malloc(sizeof(*suite));
139 suite->head = subsuite;
140 suite->tail = subsuite;
141 }
142 else {
143 suite->tail->next = subsuite;
144 suite->tail = subsuite;
145 }
146
147 if (!should_test_run(subsuite->name)) {
148 subsuite->not_run = 1;
149 return suite;
150 }
151
152 reset_status();
153 fprintf(stdout, "%-20s: ", subsuite->name);
155 fflush(stdout);
156
157 return suite;
158}
159
160static void abts_free_suite(abts_suite *suite)
161{
162 if (suite) {
163 sub_suite *dptr, *next;
164
165 for (dptr = suite->head; dptr; dptr = next) {
166 next = dptr->next;
167 free(dptr->name);
168 free(dptr);
169 }
170
171 free(suite);
172 }
173}
174
176{
177 abts_case tc;
178 sub_suite *ss;
179
180 if (!should_test_run(ts->tail->name)) {
181 return;
182 }
183 ss = ts->tail;
184
185 tc.failed = 0;
186 tc.skipped = 0;
187 tc.suite = ss;
188
189 ss->num_test++;
191
192 f(&tc, value);
193
194 if (tc.failed) {
195 ss->failed++;
196 }
197
198 if (tc.skipped) {
199 ss->skipped++;
200 }
201}
202
203static void report_summary(const char *kind_header,
204 const char *name_header,
205 const char *percent_header)
206{
207 fprintf(stdout, "%-15s\t\tTotal\t%s\t%s\n",
209 fprintf(stdout, "===================================================\n");
210}
211
212static int report(abts_suite *suite)
213{
214 int failed_count = 0;
215 int skipped_count = 0;
216 sub_suite *dptr;
217
218 if (suite && suite->tail &&!suite->tail->not_run) {
219 end_suite(suite);
220 }
221
222 for (dptr = suite->head; dptr; dptr = dptr->next) {
223 failed_count += dptr->failed;
224 }
225
226 for (dptr = suite->head; dptr; dptr = dptr->next) {
227 skipped_count += dptr->skipped;
228 }
229
230 if (list_tests) {
231 return 0;
232 }
233
234 /* Report skipped tests */
235 if (skipped_count > 0) {
236 dptr = suite->head;
237 report_summary("Skipped Tests", "Skip", "Skipped %");
238 while (dptr != NULL) {
239 if (dptr->skipped != 0) {
240 float percent = ((float)dptr->skipped / (float)dptr->num_test);
241 fprintf(stdout, "%-15s\t\t%5d\t%4d\t%8.2f%%\n", dptr->name,
242 dptr->num_test, dptr->skipped, percent * 100);
243 }
244 dptr = dptr->next;
245 }
246 }
247
248 if (failed_count == 0) {
249 printf("All tests passed.\n");
250 return 0;
251 }
252
253 /* Report failed tests */
254 dptr = suite->head;
255 if (skipped_count > 0) {
256 fprintf(stdout, "\n");
257 }
258 report_summary("Failed Tests", "Fail", " Failed %");
259 while (dptr != NULL) {
260 if (dptr->failed != 0) {
261 float percent = ((float)dptr->failed / (float)dptr->num_test);
262 fprintf(stdout, "%-15s\t\t%5d\t%4d\t%8.2f%%\n", dptr->name,
263 dptr->num_test, dptr->failed, percent * 100);
264 }
265 dptr = dptr->next;
266 }
267 return 1;
268}
269
270void abts_log_message(const char *fmt, ...)
271{
274
275 if (verbose) {
276 va_start(args, fmt);
278 va_end(args);
279 fprintf(stderr, "\n");
280 fflush(stderr);
281 }
282}
283
284#define IMPL_abts_T_equal(T, NAME, FMT, CAST) \
285void abts_##NAME##_equal(abts_case *tc, const T expected, const T actual, int lineno) \
286{ \
287 update_status(); \
288 if (tc->failed) return; \
289 \
290 if (expected == actual) return; \
291 \
292 tc->failed = TRUE; \
293 if (verbose) { \
294 fprintf(stderr, "Line %d: expected <%" FMT ">, but saw <%" FMT ">\n", \
295 lineno, CAST expected, CAST actual); \
296 fflush(stderr); \
297 } \
298}
299IMPL_abts_T_equal(int, int, "d", (int))
300IMPL_abts_T_equal(unsigned int, uint, "u", (unsigned int))
301IMPL_abts_T_equal(long, long, "ld", (long))
302IMPL_abts_T_equal(unsigned long, ulong, "lu", (unsigned long))
303IMPL_abts_T_equal(long long, llong, "lld", (long long))
304IMPL_abts_T_equal(unsigned long long, ullong, "llu", (unsigned long long))
305IMPL_abts_T_equal(size_t, size, "lu", (unsigned long))
306
307#define IMPL_abts_T_nequal(T, NAME, FMT, CAST) \
308void abts_##NAME##_nequal(abts_case *tc, const T expected, const T actual, int lineno) \
309{ \
310 update_status(); \
311 if (tc->failed) return; \
312 \
313 if (expected != actual) return; \
314 \
315 tc->failed = TRUE; \
316 if (verbose) { \
317 fprintf(stderr, "Line %d: expected something other than <%" FMT ">, " \
318 "but saw <%" FMT ">\n", \
319 lineno, CAST expected, CAST actual); \
320 fflush(stderr); \
321 } \
322}
323IMPL_abts_T_nequal(int, int, "d", (int))
324IMPL_abts_T_nequal(unsigned int, uint, "u", (unsigned int))
325IMPL_abts_T_nequal(long, long, "ld", (long))
326IMPL_abts_T_nequal(unsigned long, ulong, "lu", (unsigned long))
327IMPL_abts_T_nequal(long long, llong, "lld", (long long))
328IMPL_abts_T_nequal(unsigned long long, ullong, "llu", (unsigned long long))
329IMPL_abts_T_nequal(size_t, size, "lu", (unsigned long))
330
331void abts_str_equal(abts_case *tc, const char *expected, const char *actual, int lineno)
332{
334 if (tc->failed) return;
335
336 if (!expected && !actual) return;
337 if (expected && actual)
338 if (!strcmp(expected, actual)) return;
339
340 tc->failed = TRUE;
341 if (verbose) {
342 fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
343 fflush(stderr);
344 }
345}
346
347void abts_str_nequal(abts_case *tc, const char *expected, const char *actual,
348 size_t n, int lineno)
349{
351 if (tc->failed) return;
352
353 if (!strncmp(expected, actual, n)) return;
354
355 tc->failed = TRUE;
356 if (verbose) {
357 fprintf(stderr, "Line %d: expected something other than <%s>, but saw <%s>\n",
358 lineno, expected, actual);
359 fflush(stderr);
360 }
361}
362
363void abts_ptr_notnull(abts_case *tc, const void *ptr, int lineno)
364{
366 if (tc->failed) return;
367
368 if (ptr != NULL) return;
369
370 tc->failed = TRUE;
371 if (verbose) {
372 fprintf(stderr, "Line %d: expected non-NULL, but saw NULL\n", lineno);
373 fflush(stderr);
374 }
375}
376
377void abts_ptr_equal(abts_case *tc, const void *expected, const void *actual, int lineno)
378{
380 if (tc->failed) return;
381
382 if (expected == actual) return;
383
384 tc->failed = TRUE;
385 if (verbose) {
386 fprintf(stderr, "Line %d: expected <%p>, but saw <%p>\n", lineno, expected, actual);
387 fflush(stderr);
388 }
389}
390
391void abts_fail(abts_case *tc, const char *message, int lineno)
392{
394 if (tc->failed) return;
395
396 tc->failed = TRUE;
397 if (verbose) {
398 fprintf(stderr, "Line %d: %s\n", lineno, message);
399 fflush(stderr);
400 }
401}
402
403void abts_skip(abts_case *tc, const char *message, int lineno)
404{
406 if (tc->skipped) return;
407
408 tc->skipped = TRUE;
409 if (verbose) {
410 fprintf(stderr, "\bSKIP: Line %d: %s\n", lineno, message);
411 fflush(stderr);
412 }
413}
414
415void abts_assert(abts_case *tc, const char *message, int condition, int lineno)
416{
418 if (tc->failed) return;
419
420 if (condition) return;
421
422 tc->failed = TRUE;
423 if (verbose) {
424 fprintf(stderr, "Line %d: %s\n", lineno, message);
425 fflush(stderr);
426 }
427}
428
429void abts_true(abts_case *tc, int condition, int lineno)
430{
432 if (tc->failed) return;
433
434 if (condition) return;
435
436 tc->failed = TRUE;
437 if (verbose) {
438 fprintf(stderr, "Line %d: Condition is false, but expected true\n", lineno);
439 fflush(stderr);
440 }
441}
442
443void abts_not_impl(abts_case *tc, const char *message, int lineno)
444{
446
447 tc->suite->not_impl++;
448 if (verbose) {
449 fprintf(stderr, "Line %d: %s\n", lineno, message);
450 fflush(stderr);
451 }
452}
453
454int main(int argc, const char *const argv[]) {
455 int i;
456 int rv;
457 int list_provided = 0;
458 abts_suite *suite = NULL;
459
460 initialize();
461
463
464 for (i = 1; i < argc; i++) {
465 if (!strcmp(argv[i], "-v")) {
466 verbose = 1;
467 continue;
468 }
469 if (!strcmp(argv[i], "-x")) {
470 exclude = 1;
471 continue;
472 }
473 if (!strcmp(argv[i], "-l")) {
474 list_tests = 1;
475 continue;
476 }
477 if (!strcmp(argv[i], "-q")) {
478 quiet = 1;
479 continue;
480 }
481 if (argv[i][0] == '-') {
482 fprintf(stderr, "Invalid option: `%s'\n", argv[i]);
483 exit(1);
484 }
485 list_provided = 1;
486 }
487
488 if (list_provided) {
489 /* Waste a little space here, because it is easier than counting the
490 * number of tests listed. Besides it is at most three char *.
491 */
492 testlist = calloc(argc + 1, sizeof(char *));
493 for (i = 1; i < argc; i++) {
494 testlist[i - 1] = argv[i];
495 }
496 }
497
498 for (i = 0; i < (sizeof(alltests) / sizeof(struct testlist *)); i++) {
499 suite = alltests[i].func(suite);
500 }
501
502 rv = report(suite);
503 abts_free_suite(suite);
504 return rv;
505}
506
int n
Definition ap_regex.h:278
static int verbose
Definition abts.c:24
#define ABTS_STAT_SIZE
Definition abts.c:21
static int find_test_name(const char *testname)
Definition abts.c:31
void abts_run_test(abts_suite *ts, test_func f, void *value)
Definition abts.c:155
static void update_status(void)
Definition abts.c:62
static int quiet
Definition abts.c:26
void abts_ptr_notnull(abts_case *tc, const void *ptr, int lineno)
Definition abts.c:292
void abts_not_impl(abts_case *tc, const char *message, int lineno)
Definition abts.c:360
void abts_true(abts_case *tc, int condition, int lineno)
Definition abts.c:346
static int curr_char
Definition abts.c:23
static void end_suite(abts_suite *suite)
Definition abts.c:71
static void reset_status(void)
Definition abts.c:57
abts_suite * abts_add_suite(abts_suite *suite, const char *suite_name_full)
Definition abts.c:90
void abts_str_equal(abts_case *tc, const char *expected, const char *actual, int lineno)
Definition abts.c:259
void abts_str_nequal(abts_case *tc, const char *expected, const char *actual, size_t n, int lineno)
Definition abts.c:276
void abts_ptr_equal(abts_case *tc, const void *expected, const void *actual, int lineno)
Definition abts.c:306
void abts_fail(abts_case *tc, const char *message, int lineno)
Definition abts.c:320
static char status[6]
Definition abts.c:22
static int exclude
Definition abts.c:25
static int list_tests
Definition abts.c:27
void abts_log_message(const char *fmt,...)
Definition abts.c:216
static int should_test_run(const char *testname)
Definition abts.c:42
static int report(abts_suite *suite)
Definition abts.c:180
void abts_assert(abts_case *tc, const char *message, int condition, int lineno)
Definition abts.c:332
void abts_skip(abts_case *tc, const char *message, int lineno)
Definition abts.c:403
static void report_summary(const char *kind_header, const char *name_header, const char *percent_header)
Definition abts.c:203
static void abts_free_suite(abts_suite *suite)
Definition abts.c:160
#define IMPL_abts_T_nequal(T, NAME, FMT, CAST)
Definition abts.c:307
#define IMPL_abts_T_equal(T, NAME, FMT, CAST)
Definition abts.c:284
void(* test_func)(abts_case *tc, void *data)
Definition abts.h:65
#define TRUE
Definition abts.h:38
const struct testlist alltests[]
void initialize(void)
Definition testutil.c:42
char * strdup(const char *str)
return found
Definition core.c:2840
apr_file_t * f
apr_size_t size
const char * value
Definition apr_env.h:51
apr_pool_t int argc
Definition apr_getopt.h:104
apr_vformatter_buff_t const char * fmt
Definition apr_lib.h:175
const char char ** last
const char const char *const * args
apr_pool_t * p
Definition md_event.c:32
const char * argv[3]
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
int main(void)
Definition occhild.c:9
int failed
Definition abts.h:59
sub_suite * suite
Definition abts.h:61
int skipped
Definition abts.h:60
sub_suite * tail
Definition abts.h:54
sub_suite * head
Definition abts.h:53
char * name
Definition abts.h:42
int failed
Definition abts.h:44
int not_impl
Definition abts.h:47
struct sub_suite * next
Definition abts.h:48
int num_test
Definition abts.h:43
int not_run
Definition abts.h:46
int skipped
Definition abts.h:45
abts_suite *(* func)(abts_suite *suite)
Definition abts_tests.h:24