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.
20 #include "abts_tests.h"
23 #define ABTS_STAT_SIZE 6
24 static char status
[ABTS_STAT_SIZE
] = {'|', '/', '-', '|', '\\', '-'};
26 static int verbose
= 0;
27 static int exclude
= 0;
29 static int list_tests
= 0;
31 const char **testlist
= NULL
;
33 static int find_test_name(const char *testname
) {
35 for (i
= 0; testlist
[i
] != NULL
; i
++) {
36 if (!strcmp(testlist
[i
], testname
)) {
43 /* Determine if the test should be run at all */
44 static int should_test_run(const char *testname
) {
46 if (list_tests
== 1) {
49 if (testlist
== NULL
) {
52 found
= find_test_name(testname
);
53 if ((found
&& !exclude
) || (!found
&& exclude
)) {
59 static void reset_status(void)
64 static void update_status(void)
67 curr_char
= (curr_char
+ 1) % ABTS_STAT_SIZE
;
68 fprintf(stdout
, "\b%c", status
[curr_char
]);
73 static void end_suite(abts_suite
*suite
)
76 sub_suite
*last
= suite
->tail
;
78 fprintf(stdout
, "\b");
81 if (last
->failed
== 0) {
82 fprintf(stdout
, "SUCCESS\n");
86 fprintf(stdout
, "FAILED %d of %d\n", last
->failed
, last
->num_test
);
92 abts_suite
*abts_add_suite(abts_suite
*suite
, const char *suite_name_full
)
96 const char *suite_name
;
99 /* Only end the suite if we actually ran it */
100 if (suite
&& suite
->tail
&&!suite
->tail
->not_run
) {
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__
110 suite_name
= strrchr(suite_name_full
, '/');
114 suite_name
= suite_name_full
;
116 p
= strrchr(suite_name
, '.');
118 subsuite
->name
= memcpy(calloc(p
- suite_name
+ 1, 1),
119 suite_name
, p
- suite_name
);
122 subsuite
->name
= suite_name
;
126 fprintf(stdout
, "%s\n", subsuite
->name
);
129 subsuite
->not_run
= 0;
132 suite
= malloc(sizeof(*suite
));
133 suite
->head
= subsuite
;
134 suite
->tail
= subsuite
;
137 suite
->tail
->next
= subsuite
;
138 suite
->tail
= subsuite
;
141 if (!should_test_run(subsuite
->name
)) {
142 subsuite
->not_run
= 1;
147 fprintf(stdout
, "%-20s: ", subsuite
->name
);
154 void abts_run_test(abts_suite
*ts
, test_func f
, void *value
)
159 if (!should_test_run(ts
->tail
->name
)) {
164 tc
= malloc(sizeof(*tc
));
179 static int report(abts_suite
*suite
)
184 if (suite
&& suite
->tail
&&!suite
->tail
->not_run
) {
188 for (dptr
= suite
->head
; dptr
; dptr
= dptr
->next
) {
189 count
+= dptr
->failed
;
197 printf("All tests passed.\n");
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);
215 void abts_log_message(const char *fmt
, ...)
222 vfprintf(stderr
, fmt
, args
);
224 fprintf(stderr
, "\n");
229 void abts_int_equal(abts_case
*tc
, const int expected
, const int actual
, int lineno
)
232 if (tc
->failed
) return;
234 if (expected
== actual
) return;
238 fprintf(stderr
, "Line %d: expected <%d>, but saw <%d>\n", lineno
, expected
, actual
);
243 void abts_int_nequal(abts_case
*tc
, const int expected
, const int actual
, int lineno
)
246 if (tc
->failed
) return;
248 if (expected
!= actual
) return;
252 fprintf(stderr
, "Line %d: expected <%d>, but saw <%d>\n", lineno
, expected
, actual
);
257 void abts_str_equal(abts_case
*tc
, const char *expected
, const char *actual
, int lineno
)
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;
269 fprintf(stderr
, "Line %d: expected <%s>, but saw <%s>\n", lineno
, expected
, actual
);
274 void abts_str_nequal(abts_case
*tc
, const char *expected
, const char *actual
,
275 size_t n
, int lineno
)
278 if (tc
->failed
) return;
280 if (!strncmp(expected
, actual
, n
)) return;
284 fprintf(stderr
, "Line %d: expected <%s>, but saw <%s>\n", lineno
, expected
, actual
);
289 void abts_ptr_notnull(abts_case
*tc
, const void *ptr
, int lineno
)
292 if (tc
->failed
) return;
294 if (ptr
!= NULL
) return;
298 fprintf(stderr
, "Line %d: Expected NULL, but saw <%p>\n", lineno
, ptr
);
303 void abts_ptr_equal(abts_case
*tc
, const void *expected
, const void *actual
, int lineno
)
306 if (tc
->failed
) return;
308 if (expected
== actual
) return;
312 fprintf(stderr
, "Line %d: expected <%p>, but saw <%p>\n", lineno
, expected
, actual
);
317 void abts_fail(abts_case
*tc
, const char *message
, int lineno
)
320 if (tc
->failed
) return;
324 fprintf(stderr
, "Line %d: %s\n", lineno
, message
);
329 void abts_assert(abts_case
*tc
, const char *message
, int condition
, int lineno
)
332 if (tc
->failed
) return;
334 if (condition
) return;
338 fprintf(stderr
, "Line %d: %s\n", lineno
, message
);
343 void abts_true(abts_case
*tc
, int condition
, int lineno
)
346 if (tc
->failed
) return;
348 if (condition
) return;
352 fprintf(stderr
, "Line %d: Condition is false, but expected true\n", lineno
);
357 void abts_not_impl(abts_case
*tc
, const char *message
, int lineno
)
361 tc
->suite
->not_impl
++;
363 fprintf(stderr
, "Line %d: %s\n", lineno
, message
);
368 int main(int argc
, const char *const argv
[]) {
371 int list_provided
= 0;
372 abts_suite
*suite
= NULL
;
376 quiet
= !isatty(STDOUT_FILENO
);
378 for (i
= 1; i
< argc
; i
++) {
379 if (!strcmp(argv
[i
], "-v")) {
383 if (!strcmp(argv
[i
], "-x")) {
387 if (!strcmp(argv
[i
], "-l")) {
391 if (!strcmp(argv
[i
], "-q")) {
395 if (argv
[i
][0] == '-') {
396 fprintf(stderr
, "Invalid option: `%s'\n", argv
[i
]);
403 /* Waste a little space here, because it is easier than counting the
404 * number of tests listed. Besides it is at most three char *.
406 testlist
= calloc(argc
+ 1, sizeof(char *));
407 for (i
= 1; i
< argc
; i
++) {
408 testlist
[i
- 1] = argv
[i
];
412 for (i
= 0; i
< (sizeof(alltests
) / sizeof(struct testlist
*)); i
++) {
413 suite
= alltests
[i
].func(suite
);