1 #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP
2 #define BOOST_CORE_LIGHTWEIGHT_TEST_HPP
4 // MS compatible compilers support #pragma once
11 // boost/core/lightweight_test.hpp - lightweight test library
13 // Copyright (c) 2002, 2009, 2014 Peter Dimov
14 // Copyright (2) Beman Dawes 2010, 2011
15 // Copyright (3) Ion Gaztanaga 2013
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
22 #include <boost/assert.hpp>
23 #include <boost/current_function.hpp>
24 #include <boost/core/no_exceptions_support.hpp>
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
39 struct report_errors_reminder
41 bool called_report_errors_function;
43 report_errors_reminder() : called_report_errors_function(false) {}
45 ~report_errors_reminder()
47 BOOST_ASSERT(called_report_errors_function); // verify report_errors() was called
51 inline report_errors_reminder& report_errors_remind()
53 static report_errors_reminder r;
57 inline int & test_errors()
60 report_errors_remind();
64 inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
66 BOOST_LIGHTWEIGHT_TEST_OSTREAM
67 << file << "(" << line << "): test '" << expr << "' failed in function '"
68 << function << "'" << std::endl;
72 inline void error_impl(char const * msg, char const * file, int line, char const * function)
74 BOOST_LIGHTWEIGHT_TEST_OSTREAM
75 << file << "(" << line << "): " << msg << " in function '"
76 << function << "'" << std::endl;
80 inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
82 BOOST_LIGHTWEIGHT_TEST_OSTREAM
83 << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
84 << function << "'" << std::endl;
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 )
93 report_errors_remind();
97 BOOST_LIGHTWEIGHT_TEST_OSTREAM
98 << file << "(" << line << "): test '" << expr1 << " == " << expr2
99 << "' failed in function '" << function << "': "
100 << "'" << t << "' != '" << u << "'" << std::endl;
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 )
110 report_errors_remind();
114 BOOST_LIGHTWEIGHT_TEST_OSTREAM
115 << file << "(" << line << "): test '" << expr1 << " != " << expr2
116 << "' failed in function '" << function << "': "
117 << "'" << t << "' == '" << u << "'" << std::endl;
122 } // namespace detail
124 inline int report_errors()
126 boost::detail::report_errors_remind().called_report_errors_function = true;
128 int errors = boost::detail::test_errors();
132 BOOST_LIGHTWEIGHT_TEST_OSTREAM
133 << "No errors detected." << std::endl;
138 BOOST_LIGHTWEIGHT_TEST_OSTREAM
139 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
146 #define BOOST_TEST(expr) ((expr)? (void)0: ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
147 #define BOOST_TEST_NOT(expr) BOOST_TEST(!(expr))
149 #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
151 #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
152 #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
154 #ifndef BOOST_NO_EXCEPTIONS
155 #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
158 ::boost::detail::throw_failed_impl \
159 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
161 catch(EXCEP const&) { \
164 ::boost::detail::throw_failed_impl \
165 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
169 #define BOOST_TEST_THROWS( EXPR, EXCEP )
172 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP