summaryrefslogtreecommitdiffstats
path: root/unit/atf-src/atf-c/macros.h
blob: 485a159acecba5eff33f2a1f985cbffef198765b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* Copyright (c) 2008 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */

#if !defined(ATF_C_MACROS_H)
#define ATF_C_MACROS_H

#include <string.h>

#include <atf-c/defs.h>
#include <atf-c/error.h>
#include <atf-c/tc.h>
#include <atf-c/tp.h>
#include <atf-c/utils.h>

#define ATF_TC_NAME(tc) \
    (atfu_ ## tc ## _tc)

#define ATF_TC_PACK_NAME(tc) \
    (atfu_ ## tc ## _tc_pack)

#define ATF_TC_WITHOUT_HEAD(tc) \
    static void atfu_ ## tc ## _body(const atf_tc_t *); \
    static atf_tc_t atfu_ ## tc ## _tc; \
    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
        .m_ident = #tc, \
        .m_head = NULL, \
        .m_body = atfu_ ## tc ## _body, \
        .m_cleanup = NULL, \
    }

#define ATF_TC(tc) \
    static void atfu_ ## tc ## _head(atf_tc_t *); \
    static void atfu_ ## tc ## _body(const atf_tc_t *); \
    static atf_tc_t atfu_ ## tc ## _tc; \
    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
        .m_ident = #tc, \
        .m_head = atfu_ ## tc ## _head, \
        .m_body = atfu_ ## tc ## _body, \
        .m_cleanup = NULL, \
    }

#define ATF_TC_WITH_CLEANUP(tc) \
    static void atfu_ ## tc ## _head(atf_tc_t *); \
    static void atfu_ ## tc ## _body(const atf_tc_t *); \
    static void atfu_ ## tc ## _cleanup(const atf_tc_t *); \
    static atf_tc_t atfu_ ## tc ## _tc; \
    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
        .m_ident = #tc, \
        .m_head = atfu_ ## tc ## _head, \
        .m_body = atfu_ ## tc ## _body, \
        .m_cleanup = atfu_ ## tc ## _cleanup, \
    }

#define ATF_TC_HEAD(tc, tcptr) \
    static \
    void \
    atfu_ ## tc ## _head(atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)

#define ATF_TC_HEAD_NAME(tc) \
    (atfu_ ## tc ## _head)

#define ATF_TC_BODY(tc, tcptr) \
    static \
    void \
    atfu_ ## tc ## _body(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)

#define ATF_TC_BODY_NAME(tc) \
    (atfu_ ## tc ## _body)

#define ATF_TC_CLEANUP(tc, tcptr) \
    static \
    void \
    atfu_ ## tc ## _cleanup(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)

#define ATF_TC_CLEANUP_NAME(tc) \
    (atfu_ ## tc ## _cleanup)

#define ATF_TP_ADD_TCS(tps) \
    static atf_error_t atfu_tp_add_tcs(atf_tp_t *); \
    int atf_tp_main(int, char **, atf_error_t (*)(atf_tp_t *)); \
    \
    int \
    main(int argc, char **argv) \
    { \
        return atf_tp_main(argc, argv, atfu_tp_add_tcs); \
    } \
    static \
    atf_error_t \
    atfu_tp_add_tcs(atf_tp_t *tps)

#define ATF_TP_ADD_TC(tp, tc) \
    do { \
        atf_error_t atfu_err; \
        char **atfu_config = atf_tp_get_config(tp); \
        if (atfu_config == NULL) \
            return atf_no_memory_error(); \
        atfu_err = atf_tc_init_pack(&atfu_ ## tc ## _tc, \
                                    &atfu_ ## tc ## _tc_pack, \
                                    (const char *const *)atfu_config); \
        atf_utils_free_charpp(atfu_config); \
        if (atf_is_error(atfu_err)) \
            return atfu_err; \
        atfu_err = atf_tp_add_tc(tp, &atfu_ ## tc ## _tc); \
        if (atf_is_error(atfu_err)) \
            return atfu_err; \
    } while (0)

#define ATF_REQUIRE_MSG(expression, fmt, ...) \
    do { \
        if (!(expression)) \
            atf_tc_fail_requirement(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
    } while(0)

#define ATF_CHECK_MSG(expression, fmt, ...) \
    do { \
        if (!(expression)) \
            atf_tc_fail_check(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
    } while(0)

#define ATF_REQUIRE(expression) \
    do { \
        if (!(expression)) \
            atf_tc_fail_requirement(__FILE__, __LINE__, "%s", \
                                    #expression " not met"); \
    } while(0)

#define ATF_CHECK(expression) \
    do { \
        if (!(expression)) \
            atf_tc_fail_check(__FILE__, __LINE__, "%s", \
                              #expression " not met"); \
    } while(0)

#define ATF_REQUIRE_EQ(expected, actual) \
    ATF_REQUIRE_MSG((expected) == (actual), "%s != %s", #expected, #actual)

#define ATF_CHECK_EQ(expected, actual) \
    ATF_CHECK_MSG((expected) == (actual), "%s != %s", #expected, #actual)

#define ATF_REQUIRE_EQ_MSG(expected, actual, fmt, ...) \
    ATF_REQUIRE_MSG((expected) == (actual), "%s != %s: " fmt, \
                    #expected, #actual, ##__VA_ARGS__)

#define ATF_CHECK_EQ_MSG(expected, actual, fmt, ...) \
    ATF_CHECK_MSG((expected) == (actual), "%s != %s: " fmt, \
                  #expected, #actual, ##__VA_ARGS__)

#define ATF_REQUIRE_STREQ(expected, actual) \
    ATF_REQUIRE_MSG(strcmp(expected, actual) == 0, "%s != %s (%s != %s)", \
                    #expected, #actual, expected, actual)

#define ATF_CHECK_STREQ(expected, actual) \
    ATF_CHECK_MSG(strcmp(expected, actual) == 0, "%s != %s (%s != %s)", \
                  #expected, #actual, expected, actual)

#define ATF_REQUIRE_STREQ_MSG(expected, actual, fmt, ...) \
    ATF_REQUIRE_MSG(strcmp(expected, actual) == 0, \
                    "%s != %s (%s != %s): " fmt, \
                    #expected, #actual, expected, actual, ##__VA_ARGS__)

#define ATF_CHECK_STREQ_MSG(expected, actual, fmt, ...) \
    ATF_CHECK_MSG(strcmp(expected, actual) == 0, \
                  "%s != %s (%s != %s): " fmt, \
                  #expected, #actual, expected, actual, ##__VA_ARGS__)

#define ATF_REQUIRE_MATCH(regexp, string) \
    ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \
                    "'%s' not matched in '%s'", regexp, string);

#define ATF_CHECK_MATCH(regexp, string) \
    ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \
                  "'%s' not matched in '%s'", regexp, string);

#define ATF_REQUIRE_MATCH_MSG(regexp, string, fmt, ...) \
    ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \
                    "'%s' not matched in '%s': " fmt, regexp, string, \
                    ##__VA_ARGS__);

#define ATF_CHECK_MATCH_MSG(regexp, string, fmt, ...) \
    ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \
                  "'%s' not matched in '%s': " fmt, regexp, string, \
                  ##__VA_ARGS__);

#define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
    atf_tc_check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)

#define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
    atf_tc_require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)

#endif /* !defined(ATF_C_MACROS_H) */