13b6cf63jmmv/* Copyright (c) 2008 The NetBSD Foundation, Inc.
245147a9marcel * All rights reserved.
345147a9marcel *
445147a9marcel * Redistribution and use in source and binary forms, with or without
545147a9marcel * modification, are permitted provided that the following conditions
645147a9marcel * are met:
745147a9marcel * 1. Redistributions of source code must retain the above copyright
845147a9marcel *    notice, this list of conditions and the following disclaimer.
945147a9marcel * 2. Redistributions in binary form must reproduce the above copyright
1045147a9marcel *    notice, this list of conditions and the following disclaimer in the
1145147a9marcel *    documentation and/or other materials provided with the distribution.
1245147a9marcel *
1345147a9marcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1445147a9marcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1545147a9marcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1645147a9marcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1745147a9marcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
1845147a9marcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1945147a9marcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2045147a9marcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2145147a9marcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2245147a9marcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2345147a9marcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
243b6cf63jmmv * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
2545147a9marcel
2645147a9marcel#if !defined(ATF_C_MACROS_H)
2745147a9marcel#define ATF_C_MACROS_H
2845147a9marcel
298a14a5fjmmv#include <string.h>
308a14a5fjmmv
3145147a9marcel#include <atf-c/defs.h>
3245147a9marcel#include <atf-c/error.h>
3345147a9marcel#include <atf-c/tc.h>
3445147a9marcel#include <atf-c/tp.h>
3545147a9marcel#include <atf-c/utils.h>
3645147a9marcel
3745147a9marcel#define ATF_TC_NAME(tc) \
3845147a9marcel    (atfu_ ## tc ## _tc)
3945147a9marcel
4045147a9marcel#define ATF_TC_PACK_NAME(tc) \
4145147a9marcel    (atfu_ ## tc ## _tc_pack)
4245147a9marcel
4345147a9marcel#define ATF_TC_WITHOUT_HEAD(tc) \
4445147a9marcel    static void atfu_ ## tc ## _body(const atf_tc_t *); \
4545147a9marcel    static atf_tc_t atfu_ ## tc ## _tc; \
4645147a9marcel    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
4745147a9marcel        .m_ident = #tc, \
4845147a9marcel        .m_head = NULL, \
4945147a9marcel        .m_body = atfu_ ## tc ## _body, \
5045147a9marcel        .m_cleanup = NULL, \
5145147a9marcel    }
5245147a9marcel
5345147a9marcel#define ATF_TC(tc) \
5445147a9marcel    static void atfu_ ## tc ## _head(atf_tc_t *); \
5545147a9marcel    static void atfu_ ## tc ## _body(const atf_tc_t *); \
5645147a9marcel    static atf_tc_t atfu_ ## tc ## _tc; \
5745147a9marcel    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
5845147a9marcel        .m_ident = #tc, \
5945147a9marcel        .m_head = atfu_ ## tc ## _head, \
6045147a9marcel        .m_body = atfu_ ## tc ## _body, \
6145147a9marcel        .m_cleanup = NULL, \
6245147a9marcel    }
6345147a9marcel
6445147a9marcel#define ATF_TC_WITH_CLEANUP(tc) \
6545147a9marcel    static void atfu_ ## tc ## _head(atf_tc_t *); \
6645147a9marcel    static void atfu_ ## tc ## _body(const atf_tc_t *); \
6745147a9marcel    static void atfu_ ## tc ## _cleanup(const atf_tc_t *); \
6845147a9marcel    static atf_tc_t atfu_ ## tc ## _tc; \
6945147a9marcel    static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \
7045147a9marcel        .m_ident = #tc, \
7145147a9marcel        .m_head = atfu_ ## tc ## _head, \
7245147a9marcel        .m_body = atfu_ ## tc ## _body, \
7345147a9marcel        .m_cleanup = atfu_ ## tc ## _cleanup, \
7445147a9marcel    }
7545147a9marcel
7645147a9marcel#define ATF_TC_HEAD(tc, tcptr) \
7745147a9marcel    static \
7845147a9marcel    void \
798a14a5fjmmv    atfu_ ## tc ## _head(atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
8045147a9marcel
8145147a9marcel#define ATF_TC_HEAD_NAME(tc) \
8245147a9marcel    (atfu_ ## tc ## _head)
8345147a9marcel
8445147a9marcel#define ATF_TC_BODY(tc, tcptr) \
8545147a9marcel    static \
8645147a9marcel    void \
8745147a9marcel    atfu_ ## tc ## _body(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
8845147a9marcel
8945147a9marcel#define ATF_TC_BODY_NAME(tc) \
9045147a9marcel    (atfu_ ## tc ## _body)
9145147a9marcel
9245147a9marcel#define ATF_TC_CLEANUP(tc, tcptr) \
9345147a9marcel    static \
9445147a9marcel    void \
9545147a9marcel    atfu_ ## tc ## _cleanup(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED)
9645147a9marcel
9745147a9marcel#define ATF_TC_CLEANUP_NAME(tc) \
9845147a9marcel    (atfu_ ## tc ## _cleanup)
9945147a9marcel
10045147a9marcel#define ATF_TP_ADD_TCS(tps) \
10145147a9marcel    static atf_error_t atfu_tp_add_tcs(atf_tp_t *); \
10245147a9marcel    int atf_tp_main(int, char **, atf_error_t (*)(atf_tp_t *)); \
10345147a9marcel    \
10445147a9marcel    int \
10545147a9marcel    main(int argc, char **argv) \
10645147a9marcel    { \
10745147a9marcel        return atf_tp_main(argc, argv, atfu_tp_add_tcs); \
10845147a9marcel    } \
10945147a9marcel    static \
11045147a9marcel    atf_error_t \
11145147a9marcel    atfu_tp_add_tcs(atf_tp_t *tps)
11245147a9marcel
11345147a9marcel#define ATF_TP_ADD_TC(tp, tc) \
11445147a9marcel    do { \
11545147a9marcel        atf_error_t atfu_err; \
11645147a9marcel        char **atfu_config = atf_tp_get_config(tp); \
11745147a9marcel        if (atfu_config == NULL) \
11845147a9marcel            return atf_no_memory_error(); \
11945147a9marcel        atfu_err = atf_tc_init_pack(&atfu_ ## tc ## _tc, \
12045147a9marcel                                    &atfu_ ## tc ## _tc_pack, \
12145147a9marcel                                    (const char *const *)atfu_config); \
12245147a9marcel        atf_utils_free_charpp(atfu_config); \
12345147a9marcel        if (atf_is_error(atfu_err)) \
12445147a9marcel            return atfu_err; \
12545147a9marcel        atfu_err = atf_tp_add_tc(tp, &atfu_ ## tc ## _tc); \
12645147a9marcel        if (atf_is_error(atfu_err)) \
12745147a9marcel            return atfu_err; \
12845147a9marcel    } while (0)
12945147a9marcel
1303b6cf63jmmv#define ATF_REQUIRE_MSG(expression, fmt, ...) \
13145147a9marcel    do { \
1323b6cf63jmmv        if (!(expression)) \
13345147a9marcel            atf_tc_fail_requirement(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
13445147a9marcel    } while(0)
13545147a9marcel
1363b6cf63jmmv#define ATF_CHECK_MSG(expression, fmt, ...) \
13745147a9marcel    do { \
1383b6cf63jmmv        if (!(expression)) \
13945147a9marcel            atf_tc_fail_check(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
14045147a9marcel    } while(0)
14145147a9marcel
1423b6cf63jmmv#define ATF_REQUIRE(expression) \
14345147a9marcel    do { \
1443b6cf63jmmv        if (!(expression)) \
1453b6cf63jmmv            atf_tc_fail_requirement(__FILE__, __LINE__, "%s", \
1463b6cf63jmmv                                    #expression " not met"); \
14745147a9marcel    } while(0)
14845147a9marcel
1493b6cf63jmmv#define ATF_CHECK(expression) \
15045147a9marcel    do { \
1513b6cf63jmmv        if (!(expression)) \
1523b6cf63jmmv            atf_tc_fail_check(__FILE__, __LINE__, "%s", \
1533b6cf63jmmv                              #expression " not met"); \
15445147a9marcel    } while(0)
15545147a9marcel
1563b6cf63jmmv#define ATF_REQUIRE_EQ(expected, actual) \
1573b6cf63jmmv    ATF_REQUIRE_MSG((expected) == (actual), "%s != %s", #expected, #actual)
15845147a9marcel
1593b6cf63jmmv#define ATF_CHECK_EQ(expected, actual) \
1603b6cf63jmmv    ATF_CHECK_MSG((expected) == (actual), "%s != %s", #expected, #actual)
16145147a9marcel
1623b6cf63jmmv#define ATF_REQUIRE_EQ_MSG(expected, actual, fmt, ...) \
1633b6cf63jmmv    ATF_REQUIRE_MSG((expected) == (actual), "%s != %s: " fmt, \
1643b6cf63jmmv                    #expected, #actual, ##__VA_ARGS__)
16545147a9marcel
1663b6cf63jmmv#define ATF_CHECK_EQ_MSG(expected, actual, fmt, ...) \
1673b6cf63jmmv    ATF_CHECK_MSG((expected) == (actual), "%s != %s: " fmt, \
1683b6cf63jmmv                  #expected, #actual, ##__VA_ARGS__)
16945147a9marcel
1703b6cf63jmmv#define ATF_REQUIRE_STREQ(expected, actual) \
1713b6cf63jmmv    ATF_REQUIRE_MSG(strcmp(expected, actual) == 0, "%s != %s (%s != %s)", \
1723b6cf63jmmv                    #expected, #actual, expected, actual)
17345147a9marcel
1743b6cf63jmmv#define ATF_CHECK_STREQ(expected, actual) \
1753b6cf63jmmv    ATF_CHECK_MSG(strcmp(expected, actual) == 0, "%s != %s (%s != %s)", \
1763b6cf63jmmv                  #expected, #actual, expected, actual)
17745147a9marcel
1783b6cf63jmmv#define ATF_REQUIRE_STREQ_MSG(expected, actual, fmt, ...) \
1793b6cf63jmmv    ATF_REQUIRE_MSG(strcmp(expected, actual) == 0, \
1803b6cf63jmmv                    "%s != %s (%s != %s): " fmt, \
1813b6cf63jmmv                    #expected, #actual, expected, actual, ##__VA_ARGS__)
18245147a9marcel
1833b6cf63jmmv#define ATF_CHECK_STREQ_MSG(expected, actual, fmt, ...) \
1843b6cf63jmmv    ATF_CHECK_MSG(strcmp(expected, actual) == 0, \
1853b6cf63jmmv                  "%s != %s (%s != %s): " fmt, \
1863b6cf63jmmv                  #expected, #actual, expected, actual, ##__VA_ARGS__)
18745147a9marcel
1888a14a5fjmmv#define ATF_REQUIRE_MATCH(regexp, string) \
1898a14a5fjmmv    ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \
1908a14a5fjmmv                    "'%s' not matched in '%s'", regexp, string);
1918a14a5fjmmv
1928a14a5fjmmv#define ATF_CHECK_MATCH(regexp, string) \
1938a14a5fjmmv    ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \
1948a14a5fjmmv                  "'%s' not matched in '%s'", regexp, string);
1958a14a5fjmmv
1968a14a5fjmmv#define ATF_REQUIRE_MATCH_MSG(regexp, string, fmt, ...) \
1978a14a5fjmmv    ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \
1988a14a5fjmmv                    "'%s' not matched in '%s': " fmt, regexp, string, \
1998a14a5fjmmv                    ##__VA_ARGS__);
2008a14a5fjmmv
2018a14a5fjmmv#define ATF_CHECK_MATCH_MSG(regexp, string, fmt, ...) \
2028a14a5fjmmv    ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \
2038a14a5fjmmv                  "'%s' not matched in '%s': " fmt, regexp, string, \
2048a14a5fjmmv                  ##__VA_ARGS__);
2058a14a5fjmmv
20645147a9marcel#define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
20745147a9marcel    atf_tc_check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
20845147a9marcel
20945147a9marcel#define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
21045147a9marcel    atf_tc_require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr)
21145147a9marcel
21245147a9marcel#endif /* !defined(ATF_C_MACROS_H) */
213