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"
22 static int no_plan
= 0;
23 static int skip_all
= 0;
24 static int have_plan
= 0;
25 static unsigned int test_count
= 0; /* Number of tests that have been run */
26 static unsigned int e_tests
= 0; /* Expected number of tests to run */
27 static unsigned int failures
= 0; /* Number of tests that failed */
28 static char *todo_msg
= NULL
;
29 static const char *todo_msg_fixed
= "libtap malloc issue";
31 static int test_died
= 0;
32 static int time_tests
= 1;
33 struct timespec last_time
;
35 /* Encapsulate the pthread code in a conditional. In the absence of
36 libpthread the code does nothing */
37 #ifdef HAVE_LIBPTHREAD
39 static pthread_mutex_t M
= PTHREAD_MUTEX_INITIALIZER
;
40 #define LOCK pthread_mutex_lock(&M);
41 #define UNLOCK pthread_mutex_unlock(&M);
47 static void _expected_tests(unsigned int);
48 static void _tap_init(void);
49 static void _cleanup(void);
52 static inline void flockfile(FILE *filehandle
)
57 static inline void funlockfile(FILE *filehandle
)
64 * Generate a test result.
66 * ok -- boolean, indicates whether or not the test passed.
67 * test_name -- the name of the test, may be NULL
68 * test_comment -- a comment to print afterwards, may be NULL
70 unsigned int _gen_result(
71 int ok
, const char *func
, const char *file
, unsigned int line
, const char *test_name
, ...)
74 char *local_test_name
= NULL
;
82 /* Start by taking the test name and performing any printf()
84 if (test_name
!= NULL
) {
85 va_start(ap
, test_name
);
86 if (vasprintf(&local_test_name
, test_name
, ap
) == -1) {
87 local_test_name
= NULL
;
91 /* Make sure the test name contains more than digits
92 and spaces. Emit an error message and exit if it
94 if (local_test_name
) {
96 for (c
= local_test_name
; *c
!= '\0'; c
++) {
97 if (!isdigit((unsigned char) *c
) && !isspace((unsigned char) *c
)) {
103 if (name_is_digits
) {
104 diag(" You named your test '%s'. You shouldn't use numbers for your test names.",
106 diag(" Very confusing.");
116 printf("ok %d", test_count
);
118 if (test_name
!= NULL
) {
121 /* Print the test name, escaping any '#' characters it
123 if (local_test_name
!= NULL
) {
125 for (c
= local_test_name
; *c
!= '\0'; c
++) {
128 fputc((int) *c
, stdout
);
131 } else { /* vasprintf() failed, use a fixed message */
132 printf("%s", todo_msg_fixed
);
136 /* If we're in a todo_start() block then flag the test as being
137 TODO. todo_msg should contain the message to print at this
138 point. If it's NULL then asprintf() failed, and we should
139 use the fixed message.
141 This is not counted as a failure, so decrement the counter if
144 printf(" # TODO %s", todo_msg
? todo_msg
: todo_msg_fixed
);
153 if (getenv("HARNESS_ACTIVE") != NULL
)
156 diag(" Failed %stest (%s:%s() at line %d)",
157 todo
? "(TODO) " : "",
162 free(local_test_name
);
166 /* We only care (when testing) that ok is positive, but here we
167 specifically only want to return 1 or 0 */
172 * Initialise the TAP library. Will only do so once, however many times it's
177 static int run_once
= 0;
182 /* stdout needs to be unbuffered so that the output appears
183 in the same place relative to stderr output as it does
184 with Test::Harness */
188 * Check if the LTTNG_TESTS_TAP_AUTOTIME environment variable
189 * is set and contains at least one byte.
191 const char *autotime_env
= getenv("LTTNG_TESTS_TAP_AUTOTIME");
192 if (autotime_env
!= NULL
&& strnlen(autotime_env
, 1)) {
196 * Check if LTTNG_TESTS_TAP_AUTOTIME is '0', also check
197 * errno because strtol() can return '0' on error.
200 tap_autotime
= strtol(autotime_env
, NULL
, 10);
201 if (tap_autotime
== 0 && errno
== 0) {
208 lttng_clock_gettime(CLOCK_MONOTONIC
, &last_time
);
212 * Note that there's no plan.
214 int plan_no_plan(void)
220 if (have_plan
!= 0) {
221 fprintf(stderr
, "You tried to plan twice!\n");
236 * Note that the plan is to skip all tests
238 int plan_skip_all(const char *reason
)
249 printf(" # Skip %s", reason
);
259 * Note the number of tests that will be run.
261 int plan_tests(unsigned int tests
)
267 if (have_plan
!= 0) {
268 fprintf(stderr
, "You tried to plan twice!\n");
275 fprintf(stderr
, "You said to run 0 tests! You've got to run something.\n");
283 _expected_tests(tests
);
290 unsigned int diag(const char *fmt
, ...)
297 vfprintf(stderr
, fmt
, ap
);
305 void diag_multiline(const char *val
)
307 size_t len
, i
, line_start_idx
= 0;
312 for (i
= 0; i
< len
; i
++) {
315 if (val
[i
] != '\n') {
319 assert((i
- line_start_idx
+ 1) <= INT_MAX
);
320 line_length
= i
- line_start_idx
+ 1;
321 fprintf(stderr
, "# %.*s", line_length
, &val
[line_start_idx
]);
322 line_start_idx
= i
+ 1;
326 void _expected_tests(unsigned int tests
)
328 printf("1..%d\n", tests
);
332 int skip(unsigned int n
, const char *fmt
, ...)
335 char *skip_msg
= NULL
;
340 if (vasprintf(&skip_msg
, fmt
, ap
) == -1) {
347 printf("ok %d # skip %s\n",
349 skip_msg
!= NULL
? skip_msg
: "libtap():malloc() failed");
360 void _output_test_time(void)
362 struct timespec new_time
;
365 lttng_clock_gettime(CLOCK_MONOTONIC
, &new_time
);
366 time_ns
= elapsed_time_ns(&last_time
, &new_time
);
367 printf(" ---\n duration_ms: %ld.%ld\n ...\n",
371 lttng_clock_gettime(CLOCK_MONOTONIC
, &last_time
);
374 void todo_start(const char *fmt
, ...)
381 if (vasprintf(&todo_msg
, fmt
, ap
) == -1) {
401 int exit_status(void)
407 /* If there's no plan, just return the number of failures */
408 if (no_plan
|| !have_plan
) {
413 /* Ran too many tests? Return the number of tests that were run
414 that shouldn't have been */
415 if (e_tests
< test_count
) {
416 r
= test_count
- e_tests
;
421 /* Return the number of tests that failed + the number of tests
423 r
= failures
+ e_tests
- test_count
;
430 * Cleanup at the end of the run, produce any final output that might be
437 /* If plan_no_plan() wasn't called, and we don't have a plan,
438 and we're not skipping everything, then something happened
439 before we could produce any output */
440 if (!no_plan
&& !have_plan
&& !skip_all
) {
441 diag("Looks like your test died before it could output anything.");
447 diag("Looks like your test died just after %d.", test_count
);
452 /* No plan provided, but now we know how many tests were run, and can
453 print the header at the end */
454 if (!skip_all
&& (no_plan
|| !have_plan
)) {
455 printf("1..%d\n", test_count
);
458 if ((have_plan
&& !no_plan
) && e_tests
< test_count
) {
459 diag("Looks like you planned %d %s but ran %d extra.",
461 e_tests
== 1 ? "test" : "tests",
462 test_count
- e_tests
);
467 if ((have_plan
|| !no_plan
) && e_tests
> test_count
) {
468 diag("Looks like you planned %d %s but only ran %d.",
470 e_tests
== 1 ? "test" : "tests",
477 diag("Looks like you failed %d %s of %d.",
479 failures
== 1 ? "test" : "tests",