1 | #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP |
2 | #define BOOST_CORE_LIGHTWEIGHT_TEST_HPP |
3 | |
4 | // MS compatible compilers support #pragma once |
5 | |
6 | #if defined(_MSC_VER) |
7 | # pragma once |
8 | #endif |
9 | |
10 | // |
11 | // boost/core/lightweight_test.hpp - lightweight test library |
12 | // |
13 | // Copyright (c) 2002, 2009, 2014 Peter Dimov |
14 | // Copyright (2) Beman Dawes 2010, 2011 |
15 | // Copyright (3) Ion Gaztanaga 2013 |
16 | // |
17 | // Distributed under the Boost Software License, Version 1.0. |
18 | // See accompanying file LICENSE_1_0.txt or copy at |
19 | // http://www.boost.org/LICENSE_1_0.txt |
20 | // |
21 | |
22 | #include <boost/assert.hpp> |
23 | #include <boost/current_function.hpp> |
24 | #include <boost/core/no_exceptions_support.hpp> |
25 | #include <iostream> |
26 | |
27 | // IDE's like Visual Studio perform better if output goes to std::cout or |
28 | // some other stream, so allow user to configure output stream: |
29 | #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM |
30 | # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr |
31 | #endif |
32 | |
33 | namespace boost |
34 | { |
35 | |
36 | namespace detail |
37 | { |
38 | |
39 | struct report_errors_reminder |
40 | { |
41 | bool called_report_errors_function; |
42 | |
43 | report_errors_reminder() : called_report_errors_function(false) {} |
44 | |
45 | ~report_errors_reminder() |
46 | { |
47 | BOOST_ASSERT(called_report_errors_function); // verify report_errors() was called |
48 | } |
49 | }; |
50 | |
51 | inline report_errors_reminder& report_errors_remind() |
52 | { |
53 | static report_errors_reminder r; |
54 | return r; |
55 | } |
56 | |
57 | inline int & test_errors() |
58 | { |
59 | static int x = 0; |
60 | report_errors_remind(); |
61 | return x; |
62 | } |
63 | |
64 | inline void test_failed_impl(char const * expr, char const * file, int line, char const * function) |
65 | { |
66 | BOOST_LIGHTWEIGHT_TEST_OSTREAM |
67 | << file << "(" << line << "): test '" << expr << "' failed in function '" |
68 | << function << "'" << std::endl; |
69 | ++test_errors(); |
70 | } |
71 | |
72 | inline void error_impl(char const * msg, char const * file, int line, char const * function) |
73 | { |
74 | BOOST_LIGHTWEIGHT_TEST_OSTREAM |
75 | << file << "(" << line << "): " << msg << " in function '" |
76 | << function << "'" << std::endl; |
77 | ++test_errors(); |
78 | } |
79 | |
80 | inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function) |
81 | { |
82 | BOOST_LIGHTWEIGHT_TEST_OSTREAM |
83 | << file << "(" << line << "): Exception '" << excep << "' not thrown in function '" |
84 | << function << "'" << std::endl; |
85 | ++test_errors(); |
86 | } |
87 | |
88 | template<class T, class U> inline void test_eq_impl( char const * expr1, char const * expr2, |
89 | char const * file, int line, char const * function, T const & t, U const & u ) |
90 | { |
91 | if( t == u ) |
92 | { |
93 | report_errors_remind(); |
94 | } |
95 | else |
96 | { |
97 | BOOST_LIGHTWEIGHT_TEST_OSTREAM |
98 | << file << "(" << line << "): test '" << expr1 << " == " << expr2 |
99 | << "' failed in function '" << function << "': " |
100 | << "'" << t << "' != '" << u << "'" << std::endl; |
101 | ++test_errors(); |
102 | } |
103 | } |
104 | |
105 | template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2, |
106 | char const * file, int line, char const * function, T const & t, U const & u ) |
107 | { |
108 | if( t != u ) |
109 | { |
110 | report_errors_remind(); |
111 | } |
112 | else |
113 | { |
114 | BOOST_LIGHTWEIGHT_TEST_OSTREAM |
115 | << file << "(" << line << "): test '" << expr1 << " != " << expr2 |
116 | << "' failed in function '" << function << "': " |
117 | << "'" << t << "' == '" << u << "'" << std::endl; |
118 | ++test_errors(); |
119 | } |
120 | } |
121 | |
122 | } // namespace detail |
123 | |
124 | inline int report_errors() |
125 | { |
126 | boost::detail::report_errors_remind().called_report_errors_function = true; |
127 | |
128 | int errors = boost::detail::test_errors(); |
129 | |
130 | if( errors == 0 ) |
131 | { |
132 | BOOST_LIGHTWEIGHT_TEST_OSTREAM |
133 | << "No errors detected." << std::endl; |
134 | return 0; |
135 | } |
136 | else |
137 | { |
138 | BOOST_LIGHTWEIGHT_TEST_OSTREAM |
139 | << errors << " error" << (errors == 1? "" : "s" ) << " detected." << std::endl; |
140 | return 1; |
141 | } |
142 | } |
143 | |
144 | } // namespace boost |
145 | |
146 | #define BOOST_TEST(expr) ((expr)? (void)0: ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)) |
147 | |
148 | #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) ) |
149 | |
150 | #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) |
151 | #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) |
152 | |
153 | #ifndef BOOST_NO_EXCEPTIONS |
154 | #define BOOST_TEST_THROWS( EXPR, EXCEP ) \ |
155 | try { \ |
156 | EXPR; \ |
157 | ::boost::detail::throw_failed_impl \ |
158 | (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \ |
159 | } \ |
160 | catch(EXCEP const&) { \ |
161 | } \ |
162 | catch(...) { \ |
163 | ::boost::detail::throw_failed_impl \ |
164 | (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \ |
165 | } \ |
166 | // |
167 | #else |
168 | #define BOOST_TEST_THROWS( EXPR, EXCEP ) |
169 | #endif |
170 | |
171 | #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP |
172 | |