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, '/');
110 suite_name = strrchr(suite_name_full, '\\');
115 suite_name = suite_name_full;
117 p = strrchr(suite_name, '.');
119 subsuite->name = memcpy(calloc(p - suite_name + 1, 1),
120 suite_name, p - suite_name);
123 subsuite->name = suite_name;
127 fprintf(stdout, "%s\n", subsuite->name);
130 subsuite->not_run = 0;
133 suite = malloc(sizeof(*suite));
134 suite->head = subsuite;
135 suite->tail = subsuite;
138 suite->tail->next = subsuite;
139 suite->tail = subsuite;
142 if (!should_test_run(subsuite->name)) {
143 subsuite->not_run = 1;
148 fprintf(stdout, "%-20s: ", subsuite->name);
155 void abts_run_test(abts_suite *ts, test_func f, void *value)
160 if (!should_test_run(ts->tail->name)) {
165 tc = malloc(sizeof(*tc));
180 static int report(abts_suite *suite)
185 if (suite && suite->tail &&!suite->tail->not_run) {
189 for (dptr = suite->head; dptr; dptr = dptr->next) {
190 count += dptr->failed;
198 printf("All tests passed.\n");
203 fprintf(stdout, "%-15s\t\tTotal\tFail\tFailed %%\n", "Failed Tests");
204 fprintf(stdout, "===================================================\n");
205 while (dptr != NULL) {
206 if (dptr->failed != 0) {
207 float percent = ((float)dptr->failed / (float)dptr->num_test);
208 fprintf(stdout, "%-15s\t\t%5d\t%4d\t%6.2f%%\n", dptr->name,
209 dptr->num_test, dptr->failed, percent * 100);
216 void abts_log_message(const char *fmt, ...)
223 vfprintf(stderr, fmt, args);
225 fprintf(stderr, "\n");
230 void abts_int_equal(abts_case *tc, const int expected, const int actual, int lineno)
233 if (tc->failed) return;
235 if (expected == actual) return;
239 fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
244 void abts_int_nequal(abts_case *tc, const int expected, const int actual, int lineno)
247 if (tc->failed) return;
249 if (expected != actual) return;
253 fprintf(stderr, "Line %d: expected something other than <%d>, but saw <%d>\n",
254 lineno, expected, actual);
259 void abts_str_equal(abts_case *tc, const char *expected, const char *actual, int lineno)
262 if (tc->failed) return;
264 /* If both are NULL, match is good */
265 if (!expected && !actual) return;
266 if (expected && actual)
267 if (!strcmp(expected, actual)) return;
271 fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
276 void abts_str_nequal(abts_case *tc, const char *expected, const char *actual,
277 size_t n, int lineno)
280 if (tc->failed) return;
282 if (!strncmp(expected, actual, n)) return;
286 fprintf(stderr, "Line %d: expected something other than <%s>, but saw <%s>\n",
287 lineno, expected, actual);
292 void abts_ptr_notnull(abts_case *tc, const void *ptr, int lineno)
295 if (tc->failed) return;
297 if (ptr != NULL) return;
301 fprintf(stderr, "Line %d: expected non-NULL, but saw NULL\n", lineno);
306 void abts_ptr_equal(abts_case *tc, const void *expected, const void *actual, int lineno)
309 if (tc->failed) return;
311 if (expected == actual) return;
315 fprintf(stderr, "Line %d: expected <%p>, but saw <%p>\n", lineno, expected, actual);
320 void abts_fail(abts_case *tc, const char *message, int lineno)
323 if (tc->failed) return;
327 fprintf(stderr, "Line %d: %s\n", lineno, message);
332 void abts_assert(abts_case *tc, const char *message, int condition, int lineno)
335 if (tc->failed) return;
337 if (condition) return;
341 fprintf(stderr, "Line %d: %s\n", lineno, message);
346 void abts_true(abts_case *tc, int condition, int lineno)
349 if (tc->failed) return;
351 if (condition) return;
355 fprintf(stderr, "Line %d: Condition is false, but expected true\n", lineno);
360 void abts_not_impl(abts_case *tc, const char *message, int lineno)
364 tc->suite->not_impl++;
366 fprintf(stderr, "Line %d: %s\n", lineno, message);
371 int main(int argc, const char *const argv[]) {
374 int list_provided = 0;
375 abts_suite *suite = NULL;
379 quiet = !isatty(STDOUT_FILENO);
381 for (i = 1; i < argc; i++) {
382 if (!strcmp(argv[i], "-v")) {
386 if (!strcmp(argv[i], "-x")) {
390 if (!strcmp(argv[i], "-l")) {
394 if (!strcmp(argv[i], "-q")) {
398 if (argv[i][0] == '-') {
399 fprintf(stderr, "Invalid option: `%s'\n", argv[i]);
406 /* Waste a little space here, because it is easier than counting the
407 * number of tests listed. Besides it is at most three char *.
409 testlist = calloc(argc + 1, sizeof(char *));
410 for (i = 1; i < argc; i++) {
411 testlist[i - 1] = argv[i];
415 for (i = 0; i < (sizeof(alltests) / sizeof(struct testlist *)); i++) {
416 suite = alltests[i].func(suite);