1//
2// Automated Testing Framework (atf)
3//
4// Copyright (c) 2007, 2008, 2010 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_MATCH(regexp, string) \
105    do { \
106        if (!atf::tests::detail::match(regexp, string)) { \
107            std::ostringstream atfu_ss; \
108            atfu_ss << "Line " << __LINE__ << ": '" << string << "' does not " \
109                    << "match regexp '" << regexp << "'"; \
110            atf::tests::tc::fail(atfu_ss.str()); \
111        } \
112    } while (false)
113
114#define ATF_REQUIRE_THROW(e, x) \
115    do { \
116        try { \
117            x; \
118            std::ostringstream atfu_ss; \
119            atfu_ss << "Line " << __LINE__ << ": " #x " did not throw " \
120                        #e " as expected"; \
121            atf::tests::tc::fail(atfu_ss.str()); \
122        } catch (const e&) { \
123        } catch (const std::exception& atfu_e) { \
124            std::ostringstream atfu_ss; \
125            atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
126                        "unexpected error (not " #e "): " << atfu_e.what(); \
127            atf::tests::tc::fail(atfu_ss.str()); \
128        } catch (...) { \
129            std::ostringstream atfu_ss; \
130            atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
131                        "unexpected error (not " #e ")"; \
132            atf::tests::tc::fail(atfu_ss.str()); \
133        } \
134    } while (false)
135
136#define ATF_REQUIRE_THROW_RE(type, regexp, x) \
137    do { \
138        try { \
139            x; \
140            std::ostringstream atfu_ss; \
141            atfu_ss << "Line " << __LINE__ << ": " #x " did not throw " \
142                        #type " as expected"; \
143            atf::tests::tc::fail(atfu_ss.str()); \
144        } catch (const type& e) { \
145            if (!atf::tests::detail::match(regexp, e.what())) { \
146                std::ostringstream atfu_ss; \
147                atfu_ss << "Line " << __LINE__ << ": " #x " threw " #type "(" \
148                        << e.what() << "), but does not match '" << regexp \
149                        << "'"; \
150                atf::tests::tc::fail(atfu_ss.str()); \
151            } \
152        } catch (const std::exception& atfu_e) { \
153            std::ostringstream atfu_ss; \
154            atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
155                        "unexpected error (not " #type "): " << atfu_e.what(); \
156            atf::tests::tc::fail(atfu_ss.str()); \
157        } catch (...) { \
158            std::ostringstream atfu_ss; \
159            atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
160                        "unexpected error (not " #type ")"; \
161            atf::tests::tc::fail(atfu_ss.str()); \
162        } \
163    } while (false)
164
165#define ATF_CHECK_ERRNO(exp_errno, bool_expr) \
166    atf::tests::tc::check_errno(__FILE__, __LINE__, exp_errno, #bool_expr, \
167                                bool_expr)
168
169#define ATF_REQUIRE_ERRNO(exp_errno, bool_expr) \
170    atf::tests::tc::require_errno(__FILE__, __LINE__, exp_errno, #bool_expr, \
171                                  bool_expr)
172
173#define ATF_INIT_TEST_CASES(tcs) \
174    namespace atf { \
175        namespace tests { \
176            int run_tp(int, char* const*, \
177                       void (*)(std::vector< atf::tests::tc * >&)); \
178        } \
179    } \
180    \
181    static void atfu_init_tcs(std::vector< atf::tests::tc * >&); \
182    \
183    int \
184    main(int argc, char* const* argv) \
185    { \
186        return atf::tests::run_tp(argc, argv, atfu_init_tcs); \
187    } \
188    \
189    static \
190    void \
191    atfu_init_tcs(std::vector< atf::tests::tc * >& tcs)
192
193#define ATF_ADD_TEST_CASE(tcs, tcname) \
194    do { \
195        atf::tests::tc* tcptr = new atfu_tc_ ## tcname(); \
196        (tcs).push_back(tcptr); \
197    } while (0);
198
199#endif // !defined(_ATF_CXX_MACROS_HPP_)
200