2 * Copyright (c) 2004 Nik Clayton
3 * 2017 Jérémie Galarneau
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <babeltrace/assert-internal.h>
38 static int no_plan
= 0;
39 static int skip_all
= 0;
40 static int have_plan
= 0;
41 static unsigned int test_count
= 0; /* Number of tests that have been run */
42 static unsigned int e_tests
= 0; /* Expected number of tests to run */
43 static unsigned int failures
= 0; /* Number of tests that failed */
44 static char *todo_msg
= NULL
;
45 static char *todo_msg_fixed
= "libtap malloc issue";
47 static int test_died
= 0;
49 /* Encapsulate the pthread code in a conditional. In the absence of
50 libpthread the code does nothing */
51 #ifdef HAVE_LIBPTHREAD
53 static pthread_mutex_t M
= PTHREAD_MUTEX_INITIALIZER
;
54 # define LOCK pthread_mutex_lock(&M);
55 # define UNLOCK pthread_mutex_unlock(&M);
61 static void _expected_tests(unsigned int);
62 static void _tap_init(void);
63 static void _cleanup(void);
67 void flockfile (FILE * filehandle
) {
72 void funlockfile(FILE * filehandle
) {
78 * Generate a test result.
80 * ok -- boolean, indicates whether or not the test passed.
81 * test_name -- the name of the test, may be NULL
82 * test_comment -- a comment to print afterwards, may be NULL
85 _gen_result(int ok
, const char *func
, char *file
, unsigned int line
,
89 char *local_test_name
= NULL
;
97 /* Start by taking the test name and performing any printf()
99 if(test_name
!= NULL
) {
100 va_start(ap
, test_name
);
101 if (vasprintf(&local_test_name
, test_name
, ap
) == -1) {
102 local_test_name
= NULL
;
106 /* Make sure the test name contains more than digits
107 and spaces. Emit an error message and exit if it
109 if(local_test_name
) {
111 for(c
= local_test_name
; *c
!= '\0'; c
++) {
112 if(!isdigit(*c
) && !isspace(*c
)) {
119 diag(" You named your test '%s'. You shouldn't use numbers for your test names.", local_test_name
);
120 diag(" Very confusing.");
130 printf("ok %d", test_count
);
132 if(test_name
!= NULL
) {
135 /* Print the test name, escaping any '#' characters it
137 if(local_test_name
!= NULL
) {
139 for(c
= local_test_name
; *c
!= '\0'; c
++) {
142 fputc((int)*c
, stdout
);
145 } else { /* vasprintf() failed, use a fixed message */
146 printf("%s", todo_msg_fixed
);
150 /* If we're in a todo_start() block then flag the test as being
151 TODO. todo_msg should contain the message to print at this
152 point. If it's NULL then asprintf() failed, and we should
153 use the fixed message.
155 This is not counted as a failure, so decrement the counter if
158 printf(" # TODO %s", todo_msg
? todo_msg
: todo_msg_fixed
);
166 if(getenv("HARNESS_ACTIVE") != NULL
)
169 diag(" Failed %stest (%s:%s() at line %d)",
170 todo
? "(TODO) " : "", file
, func
, line
);
172 free(local_test_name
);
176 /* We only care (when testing) that ok is positive, but here we
177 specifically only want to return 1 or 0 */
182 * Initialise the TAP library. Will only do so once, however many times it's
188 static int run_once
= 0;
193 /* stdout needs to be unbuffered so that the output appears
194 in the same place relative to stderr output as it does
195 with Test::Harness */
202 * Note that there's no plan.
213 fprintf(stderr
, "You tried to plan twice!\n");
228 * Note that the plan is to skip all tests
231 plan_skip_all(char *reason
)
243 printf(" # Skip %s", reason
);
253 * Note the number of tests that will be run.
256 plan_tests(unsigned int tests
)
264 fprintf(stderr
, "You tried to plan twice!\n");
271 fprintf(stderr
, "You said to run 0 tests! You've got to run something.\n");
279 _expected_tests(tests
);
294 vfprintf(stderr
, fmt
, ap
);
303 diag_multiline(const char *val
)
305 size_t len
, i
, line_start_idx
= 0;
310 for (i
= 0; i
< len
; i
++) {
313 if (val
[i
] != '\n') {
317 BT_ASSERT((i
- line_start_idx
+ 1) <= INT_MAX
);
318 line_length
= i
- line_start_idx
+ 1;
319 fprintf(stderr
, "# %.*s", line_length
, &val
[line_start_idx
]);
320 line_start_idx
= i
+ 1;
325 _expected_tests(unsigned int tests
)
328 printf("1..%d\n", tests
);
333 skip(unsigned int n
, char *fmt
, ...)
336 char *skip_msg
= NULL
;
341 if (asprintf(&skip_msg
, fmt
, ap
) == -1) {
348 printf("ok %d # skip %s\n", test_count
,
350 skip_msg
: "libtap():malloc() failed");
361 todo_start(char *fmt
, ...)
368 if (vasprintf(&todo_msg
, fmt
, ap
) == -1) {
397 /* If there's no plan, just return the number of failures */
398 if(no_plan
|| !have_plan
) {
403 /* Ran too many tests? Return the number of tests that were run
404 that shouldn't have been */
405 if(e_tests
< test_count
) {
406 r
= test_count
- e_tests
;
411 /* Return the number of tests that failed + the number of tests
413 r
= failures
+ e_tests
- test_count
;
420 * Cleanup at the end of the run, produce any final output that might be
429 /* If plan_no_plan() wasn't called, and we don't have a plan,
430 and we're not skipping everything, then something happened
431 before we could produce any output */
432 if(!no_plan
&& !have_plan
&& !skip_all
) {
433 diag("Looks like your test died before it could output anything.");
439 diag("Looks like your test died just after %d.", test_count
);
445 /* No plan provided, but now we know how many tests were run, and can
446 print the header at the end */
447 if(!skip_all
&& (no_plan
|| !have_plan
)) {
448 printf("1..%d\n", test_count
);
451 if((have_plan
&& !no_plan
) && e_tests
< test_count
) {
452 diag("Looks like you planned %d %s but ran %d extra.",
453 e_tests
, e_tests
== 1 ? "test" : "tests", test_count
- e_tests
);
458 if((have_plan
|| !no_plan
) && e_tests
> test_count
) {
459 diag("Looks like you planned %d %s but only ran %d.",
460 e_tests
, e_tests
== 1 ? "test" : "tests", test_count
);
466 diag("Looks like you failed %d %s of %d.",
467 failures
, failures
== 1 ? "test" : "tests", test_count
);