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);
182 skip(const char *reason, ...)
184 printf("ok %lu # skip", testnum++);
185 if (reason != NULL) {
188 va_start(args, reason);
190 vprintf(reason, args);
198 * Report the same status on the next count tests.
201 ok_block(unsigned long count, int status, const char *format, ...)
205 for (i = 0; i < count; i++) {
206 printf("%sok %lu", status ? "" : "not ", testnum++);
209 if (format != NULL) {
212 va_start(args, format);
213 print_desc(format, args);
222 * Skip the next count tests.
225 skip_block(unsigned long count, const char *reason, ...)
229 for (i = 0; i < count; i++) {
230 printf("ok %lu # skip", testnum++);
231 if (reason != NULL) {
234 va_start(args, reason);
236 vprintf(reason, args);
245 * Takes an expected integer and a seen integer and assumes the test passes
246 * if those two numbers match.
249 is_int(long wanted, long seen, const char *format, ...)
252 printf("ok %lu", testnum++);
254 printf("# wanted: %ld\n# seen: %ld\n", wanted, seen);
255 printf("not ok %lu", testnum++);
258 if (format != NULL) {
261 va_start(args, format);
262 print_desc(format, args);
270 * Takes a string and what the string should be, and assumes the test passes
271 * if those strings match (using strcmp).
274 is_string(const char *wanted, const char *seen, const char *format, ...)
280 if (strcmp(wanted, seen) == 0)
281 printf("ok %lu", testnum++);
283 printf("# wanted: %s\n# seen: %s\n", wanted, seen);
284 printf("not ok %lu", testnum++);
287 if (format != NULL) {
290 va_start(args, format);
291 print_desc(format, args);
299 * Takes an expected double and a seen double and assumes the test passes if
300 * those two numbers match.
303 is_double(double wanted, double seen, const char *format, ...)
306 printf("ok %lu", testnum++);
308 printf("# wanted: %g\n# seen: %g\n", wanted, seen);
309 printf("not ok %lu", testnum++);
312 if (format != NULL) {
315 va_start(args, format);
316 print_desc(format, args);
324 * Takes an expected unsigned long and a seen unsigned long and assumes the
325 * test passes if the two numbers match. Otherwise, reports them in hex.
328 is_hex(unsigned long wanted, unsigned long seen, const char *format, ...)
331 printf("ok %lu", testnum++);
333 printf("# wanted: %lx\n# seen: %lx\n", (unsigned long) wanted,
334 (unsigned long) seen);
335 printf("not ok %lu", testnum++);
338 if (format != NULL) {
341 va_start(args, format);
342 print_desc(format, args);
350 * Bail out with an error.
353 bail(const char *format, ...)
358 printf("Bail out! ");
359 va_start(args, format);
360 vprintf(format, args);
368 * Bail out with an error, appending strerror(errno).
371 sysbail(const char *format, ...)
377 printf("Bail out! ");
378 va_start(args, format);
379 vprintf(format, args);
381 printf(": %s\n", strerror(oerrno));
387 * Report a diagnostic to stderr.
390 diag(const char *format, ...)
396 va_start(args, format);
397 vprintf(format, args);
404 * Report a diagnostic to stderr, appending strerror(errno).
407 sysdiag(const char *format, ...)
414 va_start(args, format);
415 vprintf(format, args);
417 printf(": %s\n", strerror(oerrno));