xref: /freebsd/contrib/atf/atf-c/macros.h (revision ab0b9f6b3073e6c4d1dfbf07444d7db67a189a96)
1 /*
2  * Automated Testing Framework (atf)
3  *
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
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 NETBSD FOUNDATION, INC. AND
17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #if !defined(ATF_C_MACROS_H)
31 #define ATF_C_MACROS_H
32 
33 #include <string.h>
34 
35 #include <atf-c/defs.h>
36 #include <atf-c/error.h>
37 #include <atf-c/tc.h>
38 #include <atf-c/tp.h>
39 #include <atf-c/utils.h>
40 
41 #define ATF_TC_NAME(tc) \
42     (atfu_ ## tc ## _tc)
43 
44 #define ATF_TC_PACK_NAME(tc) \
45     (atfu_ ## tc ## _tc_pack)
46 
47 #define ATF_TC_WITHOUT_HEAD(tc) \
48     static void atfu_ ## tc ## _body(const atf_tc_t *); \
49     static atf_tc_t atfu_ ## tc ## _tc; \
50     static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
51         .m_ident = #tc, \
52         .m_head = NULL, \
53         .m_body = atfu_ ## tc ## _body, \
54         .m_cleanup = NULL, \
55     }
56 
57 #define ATF_TC(tc) \
58     static void atfu_ ## tc ## _head(atf_tc_t *); \
59     static void atfu_ ## tc ## _body(const atf_tc_t *); \
60     static atf_tc_t atfu_ ## tc ## _tc; \
61     static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
62         .m_ident = #tc, \
63         .m_head = atfu_ ## tc ## _head, \
64         .m_body = atfu_ ## tc ## _body, \
65         .m_cleanup = NULL, \
66     }
67 
68 #define ATF_TC_WITH_CLEANUP(tc) \
69     static void atfu_ ## tc ## _head(atf_tc_t *); \
70     static void atfu_ ## tc ## _body(const atf_tc_t *); \
71     static void atfu_ ## tc ## _cleanup(const atf_tc_t *); \
72     static atf_tc_t atfu_ ## tc ## _tc; \
73     static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
74         .m_ident = #tc, \
75         .m_head = atfu_ ## tc ## _head, \
76         .m_body = atfu_ ## tc ## _body, \
77         .m_cleanup = atfu_ ## tc ## _cleanup, \
78     }
79 
80 #define ATF_TC_HEAD(tc, tcptr) \
81     static \
82     void \
83     atfu_ ## tc ## _head(atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
84 
85 #define ATF_TC_HEAD_NAME(tc) \
86     (atfu_ ## tc ## _head)
87 
88 #define ATF_TC_BODY(tc, tcptr) \
89     static \
90     void \
91     atfu_ ## tc ## _body(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
92 
93 #define ATF_TC_BODY_NAME(tc) \
94     (atfu_ ## tc ## _body)
95 
96 #define ATF_TC_CLEANUP(tc, tcptr) \
97     static \
98     void \
99     atfu_ ## tc ## _cleanup(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
100 
101 #define ATF_TC_CLEANUP_NAME(tc) \
102     (atfu_ ## tc ## _cleanup)
103 
104 #define ATF_TP_ADD_TCS(tps) \
105     static atf_error_t atfu_tp_add_tcs(atf_tp_t *); \
106     int atf_tp_main(int, char **, atf_error_t (*)(atf_tp_t *)); \
107     \
108     int \
109     main(int argc, char **argv) \
110     { \
111         return atf_tp_main(argc, argv, atfu_tp_add_tcs); \
112     } \
113     static \
114     atf_error_t \
115     atfu_tp_add_tcs(atf_tp_t *tps)
116 
117 #define ATF_TP_ADD_TC(tp, tc) \
118     do { \
119         atf_error_t atfu_err; \
120         char **atfu_config = atf_tp_get_config(tp); \
121         if (atfu_config == NULL) \
122             return atf_no_memory_error(); \
123         atfu_err = atf_tc_init_pack(&atfu_ ## tc ## _tc, \
124                                     &atfu_ ## tc ## _tc_pack, \
125                                     (const char *const *)atfu_config); \
126         atf_utils_free_charpp(atfu_config); \
127         if (atf_is_error(atfu_err)) \
128             return atfu_err; \
129         atfu_err = atf_tp_add_tc(tp, &atfu_ ## tc ## _tc); \
130         if (atf_is_error(atfu_err)) \
131             return atfu_err; \
132     } while (0)
133 
134 #define ATF_REQUIRE_MSG(x, fmt, ...) \
135     do { \
136         if (!(x)) \
137             atf_tc_fail_requirement(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
138     } while(0)
139 
140 #define ATF_CHECK_MSG(x, fmt, ...) \
141     do { \
142         if (!(x)) \
143             atf_tc_fail_check(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
144     } while(0)
145 
146 #define ATF_REQUIRE(x) \
147     do { \
148         if (!(x)) \
149             atf_tc_fail_requirement(__FILE__, __LINE__, "%s", #x " not met"); \
150     } while(0)
151 
152 #define ATF_CHECK(x) \
153     do { \
154         if (!(x)) \
155             atf_tc_fail_check(__FILE__, __LINE__, "%s", #x " not met"); \
156     } while(0)
157 
158 #define ATF_REQUIRE_EQ(x, y) \
159     ATF_REQUIRE_MSG((x) == (y), "%s != %s", #x, #y)
160 
161 #define ATF_CHECK_EQ(x, y) \
162     ATF_CHECK_MSG((x) == (y), "%s != %s", #x, #y)
163 
164 #define ATF_REQUIRE_EQ_MSG(x, y, fmt, ...) \
165     ATF_REQUIRE_MSG((x) == (y), "%s != %s: " fmt, #x, #y, ##__VA_ARGS__)
166 
167 #define ATF_CHECK_EQ_MSG(x, y, fmt, ...) \
168     ATF_CHECK_MSG((x) == (y), "%s != %s: " fmt, #x, #y, ##__VA_ARGS__)
169 
170 #define ATF_REQUIRE_STREQ(x, y) \
171     ATF_REQUIRE_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s)", #x, #y, x, y)
172 
173 #define ATF_CHECK_STREQ(x, y) \
174     ATF_CHECK_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s)", #x, #y, x, y)
175 
176 #define ATF_REQUIRE_STREQ_MSG(x, y, fmt, ...) \
177     ATF_REQUIRE_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s): " fmt, \
178                     #x, #y, x, y, ##__VA_ARGS__)
179 
180 #define ATF_CHECK_STREQ_MSG(x, y, fmt, ...) \
181     ATF_CHECK_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s): " fmt, \
182                     #x, #y, x, y, ##__VA_ARGS__)
183 
184 #define ATF_REQUIRE_MATCH(regexp, string) \
185     ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \
186                     "'%s' not matched in '%s'", regexp, string);
187 
188 #define ATF_CHECK_MATCH(regexp, string) \
189     ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \
190                   "'%s' not matched in '%s'", regexp, string);
191 
192 #define ATF_REQUIRE_MATCH_MSG(regexp, string, fmt, ...) \
193     ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \
194                     "'%s' not matched in '%s': " fmt, regexp, string, \
195                     ##__VA_ARGS__);
196 
197 #define ATF_CHECK_MATCH_MSG(regexp, string, fmt, ...) \
198     ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \
199                   "'%s' not matched in '%s': " fmt, regexp, string, \
200                   ##__VA_ARGS__);
201 
202 #define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
203     atf_tc_check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
204 
205 #define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
206     atf_tc_require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
207 
208 #endif /* !defined(ATF_C_MACROS_H) */
209