Allow external override for XLATE support.
[apr-util.git] / test / abts.c
blob03ae16d1b339962b68d9e89ce43a31f72933b448
1 /* Copyright 2000-2004 Ryan Bloom
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 * Portions of this file were taken from testall.c in the APR test suite,
16 * written by members of the Apache Software Foundation.
19 #include "abts.h"
20 #include "abts_tests.h"
21 #include "testutil.h"
23 #define ABTS_STAT_SIZE 6
24 static char status[ABTS_STAT_SIZE] = {'|', '/', '-', '|', '\\', '-'};
25 static int curr_char;
26 static int verbose = 0;
27 static int exclude = 0;
28 static int quiet = 0;
29 static int list_tests = 0;
31 const char **testlist = NULL;
33 static int find_test_name(const char *testname) {
34 int i;
35 for (i = 0; testlist[i] != NULL; i++) {
36 if (!strcmp(testlist[i], testname)) {
37 return 1;
40 return 0;
43 /* Determine if the test should be run at all */
44 static int should_test_run(const char *testname) {
45 int found = 0;
46 if (list_tests == 1) {
47 return 0;
49 if (testlist == NULL) {
50 return 1;
52 found = find_test_name(testname);
53 if ((found && !exclude) || (!found && exclude)) {
54 return 1;
56 return 0;
59 static void reset_status(void)
61 curr_char = 0;
64 static void update_status(void)
66 if (!quiet) {
67 curr_char = (curr_char + 1) % ABTS_STAT_SIZE;
68 fprintf(stdout, "\b%c", status[curr_char]);
69 fflush(stdout);
73 static void end_suite(abts_suite *suite)
75 if (suite != NULL) {
76 sub_suite *last = suite->tail;
77 if (!quiet) {
78 fprintf(stdout, "\b");
79 fflush(stdout);
81 if (last->failed == 0) {
82 fprintf(stdout, "SUCCESS\n");
83 fflush(stdout);
85 else {
86 fprintf(stdout, "FAILED %d of %d\n", last->failed, last->num_test);
87 fflush(stdout);
92 abts_suite *abts_add_suite(abts_suite *suite, const char *suite_name_full)
94 sub_suite *subsuite;
95 char *p;
96 const char *suite_name;
97 curr_char = 0;
99 /* Only end the suite if we actually ran it */
100 if (suite && suite->tail &&!suite->tail->not_run) {
101 end_suite(suite);
104 subsuite = malloc(sizeof(*subsuite));
105 subsuite->num_test = 0;
106 subsuite->failed = 0;
107 subsuite->next = NULL;
108 /* suite_name_full may be an absolute path depending on __FILE__
109 * expansion */
110 suite_name = strrchr(suite_name_full, '/');
111 if (suite_name) {
112 suite_name++;
113 } else {
114 suite_name = suite_name_full;
116 p = strrchr(suite_name, '.');
117 if (p) {
118 subsuite->name = memcpy(calloc(p - suite_name + 1, 1),
119 suite_name, p - suite_name);
121 else {
122 subsuite->name = suite_name;
125 if (list_tests) {
126 fprintf(stdout, "%s\n", subsuite->name);
129 subsuite->not_run = 0;
131 if (suite == NULL) {
132 suite = malloc(sizeof(*suite));
133 suite->head = subsuite;
134 suite->tail = subsuite;
136 else {
137 suite->tail->next = subsuite;
138 suite->tail = subsuite;
141 if (!should_test_run(subsuite->name)) {
142 subsuite->not_run = 1;
143 return suite;
146 reset_status();
147 fprintf(stdout, "%-20s: ", subsuite->name);
148 update_status();
149 fflush(stdout);
151 return suite;
154 void abts_run_test(abts_suite *ts, test_func f, void *value)
156 abts_case *tc;
157 sub_suite *ss;
159 if (!should_test_run(ts->tail->name)) {
160 return;
162 ss = ts->tail;
164 tc = malloc(sizeof(*tc));
165 tc->failed = 0;
166 tc->suite = ss;
168 ss->num_test++;
169 update_status();
171 f(tc, value);
173 if (tc->failed) {
174 ss->failed++;
176 free(tc);
179 static int report(abts_suite *suite)
181 int count = 0;
182 sub_suite *dptr;
184 if (suite && suite->tail &&!suite->tail->not_run) {
185 end_suite(suite);
188 for (dptr = suite->head; dptr; dptr = dptr->next) {
189 count += dptr->failed;
192 if (list_tests) {
193 return 0;
196 if (count == 0) {
197 printf("All tests passed.\n");
198 return 0;
201 dptr = suite->head;
202 fprintf(stdout, "%-15s\t\tTotal\tFail\tFailed %%\n", "Failed Tests");
203 fprintf(stdout, "===================================================\n");
204 while (dptr != NULL) {
205 if (dptr->failed != 0) {
206 float percent = ((float)dptr->failed / (float)dptr->num_test);
207 fprintf(stdout, "%-15s\t\t%5d\t%4d\t%6.2f%%\n", dptr->name,
208 dptr->num_test, dptr->failed, percent * 100);
210 dptr = dptr->next;
212 return 1;
215 void abts_log_message(const char *fmt, ...)
217 va_list args;
218 update_status();
220 if (verbose) {
221 va_start(args, fmt);
222 vfprintf(stderr, fmt, args);
223 va_end(args);
224 fprintf(stderr, "\n");
225 fflush(stderr);
229 void abts_int_equal(abts_case *tc, const int expected, const int actual, int lineno)
231 update_status();
232 if (tc->failed) return;
234 if (expected == actual) return;
236 tc->failed = TRUE;
237 if (verbose) {
238 fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
239 fflush(stderr);
243 void abts_int_nequal(abts_case *tc, const int expected, const int actual, int lineno)
245 update_status();
246 if (tc->failed) return;
248 if (expected != actual) return;
250 tc->failed = TRUE;
251 if (verbose) {
252 fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
253 fflush(stderr);
257 void abts_str_equal(abts_case *tc, const char *expected, const char *actual, int lineno)
259 update_status();
260 if (tc->failed) return;
262 /* If both are NULL, match is good */
263 if (!expected && !actual) return;
264 if (expected && actual)
265 if (!strcmp(expected, actual)) return;
267 tc->failed = TRUE;
268 if (verbose) {
269 fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
270 fflush(stderr);
274 void abts_str_nequal(abts_case *tc, const char *expected, const char *actual,
275 size_t n, int lineno)
277 update_status();
278 if (tc->failed) return;
280 if (!strncmp(expected, actual, n)) return;
282 tc->failed = TRUE;
283 if (verbose) {
284 fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
285 fflush(stderr);
289 void abts_ptr_notnull(abts_case *tc, const void *ptr, int lineno)
291 update_status();
292 if (tc->failed) return;
294 if (ptr != NULL) return;
296 tc->failed = TRUE;
297 if (verbose) {
298 fprintf(stderr, "Line %d: Expected NULL, but saw <%p>\n", lineno, ptr);
299 fflush(stderr);
303 void abts_ptr_equal(abts_case *tc, const void *expected, const void *actual, int lineno)
305 update_status();
306 if (tc->failed) return;
308 if (expected == actual) return;
310 tc->failed = TRUE;
311 if (verbose) {
312 fprintf(stderr, "Line %d: expected <%p>, but saw <%p>\n", lineno, expected, actual);
313 fflush(stderr);
317 void abts_fail(abts_case *tc, const char *message, int lineno)
319 update_status();
320 if (tc->failed) return;
322 tc->failed = TRUE;
323 if (verbose) {
324 fprintf(stderr, "Line %d: %s\n", lineno, message);
325 fflush(stderr);
329 void abts_assert(abts_case *tc, const char *message, int condition, int lineno)
331 update_status();
332 if (tc->failed) return;
334 if (condition) return;
336 tc->failed = TRUE;
337 if (verbose) {
338 fprintf(stderr, "Line %d: %s\n", lineno, message);
339 fflush(stderr);
343 void abts_true(abts_case *tc, int condition, int lineno)
345 update_status();
346 if (tc->failed) return;
348 if (condition) return;
350 tc->failed = TRUE;
351 if (verbose) {
352 fprintf(stderr, "Line %d: Condition is false, but expected true\n", lineno);
353 fflush(stderr);
357 void abts_not_impl(abts_case *tc, const char *message, int lineno)
359 update_status();
361 tc->suite->not_impl++;
362 if (verbose) {
363 fprintf(stderr, "Line %d: %s\n", lineno, message);
364 fflush(stderr);
368 int main(int argc, const char *const argv[]) {
369 int i;
370 int rv;
371 int list_provided = 0;
372 abts_suite *suite = NULL;
374 initialize();
375 for (i = 1; i < argc; i++) {
376 if (!strcmp(argv[i], "-v")) {
377 verbose = 1;
378 continue;
380 if (!strcmp(argv[i], "-x")) {
381 exclude = 1;
382 continue;
384 if (!strcmp(argv[i], "-l")) {
385 list_tests = 1;
386 continue;
388 if (!strcmp(argv[i], "-q")) {
389 quiet = 1;
390 continue;
392 if (argv[i][0] == '-') {
393 fprintf(stderr, "Invalid option: `%s'\n", argv[i]);
394 exit(1);
396 list_provided = 1;
399 if (list_provided) {
400 /* Waste a little space here, because it is easier than counting the
401 * number of tests listed. Besides it is at most three char *.
403 testlist = calloc(argc + 1, sizeof(char *));
404 for (i = 1; i < argc; i++) {
405 testlist[i - 1] = argv[i];
409 for (i = 0; i < (sizeof(alltests) / sizeof(struct testlist *)); i++) {
410 suite = alltests[i].func(suite);
413 rv = report(suite);
414 return rv;