1 /* $NetBSD: macros.h,v 1.4 2014/12/10 04:38:03 christos Exp $ */ 2 3 /* 4 * Automated Testing Framework (atf) 5 * 6 * Copyright (c) 2008 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 19 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 29 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #if !defined(ATF_C_MACROS_H) 33 #define ATF_C_MACROS_H 34 35 #include <string.h> 36 37 #include <atf-c/defs.h> 38 #include <atf-c/error.h> 39 #include <atf-c/tc.h> 40 #include <atf-c/tp.h> 41 #include <atf-c/utils.h> 42 43 #define ATF_TC_NAME(tc) \ 44 (atfu_ ## tc ## _tc) 45 46 #define ATF_TC_PACK_NAME(tc) \ 47 (atfu_ ## tc ## _tc_pack) 48 49 #define ATF_TC_WITHOUT_HEAD(tc) \ 50 static void atfu_ ## tc ## _body(const atf_tc_t *); \ 51 static atf_tc_t atfu_ ## tc ## _tc; \ 52 static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \ 53 .m_ident = #tc, \ 54 .m_head = NULL, \ 55 .m_body = atfu_ ## tc ## _body, \ 56 .m_cleanup = NULL, \ 57 } 58 59 #define ATF_TC(tc) \ 60 static void atfu_ ## tc ## _head(atf_tc_t *); \ 61 static void atfu_ ## tc ## _body(const atf_tc_t *); \ 62 static atf_tc_t atfu_ ## tc ## _tc; \ 63 static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \ 64 .m_ident = #tc, \ 65 .m_head = atfu_ ## tc ## _head, \ 66 .m_body = atfu_ ## tc ## _body, \ 67 .m_cleanup = NULL, \ 68 } 69 70 #define ATF_TC_WITH_CLEANUP(tc) \ 71 static void atfu_ ## tc ## _head(atf_tc_t *); \ 72 static void atfu_ ## tc ## _body(const atf_tc_t *); \ 73 static void atfu_ ## tc ## _cleanup(const atf_tc_t *); \ 74 static atf_tc_t atfu_ ## tc ## _tc; \ 75 static atf_tc_pack_t atfu_ ## tc ## _tc_pack = { \ 76 .m_ident = #tc, \ 77 .m_head = atfu_ ## tc ## _head, \ 78 .m_body = atfu_ ## tc ## _body, \ 79 .m_cleanup = atfu_ ## tc ## _cleanup, \ 80 } 81 82 #define ATF_TC_HEAD(tc, tcptr) \ 83 static \ 84 void \ 85 atfu_ ## tc ## _head(atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED) 86 87 #define ATF_TC_HEAD_NAME(tc) \ 88 (atfu_ ## tc ## _head) 89 90 #define ATF_TC_BODY(tc, tcptr) \ 91 static \ 92 void \ 93 atfu_ ## tc ## _body(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED) 94 95 #define ATF_TC_BODY_NAME(tc) \ 96 (atfu_ ## tc ## _body) 97 98 #define ATF_TC_CLEANUP(tc, tcptr) \ 99 static \ 100 void \ 101 atfu_ ## tc ## _cleanup(const atf_tc_t *tcptr ATF_DEFS_ATTRIBUTE_UNUSED) 102 103 #define ATF_TC_CLEANUP_NAME(tc) \ 104 (atfu_ ## tc ## _cleanup) 105 106 #define ATF_TP_ADD_TCS(tps) \ 107 static atf_error_t atfu_tp_add_tcs(atf_tp_t *); \ 108 int atf_tp_main(int, char **, atf_error_t (*)(atf_tp_t *)); \ 109 \ 110 int \ 111 main(int argc, char **argv) \ 112 { \ 113 return atf_tp_main(argc, argv, atfu_tp_add_tcs); \ 114 } \ 115 static \ 116 atf_error_t \ 117 atfu_tp_add_tcs(atf_tp_t *tps) 118 119 #define ATF_TP_ADD_TC(tp, tc) \ 120 do { \ 121 atf_error_t atfu_err; \ 122 char **atfu_config = atf_tp_get_config(tp); \ 123 if (atfu_config == NULL) \ 124 return atf_no_memory_error(); \ 125 atfu_err = atf_tc_init_pack(&atfu_ ## tc ## _tc, \ 126 &atfu_ ## tc ## _tc_pack, \ 127 (const char *const *)atfu_config); \ 128 atf_utils_free_charpp(atfu_config); \ 129 if (atf_is_error(atfu_err)) \ 130 return atfu_err; \ 131 atfu_err = atf_tp_add_tc(tp, &atfu_ ## tc ## _tc); \ 132 if (atf_is_error(atfu_err)) \ 133 return atfu_err; \ 134 } while (/*CONSTCOND*/0) 135 136 #define ATF_REQUIRE_MSG(x, fmt, ...) \ 137 do { \ 138 if (!(x)) \ 139 atf_tc_fail_requirement(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \ 140 } while(/*CONSTCOND*/0) 141 142 #define ATF_CHECK_MSG(x, fmt, ...) \ 143 do { \ 144 if (!(x)) \ 145 atf_tc_fail_check(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \ 146 } while(/*CONSTCOND*/0) 147 148 #define ATF_REQUIRE(x) \ 149 do { \ 150 if (!(x)) \ 151 atf_tc_fail_requirement(__FILE__, __LINE__, "%s", #x " not met"); \ 152 } while(/*CONSTCOND*/0) 153 154 #define ATF_CHECK(x) \ 155 do { \ 156 if (!(x)) \ 157 atf_tc_fail_check(__FILE__, __LINE__, "%s", #x " not met"); \ 158 } while(/*CONSTCOND*/0) 159 160 #define ATF_REQUIRE_EQ(x, y) \ 161 ATF_REQUIRE_MSG((x) == (y), "%s != %s", #x, #y) 162 163 #define ATF_CHECK_EQ(x, y) \ 164 ATF_CHECK_MSG((x) == (y), "%s != %s", #x, #y) 165 166 #define ATF_REQUIRE_EQ_MSG(x, y, fmt, ...) \ 167 ATF_REQUIRE_MSG((x) == (y), "%s != %s: " fmt, #x, #y, ##__VA_ARGS__) 168 169 #define ATF_CHECK_EQ_MSG(x, y, fmt, ...) \ 170 ATF_CHECK_MSG((x) == (y), "%s != %s: " fmt, #x, #y, ##__VA_ARGS__) 171 172 #define ATF_REQUIRE_STREQ(x, y) \ 173 ATF_REQUIRE_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s)", #x, #y, x, y) 174 175 #define ATF_CHECK_STREQ(x, y) \ 176 ATF_CHECK_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s)", #x, #y, x, y) 177 178 #define ATF_REQUIRE_STREQ_MSG(x, y, fmt, ...) \ 179 ATF_REQUIRE_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s): " fmt, \ 180 #x, #y, x, y, ##__VA_ARGS__) 181 182 #define ATF_CHECK_STREQ_MSG(x, y, fmt, ...) \ 183 ATF_CHECK_MSG(strcmp(x, y) == 0, "%s != %s (%s != %s): " fmt, \ 184 #x, #y, x, y, ##__VA_ARGS__) 185 186 #define ATF_REQUIRE_MATCH(regexp, string) \ 187 ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \ 188 "'%s' not matched in '%s'", regexp, string); 189 190 #define ATF_CHECK_MATCH(regexp, string) \ 191 ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \ 192 "'%s' not matched in '%s'", regexp, string); 193 194 #define ATF_REQUIRE_MATCH_MSG(regexp, string, fmt, ...) \ 195 ATF_REQUIRE_MSG(atf_utils_grep_string("%s", string, regexp), \ 196 "'%s' not matched in '%s': " fmt, regexp, string, \ 197 ##__VA_ARGS__); 198 199 #define ATF_CHECK_MATCH_MSG(regexp, string, fmt, ...) \ 200 ATF_CHECK_MSG(atf_utils_grep_string("%s", string, regexp), \ 201 "'%s' not matched in '%s': " fmt, regexp, string, \ 202 ##__VA_ARGS__); 203 204 #define ATF_CHECK_ERRNO(exp_errno, bool_expr) \ 205 atf_tc_check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr) 206 207 #define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \ 208 atf_tc_require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, bool_expr) 209 210 #endif /* !defined(ATF_C_MACROS_H) */ 211