Add build system, remove dependency on glib, add TAP library
[argpar.git] / tests / tap / tap.c
1 /*-
2 * Copyright (c) 2004 Nik Clayton
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27 #define _GNU_SOURCE
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "tap.h"
34
35 static int no_plan = 0;
36 static int skip_all = 0;
37 static int have_plan = 0;
38 static unsigned int test_count = 0; /* Number of tests that have been run */
39 static unsigned int e_tests = 0; /* Expected number of tests to run */
40 static unsigned int failures = 0; /* Number of tests that failed */
41 static char *todo_msg = NULL;
42 static char *todo_msg_fixed = "libtap malloc issue";
43 static int todo = 0;
44 static int test_died = 0;
45 static const int exit_die = 255; /* exit-code on die() */
46
47
48 /* Encapsulate the pthread code in a conditional. In the absence of
49 libpthread the code does nothing */
50 #if defined(LIBTAP_ENABLE_BROKEN_THREAD_SAFE) && defined(HAVE_LIBPTHREAD)
51 #include <pthread.h>
52 static pthread_mutex_t M = PTHREAD_MUTEX_INITIALIZER;
53 # define LOCK pthread_mutex_lock(&M);
54 # define UNLOCK pthread_mutex_unlock(&M);
55 #else
56 # define LOCK
57 # define UNLOCK
58 #endif
59
60 static void _expected_tests(unsigned int);
61 static void _tap_init(void);
62 static void _cleanup(void);
63
64 /*
65 * Generate a test result.
66 *
67 * ok -- boolean, indicates whether or not the test passed.
68 * test_name -- the name of the test, may be NULL
69 * test_comment -- a comment to print afterwards, may be NULL
70 */
71 unsigned int
72 _gen_result(int ok, const char *func, const char *file, unsigned int line,
73 const char *test_name, ...)
74 {
75 va_list ap;
76 char *local_test_name = NULL;
77 char *c;
78 int name_is_digits;
79
80 LOCK;
81
82 test_count++;
83
84 /* Start by taking the test name and performing any printf()
85 expansions on it */
86 if(test_name != NULL) {
87 va_start(ap, test_name);
88 if (vasprintf(&local_test_name, test_name, ap) < 0)
89 {
90 local_test_name = NULL;
91 }
92 va_end(ap);
93
94 /* Make sure the test name contains more than digits
95 and spaces. Emit an error message and exit if it
96 does */
97 if(local_test_name) {
98 name_is_digits = 1;
99 for(c = local_test_name; *c != '\0'; c++) {
100 if(!isdigit(*c) && !isspace(*c)) {
101 name_is_digits = 0;
102 break;
103 }
104 }
105
106 if(name_is_digits) {
107 diag(" You named your test '%s'. You shouldn't use numbers for your test names.", local_test_name);
108 diag(" Very confusing.");
109 }
110 }
111 }
112
113 if(!ok) {
114 printf("not ");
115 failures++;
116 }
117
118 printf("ok %d", test_count);
119
120 if(test_name != NULL) {
121 printf(" - ");
122
123 /* Print the test name, escaping any '#' characters it
124 might contain */
125 if(local_test_name != NULL) {
126 flockfile(stdout);
127 for(c = local_test_name; *c != '\0'; c++) {
128 if(*c == '#')
129 fputc('\\', stdout);
130 fputc((int)*c, stdout);
131 }
132 funlockfile(stdout);
133 } else { /* vasprintf() failed, use a fixed message */
134 printf("%s", todo_msg_fixed);
135 }
136 }
137
138 /* If we're in a todo_start() block then flag the test as being
139 TODO. todo_msg should contain the message to print at this
140 point. If it's NULL then asprintf() failed, and we should
141 use the fixed message.
142
143 This is not counted as a failure, so decrement the counter if
144 the test failed. */
145 if(todo) {
146 printf(" # TODO %s", todo_msg ? todo_msg : todo_msg_fixed);
147 if(!ok)
148 failures--;
149 }
150
151 printf("\n");
152
153 if(!ok)
154 diag(" Failed %stest (%s:%s() at line %d)",
155 todo ? "(TODO) " : "", file, func, line);
156
157 free(local_test_name);
158
159 UNLOCK;
160
161 /* We only care (when testing) that ok is positive, but here we
162 specifically only want to return 1 or 0 */
163 return ok ? 1 : 0;
164 }
165
166 /*
167 * Initialise the TAP library. Will only do so once, however many times it's
168 * called.
169 */
170 void
171 _tap_init(void)
172 {
173 static int run_once = 0;
174
175 LOCK;
176
177 if(!run_once) {
178 atexit(_cleanup);
179
180 /* stdout needs to be unbuffered so that the output appears
181 in the same place relative to stderr output as it does
182 with Test::Harness */
183 setbuf(stdout, 0);
184 run_once = 1;
185 }
186
187 UNLOCK;
188 }
189
190 /*
191 * Note that there's no plan.
192 */
193 int
194 plan_no_plan(void)
195 {
196
197 LOCK;
198
199 _tap_init();
200
201 if(have_plan != 0) {
202 fprintf(stderr, "You tried to plan twice!\n");
203 test_died = 1;
204 UNLOCK;
205 exit(exit_die);
206 }
207
208 have_plan = 1;
209 no_plan = 1;
210
211 UNLOCK;
212
213 return 1;
214 }
215
216 /*
217 * Note that the plan is to skip all tests
218 */
219 int
220 plan_skip_all(const char *reason)
221 {
222
223 LOCK;
224
225 _tap_init();
226
227 skip_all = 1;
228
229 printf("1..0");
230
231 if(reason != NULL)
232 printf(" # SKIP %s", reason);
233
234 printf("\n");
235
236 UNLOCK;
237
238 exit(0);
239 }
240
241 /*
242 * Note the number of tests that will be run.
243 */
244 int
245 plan_tests(unsigned int tests)
246 {
247
248 LOCK;
249
250 _tap_init();
251
252 if(have_plan != 0) {
253 fprintf(stderr, "You tried to plan twice!\n");
254 test_died = 1;
255 UNLOCK;
256 exit(exit_die);
257 }
258
259 if(tests == 0) {
260 fprintf(stderr, "You said to run 0 tests! You've got to run something.\n");
261 test_died = 1;
262 UNLOCK;
263 exit(exit_die);
264 }
265
266 have_plan = 1;
267
268 _expected_tests(tests);
269
270 UNLOCK;
271
272 return 1;
273 }
274
275 unsigned int
276 diag(const char *fmt, ...)
277 {
278 va_list ap;
279
280 LOCK;
281
282 fputs("# ", stderr);
283
284 va_start(ap, fmt);
285 vfprintf(stderr, fmt, ap);
286 va_end(ap);
287
288 fputs("\n", stderr);
289
290 UNLOCK;
291
292 return 0;
293 }
294
295 void
296 _expected_tests(unsigned int tests)
297 {
298
299 LOCK;
300
301 printf("1..%d\n", tests);
302 e_tests = tests;
303
304 UNLOCK;
305 }
306
307 int
308 skip(unsigned int n, const char *fmt, ...)
309 {
310 va_list ap;
311 char *skip_msg;
312
313 LOCK;
314
315 va_start(ap, fmt);
316 if (vasprintf(&skip_msg, fmt, ap) < 0)
317 {
318 skip_msg = NULL;
319 }
320 va_end(ap);
321
322 while(n-- > 0) {
323 test_count++;
324 printf("ok %d # skip %s\n", test_count,
325 skip_msg != NULL ?
326 skip_msg : "libtap():malloc() failed");
327 }
328
329 free(skip_msg);
330
331 UNLOCK;
332
333 return 1;
334 }
335
336 void
337 todo_start(const char *fmt, ...)
338 {
339 va_list ap;
340
341 LOCK;
342
343 va_start(ap, fmt);
344 if (vasprintf(&todo_msg, fmt, ap) < 0)
345 {
346 todo_msg = NULL;
347 }
348 va_end(ap);
349
350 todo = 1;
351
352 UNLOCK;
353 }
354
355 void
356 todo_end(void)
357 {
358
359 LOCK;
360
361 todo = 0;
362 free(todo_msg);
363
364 UNLOCK;
365 }
366
367 int
368 exit_status(void)
369 {
370 int r;
371
372 LOCK;
373
374 /* If there's no plan, just return the number of failures */
375 if(no_plan || !have_plan) {
376 UNLOCK;
377 return failures;
378 }
379
380 /* Ran too many tests? Return the number of tests that were run
381 that shouldn't have been */
382 if(e_tests < test_count) {
383 r = test_count - e_tests;
384 UNLOCK;
385 return r;
386 }
387
388 /* Return the number of tests that failed + the number of tests
389 that weren't run */
390 r = failures + e_tests - test_count;
391 UNLOCK;
392
393 return r;
394 }
395
396 /*
397 * Cleanup at the end of the run, produce any final output that might be
398 * required.
399 */
400 void
401 _cleanup(void)
402 {
403
404 LOCK;
405
406 /* If plan_no_plan() wasn't called, and we don't have a plan,
407 and we're not skipping everything, then something happened
408 before we could produce any output */
409 if(!no_plan && !have_plan && !skip_all) {
410 diag("Looks like your test died before it could output anything.");
411 UNLOCK;
412 return;
413 }
414
415 if(test_died) {
416 diag("Looks like your test exited with %d just after %d.", exit_die, test_count);
417 UNLOCK;
418 return;
419 }
420
421
422 /* No plan provided, but now we know how many tests were run, and can
423 print the header at the end */
424 if(!skip_all && (no_plan || !have_plan)) {
425 printf("1..%d\n", test_count);
426 }
427
428 if((have_plan && !no_plan) && e_tests < test_count) {
429 diag("Looks like you planned %d test%s but ran %d extra.",
430 e_tests, e_tests == 1 ? "":"s", test_count - e_tests);
431 UNLOCK;
432 return;
433 }
434
435 if((have_plan || !no_plan) && e_tests > test_count) {
436 diag("Looks like you planned %d test%s but ran %d.",
437 e_tests, e_tests == 1 ? "":"s", test_count);
438 UNLOCK;
439 return;
440 }
441
442 if(failures)
443 diag("Looks like you failed %d test%s of %d.",
444 failures, failures == 1 ? "":"s", test_count);
445
446 UNLOCK;
447 }
This page took 0.037646 seconds and 4 git commands to generate.