2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (C) 2004 Nik Clayton
15 static int no_plan
= 0;
16 static int skip_all
= 0;
17 static int have_plan
= 0;
18 static unsigned int test_count
= 0; /* Number of tests that have been run */
19 static unsigned int e_tests
= 0; /* Expected number of tests to run */
20 static unsigned int failures
= 0; /* Number of tests that failed */
21 static char *todo_msg
= NULL
;
22 static char *todo_msg_fixed
= "libtap malloc issue";
24 static int test_died
= 0;
26 /* Encapsulate the pthread code in a conditional. In the absence of
27 libpthread the code does nothing */
28 #ifdef HAVE_LIBPTHREAD
30 static pthread_mutex_t M
= PTHREAD_MUTEX_INITIALIZER
;
31 # define LOCK pthread_mutex_lock(&M);
32 # define UNLOCK pthread_mutex_unlock(&M);
38 static void _expected_tests(unsigned int);
39 static void _tap_init(void);
40 static void _cleanup(void);
43 * Generate a test result.
45 * ok -- boolean, indicates whether or not the test passed.
46 * test_name -- the name of the test, may be NULL
47 * test_comment -- a comment to print afterwards, may be NULL
50 _gen_result(int ok
, const char *func
, char *file
, unsigned int line
,
54 char *local_test_name
= NULL
;
62 /* Start by taking the test name and performing any printf()
64 if(test_name
!= NULL
) {
65 va_start(ap
, test_name
);
66 if (vasprintf(&local_test_name
, test_name
, ap
) == -1) {
67 local_test_name
= NULL
;
71 /* Make sure the test name contains more than digits
72 and spaces. Emit an error message and exit if it
76 for(c
= local_test_name
; *c
!= '\0'; c
++) {
77 if(!isdigit(*c
) && !isspace(*c
)) {
84 diag(" You named your test '%s'. You shouldn't use numbers for your test names.", local_test_name
);
85 diag(" Very confusing.");
95 printf("ok %d", test_count
);
97 if(test_name
!= NULL
) {
100 /* Print the test name, escaping any '#' characters it
102 if(local_test_name
!= NULL
) {
104 for(c
= local_test_name
; *c
!= '\0'; c
++) {
107 fputc((int)*c
, stdout
);
110 } else { /* vasprintf() failed, use a fixed message */
111 printf("%s", todo_msg_fixed
);
115 /* If we're in a todo_start() block then flag the test as being
116 TODO. todo_msg should contain the message to print at this
117 point. If it's NULL then asprintf() failed, and we should
118 use the fixed message.
120 This is not counted as a failure, so decrement the counter if
123 printf(" # TODO %s", todo_msg
? todo_msg
: todo_msg_fixed
);
131 if(getenv("HARNESS_ACTIVE") != NULL
)
134 diag(" Failed %stest (%s:%s() at line %d)",
135 todo
? "(TODO) " : "", file
, func
, line
);
137 free(local_test_name
);
141 /* We only care (when testing) that ok is positive, but here we
142 specifically only want to return 1 or 0 */
147 * Initialise the TAP library. Will only do so once, however many times it's
153 static int run_once
= 0;
158 /* stdout needs to be unbuffered so that the output appears
159 in the same place relative to stderr output as it does
160 with Test::Harness */
167 * Note that there's no plan.
178 fprintf(stderr
, "You tried to plan twice!\n");
193 * Note that the plan is to skip all tests
196 plan_skip_all(char *reason
)
208 printf(" # Skip %s", reason
);
218 * Note the number of tests that will be run.
221 plan_tests(unsigned int tests
)
229 fprintf(stderr
, "You tried to plan twice!\n");
236 fprintf(stderr
, "You said to run 0 tests! You've got to run something.\n");
244 _expected_tests(tests
);
259 vfprintf(stderr
, fmt
, ap
);
268 _expected_tests(unsigned int tests
)
271 printf("1..%d\n", tests
);
276 skip(unsigned int n
, char *fmt
, ...)
279 char *skip_msg
= NULL
;
284 if (asprintf(&skip_msg
, fmt
, ap
) == -1) {
291 printf("ok %d # skip %s\n", test_count
,
293 skip_msg
: "libtap():malloc() failed");
304 todo_start(char *fmt
, ...)
311 if (vasprintf(&todo_msg
, fmt
, ap
) == -1) {
340 /* If there's no plan, just return the number of failures */
341 if(no_plan
|| !have_plan
) {
346 /* Ran too many tests? Return the number of tests that were run
347 that shouldn't have been */
348 if(e_tests
< test_count
) {
349 r
= test_count
- e_tests
;
354 /* Return the number of tests that failed + the number of tests
356 r
= failures
+ e_tests
- test_count
;
363 * Cleanup at the end of the run, produce any final output that might be
372 /* If plan_no_plan() wasn't called, and we don't have a plan,
373 and we're not skipping everything, then something happened
374 before we could produce any output */
375 if(!no_plan
&& !have_plan
&& !skip_all
) {
376 diag("Looks like your test died before it could output anything.");
382 diag("Looks like your test died just after %d.", test_count
);
388 /* No plan provided, but now we know how many tests were run, and can
389 print the header at the end */
390 if(!skip_all
&& (no_plan
|| !have_plan
)) {
391 printf("1..%d\n", test_count
);
394 if((have_plan
&& !no_plan
) && e_tests
< test_count
) {
395 diag("Looks like you planned %d %s but ran %d extra.",
396 e_tests
, e_tests
== 1 ? "test" : "tests", test_count
- e_tests
);
401 if((have_plan
|| !no_plan
) && e_tests
> test_count
) {
402 diag("Looks like you planned %d %s but only ran %d.",
403 e_tests
, e_tests
== 1 ? "test" : "tests", test_count
);
409 diag("Looks like you failed %d %s of %d.",
410 failures
, failures
== 1 ? "test" : "tests", test_count
);