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