xref: /freebsd/contrib/atf/atf-c/macros.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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) \
171     ATF_REQUIRE_MSG(strcmp(expected, actual) == 0, "%s != %s (%s != %s)", \
172                     #expected, #actual, expected, actual)
173 
174 #define ATF_CHECK_STREQ(expected, actual) \
175     ATF_CHECK_MSG(strcmp(expected, actual) == 0, "%s != %s (%s != %s)", \
176                   #expected, #actual, expected, actual)
177 
178 #define ATF_REQUIRE_STREQ_MSG(expected, actual, fmt, ...) \
179     ATF_REQUIRE_MSG(strcmp(expected, actual) == 0, \
180                     "%s != %s (%s != %s): " fmt, \
181                     #expected, #actual, expected, actual, ##__VA_ARGS__)
182 
183 #define ATF_CHECK_STREQ_MSG(expected, actual, fmt, ...) \
184     ATF_CHECK_MSG(strcmp(expected, actual) == 0, \
185                   "%s != %s (%s != %s): " fmt, \
186                   #expected, #actual, expected, actual, ##__VA_ARGS__)
187 
188 #define ATF_REQUIRE_INTEQ(expected, actual) \
189     ATF_REQUIRE_MSG((expected) == (actual), "%s != %s (%jd != %jd)", \
190                     #expected, #actual, (intmax_t)(expected),        \
191                     (intmax_t)(actual))
192 
193 #define ATF_CHECK_INTEQ(expected, actual) \
194     ATF_CHECK_MSG((expected) == (actual), "%s != %s (%jd != %jd)", #expected, \
195                   #actual, (intmax_t)(expected), (intmax_t)(actual))
196 
197 #define ATF_REQUIRE_INTEQ_MSG(expected, actual, fmt, ...) \
198     ATF_REQUIRE_MSG((expected) == (actual), "%s != %s (%jd != %jd): " fmt, \
199                     #expected, #actual, (intmax_t)(expected), \
200                     (intmax_t)(actual), ##__VA_ARGS__)
201 
202 #define ATF_CHECK_INTEQ_MSG(expected, actual, fmt, ...) \
203     ATF_CHECK_MSG((expected) == (actual), "%s != %s (%jd != %jd): " fmt, \
204                   #expected, #actual, (intmax_t)(expected), \
205                   (intmax_t)(actual), ##__VA_ARGS__)
206 
207 #define ATF_REQUIRE_MATCH(regexp, string) \
208     ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \
209                     "'%s' not matched in '%s'", regexp, string);
210 
211 #define ATF_CHECK_MATCH(regexp, string) \
212     ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \
213                   "'%s' not matched in '%s'", regexp, string);
214 
215 #define ATF_REQUIRE_MATCH_MSG(regexp, string, fmt, ...) \
216     ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \
217                     "'%s' not matched in '%s': " fmt, regexp, string, \
218                     ##__VA_ARGS__);
219 
220 #define ATF_CHECK_MATCH_MSG(regexp, string, fmt, ...) \
221     ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \
222                   "'%s' not matched in '%s': " fmt, regexp, string, \
223                   ##__VA_ARGS__);
224 
225 #define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
226     atf_tc_check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
227 
228 #define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
229     atf_tc_require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
230 
231 #endif /* !defined(ATF_C_MACROS_H) */
232