1/*
2 * Automated Testing Framework (atf)
3 *
4 * Copyright (c) 2008, 2009, 2010 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/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)
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)
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)
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(x, fmt, ...) \
131    do { \
132        if (!(x)) \
133            atf_tc_fail_requirement(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
134    } while(0)
135
136#define ATF_CHECK_MSG(x, fmt, ...) \
137    do { \
138        if (!(x)) \
139            atf_tc_fail_check(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
140    } while(0)
141
142#define ATF_REQUIRE(x) \
143    do { \
144        if (!(x)) \
145            atf_tc_fail_requirement(__FILE__, __LINE__, "%s", #x " not met"); \
146    } while(0)
147
148#define ATF_CHECK(x) \
149    do { \
150        if (!(x)) \
151            atf_tc_fail_check(__FILE__, __LINE__, "%s", #x " not met"); \
152    } while(0)
153
154#define ATF_REQUIRE_EQ(x, y) \
155    ATF_REQUIRE_MSG((x) == (y), "%s != %s", #x, #y)
156
157#define ATF_CHECK_EQ(x, y) \
158    ATF_CHECK_MSG((x) == (y), "%s != %s", #x, #y)
159
160#define ATF_REQUIRE_EQ_MSG(x, y, fmt, ...) \
161    ATF_REQUIRE_MSG((x) == (y), "%s != %s: " fmt, #x, #y, ##__VA_ARGS__)
162
163#define ATF_CHECK_EQ_MSG(x, y, fmt, ...) \
164    ATF_CHECK_MSG((x) == (y), "%s != %s: " fmt, #x, #y, ##__VA_ARGS__)
165
166#define ATF_REQUIRE_STREQ(x, y) \
167    ATF_REQUIRE_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s)", #x, #y, x, y)
168
169#define ATF_CHECK_STREQ(x, y) \
170    ATF_CHECK_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s)", #x, #y, x, y)
171
172#define ATF_REQUIRE_STREQ_MSG(x, y, fmt, ...) \
173    ATF_REQUIRE_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s): " fmt, \
174                    #x, #y, x, y, ##__VA_ARGS__)
175
176#define ATF_CHECK_STREQ_MSG(x, y, fmt, ...) \
177    ATF_CHECK_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s): " fmt, \
178                    #x, #y, x, y, ##__VA_ARGS__)
179
180#define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
181    atf_tc_check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
182
183#define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
184    atf_tc_require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
185
186#endif /* !defined(ATF_C_MACROS_H) */
187