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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "abts_tests.h"
21 #define ABTS_STAT_SIZE 6
22 static char status[ABTS_STAT_SIZE] = {'|', '/', '-', '|', '\\', '-'};
24 static int verbose = 0;
25 static int exclude = 0;
27 static int list_tests = 0;
29 const char **testlist = NULL;
31 static int find_test_name(const char *testname) {
33 for (i = 0; testlist[i] != NULL; i++) {
34 if (!strcmp(testlist[i], testname)) {
41 /* Determine if the test should be run at all */
42 static int should_test_run(const char *testname) {
44 if (list_tests == 1) {
47 if (testlist == NULL) {
50 found = find_test_name(testname);
51 if ((found && !exclude) || (!found && exclude)) {
57 static void reset_status(void)
62 static void update_status(void)
65 curr_char = (curr_char + 1) % ABTS_STAT_SIZE;
66 fprintf(stdout, "\b%c", status[curr_char]);
71 static void end_suite(abts_suite *suite)
74 sub_suite *last = suite->tail;
76 fprintf(stdout, "\b");
79 if (last->failed == 0) {
80 fprintf(stdout, "SUCCESS\n");
84 fprintf(stdout, "FAILED %d of %d\n", last->failed, last->num_test);
90 abts_suite *abts_add_suite(abts_suite *suite, const char *suite_name_full)
94 const char *suite_name;
97 /* Only end the suite if we actually ran it */
98 if (suite && suite->tail &&!suite->tail->not_run) {
102 subsuite = malloc(sizeof(*subsuite));
103 subsuite->num_test = 0;
104 subsuite->failed = 0;
105 subsuite->next = NULL;
106 /* suite_name_full may be an absolute path depending on __FILE__
108 suite_name = strrchr(suite_name_full, '/');
112 suite_name = suite_name_full;
114 p = strrchr(suite_name, '.');
116 subsuite->name = memcpy(calloc(p - suite_name + 1, 1),
117 suite_name, p - suite_name);
120 subsuite->name = suite_name;
124 fprintf(stdout, "%s\n", subsuite->name);
127 subsuite->not_run = 0;
130 suite = malloc(sizeof(*suite));
131 suite->head = subsuite;
132 suite->tail = subsuite;
135 suite->tail->next = subsuite;
136 suite->tail = subsuite;
139 if (!should_test_run(subsuite->name)) {
140 subsuite->not_run = 1;
145 fprintf(stdout, "%-20s: ", subsuite->name);
152 void abts_run_test(abts_suite *ts, test_func f, void *value)
157 if (!should_test_run(ts->tail->name)) {
162 tc = malloc(sizeof(*tc));
177 static int report(abts_suite *suite)
182 if (suite && suite->tail &&!suite->tail->not_run) {
186 for (dptr = suite->head; dptr; dptr = dptr->next) {
187 count += dptr->failed;
195 printf("All tests passed.\n");
200 fprintf(stdout, "%-15s\t\tTotal\tFail\tFailed %%\n", "Failed Tests");
201 fprintf(stdout, "===================================================\n");
202 while (dptr != NULL) {
203 if (dptr->failed != 0) {
204 float percent = ((float)dptr->failed / (float)dptr->num_test);
205 fprintf(stdout, "%-15s\t\t%5d\t%4d\t%6.2f%%\n", dptr->name,
206 dptr->num_test, dptr->failed, percent * 100);
213 void abts_log_message(const char *fmt, ...)
220 vfprintf(stderr, fmt, args);
222 fprintf(stderr, "\n");
227 void abts_int_equal(abts_case *tc, const int expected, const int actual, int lineno)
230 if (tc->failed) return;
232 if (expected == actual) return;
236 fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
241 void abts_int_nequal(abts_case *tc, const int expected, const int actual, int lineno)
244 if (tc->failed) return;
246 if (expected != actual) return;
250 fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
255 void abts_str_equal(abts_case *tc, const char *expected, const char *actual, int lineno)
258 if (tc->failed) return;
260 /* If both are NULL, match is good */
261 if (!expected && !actual) return;
262 if (expected && actual)
263 if (!strcmp(expected, actual)) return;
267 fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
272 void abts_str_nequal(abts_case *tc, const char *expected, const char *actual,
273 size_t n, int lineno)
276 if (tc->failed) return;
278 if (!strncmp(expected, actual, n)) return;
282 fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
287 void abts_ptr_notnull(abts_case *tc, const void *ptr, int lineno)
290 if (tc->failed) return;
292 if (ptr != NULL) return;
296 fprintf(stderr, "Line %d: expected non-NULL, but saw NULL\n", lineno);
301 void abts_ptr_equal(abts_case *tc, const void *expected, const void *actual, int lineno)
304 if (tc->failed) return;
306 if (expected == actual) return;
310 fprintf(stderr, "Line %d: expected <%p>, but saw <%p>\n", lineno, expected, actual);
315 void abts_fail(abts_case *tc, const char *message, int lineno)
318 if (tc->failed) return;
322 fprintf(stderr, "Line %d: %s\n", lineno, message);
327 void abts_assert(abts_case *tc, const char *message, int condition, int lineno)
330 if (tc->failed) return;
332 if (condition) return;
336 fprintf(stderr, "Line %d: %s\n", lineno, message);
341 void abts_true(abts_case *tc, int condition, int lineno)
344 if (tc->failed) return;
346 if (condition) return;
350 fprintf(stderr, "Line %d: Condition is false, but expected true\n", lineno);
355 void abts_not_impl(abts_case *tc, const char *message, int lineno)
359 tc->suite->not_impl++;
361 fprintf(stderr, "Line %d: %s\n", lineno, message);
366 int main(int argc, const char *const argv[]) {
369 int list_provided = 0;
370 abts_suite *suite = NULL;
374 quiet = !isatty(STDOUT_FILENO);
376 for (i = 1; i < argc; i++) {
377 if (!strcmp(argv[i], "-v")) {
381 if (!strcmp(argv[i], "-x")) {
385 if (!strcmp(argv[i], "-l")) {
389 if (!strcmp(argv[i], "-q")) {
393 if (argv[i][0] == '-') {
394 fprintf(stderr, "Invalid option: `%s'\n", argv[i]);
401 /* Waste a little space here, because it is easier than counting the
402 * number of tests listed. Besides it is at most three char *.
404 testlist = calloc(argc + 1, sizeof(char *));
405 for (i = 1; i < argc; i++) {
406 testlist[i - 1] = argv[i];
410 for (i = 0; i < (sizeof(alltests) / sizeof(struct testlist *)); i++) {
411 suite = alltests[i].func(suite);