1275988Sngie/* Copyright (c) 2008 The NetBSD Foundation, Inc.
2240116Smarcel * All rights reserved.
3240116Smarcel *
4240116Smarcel * Redistribution and use in source and binary forms, with or without
5240116Smarcel * modification, are permitted provided that the following conditions
6240116Smarcel * are met:
7240116Smarcel * 1. Redistributions of source code must retain the above copyright
8240116Smarcel *    notice, this list of conditions and the following disclaimer.
9240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
10240116Smarcel *    notice, this list of conditions and the following disclaimer in the
11240116Smarcel *    documentation and/or other materials provided with the distribution.
12240116Smarcel *
13240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24275988Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25240116Smarcel
26240116Smarcel#if !defined(ATF_C_MACROS_H)
27240116Smarcel#define ATF_C_MACROS_H
28240116Smarcel
29243054Smarcel#include <string.h>
30243054Smarcel
31240116Smarcel#include <atf-c/defs.h>
32240116Smarcel#include <atf-c/error.h>
33240116Smarcel#include <atf-c/tc.h>
34240116Smarcel#include <atf-c/tp.h>
35240116Smarcel#include <atf-c/utils.h>
36240116Smarcel
37240116Smarcel#define ATF_TC_NAME(tc) \
38240116Smarcel    (atfu_ ## tc ## _tc)
39240116Smarcel
40240116Smarcel#define ATF_TC_PACK_NAME(tc) \
41240116Smarcel    (atfu_ ## tc ## _tc_pack)
42240116Smarcel
43240116Smarcel#define ATF_TC_WITHOUT_HEAD(tc) \
44240116Smarcel    static void atfu_ ## tc ## _body(const atf_tc_t *); \
45240116Smarcel    static atf_tc_t atfu_ ## tc ## _tc; \
46240116Smarcel    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
47240116Smarcel        .m_ident = #tc, \
48240116Smarcel        .m_head = NULL, \
49240116Smarcel        .m_body = atfu_ ## tc ## _body, \
50240116Smarcel        .m_cleanup = NULL, \
51240116Smarcel    }
52240116Smarcel
53240116Smarcel#define ATF_TC(tc) \
54240116Smarcel    static void atfu_ ## tc ## _head(atf_tc_t *); \
55240116Smarcel    static void atfu_ ## tc ## _body(const atf_tc_t *); \
56240116Smarcel    static atf_tc_t atfu_ ## tc ## _tc; \
57240116Smarcel    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
58240116Smarcel        .m_ident = #tc, \
59240116Smarcel        .m_head = atfu_ ## tc ## _head, \
60240116Smarcel        .m_body = atfu_ ## tc ## _body, \
61240116Smarcel        .m_cleanup = NULL, \
62240116Smarcel    }
63240116Smarcel
64240116Smarcel#define ATF_TC_WITH_CLEANUP(tc) \
65240116Smarcel    static void atfu_ ## tc ## _head(atf_tc_t *); \
66240116Smarcel    static void atfu_ ## tc ## _body(const atf_tc_t *); \
67240116Smarcel    static void atfu_ ## tc ## _cleanup(const atf_tc_t *); \
68240116Smarcel    static atf_tc_t atfu_ ## tc ## _tc; \
69240116Smarcel    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
70240116Smarcel        .m_ident = #tc, \
71240116Smarcel        .m_head = atfu_ ## tc ## _head, \
72240116Smarcel        .m_body = atfu_ ## tc ## _body, \
73240116Smarcel        .m_cleanup = atfu_ ## tc ## _cleanup, \
74240116Smarcel    }
75240116Smarcel
76240116Smarcel#define ATF_TC_HEAD(tc, tcptr) \
77240116Smarcel    static \
78240116Smarcel    void \
79260029Sjmmv    atfu_ ## tc ## _head(atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
80240116Smarcel
81240116Smarcel#define ATF_TC_HEAD_NAME(tc) \
82240116Smarcel    (atfu_ ## tc ## _head)
83240116Smarcel
84240116Smarcel#define ATF_TC_BODY(tc, tcptr) \
85240116Smarcel    static \
86240116Smarcel    void \
87240116Smarcel    atfu_ ## tc ## _body(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
88240116Smarcel
89240116Smarcel#define ATF_TC_BODY_NAME(tc) \
90240116Smarcel    (atfu_ ## tc ## _body)
91240116Smarcel
92240116Smarcel#define ATF_TC_CLEANUP(tc, tcptr) \
93240116Smarcel    static \
94240116Smarcel    void \
95240116Smarcel    atfu_ ## tc ## _cleanup(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
96240116Smarcel
97240116Smarcel#define ATF_TC_CLEANUP_NAME(tc) \
98240116Smarcel    (atfu_ ## tc ## _cleanup)
99240116Smarcel
100240116Smarcel#define ATF_TP_ADD_TCS(tps) \
101240116Smarcel    static atf_error_t atfu_tp_add_tcs(atf_tp_t *); \
102240116Smarcel    int atf_tp_main(int, char **, atf_error_t (*)(atf_tp_t *)); \
103240116Smarcel    \
104240116Smarcel    int \
105240116Smarcel    main(int argc, char **argv) \
106240116Smarcel    { \
107240116Smarcel        return atf_tp_main(argc, argv, atfu_tp_add_tcs); \
108240116Smarcel    } \
109240116Smarcel    static \
110240116Smarcel    atf_error_t \
111240116Smarcel    atfu_tp_add_tcs(atf_tp_t *tps)
112240116Smarcel
113240116Smarcel#define ATF_TP_ADD_TC(tp, tc) \
114240116Smarcel    do { \
115240116Smarcel        atf_error_t atfu_err; \
116240116Smarcel        char **atfu_config = atf_tp_get_config(tp); \
117240116Smarcel        if (atfu_config == NULL) \
118240116Smarcel            return atf_no_memory_error(); \
119240116Smarcel        atfu_err = atf_tc_init_pack(&atfu_ ## tc ## _tc, \
120240116Smarcel                                    &atfu_ ## tc ## _tc_pack, \
121240116Smarcel                                    (const char *const *)atfu_config); \
122240116Smarcel        atf_utils_free_charpp(atfu_config); \
123240116Smarcel        if (atf_is_error(atfu_err)) \
124240116Smarcel            return atfu_err; \
125240116Smarcel        atfu_err = atf_tp_add_tc(tp, &atfu_ ## tc ## _tc); \
126240116Smarcel        if (atf_is_error(atfu_err)) \
127240116Smarcel            return atfu_err; \
128240116Smarcel    } while (0)
129240116Smarcel
130275988Sngie#define ATF_REQUIRE_MSG(expression, fmt, ...) \
131240116Smarcel    do { \
132275988Sngie        if (!(expression)) \
133240116Smarcel            atf_tc_fail_requirement(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
134240116Smarcel    } while(0)
135240116Smarcel
136275988Sngie#define ATF_CHECK_MSG(expression, fmt, ...) \
137240116Smarcel    do { \
138275988Sngie        if (!(expression)) \
139240116Smarcel            atf_tc_fail_check(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
140240116Smarcel    } while(0)
141240116Smarcel
142275988Sngie#define ATF_REQUIRE(expression) \
143240116Smarcel    do { \
144275988Sngie        if (!(expression)) \
145275988Sngie            atf_tc_fail_requirement(__FILE__, __LINE__, "%s", \
146275988Sngie                                    #expression " not met"); \
147240116Smarcel    } while(0)
148240116Smarcel
149275988Sngie#define ATF_CHECK(expression) \
150240116Smarcel    do { \
151275988Sngie        if (!(expression)) \
152275988Sngie            atf_tc_fail_check(__FILE__, __LINE__, "%s", \
153275988Sngie                              #expression " not met"); \
154240116Smarcel    } while(0)
155240116Smarcel
156275988Sngie#define ATF_REQUIRE_EQ(expected, actual) \
157275988Sngie    ATF_REQUIRE_MSG((expected) == (actual), "%s != %s", #expected, #actual)
158240116Smarcel
159275988Sngie#define ATF_CHECK_EQ(expected, actual) \
160275988Sngie    ATF_CHECK_MSG((expected) == (actual), "%s != %s", #expected, #actual)
161240116Smarcel
162275988Sngie#define ATF_REQUIRE_EQ_MSG(expected, actual, fmt, ...) \
163275988Sngie    ATF_REQUIRE_MSG((expected) == (actual), "%s != %s: " fmt, \
164275988Sngie                    #expected, #actual, ##__VA_ARGS__)
165240116Smarcel
166275988Sngie#define ATF_CHECK_EQ_MSG(expected, actual, fmt, ...) \
167275988Sngie    ATF_CHECK_MSG((expected) == (actual), "%s != %s: " fmt, \
168275988Sngie                  #expected, #actual, ##__VA_ARGS__)
169240116Smarcel
170275988Sngie#define ATF_REQUIRE_STREQ(expected, actual) \
171275988Sngie    ATF_REQUIRE_MSG(strcmp(expected, actual) == 0, "%s != %s (%s != %s)", \
172275988Sngie                    #expected, #actual, expected, actual)
173240116Smarcel
174275988Sngie#define ATF_CHECK_STREQ(expected, actual) \
175275988Sngie    ATF_CHECK_MSG(strcmp(expected, actual) == 0, "%s != %s (%s != %s)", \
176275988Sngie                  #expected, #actual, expected, actual)
177240116Smarcel
178275988Sngie#define ATF_REQUIRE_STREQ_MSG(expected, actual, fmt, ...) \
179275988Sngie    ATF_REQUIRE_MSG(strcmp(expected, actual) == 0, \
180275988Sngie                    "%s != %s (%s != %s): " fmt, \
181275988Sngie                    #expected, #actual, expected, actual, ##__VA_ARGS__)
182240116Smarcel
183275988Sngie#define ATF_CHECK_STREQ_MSG(expected, actual, fmt, ...) \
184275988Sngie    ATF_CHECK_MSG(strcmp(expected, actual) == 0, \
185275988Sngie                  "%s != %s (%s != %s): " fmt, \
186275988Sngie                  #expected, #actual, expected, actual, ##__VA_ARGS__)
187240116Smarcel
188260029Sjmmv#define ATF_REQUIRE_MATCH(regexp, string) \
189260029Sjmmv    ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \
190260029Sjmmv                    "'%s' not matched in '%s'", regexp, string);
191260029Sjmmv
192260029Sjmmv#define ATF_CHECK_MATCH(regexp, string) \
193260029Sjmmv    ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \
194260029Sjmmv                  "'%s' not matched in '%s'", regexp, string);
195260029Sjmmv
196260029Sjmmv#define ATF_REQUIRE_MATCH_MSG(regexp, string, fmt, ...) \
197260029Sjmmv    ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \
198260029Sjmmv                    "'%s' not matched in '%s': " fmt, regexp, string, \
199260029Sjmmv                    ##__VA_ARGS__);
200260029Sjmmv
201260029Sjmmv#define ATF_CHECK_MATCH_MSG(regexp, string, fmt, ...) \
202260029Sjmmv    ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \
203260029Sjmmv                  "'%s' not matched in '%s': " fmt, regexp, string, \
204260029Sjmmv                  ##__VA_ARGS__);
205260029Sjmmv
206240116Smarcel#define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
207240116Smarcel    atf_tc_check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
208240116Smarcel
209240116Smarcel#define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
210240116Smarcel    atf_tc_require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
211240116Smarcel
212240116Smarcel#endif /* !defined(ATF_C_MACROS_H) */
213