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 <atf-c/defs.h>
34#include <atf-c/tc.h>
35#include <atf-c/tp.h>
36#include <atf-c/utils.h>
37
38#define ATF_TC_NAME(tc) \
39    (atfu_ ## tc ## _tc)
40
41#define ATF_TC_PACK_NAME(tc) \
42    (atfu_ ## tc ## _tc_pack)
43
44#define ATF_TC_WITHOUT_HEAD(tc) \
45    static void atfu_ ## tc ## _body(const atf_tc_t *); \
46    static atf_tc_t atfu_ ## tc ## _tc; \
47    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
48        .m_ident = #tc, \
49        .m_head = NULL, \
50        .m_body = atfu_ ## tc ## _body, \
51        .m_cleanup = NULL, \
52    }
53
54#define ATF_TC(tc) \
55    static void atfu_ ## tc ## _head(atf_tc_t *); \
56    static void atfu_ ## tc ## _body(const atf_tc_t *); \
57    static atf_tc_t atfu_ ## tc ## _tc; \
58    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
59        .m_ident = #tc, \
60        .m_head = atfu_ ## tc ## _head, \
61        .m_body = atfu_ ## tc ## _body, \
62        .m_cleanup = NULL, \
63    }
64
65#define ATF_TC_WITH_CLEANUP(tc) \
66    static void atfu_ ## tc ## _head(atf_tc_t *); \
67    static void atfu_ ## tc ## _body(const atf_tc_t *); \
68    static void atfu_ ## tc ## _cleanup(const atf_tc_t *); \
69    static atf_tc_t atfu_ ## tc ## _tc; \
70    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
71        .m_ident = #tc, \
72        .m_head = atfu_ ## tc ## _head, \
73        .m_body = atfu_ ## tc ## _body, \
74        .m_cleanup = atfu_ ## tc ## _cleanup, \
75    }
76
77#define ATF_TC_HEAD(tc, tcptr) \
78    static \
79    void \
80    atfu_ ## tc ## _head(atf_tc_t *tcptr)
81
82#define ATF_TC_HEAD_NAME(tc) \
83    (atfu_ ## tc ## _head)
84
85#define ATF_TC_BODY(tc, tcptr) \
86    static \
87    void \
88    atfu_ ## tc ## _body(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
89
90#define ATF_TC_BODY_NAME(tc) \
91    (atfu_ ## tc ## _body)
92
93#define ATF_TC_CLEANUP(tc, tcptr) \
94    static \
95    void \
96    atfu_ ## tc ## _cleanup(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
97
98#define ATF_TC_CLEANUP_NAME(tc) \
99    (atfu_ ## tc ## _cleanup)
100
101#define ATF_TP_ADD_TCS(tps) \
102    static atf_error_t atfu_tp_add_tcs(atf_tp_t *); \
103    int atf_tp_main(int, char **, atf_error_t (*)(atf_tp_t *)); \
104    \
105    int \
106    main(int argc, char **argv) \
107    { \
108        return atf_tp_main(argc, argv, atfu_tp_add_tcs); \
109    } \
110    static \
111    atf_error_t \
112    atfu_tp_add_tcs(atf_tp_t *tps)
113
114#define ATF_TP_ADD_TC(tp, tc) \
115    do { \
116        atf_error_t atfu_err; \
117        char **atfu_config = atf_tp_get_config(tp); \
118        if (atfu_config == NULL) \
119            return atf_no_memory_error(); \
120        atfu_err = atf_tc_init_pack(&atfu_ ## tc ## _tc, \
121                                    &atfu_ ## tc ## _tc_pack, \
122                                    (const char *const *)atfu_config); \
123        atf_utils_free_charpp(atfu_config); \
124        if (atf_is_error(atfu_err)) \
125            return atfu_err; \
126        atfu_err = atf_tp_add_tc(tp, &atfu_ ## tc ## _tc); \
127        if (atf_is_error(atfu_err)) \
128            return atfu_err; \
129    } while (0)
130
131#define ATF_REQUIRE_MSG(x, fmt, ...) \
132    do { \
133        if (!(x)) \
134            atf_tc_fail_requirement(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
135    } while(0)
136
137#define ATF_CHECK_MSG(x, fmt, ...) \
138    do { \
139        if (!(x)) \
140            atf_tc_fail_check(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
141    } while(0)
142
143#define ATF_REQUIRE(x) \
144    do { \
145        if (!(x)) \
146            atf_tc_fail_requirement(__FILE__, __LINE__, "%s", #x " not met"); \
147    } while(0)
148
149#define ATF_CHECK(x) \
150    do { \
151        if (!(x)) \
152            atf_tc_fail_check(__FILE__, __LINE__, "%s", #x " not met"); \
153    } while(0)
154
155#define ATF_REQUIRE_EQ(x, y) \
156    ATF_REQUIRE_MSG((x) == (y), "%s != %s", #x, #y)
157
158#define ATF_CHECK_EQ(x, y) \
159    ATF_CHECK_MSG((x) == (y), "%s != %s", #x, #y)
160
161#define ATF_REQUIRE_EQ_MSG(x, y, fmt, ...) \
162    ATF_REQUIRE_MSG((x) == (y), "%s != %s: " fmt, #x, #y, ##__VA_ARGS__)
163
164#define ATF_CHECK_EQ_MSG(x, y, fmt, ...) \
165    ATF_CHECK_MSG((x) == (y), "%s != %s: " fmt, #x, #y, ##__VA_ARGS__)
166
167#define ATF_REQUIRE_STREQ(x, y) \
168    ATF_REQUIRE_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s)", #x, #y, x, y)
169
170#define ATF_CHECK_STREQ(x, y) \
171    ATF_CHECK_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s)", #x, #y, x, y)
172
173#define ATF_REQUIRE_STREQ_MSG(x, y, fmt, ...) \
174    ATF_REQUIRE_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s): " fmt, \
175                    #x, #y, x, y, ##__VA_ARGS__)
176
177#define ATF_CHECK_STREQ_MSG(x, y, fmt, ...) \
178    ATF_CHECK_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s): " fmt, \
179                    #x, #y, x, y, ##__VA_ARGS__)
180
181#define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
182    atf_tc_check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
183
184#define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
185    atf_tc_require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
186
187#endif /* !defined(ATF_C_MACROS_H) */
188