xref: /freebsd/contrib/atf/atf-c/macros.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25 
26 #if !defined(ATF_C_MACROS_H)
27 #define ATF_C_MACROS_H
28 
29 #include <string.h>
30 
31 #include <atf-c/defs.h>
32 #include <atf-c/error.h>
33 #include <atf-c/tc.h>
34 #include <atf-c/tp.h>
35 #include <atf-c/utils.h>
36 
37 #define ATF_TC_NAME(tc) \
38     (atfu_ ## tc ## _tc)
39 
40 #define ATF_TC_PACK_NAME(tc) \
41     (atfu_ ## tc ## _tc_pack)
42 
43 #define ATF_TC_WITHOUT_HEAD(tc) \
44     static void atfu_ ## tc ## _body(const atf_tc_t *); \
45     static atf_tc_t atfu_ ## tc ## _tc; \
46     static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
47         .m_ident = #tc, \
48         .m_head = NULL, \
49         .m_body = atfu_ ## tc ## _body, \
50         .m_cleanup = NULL, \
51     }
52 
53 #define ATF_TC(tc) \
54     static void atfu_ ## tc ## _head(atf_tc_t *); \
55     static void atfu_ ## tc ## _body(const atf_tc_t *); \
56     static atf_tc_t atfu_ ## tc ## _tc; \
57     static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
58         .m_ident = #tc, \
59         .m_head = atfu_ ## tc ## _head, \
60         .m_body = atfu_ ## tc ## _body, \
61         .m_cleanup = NULL, \
62     }
63 
64 #define ATF_TC_WITH_CLEANUP(tc) \
65     static void atfu_ ## tc ## _head(atf_tc_t *); \
66     static void atfu_ ## tc ## _body(const atf_tc_t *); \
67     static void atfu_ ## tc ## _cleanup(const atf_tc_t *); \
68     static atf_tc_t atfu_ ## tc ## _tc; \
69     static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
70         .m_ident = #tc, \
71         .m_head = atfu_ ## tc ## _head, \
72         .m_body = atfu_ ## tc ## _body, \
73         .m_cleanup = atfu_ ## tc ## _cleanup, \
74     }
75 
76 #define ATF_TC_HEAD(tc, tcptr) \
77     static \
78     void \
79     atfu_ ## tc ## _head(atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
80 
81 #define ATF_TC_HEAD_NAME(tc) \
82     (atfu_ ## tc ## _head)
83 
84 #define ATF_TC_BODY(tc, tcptr) \
85     static \
86     void \
87     atfu_ ## tc ## _body(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
88 
89 #define ATF_TC_BODY_NAME(tc) \
90     (atfu_ ## tc ## _body)
91 
92 #define ATF_TC_CLEANUP(tc, tcptr) \
93     static \
94     void \
95     atfu_ ## tc ## _cleanup(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
96 
97 #define ATF_TC_CLEANUP_NAME(tc) \
98     (atfu_ ## tc ## _cleanup)
99 
100 #define ATF_TP_ADD_TCS(tps) \
101     static atf_error_t atfu_tp_add_tcs(atf_tp_t *); \
102     int atf_tp_main(int, char **, atf_error_t (*)(atf_tp_t *)); \
103     \
104     int \
105     main(int argc, char **argv) \
106     { \
107         return atf_tp_main(argc, argv, atfu_tp_add_tcs); \
108     } \
109     static \
110     atf_error_t \
111     atfu_tp_add_tcs(atf_tp_t *tps)
112 
113 #define ATF_TP_ADD_TC(tp, tc) \
114     do { \
115         atf_error_t atfu_err; \
116         char **atfu_config = atf_tp_get_config(tp); \
117         if (atfu_config == NULL) \
118             return atf_no_memory_error(); \
119         atfu_err = atf_tc_init_pack(&atfu_ ## tc ## _tc, \
120                                     &atfu_ ## tc ## _tc_pack, \
121                                     (const char *const *)atfu_config); \
122         atf_utils_free_charpp(atfu_config); \
123         if (atf_is_error(atfu_err)) \
124             return atfu_err; \
125         atfu_err = atf_tp_add_tc(tp, &atfu_ ## tc ## _tc); \
126         if (atf_is_error(atfu_err)) \
127             return atfu_err; \
128     } while (0)
129 
130 #define ATF_REQUIRE_MSG(expression, fmt, ...) \
131     do { \
132         if (!(expression)) \
133             atf_tc_fail_requirement(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
134     } while(0)
135 
136 #define ATF_CHECK_MSG(expression, fmt, ...) \
137     do { \
138         if (!(expression)) \
139             atf_tc_fail_check(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
140     } while(0)
141 
142 #define ATF_REQUIRE(expression) \
143     do { \
144         if (!(expression)) \
145             atf_tc_fail_requirement(__FILE__, __LINE__, "%s", \
146                                     #expression " not met"); \
147     } while(0)
148 
149 #define ATF_CHECK(expression) \
150     do { \
151         if (!(expression)) \
152             atf_tc_fail_check(__FILE__, __LINE__, "%s", \
153                               #expression " not met"); \
154     } while(0)
155 
156 #define ATF_REQUIRE_EQ(expected, actual) \
157     ATF_REQUIRE_MSG((expected) == (actual), "%s != %s", #expected, #actual)
158 
159 #define ATF_CHECK_EQ(expected, actual) \
160     ATF_CHECK_MSG((expected) == (actual), "%s != %s", #expected, #actual)
161 
162 #define ATF_REQUIRE_EQ_MSG(expected, actual, fmt, ...) \
163     ATF_REQUIRE_MSG((expected) == (actual), "%s != %s: " fmt, \
164                     #expected, #actual, ##__VA_ARGS__)
165 
166 #define ATF_CHECK_EQ_MSG(expected, actual, fmt, ...) \
167     ATF_CHECK_MSG((expected) == (actual), "%s != %s: " fmt, \
168                   #expected, #actual, ##__VA_ARGS__)
169 
170 #define ATF_REQUIRE_STREQ(expected, actual) do {			\
171 	const char *_atf_expected = (expected);				\
172 	const char *_atf_actual = (actual);				\
173 	ATF_REQUIRE_MSG(strcmp(_atf_expected, _atf_actual) == 0,	\
174 	    "%s != %s (%s != %s)", #expected, #actual,			\
175 	    _atf_expected, _atf_actual);				\
176     } while (0)
177 
178 #define ATF_CHECK_STREQ(expected, actual) do {				\
179 	const char *_atf_expected = (expected);				\
180 	const char *_atf_actual = (actual);				\
181 	ATF_CHECK_MSG(strcmp(_atf_expected, _atf_actual) == 0,		\
182 	    "%s != %s (%s != %s)", #expected, #actual,			\
183 	    _atf_expected, _atf_actual);				\
184     } while (0)
185 
186 #define ATF_REQUIRE_STREQ_MSG(expected, actual, fmt, ...) do {		\
187 	const char *_atf_expected = (expected);				\
188 	const char *_atf_actual = (actual);				\
189 	ATF_REQUIRE_MSG(strcmp(_atf_expected, _atf_actual) == 0,	\
190 	    "%s != %s (%s != %s): " fmt, #expected, #actual,		\
191 	    _atf_expected, _atf_actual, ##__VA_ARGS__);			\
192     } while (0)
193 
194 #define ATF_CHECK_STREQ_MSG(expected, actual, fmt, ...) do { 		\
195 	const char *_atf_expected = (expected);				\
196 	const char *_atf_actual = (actual);				\
197 	ATF_CHECK_MSG(strcmp(_atf_expected, _atf_actual) == 0,		\
198 	    "%s != %s (%s != %s): " fmt, #expected, #actual,		\
199 	    _atf_expected, _atf_actual, ##__VA_ARGS__);			\
200     } while (0)
201 
202 #define ATF_REQUIRE_INTEQ(expected, actual) do {			\
203 	intmax_t _atf_expected = (expected);				\
204 	intmax_t _atf_actual = (actual);				\
205 	ATF_REQUIRE_MSG(_atf_expected == _atf_actual,			\
206 	    "%s != %s (%jd != %jd)", #expected, #actual,		\
207 	    _atf_expected, _atf_actual);				\
208     } while (0)
209 
210 #define ATF_CHECK_INTEQ(expected, actual) do {				\
211 	intmax_t _atf_expected = (expected);				\
212 	intmax_t _atf_actual = (actual);				\
213 	ATF_CHECK_MSG(_atf_expected == _atf_actual,			\
214 	    "%s != %s (%jd != %jd)", #expected, #actual,		\
215 	    _atf_expected, _atf_actual);				\
216     } while (0)
217 
218 #define ATF_REQUIRE_INTEQ_MSG(expected, actual, fmt, ...) do {		\
219 	intmax_t _atf_expected = (expected);				\
220 	intmax_t _atf_actual = (actual);				\
221 	ATF_REQUIRE_MSG(_atf_expected == _atf_actual,			\
222 	    "%s != %s (%jd != %jd): " fmt, #expected, #actual,		\
223 	    _atf_expected, _atf_actual, ##__VA_ARGS__);			\
224     } while (0)
225 
226 #define ATF_CHECK_INTEQ_MSG(expected, actual, fmt, ...) do {		\
227 	intmax_t _atf_expected = (expected);				\
228 	intmax_t _atf_actual = (actual);				\
229 	ATF_CHECK_MSG(_atf_expected == _atf_actual,			\
230 	    "%s != %s (%jd != %jd): " fmt, #expected, #actual,		\
231 	    _atf_expected, _atf_actual, ##__VA_ARGS__);			\
232     } while (0)
233 
234 #define ATF_REQUIRE_MATCH(regexp, string) do {				\
235 	const char *_atf_regexp = (regexp);				\
236 	const char *_atf_string = (string);				\
237 	ATF_REQUIRE_MSG(atf_utils_grep_string("%s", _atf_string, _atf_regexp),	\
238 	    "'%s' not matched in '%s'", _atf_regexp, _atf_string);	\
239     } while (0)
240 
241 #define ATF_CHECK_MATCH(regexp, string) do {				\
242 	const char *_atf_regexp = (regexp);				\
243 	const char *_atf_string = (string);				\
244 	ATF_CHECK_MSG(atf_utils_grep_string("%s", _atf_string, _atf_regexp), \
245 	    "'%s' not matched in '%s'", _atf_regexp, _atf_string);	\
246     } while (0)
247 
248 #define ATF_REQUIRE_MATCH_MSG(regexp, string, fmt, ...) do {		\
249 	const char *_atf_regexp = (regexp);				\
250 	const char *_atf_string = (string);				\
251 	ATF_REQUIRE_MSG(atf_utils_grep_string("%s", _atf_string, _atf_regexp),	\
252 	    "'%s' not matched in '%s': " fmt, _atf_regexp, _atf_string,	\
253 	    ##__VA_ARGS__);						\
254     } while (0)
255 
256 #define ATF_CHECK_MATCH_MSG(regexp, string, fmt, ...) do {		\
257 	const char *_atf_regexp = (regexp);				\
258 	const char *_atf_string = (string);				\
259 	ATF_CHECK_MSG(atf_utils_grep_string("%s", _atf_string, _atf_regexp),	\
260 	    "'%s' not matched in '%s': " fmt, _atf_regexp, _atf_string,	\
261 	    ##__VA_ARGS__);						\
262     } while (0)
263 
264 #define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
265     atf_tc_check_errno(__FILE__, __LINE__, (exp_errno), #bool_expr, (bool_expr))
266 
267 #define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
268     atf_tc_require_errno(__FILE__, __LINE__, (exp_errno), #bool_expr, (bool_expr))
269 
270 #endif /* !defined(ATF_C_MACROS_H) */
271