1 #ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
2 #define BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // boost/detail/lightweight_test.hpp - lightweight test library
13 // Copyright (c) 2002, 2009 Peter Dimov
14 // Copyright (2) Beman Dawes 2010, 2011
16 // Distributed under the Boost Software License, Version 1.0.
17 // See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt
20 // BOOST_TEST(expression)
21 // BOOST_ERROR(message)
22 // BOOST_TEST_EQ(expr1, expr2)
24 // int boost::report_errors()
28 #include <boost/current_function.hpp>
29 #include <boost/assert.hpp>
31 // IDE's like Visual Studio perform better if output goes to std::cout or
32 // some other stream, so allow user to configure output stream:
33 #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
34 # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
43 struct report_errors_reminder
45 bool called_report_errors_function;
46 report_errors_reminder() : called_report_errors_function(false) {}
47 ~report_errors_reminder()
49 BOOST_ASSERT(called_report_errors_function); // verify report_errors() was called
53 inline report_errors_reminder& report_errors_remind()
55 static report_errors_reminder r;
59 inline int & test_errors()
62 report_errors_remind();
66 inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
68 BOOST_LIGHTWEIGHT_TEST_OSTREAM
69 << file << "(" << line << "): test '" << expr << "' failed in function '"
70 << function << "'" << std::endl;
74 inline void error_impl(char const * msg, char const * file, int line, char const * function)
76 BOOST_LIGHTWEIGHT_TEST_OSTREAM
77 << file << "(" << line << "): " << msg << " in function '"
78 << function << "'" << std::endl;
82 template<class T, class U> inline void test_eq_impl( char const * expr1, char const * expr2,
83 char const * file, int line, char const * function, T const & t, U const & u )
90 BOOST_LIGHTWEIGHT_TEST_OSTREAM
91 << file << "(" << line << "): test '" << expr1 << " == " << expr2
92 << "' failed in function '" << function << "': "
93 << "'" << t << "' != '" << u << "'" << std::endl;
98 template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2,
99 char const * file, int line, char const * function, T const & t, U const & u )
106 BOOST_LIGHTWEIGHT_TEST_OSTREAM
107 << file << "(" << line << "): test '" << expr1 << " != " << expr2
108 << "' failed in function '" << function << "': "
109 << "'" << t << "' == '" << u << "'" << std::endl;
114 } // namespace detail
116 inline int report_errors()
118 detail::report_errors_remind().called_report_errors_function = true;
120 int errors = detail::test_errors();
124 BOOST_LIGHTWEIGHT_TEST_OSTREAM
125 << "No errors detected." << std::endl;
130 BOOST_LIGHTWEIGHT_TEST_OSTREAM
131 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
138 #define BOOST_TEST(expr) ((expr)? (void)0: ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
139 #define BOOST_ERROR(msg) ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
140 #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
141 #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
143 #endif // #ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED