2 * Some utility routines for writing tests.
4 * Herein are a variety of utility routines for writing tests. All routines
5 * of the form ok*() take a test number and some number of appropriate
6 * arguments, check to be sure the results match the expected output using the
7 * arguments, and print out something appropriate for that test number. Other
8 * utility routines help in constructing more complex tests.
10 * Copyright 2009, 2010 Russ Allbery <rra@stanford.edu>
11 * Copyright 2006, 2007, 2008
12 * Board of Trustees, Leland Stanford Jr. University
13 * Copyright (c) 2004, 2005, 2006
14 * by Internet Systems Consortium, Inc. ("ISC")
15 * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
16 * 2002, 2003 by The Internet Software Consortium and Rich Salz
18 * See LICENSE for licensing terms.
26 #include <sys/types.h>
31 #include <tap/basic.h>
34 * The test count. Always contains the number that will be used for the next
37 unsigned long testnum
= 1;
40 * Status information stored so that we can give a test summary at the end of
41 * the test case. We store the planned final test and the count of failures.
42 * We can get the highest test count from testnum.
44 * We also store the PID of the process that called plan() and only summarize
45 * results when that process exits, so as to not misreport results in forked
48 * If _lazy is true, we're doing lazy planning and will print out the plan
49 * based on the last test number at the end of testing.
51 static unsigned long _planned
= 0;
52 static unsigned long _failed
= 0;
53 static pid_t _process
= 0;
58 * Our exit handler. Called on completion of the test to report a summary of
59 * results provided we're still in the original process.
64 unsigned long highest
= testnum
- 1;
66 if (_planned
== 0 && !_lazy
)
68 if (_process
!= 0 && getpid() == _process
) {
70 printf("1..%lu\n", highest
);
73 if (_planned
> highest
)
74 printf("# Looks like you planned %lu test%s but only ran %lu\n",
75 _planned
, (_planned
> 1 ? "s" : ""), highest
);
76 else if (_planned
< highest
)
77 printf("# Looks like you planned %lu test%s but ran %lu extra\n",
78 _planned
, (_planned
> 1 ? "s" : ""), highest
- _planned
);
80 printf("# Looks like you failed %lu test%s of %lu\n", _failed
,
81 (_failed
> 1 ? "s" : ""), _planned
);
82 else if (_planned
> 1)
83 printf("# All %lu tests successful or skipped\n", _planned
);
85 printf("# %lu test successful or skipped\n", _planned
);
91 * Initialize things. Turns on line buffering on stdout and then prints out
92 * the number of tests in the test suite.
95 plan(unsigned long count
)
97 if (setvbuf(stdout
, NULL
, _IOLBF
, BUFSIZ
) != 0)
98 fprintf(stderr
, "# cannot set stdout to line buffered: %s\n",
100 printf("1..%lu\n", count
);
109 * Initialize things for lazy planning, where we'll automatically print out a
110 * plan at the end of the program. Turns on line buffering on stdout as well.
115 if (setvbuf(stdout
, NULL
, _IOLBF
, BUFSIZ
) != 0)
116 fprintf(stderr
, "# cannot set stdout to line buffered: %s\n",
126 * Skip the entire test suite and exits. Should be called instead of plan(),
127 * not after it, since it prints out a special plan line.
130 skip_all(const char *format
, ...)
132 printf("1..0 # skip");
133 if (format
!= NULL
) {
137 va_start(args
, format
);
138 vprintf(format
, args
);
147 * Print the test description.
150 print_desc(const char *format
, va_list args
)
153 vprintf(format
, args
);
158 * Takes a boolean success value and assumes the test passes if that value
159 * is true and fails if that value is false.
162 ok(int success
, const char *format
, ...)
164 printf("%sok %lu", success
? "" : "not ", testnum
++);
167 if (format
!= NULL
) {
170 va_start(args
, format
);
171 print_desc(format
, args
);
179 * Same as ok(), but takes the format arguments as a va_list.
182 okv(int success
, const char *format
, va_list args
)
184 printf("%sok %lu", success
? "" : "not ", testnum
++);
188 print_desc(format
, args
);
197 skip(const char *reason
, ...)
199 printf("ok %lu # skip", testnum
++);
200 if (reason
!= NULL
) {
203 va_start(args
, reason
);
205 vprintf(reason
, args
);
213 * Report the same status on the next count tests.
216 ok_block(unsigned long count
, int status
, const char *format
, ...)
220 for (i
= 0; i
< count
; i
++) {
221 printf("%sok %lu", status
? "" : "not ", testnum
++);
224 if (format
!= NULL
) {
227 va_start(args
, format
);
228 print_desc(format
, args
);
237 * Skip the next count tests.
240 skip_block(unsigned long count
, const char *reason
, ...)
244 for (i
= 0; i
< count
; i
++) {
245 printf("ok %lu # skip", testnum
++);
246 if (reason
!= NULL
) {
249 va_start(args
, reason
);
251 vprintf(reason
, args
);
260 * Takes an expected integer and a seen integer and assumes the test passes
261 * if those two numbers match.
264 is_int(long wanted
, long seen
, const char *format
, ...)
267 printf("ok %lu", testnum
++);
269 printf("# wanted: %ld\n# seen: %ld\n", wanted
, seen
);
270 printf("not ok %lu", testnum
++);
273 if (format
!= NULL
) {
276 va_start(args
, format
);
277 print_desc(format
, args
);
285 * Takes a string and what the string should be, and assumes the test passes
286 * if those strings match (using strcmp).
289 is_string(const char *wanted
, const char *seen
, const char *format
, ...)
295 if (strcmp(wanted
, seen
) == 0)
296 printf("ok %lu", testnum
++);
298 printf("# wanted: %s\n# seen: %s\n", wanted
, seen
);
299 printf("not ok %lu", testnum
++);
302 if (format
!= NULL
) {
305 va_start(args
, format
);
306 print_desc(format
, args
);
314 * Takes an expected double and a seen double and assumes the test passes if
315 * those two numbers match.
318 is_double(double wanted
, double seen
, const char *format
, ...)
321 printf("ok %lu", testnum
++);
323 printf("# wanted: %g\n# seen: %g\n", wanted
, seen
);
324 printf("not ok %lu", testnum
++);
327 if (format
!= NULL
) {
330 va_start(args
, format
);
331 print_desc(format
, args
);
339 * Takes an expected unsigned long and a seen unsigned long and assumes the
340 * test passes if the two numbers match. Otherwise, reports them in hex.
343 is_hex(unsigned long wanted
, unsigned long seen
, const char *format
, ...)
346 printf("ok %lu", testnum
++);
348 printf("# wanted: %lx\n# seen: %lx\n", (unsigned long) wanted
,
349 (unsigned long) seen
);
350 printf("not ok %lu", testnum
++);
353 if (format
!= NULL
) {
356 va_start(args
, format
);
357 print_desc(format
, args
);
365 * Bail out with an error.
368 bail(const char *format
, ...)
373 printf("Bail out! ");
374 va_start(args
, format
);
375 vprintf(format
, args
);
383 * Bail out with an error, appending strerror(errno).
386 sysbail(const char *format
, ...)
392 printf("Bail out! ");
393 va_start(args
, format
);
394 vprintf(format
, args
);
396 printf(": %s\n", strerror(oerrno
));
402 * Report a diagnostic to stderr.
405 diag(const char *format
, ...)
411 va_start(args
, format
);
412 vprintf(format
, args
);
419 * Report a diagnostic to stderr, appending strerror(errno).
422 sysdiag(const char *format
, ...)
429 va_start(args
, format
);
430 vprintf(format
, args
);
432 printf(": %s\n", strerror(oerrno
));