xref: /netbsd-src/external/bsd/atf/dist/atf-c++/macros.hpp (revision a7e090f70e491979434963c9a27df4020fe0a18b)
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007, 2008 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(name) \
40     class atfu_tc_ ## name : public atf::tests::tc { \
41         void head(void); \
42         void body(void) const; \
43     public: \
44         atfu_tc_ ## name(void) : atf::tests::tc(#name) {} \
45     };
46 
47 #define ATF_TEST_CASE_WITH_CLEANUP(name) \
48     class atfu_tc_ ## name : public atf::tests::tc { \
49         void head(void); \
50         void body(void) const; \
51         void cleanup(void) const; \
52     public: \
53         atfu_tc_ ## name(void) : atf::tests::tc(#name) {} \
54     };
55 
56 #define ATF_TEST_CASE_NAME(name) atfu_tc_ ## name
57 
58 #define ATF_TEST_CASE_HEAD(name) \
59     void \
60     atfu_tc_ ## name::head(void)
61 
62 #define ATF_TEST_CASE_BODY(name) \
63     void \
64     atfu_tc_ ## name::body(void) \
65         const
66 
67 #define ATF_TEST_CASE_CLEANUP(name) \
68     void \
69     atfu_tc_ ## name::cleanup(void) \
70         const
71 
72 #define ATF_FAIL(reason) atf::tests::tc::fail(reason)
73 
74 #define ATF_SKIP(reason) atf::tests::tc::skip(reason)
75 
76 #define ATF_PASS() atf::tests::tc::pass()
77 
78 #define ATF_CHECK(x) \
79     do { \
80         if (!(x)) { \
81             std::ostringstream atfu_ss; \
82             atfu_ss << "Line " << __LINE__ << ": " << #x << " not met"; \
83             atf::tests::tc::fail(atfu_ss.str()); \
84         } \
85     } while (false)
86 
87 #define ATF_CHECK_EQUAL(x, y) \
88     do { \
89         if ((x) != (y)) { \
90             std::ostringstream atfu_ss; \
91             atfu_ss << "Line " << __LINE__ << ": " << #x << " != " << #y \
92                      << " (" << (x) << " != " << (y) << ")"; \
93             atf::tests::tc::fail(atfu_ss.str()); \
94         } \
95     } while (false)
96 
97 #define ATF_CHECK_THROW(x, e) \
98     do { \
99         try { \
100             x; \
101             std::ostringstream atfu_ss; \
102             atfu_ss << "Line " << __LINE__ << ": " #x " did not throw " \
103                         #e " as expected"; \
104             atf::tests::tc::fail(atfu_ss.str()); \
105         } catch (const e& atfu_eo) { \
106         } catch (const std::exception& atfu_e) { \
107             std::ostringstream atfu_ss; \
108             atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
109                         "unexpected error (not " #e "): " << atfu_e.what(); \
110             atf::tests::tc::fail(atfu_ss.str()); \
111         } catch (...) { \
112             std::ostringstream atfu_ss; \
113             atfu_ss << "Line " << __LINE__ << ": " #x " threw an " \
114                         "unexpected error (not " #e ")"; \
115             atf::tests::tc::fail(atfu_ss.str()); \
116         } \
117     } while (false)
118 
119 #define ATF_INIT_TEST_CASES(tcs) \
120     namespace atf { \
121         namespace tests { \
122             int run_tp(int, char* const*, \
123                        void (*)(std::vector< atf::tests::tc * >&)); \
124         } \
125     } \
126     \
127     static void atfu_init_tcs(std::vector< atf::tests::tc * >&); \
128     \
129     int \
130     main(int argc, char* const* argv) \
131     { \
132         return atf::tests::run_tp(argc, argv, atfu_init_tcs); \
133     } \
134     \
135     static \
136     void \
137     atfu_init_tcs(std::vector< atf::tests::tc * >& tcs)
138 
139 #define ATF_ADD_TEST_CASE(tcs, tcname) \
140     do { \
141         atf::tests::tc* tcptr = new atfu_tc_ ## tcname(); \
142         (tcs).push_back(tcptr); \
143     } while (0);
144 
145 #endif // !defined(_ATF_CXX_MACROS_HPP_)
146