| 1 | /* |
|---|
| 2 | * Some utility routines for writing tests. |
|---|
| 3 | * |
|---|
| 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. |
|---|
| 9 | * |
|---|
| 10 | * Copyright 2009 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 |
|---|
| 17 | * |
|---|
| 18 | * See LICENSE for licensing terms. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include <errno.h> |
|---|
| 22 | #include <stdarg.h> |
|---|
| 23 | #include <stdio.h> |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | #include <sys/time.h> |
|---|
| 27 | #include <sys/wait.h> |
|---|
| 28 | |
|---|
| 29 | #include <tap/basic.h> |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | /* |
|---|
| 33 | * The test count. Always contains the number that will be used for the next |
|---|
| 34 | * test status. |
|---|
| 35 | */ |
|---|
| 36 | int testnum = 1; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /* |
|---|
| 40 | * Initialize things. Turns on line buffering on stdout and then prints out |
|---|
| 41 | * the number of tests in the test suite. |
|---|
| 42 | */ |
|---|
| 43 | void |
|---|
| 44 | plan(int count) |
|---|
| 45 | { |
|---|
| 46 | if (setvbuf(stdout, NULL, _IOLBF, BUFSIZ) != 0) |
|---|
| 47 | fprintf(stderr, "cannot set stdout to line buffered: %s\n", |
|---|
| 48 | strerror(errno)); |
|---|
| 49 | printf("1..%d\n", count); |
|---|
| 50 | testnum = 1; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | /* |
|---|
| 55 | * Skip the entire test suite and exits. Should be called instead of plan(), |
|---|
| 56 | * not after it, since it prints out a special plan line. |
|---|
| 57 | */ |
|---|
| 58 | void |
|---|
| 59 | skip_all(const char *format, ...) |
|---|
| 60 | { |
|---|
| 61 | printf("1..0 # skip"); |
|---|
| 62 | if (format != NULL) { |
|---|
| 63 | va_list args; |
|---|
| 64 | |
|---|
| 65 | putchar(' '); |
|---|
| 66 | va_start(args, format); |
|---|
| 67 | vprintf(format, args); |
|---|
| 68 | va_end(args); |
|---|
| 69 | } |
|---|
| 70 | putchar('\n'); |
|---|
| 71 | exit(0); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /* |
|---|
| 76 | * Print the test description. |
|---|
| 77 | */ |
|---|
| 78 | static void |
|---|
| 79 | print_desc(const char *format, va_list args) |
|---|
| 80 | { |
|---|
| 81 | printf(" - "); |
|---|
| 82 | vprintf(format, args); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | /* |
|---|
| 87 | * Takes a boolean success value and assumes the test passes if that value |
|---|
| 88 | * is true and fails if that value is false. |
|---|
| 89 | */ |
|---|
| 90 | void |
|---|
| 91 | ok(int success, const char *format, ...) |
|---|
| 92 | { |
|---|
| 93 | printf("%sok %d", success ? "" : "not ", testnum++); |
|---|
| 94 | if (format != NULL) { |
|---|
| 95 | va_list args; |
|---|
| 96 | |
|---|
| 97 | va_start(args, format); |
|---|
| 98 | print_desc(format, args); |
|---|
| 99 | va_end(args); |
|---|
| 100 | } |
|---|
| 101 | putchar('\n'); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /* |
|---|
| 106 | * Skip a test. |
|---|
| 107 | */ |
|---|
| 108 | void |
|---|
| 109 | skip(const char *reason, ...) |
|---|
| 110 | { |
|---|
| 111 | printf("ok %d # skip", testnum++); |
|---|
| 112 | if (reason != NULL) { |
|---|
| 113 | va_list args; |
|---|
| 114 | |
|---|
| 115 | va_start(args, reason); |
|---|
| 116 | putchar(' '); |
|---|
| 117 | vprintf(reason, args); |
|---|
| 118 | va_end(args); |
|---|
| 119 | } |
|---|
| 120 | putchar('\n'); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | /* |
|---|
| 125 | * Report the same status on the next count tests. |
|---|
| 126 | */ |
|---|
| 127 | void |
|---|
| 128 | ok_block(int count, int status, const char *format, ...) |
|---|
| 129 | { |
|---|
| 130 | int i; |
|---|
| 131 | |
|---|
| 132 | for (i = 0; i < count; i++) { |
|---|
| 133 | printf("%sok %d", status ? "" : "not ", testnum++); |
|---|
| 134 | if (format != NULL) { |
|---|
| 135 | va_list args; |
|---|
| 136 | |
|---|
| 137 | va_start(args, format); |
|---|
| 138 | print_desc(format, args); |
|---|
| 139 | va_end(args); |
|---|
| 140 | } |
|---|
| 141 | putchar('\n'); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | /* |
|---|
| 147 | * Skip the next count tests. |
|---|
| 148 | */ |
|---|
| 149 | void |
|---|
| 150 | skip_block(int count, const char *reason, ...) |
|---|
| 151 | { |
|---|
| 152 | int i; |
|---|
| 153 | |
|---|
| 154 | for (i = 0; i < count; i++) { |
|---|
| 155 | printf("ok %d # skip", testnum++); |
|---|
| 156 | if (reason != NULL) { |
|---|
| 157 | va_list args; |
|---|
| 158 | |
|---|
| 159 | va_start(args, reason); |
|---|
| 160 | putchar(' '); |
|---|
| 161 | vprintf(reason, args); |
|---|
| 162 | va_end(args); |
|---|
| 163 | } |
|---|
| 164 | putchar('\n'); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | /* |
|---|
| 170 | * Takes an expected integer and a seen integer and assumes the test passes |
|---|
| 171 | * if those two numbers match. |
|---|
| 172 | */ |
|---|
| 173 | void |
|---|
| 174 | is_int(int wanted, int seen, const char *format, ...) |
|---|
| 175 | { |
|---|
| 176 | if (wanted == seen) |
|---|
| 177 | printf("ok %d", testnum++); |
|---|
| 178 | else { |
|---|
| 179 | printf("# wanted: %d\n# seen: %d\n", wanted, seen); |
|---|
| 180 | printf("not ok %d", testnum++); |
|---|
| 181 | } |
|---|
| 182 | if (format != NULL) { |
|---|
| 183 | va_list args; |
|---|
| 184 | |
|---|
| 185 | va_start(args, format); |
|---|
| 186 | print_desc(format, args); |
|---|
| 187 | va_end(args); |
|---|
| 188 | } |
|---|
| 189 | putchar('\n'); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | /* |
|---|
| 194 | * Takes a string and what the string should be, and assumes the test passes |
|---|
| 195 | * if those strings match (using strcmp). |
|---|
| 196 | */ |
|---|
| 197 | void |
|---|
| 198 | is_string(const char *wanted, const char *seen, const char *format, ...) |
|---|
| 199 | { |
|---|
| 200 | if (wanted == NULL) |
|---|
| 201 | wanted = "(null)"; |
|---|
| 202 | if (seen == NULL) |
|---|
| 203 | seen = "(null)"; |
|---|
| 204 | if (strcmp(wanted, seen) == 0) |
|---|
| 205 | printf("ok %d", testnum++); |
|---|
| 206 | else { |
|---|
| 207 | printf("# wanted: %s\n# seen: %s\n", wanted, seen); |
|---|
| 208 | printf("not ok %d", testnum++); |
|---|
| 209 | } |
|---|
| 210 | if (format != NULL) { |
|---|
| 211 | va_list args; |
|---|
| 212 | |
|---|
| 213 | va_start(args, format); |
|---|
| 214 | print_desc(format, args); |
|---|
| 215 | va_end(args); |
|---|
| 216 | } |
|---|
| 217 | putchar('\n'); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | /* |
|---|
| 222 | * Takes an expected integer and a seen integer and assumes the test passes if |
|---|
| 223 | * those two numbers match. |
|---|
| 224 | */ |
|---|
| 225 | void |
|---|
| 226 | is_double(double wanted, double seen, const char *format, ...) |
|---|
| 227 | { |
|---|
| 228 | if (wanted == seen) |
|---|
| 229 | printf("ok %d", testnum++); |
|---|
| 230 | else { |
|---|
| 231 | printf("# wanted: %g\n# seen: %g\n", wanted, seen); |
|---|
| 232 | printf("not ok %d", testnum++); |
|---|
| 233 | } |
|---|
| 234 | if (format != NULL) { |
|---|
| 235 | va_list args; |
|---|
| 236 | |
|---|
| 237 | va_start(args, format); |
|---|
| 238 | print_desc(format, args); |
|---|
| 239 | va_end(args); |
|---|
| 240 | } |
|---|
| 241 | putchar('\n'); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | /* |
|---|
| 246 | * Takes an expected unsigned long and a seen unsigned long and assumes the |
|---|
| 247 | * test passes if the two numbers match. Otherwise, reports them in hex. |
|---|
| 248 | */ |
|---|
| 249 | void |
|---|
| 250 | is_hex(unsigned long wanted, unsigned long seen, const char *format, ...) |
|---|
| 251 | { |
|---|
| 252 | if (wanted == seen) |
|---|
| 253 | printf("ok %d", testnum++); |
|---|
| 254 | else { |
|---|
| 255 | printf("# wanted: %lx\n# seen: %lx\n", (unsigned long) wanted, |
|---|
| 256 | (unsigned long) seen); |
|---|
| 257 | printf("not ok %d", testnum++); |
|---|
| 258 | } |
|---|
| 259 | if (format != NULL) { |
|---|
| 260 | va_list args; |
|---|
| 261 | |
|---|
| 262 | va_start(args, format); |
|---|
| 263 | print_desc(format, args); |
|---|
| 264 | va_end(args); |
|---|
| 265 | } |
|---|
| 266 | putchar('\n'); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | /* |
|---|
| 271 | * Bail out with an error. |
|---|
| 272 | */ |
|---|
| 273 | void |
|---|
| 274 | bail(const char *format, ...) |
|---|
| 275 | { |
|---|
| 276 | va_list args; |
|---|
| 277 | |
|---|
| 278 | fflush(stdout); |
|---|
| 279 | printf("Bail out! "); |
|---|
| 280 | va_start(args, format); |
|---|
| 281 | vprintf(format, args); |
|---|
| 282 | va_end(args); |
|---|
| 283 | printf("\n"); |
|---|
| 284 | exit(1); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | /* |
|---|
| 289 | * Bail out with an error, appending strerror(errno). |
|---|
| 290 | */ |
|---|
| 291 | void |
|---|
| 292 | sysbail(const char *format, ...) |
|---|
| 293 | { |
|---|
| 294 | va_list args; |
|---|
| 295 | int oerrno = errno; |
|---|
| 296 | |
|---|
| 297 | fflush(stdout); |
|---|
| 298 | printf("Bail out! "); |
|---|
| 299 | va_start(args, format); |
|---|
| 300 | vprintf(format, args); |
|---|
| 301 | va_end(args); |
|---|
| 302 | printf(": %s\n", strerror(oerrno)); |
|---|
| 303 | exit(1); |
|---|
| 304 | } |
|---|