Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / external / c-tap-harness / tests / tap / basic.h
1 /*
2 * Basic utility routines for the TAP protocol.
3 *
4 * This file is part of C TAP Harness. The current version plus supporting
5 * documentation is at <http://www.eyrie.org/~eagle/software/c-tap-harness/>.
6 *
7 * Copyright 2009, 2010, 2011, 2012 Russ Allbery <rra@stanford.edu>
8 * Copyright 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2011, 2012
9 * The Board of Trustees of the Leland Stanford Junior University
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 */
29
30 #ifndef TAP_BASIC_H
31 #define TAP_BASIC_H 1
32
33 #include <tests/tap/macros.h>
34 #include <stdarg.h> /* va_list */
35 #include <sys/types.h> /* size_t */
36
37 /*
38 * Used for iterating through arrays. ARRAY_SIZE returns the number of
39 * elements in the array (useful for a < upper bound in a for loop) and
40 * ARRAY_END returns a pointer to the element past the end (ISO C99 makes it
41 * legal to refer to such a pointer as long as it's never dereferenced).
42 */
43 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
44 #define ARRAY_END(array) (&(array)[ARRAY_SIZE(array)])
45
46 BEGIN_DECLS
47
48 /*
49 * The test count. Always contains the number that will be used for the next
50 * test status.
51 */
52 extern unsigned long testnum;
53
54 /* Print out the number of tests and set standard output to line buffered. */
55 void plan(unsigned long count);
56
57 /*
58 * Prepare for lazy planning, in which the plan will be printed automatically
59 * at the end of the test program.
60 */
61 void plan_lazy(void);
62
63 /* Skip the entire test suite. Call instead of plan. */
64 void skip_all(const char *format, ...)
65 __attribute__((__noreturn__, __format__(printf, 1, 2)));
66
67 /*
68 * Basic reporting functions. The okv() function is the same as ok() but
69 * takes the test description as a va_list to make it easier to reuse the
70 * reporting infrastructure when writing new tests.
71 */
72 void ok(int success, const char *format, ...)
73 __attribute__((__format__(printf, 2, 3)));
74 void okv(int success, const char *format, va_list args);
75 void skip(const char *reason, ...)
76 __attribute__((__format__(printf, 1, 2)));
77
78 /* Report the same status on, or skip, the next count tests. */
79 void ok_block(unsigned long count, int success, const char *format, ...)
80 __attribute__((__format__(printf, 3, 4)));
81 void skip_block(unsigned long count, const char *reason, ...)
82 __attribute__((__format__(printf, 2, 3)));
83
84 /* Check an expected value against a seen value. */
85 void is_int(long wanted, long seen, const char *format, ...)
86 __attribute__((__format__(printf, 3, 4)));
87 void is_string(const char *wanted, const char *seen, const char *format, ...)
88 __attribute__((__format__(printf, 3, 4)));
89 void is_hex(unsigned long wanted, unsigned long seen, const char *format, ...)
90 __attribute__((__format__(printf, 3, 4)));
91
92 /* Bail out with an error. sysbail appends strerror(errno). */
93 void bail(const char *format, ...)
94 __attribute__((__noreturn__, __nonnull__, __format__(printf, 1, 2)));
95 void sysbail(const char *format, ...)
96 __attribute__((__noreturn__, __nonnull__, __format__(printf, 1, 2)));
97
98 /* Report a diagnostic to stderr prefixed with #. */
99 void diag(const char *format, ...)
100 __attribute__((__nonnull__, __format__(printf, 1, 2)));
101 void sysdiag(const char *format, ...)
102 __attribute__((__nonnull__, __format__(printf, 1, 2)));
103
104 /* Allocate memory, reporting a fatal error with bail on failure. */
105 void *bcalloc(size_t, size_t)
106 __attribute__((__alloc_size__(1, 2), __malloc__));
107 void *bmalloc(size_t)
108 __attribute__((__alloc_size__(1), __malloc__));
109 void *brealloc(void *, size_t)
110 __attribute__((__alloc_size__(2), __malloc__));
111 char *bstrdup(const char *)
112 __attribute__((__malloc__, __nonnull__));
113 char *bstrndup(const char *, size_t)
114 __attribute__((__malloc__, __nonnull__));
115
116 /*
117 * Find a test file under BUILD or SOURCE, returning the full path. The
118 * returned path should be freed with test_file_path_free().
119 */
120 char *test_file_path(const char *file)
121 __attribute__((__malloc__, __nonnull__));
122 void test_file_path_free(char *path);
123
124 /*
125 * Create a temporary directory relative to BUILD and return the path. The
126 * returned path should be freed with test_tmpdir_free.
127 */
128 char *test_tmpdir(void)
129 __attribute__((__malloc__));
130 void test_tmpdir_free(char *path);
131
132 END_DECLS
133
134 #endif /* TAP_BASIC_H */