1//
2// Automated Testing Framework (atf)
3//
4// Copyright (c) 2007 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_CXX_MACROS_HPP_)
31#define _ATF_CXX_MACROS_HPP_
32
33#include <sstream>
34#include <stdexcept>
35#include <vector>
36
37#include <atf-c++/tests.hpp>
38
39#define ATF_TEST_CASE_WITHOUT_HEAD(name) \
40    class atfu_tc_ ## name : public atf::tests::tc { \
41        void body(void) const; \
42    public: \
43        atfu_tc_ ## name(void) : atf::tests::tc(#name, false) {} \
44    };
45
46#define ATF_TEST_CASE(name) \
47    class atfu_tc_ ## name : public atf::tests::tc { \
48        void head(void); \
49        void body(void) const; \
50    public: \
51        atfu_tc_ ## name(void) : atf::tests::tc(#name, false) {} \
52    };
53
54#define ATF_TEST_CASE_WITH_CLEANUP(name) \
55    class atfu_tc_ ## name : public atf::tests::tc { \
56        void head(void); \
57        void body(void) const; \
58        void cleanup(void) const; \
59    public: \
60        atfu_tc_ ## name(void) : atf::tests::tc(#name, true) {} \
61    };
62
63#define ATF_TEST_CASE_NAME(name) atfu_tc_ ## name
64
65#define ATF_TEST_CASE_HEAD(name) \
66    void \
67    atfu_tc_ ## name::head(void)
68
69#define ATF_TEST_CASE_BODY(name) \
70    void \
71    atfu_tc_ ## name::body(void) \
72        const
73
74#define ATF_TEST_CASE_CLEANUP(name) \
75    void \
76    atfu_tc_ ## name::cleanup(void) \
77        const
78
79#define ATF_FAIL(reason) atf::tests::tc::fail(reason)
80
81#define ATF_SKIP(reason) atf::tests::tc::skip(reason)
82
83#define ATF_PASS() atf::tests::tc::pass()
84
85#define ATF_REQUIRE(x) \
86    do { \
87        if (!(x)) { \
88            std::ostringstream atfu_ss; \
89            atfu_ss << "Line " << __LINE__ << ": " << #x << " not met"; \
90            atf::tests::tc::fail(atfu_ss.str()); \
91        } \
92    } while (false)
93
94#define ATF_REQUIRE_EQ(x, y) \
95    do { \
96        if ((x) != (y)) { \
97            std::ostringstream atfu_ss; \
98            atfu_ss << "Line " << __LINE__ << ": " << #x << " != " << #y \
99                     << " (" << (x) << " != " << (y) << ")"; \
100            atf::tests::tc::fail(atfu_ss.str()); \
101        } \
102    } while (false)
103
104#define ATF_REQUIRE_IN(element, collection) \
105    ATF_REQUIRE((collection).find(element) != (collection).end())
106
107#define ATF_REQUIRE_NOT_IN(element, collection) \
108    ATF_REQUIRE((collection).find(element) == (collection).end())
109
110#define ATF_REQUIRE_MATCH(regexp, string) \
111    do { \
112        if (!atf::tests::detail::match(regexp, string)) { \
113            std::ostringstream atfu_ss; \
114            atfu_ss << "Line " << __LINE__ << ": '" << string << "' does not " \
115                    << "match regexp '" << regexp << "'"; \
116            atf::tests::tc::fail(atfu_ss.str()); \
117        } \
118    } while (false)
119
120#define ATF_REQUIRE_THROW(e, x) \
121    do { \
122        try { \
123            x; \
124            std::ostringstream atfu_ss; \
125            atfu_ss << "Line " << __LINE__ << ": " #x " did not throw " \
126                        #e " as expected"; \
127            atf::tests::tc::fail(atfu_ss.str()); \
128        } catch (const e&) { \
129        } catch (const std::exception& atfu_e) { \
130            std::ostringstream atfu_ss; \
131            atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
132                        "unexpected error (not " #e "): " << atfu_e.what(); \
133            atf::tests::tc::fail(atfu_ss.str()); \
134        } catch (...) { \
135            std::ostringstream atfu_ss; \
136            atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
137                        "unexpected error (not " #e ")"; \
138            atf::tests::tc::fail(atfu_ss.str()); \
139        } \
140    } while (false)
141
142#define ATF_REQUIRE_THROW_RE(type, regexp, x) \
143    do { \
144        try { \
145            x; \
146            std::ostringstream atfu_ss; \
147            atfu_ss << "Line " << __LINE__ << ": " #x " did not throw " \
148                        #type " as expected"; \
149            atf::tests::tc::fail(atfu_ss.str()); \
150        } catch (const type& e) { \
151            if (!atf::tests::detail::match(regexp, e.what())) { \
152                std::ostringstream atfu_ss; \
153                atfu_ss << "Line " << __LINE__ << ": " #x " threw " #type "(" \
154                        << e.what() << "), but does not match '" << regexp \
155                        << "'"; \
156                atf::tests::tc::fail(atfu_ss.str()); \
157            } \
158        } catch (const std::exception& atfu_e) { \
159            std::ostringstream atfu_ss; \
160            atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
161                        "unexpected error (not " #type "): " << atfu_e.what(); \
162            atf::tests::tc::fail(atfu_ss.str()); \
163        } catch (...) { \
164            std::ostringstream atfu_ss; \
165            atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
166                        "unexpected error (not " #type ")"; \
167            atf::tests::tc::fail(atfu_ss.str()); \
168        } \
169    } while (false)
170
171#define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
172    atf::tests::tc::check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, \
173                                bool_expr)
174
175#define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
176    atf::tests::tc::require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, \
177                                  bool_expr)
178
179#define ATF_INIT_TEST_CASES(tcs) \
180    namespace atf { \
181        namespace tests { \
182            int run_tp(int, char* const*, \
183                       void (*)(std::vector< atf::tests::tc * >&)); \
184        } \
185    } \
186    \
187    static void atfu_init_tcs(std::vector< atf::tests::tc * >&); \
188    \
189    int \
190    main(int argc, char* const* argv) \
191    { \
192        return atf::tests::run_tp(argc, argv, atfu_init_tcs); \
193    } \
194    \
195    static \
196    void \
197    atfu_init_tcs(std::vector< atf::tests::tc * >& tcs)
198
199#define ATF_ADD_TEST_CASE(tcs, tcname) \
200    do { \
201        atf::tests::tc* tcptr = new atfu_tc_ ## tcname(); \
202        (tcs).push_back(tcptr); \
203    } while (0);
204
205#endif // !defined(_ATF_CXX_MACROS_HPP_)
206