2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (C) 2004 Nik Clayton
5 * Copyright (C) 2017 Jérémie Galarneau
9 #include "common/compat/time.hpp"
21 static int no_plan
= 0;
22 static int skip_all
= 0;
23 static int have_plan
= 0;
24 static unsigned int test_count
= 0; /* Number of tests that have been run */
25 static unsigned int e_tests
= 0; /* Expected number of tests to run */
26 static unsigned int failures
= 0; /* Number of tests that failed */
27 static char *todo_msg
= NULL
;
28 static const char *todo_msg_fixed
= "libtap malloc issue";
30 static int test_died
= 0;
31 static int time_tests
= 1;
32 struct timespec last_time
;
34 /* Encapsulate the pthread code in a conditional. In the absence of
35 libpthread the code does nothing */
36 #ifdef HAVE_LIBPTHREAD
38 static pthread_mutex_t M
= PTHREAD_MUTEX_INITIALIZER
;
39 #define LOCK pthread_mutex_lock(&M);
40 #define UNLOCK pthread_mutex_unlock(&M);
46 static void _expected_tests(unsigned int);
47 static void _tap_init(void);
48 static void _cleanup(void);
51 static inline void flockfile(FILE *filehandle
)
56 static inline void funlockfile(FILE *filehandle
)
63 * Generate a test result.
65 * ok -- boolean, indicates whether or not the test passed.
66 * test_name -- the name of the test, may be NULL
67 * test_comment -- a comment to print afterwards, may be NULL
69 unsigned int _gen_result(
70 int ok
, const char *func
, const char *file
, unsigned int line
, const char *test_name
, ...)
73 char *local_test_name
= NULL
;
81 /* Start by taking the test name and performing any printf()
83 if (test_name
!= NULL
) {
84 va_start(ap
, test_name
);
85 if (vasprintf(&local_test_name
, test_name
, ap
) == -1) {
86 local_test_name
= NULL
;
90 /* Make sure the test name contains more than digits
91 and spaces. Emit an error message and exit if it
93 if (local_test_name
) {
95 for (c
= local_test_name
; *c
!= '\0'; c
++) {
96 if (!isdigit((unsigned char) *c
) && !isspace((unsigned char) *c
)) {
102 if (name_is_digits
) {
103 diag(" You named your test '%s'. You shouldn't use numbers for your test names.",
105 diag(" Very confusing.");
115 printf("ok %d", test_count
);
117 if (test_name
!= NULL
) {
120 /* Print the test name, escaping any '#' characters it
122 if (local_test_name
!= NULL
) {
124 for (c
= local_test_name
; *c
!= '\0'; c
++) {
127 fputc((int) *c
, stdout
);
130 } else { /* vasprintf() failed, use a fixed message */
131 printf("%s", todo_msg_fixed
);
135 /* If we're in a todo_start() block then flag the test as being
136 TODO. todo_msg should contain the message to print at this
137 point. If it's NULL then asprintf() failed, and we should
138 use the fixed message.
140 This is not counted as a failure, so decrement the counter if
143 printf(" # TODO %s", todo_msg
? todo_msg
: todo_msg_fixed
);
152 if (getenv("HARNESS_ACTIVE") != NULL
)
155 diag(" Failed %stest (%s:%s() at line %d)",
156 todo
? "(TODO) " : "",
161 free(local_test_name
);
165 /* We only care (when testing) that ok is positive, but here we
166 specifically only want to return 1 or 0 */
171 * Initialise the TAP library. Will only do so once, however many times it's
176 static int run_once
= 0;
181 /* stdout needs to be unbuffered so that the output appears
182 in the same place relative to stderr output as it does
183 with Test::Harness */
186 char *autotime_env
= getenv("TAP_AUTOTIME");
187 if (autotime_env
!= NULL
) {
188 time_tests
= atoi(autotime_env
);
189 if (time_tests
!= 0) {
196 lttng_clock_gettime(CLOCK_MONOTONIC
, &last_time
);
200 * Note that there's no plan.
202 int plan_no_plan(void)
208 if (have_plan
!= 0) {
209 fprintf(stderr
, "You tried to plan twice!\n");
224 * Note that the plan is to skip all tests
226 int plan_skip_all(const char *reason
)
237 printf(" # Skip %s", reason
);
247 * Note the number of tests that will be run.
249 int plan_tests(unsigned int tests
)
255 if (have_plan
!= 0) {
256 fprintf(stderr
, "You tried to plan twice!\n");
263 fprintf(stderr
, "You said to run 0 tests! You've got to run something.\n");
271 _expected_tests(tests
);
278 unsigned int diag(const char *fmt
, ...)
285 vfprintf(stderr
, fmt
, ap
);
293 void diag_multiline(const char *val
)
295 size_t len
, i
, line_start_idx
= 0;
300 for (i
= 0; i
< len
; i
++) {
303 if (val
[i
] != '\n') {
307 assert((i
- line_start_idx
+ 1) <= INT_MAX
);
308 line_length
= i
- line_start_idx
+ 1;
309 fprintf(stderr
, "# %.*s", line_length
, &val
[line_start_idx
]);
310 line_start_idx
= i
+ 1;
314 void _expected_tests(unsigned int tests
)
316 printf("1..%d\n", tests
);
320 int skip(unsigned int n
, const char *fmt
, ...)
323 char *skip_msg
= NULL
;
328 if (vasprintf(&skip_msg
, fmt
, ap
) == -1) {
335 printf("ok %d # skip %s\n",
337 skip_msg
!= NULL
? skip_msg
: "libtap():malloc() failed");
348 void _output_test_time(void)
350 struct timespec new_time
;
353 lttng_clock_gettime(CLOCK_MONOTONIC
, &new_time
);
354 time_ns
= elapsed_time_ns(&last_time
, &new_time
);
355 printf(" ---\n duration_ms: %ld.%ld\n ...\n",
359 lttng_clock_gettime(CLOCK_MONOTONIC
, &last_time
);
362 void todo_start(const char *fmt
, ...)
369 if (vasprintf(&todo_msg
, fmt
, ap
) == -1) {
389 int exit_status(void)
395 /* If there's no plan, just return the number of failures */
396 if (no_plan
|| !have_plan
) {
401 /* Ran too many tests? Return the number of tests that were run
402 that shouldn't have been */
403 if (e_tests
< test_count
) {
404 r
= test_count
- e_tests
;
409 /* Return the number of tests that failed + the number of tests
411 r
= failures
+ e_tests
- test_count
;
418 * Cleanup at the end of the run, produce any final output that might be
425 /* If plan_no_plan() wasn't called, and we don't have a plan,
426 and we're not skipping everything, then something happened
427 before we could produce any output */
428 if (!no_plan
&& !have_plan
&& !skip_all
) {
429 diag("Looks like your test died before it could output anything.");
435 diag("Looks like your test died just after %d.", test_count
);
440 /* No plan provided, but now we know how many tests were run, and can
441 print the header at the end */
442 if (!skip_all
&& (no_plan
|| !have_plan
)) {
443 printf("1..%d\n", test_count
);
446 if ((have_plan
&& !no_plan
) && e_tests
< test_count
) {
447 diag("Looks like you planned %d %s but ran %d extra.",
449 e_tests
== 1 ? "test" : "tests",
450 test_count
- e_tests
);
455 if ((have_plan
|| !no_plan
) && e_tests
> test_count
) {
456 diag("Looks like you planned %d %s but only ran %d.",
458 e_tests
== 1 ? "test" : "tests",
465 diag("Looks like you failed %d %s of %d.",
467 failures
== 1 ? "test" : "tests",