2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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.
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.
30 #if !defined(_ATF_CXX_MACROS_HPP_)
31 #define _ATF_CXX_MACROS_HPP_
37 #include <atf-c++/tests.hpp>
39 // Do not define inline methods for the test case classes. Doing so
40 // significantly increases the memory requirements of GNU G++ during
43 #define ATF_TEST_CASE_WITHOUT_HEAD(name) \
45 class atfu_tc_ ## name : public atf::tests::tc { \
46 void body(void) const; \
48 atfu_tc_ ## name(void); \
50 static atfu_tc_ ## name* atfu_tcptr_ ## name; \
51 atfu_tc_ ## name::atfu_tc_ ## name(void) : atf::tests::tc(#name, false) {} \
54 #define ATF_TEST_CASE(name) \
56 class atfu_tc_ ## name : public atf::tests::tc { \
58 void body(void) const; \
60 atfu_tc_ ## name(void); \
62 static atfu_tc_ ## name* atfu_tcptr_ ## name; \
63 atfu_tc_ ## name::atfu_tc_ ## name(void) : atf::tests::tc(#name, false) {} \
66 #define ATF_TEST_CASE_WITH_CLEANUP(name) \
68 class atfu_tc_ ## name : public atf::tests::tc { \
70 void body(void) const; \
71 void cleanup(void) const; \
73 atfu_tc_ ## name(void); \
75 static atfu_tc_ ## name* atfu_tcptr_ ## name; \
76 atfu_tc_ ## name::atfu_tc_ ## name(void) : atf::tests::tc(#name, true) {} \
79 #define ATF_TEST_CASE_NAME(name) atfu_tc_ ## name
80 #define ATF_TEST_CASE_USE(name) (atfu_tcptr_ ## name) = NULL
82 #define ATF_TEST_CASE_HEAD(name) \
84 atfu_tc_ ## name::head(void)
86 #define ATF_TEST_CASE_BODY(name) \
88 atfu_tc_ ## name::body(void) \
91 #define ATF_TEST_CASE_CLEANUP(name) \
93 atfu_tc_ ## name::cleanup(void) \
96 #define ATF_FAIL(reason) atf::tests::tc::fail(reason)
98 #define ATF_SKIP(reason) atf::tests::tc::skip(reason)
100 #define ATF_PASS() atf::tests::tc::pass()
102 #define ATF_REQUIRE(x) \
105 std::ostringstream atfu_ss; \
106 atfu_ss << "Line " << __LINE__ << ": " << #x << " not met"; \
107 atf::tests::tc::fail(atfu_ss.str()); \
111 #define ATF_REQUIRE_EQ(x, y) \
114 std::ostringstream atfu_ss; \
115 atfu_ss << "Line " << __LINE__ << ": " << #x << " != " << #y \
116 << " (" << (x) << " != " << (y) << ")"; \
117 atf::tests::tc::fail(atfu_ss.str()); \
121 #define ATF_REQUIRE_IN(element, collection) \
122 ATF_REQUIRE((collection).find(element) != (collection).end())
124 #define ATF_REQUIRE_NOT_IN(element, collection) \
125 ATF_REQUIRE((collection).find(element) == (collection).end())
127 #define ATF_REQUIRE_MATCH(regexp, string) \
129 if (!atf::tests::detail::match(regexp, string)) { \
130 std::ostringstream atfu_ss; \
131 atfu_ss << "Line " << __LINE__ << ": '" << string << "' does not " \
132 << "match regexp '" << regexp << "'"; \
133 atf::tests::tc::fail(atfu_ss.str()); \
137 #define ATF_REQUIRE_THROW(e, x) \
141 std::ostringstream atfu_ss; \
142 atfu_ss << "Line " << __LINE__ << ": " #x " did not throw " \
144 atf::tests::tc::fail(atfu_ss.str()); \
145 } catch (const e&) { \
146 } catch (const std::exception& atfu_e) { \
147 std::ostringstream atfu_ss; \
148 atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
149 "unexpected error (not " #e "): " << atfu_e.what(); \
150 atf::tests::tc::fail(atfu_ss.str()); \
152 std::ostringstream atfu_ss; \
153 atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
154 "unexpected error (not " #e ")"; \
155 atf::tests::tc::fail(atfu_ss.str()); \
159 #define ATF_REQUIRE_THROW_RE(type, regexp, x) \
163 std::ostringstream atfu_ss; \
164 atfu_ss << "Line " << __LINE__ << ": " #x " did not throw " \
165 #type " as expected"; \
166 atf::tests::tc::fail(atfu_ss.str()); \
167 } catch (const type& e) { \
168 if (!atf::tests::detail::match(regexp, e.what())) { \
169 std::ostringstream atfu_ss; \
170 atfu_ss << "Line " << __LINE__ << ": " #x " threw " #type "(" \
171 << e.what() << "), but does not match '" << regexp \
173 atf::tests::tc::fail(atfu_ss.str()); \
175 } catch (const std::exception& atfu_e) { \
176 std::ostringstream atfu_ss; \
177 atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
178 "unexpected error (not " #type "): " << atfu_e.what(); \
179 atf::tests::tc::fail(atfu_ss.str()); \
181 std::ostringstream atfu_ss; \
182 atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
183 "unexpected error (not " #type ")"; \
184 atf::tests::tc::fail(atfu_ss.str()); \
188 #define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
189 atf::tests::tc::check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, \
192 #define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
193 atf::tests::tc::require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, \
196 #define ATF_INIT_TEST_CASES(tcs) \
199 int run_tp(int, char* const*, \
200 void (*)(std::vector< atf::tests::tc * >&)); \
204 static void atfu_init_tcs(std::vector< atf::tests::tc * >&); \
207 main(int argc, char* const* argv) \
209 return atf::tests::run_tp(argc, argv, atfu_init_tcs); \
214 atfu_init_tcs(std::vector< atf::tests::tc * >& tcs)
216 #define ATF_ADD_TEST_CASE(tcs, tcname) \
218 atfu_tcptr_ ## tcname = new atfu_tc_ ## tcname(); \
219 (tcs).push_back(atfu_tcptr_ ## tcname); \
222 #endif // !defined(_ATF_CXX_MACROS_HPP_)