2 * Copyright (c) 2004 Nik Clayton
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 /* FIXME: The real fix is an asprintf module. */
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 const char *todo_msg_fixed = "libtap malloc issue";
47 static int test_died = 0;
50 /* Encapsulate the pthread code in a conditional. In the absence of
51 libpthread the code does nothing */
52 #ifdef HAVE_LIBPTHREAD
54 static pthread_mutex_t M = PTHREAD_MUTEX_INITIALIZER;
55 # define LOCK pthread_mutex_lock(&M)
56 # define UNLOCK pthread_mutex_unlock(&M)
63 _expected_tests(unsigned int tests)
66 printf("1..%d\n", tests);
71 diagv(const char *fmt, va_list ap)
74 vfprintf(stdout, fmt, ap);
79 _diag(const char *fmt, ...)
88 * Generate a test result.
90 * ok -- boolean, indicates whether or not the test passed.
91 * test_name -- the name of the test, may be NULL
92 * test_comment -- a comment to print afterwards, may be NULL
95 _gen_result(int ok, const char *func, const char *file, unsigned int line,
96 const char *test_name, ...)
99 char *local_test_name = NULL;
107 /* Start by taking the test name and performing any printf()
109 if(test_name != NULL) {
110 va_start(ap, test_name);
111 if (vasprintf(&local_test_name, test_name, ap) < 0)
112 local_test_name = NULL;
115 /* Make sure the test name contains more than digits
116 and spaces. Emit an error message and exit if it
118 if(local_test_name) {
120 for(c = local_test_name; *c != '\0'; c++) {
121 if(!isdigit(*c) && !isspace(*c)) {
128 _diag(" You named your test '%s'. You shouldn't use numbers for your test names.", local_test_name);
129 _diag(" Very confusing.");
139 printf("ok %d", test_count);
141 if(test_name != NULL) {
144 /* Print the test name, escaping any '#' characters it
146 if(local_test_name != NULL) {
148 for(c = local_test_name; *c != '\0'; c++) {
151 fputc((int)*c, stdout);
154 } else { /* vasprintf() failed, use a fixed message */
155 printf("%s", todo_msg_fixed);
159 /* If we're in a todo_start() block then flag the test as being
160 TODO. todo_msg should contain the message to print at this
161 point. If it's NULL then asprintf() failed, and we should
162 use the fixed message.
164 This is not counted as a failure, so decrement the counter if
167 printf(" # TODO %s", todo_msg ? todo_msg : todo_msg_fixed);
175 _diag(" Failed %stest (%s:%s() at line %d)",
176 todo ? "(TODO) " : "", file, func, line);
178 free(local_test_name);
182 /* We only care (when testing) that ok is positive, but here we
183 specifically only want to return 1 or 0 */
188 * Cleanup at the end of the run, produce any final output that might be
194 /* If we forked, don't do cleanup in child! */
195 if (getpid() != test_pid)
200 /* If plan_no_plan() wasn't called, and we don't have a plan,
201 and we're not skipping everything, then something happened
202 before we could produce any output */
203 if(!no_plan && !have_plan && !skip_all) {
204 _diag("Looks like your test died before it could output anything.");
210 _diag("Looks like your test died just after %d.", test_count);
216 /* No plan provided, but now we know how many tests were run, and can
217 print the header at the end */
218 if(!skip_all && (no_plan || !have_plan)) {
219 printf("1..%d\n", test_count);
222 if((have_plan && !no_plan) && e_tests < test_count) {
223 _diag("Looks like you planned %d tests but ran %d extra.",
224 e_tests, test_count - e_tests);
229 if((have_plan || !no_plan) && e_tests > test_count) {
230 _diag("Looks like you planned %d tests but only ran %d.",
231 e_tests, test_count);
233 _diag("Looks like you failed %d tests of %d run.",
234 failures, test_count);
241 _diag("Looks like you failed %d tests of %d.",
242 failures, test_count);
248 * Initialise the TAP library. Will only do so once, however many times it's
254 static int run_once = 0;
260 /* stdout needs to be unbuffered so that the output appears
261 in the same place relative to stderr output as it does
262 with Test::Harness */
263 // setbuf(stdout, 0);
269 * Note that there's no plan.
280 fprintf(stderr, "You tried to plan twice!\n");
293 * Note that the plan is to skip all tests
296 plan_skip_all(const char *reason)
308 printf(" # Skip %s", reason);
316 * Note the number of tests that will be run.
319 plan_tests(unsigned int tests)
327 fprintf(stderr, "You tried to plan twice!\n");
334 fprintf(stderr, "You said to run 0 tests! You've got to run something.\n");
342 _expected_tests(tests);
348 diag(const char *fmt, ...)
362 skip(unsigned int n, const char *fmt, ...)
370 if (vasprintf(&skip_msg, fmt, ap) < 0)
376 printf("ok %d # skip %s\n", test_count,
378 skip_msg : "libtap():malloc() failed");
387 todo_start(const char *fmt, ...)
394 if (vasprintf(&todo_msg, fmt, ap) < 0)
422 /* If there's no plan, just return the number of failures */
423 if(no_plan || !have_plan) {
428 /* Ran too many tests? Return the number of tests that were run
429 that shouldn't have been */
430 if(e_tests < test_count) {
431 r = test_count - e_tests;
436 /* Return the number of tests that failed + the number of tests
438 r = failures + e_tests - test_count;