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 // Copyright 2018 Glen Joseph Fernandes
18 // (glenjofe@gmail.com)
20 // Distributed under the Boost Software License, Version 1.0.
21 // See accompanying file LICENSE_1_0.txt or copy at
22 // http://www.boost.org/LICENSE_1_0.txt
25 #include <boost/current_function.hpp>
26 #include <boost/config.hpp>
33 #if defined(_MSC_VER) && defined(_CPPLIB_VER) && defined(_DEBUG)
37 // IDE's like Visual Studio perform better if output goes to std::cout or
38 // some other stream, so allow user to configure output stream:
39 #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
40 # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
54 #if defined(_MSC_VER) && (_MSC_VER > 1310)
55 // disable message boxes on assert(), abort()
56 ::_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
58 #if defined(_MSC_VER) && defined(_CPPLIB_VER) && defined(_DEBUG)
59 // disable message boxes on iterator debugging violations
60 _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
61 _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDERR );
67 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "main() should return report_errors()" << std::endl;
85 inline test_result& test_results()
87 static test_result instance;
91 inline int& test_errors()
93 return test_results().errors();
96 inline bool test_impl(char const * expr, char const * file, int line, char const * function, bool v)
105 BOOST_LIGHTWEIGHT_TEST_OSTREAM
106 << file << "(" << line << "): test '" << expr << "' failed in function '"
107 << function << "'" << std::endl;
108 ++test_results().errors();
113 inline void error_impl(char const * msg, char const * file, int line, char const * function)
115 BOOST_LIGHTWEIGHT_TEST_OSTREAM
116 << file << "(" << line << "): " << msg << " in function '"
117 << function << "'" << std::endl;
118 ++test_results().errors();
121 inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
123 BOOST_LIGHTWEIGHT_TEST_OSTREAM
124 << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
125 << function << "'" << std::endl;
126 ++test_results().errors();
129 // In the comparisons below, it is possible that T and U are signed and unsigned integer types, which generates warnings in some compilers.
130 // A cleaner fix would require common_type trait or some meta-programming, which would introduce a dependency on Boost.TypeTraits. To avoid
131 // the dependency we just disable the warnings.
132 #if defined(__clang__) && defined(__has_warning)
133 # if __has_warning("-Wsign-compare")
134 # pragma clang diagnostic push
135 # pragma clang diagnostic ignored "-Wsign-compare"
137 #elif defined(_MSC_VER)
138 # pragma warning(push)
139 # pragma warning(disable: 4389)
140 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
141 # pragma GCC diagnostic push
142 # pragma GCC diagnostic ignored "-Wsign-compare"
145 // specialize test output for char pointers to avoid printing as cstring
146 template <class T> inline const T& test_output_impl(const T& v) { return v; }
147 inline const void* test_output_impl(const char* v) { return v; }
148 inline const void* test_output_impl(const unsigned char* v) { return v; }
149 inline const void* test_output_impl(const signed char* v) { return v; }
150 inline const void* test_output_impl(char* v) { return v; }
151 inline const void* test_output_impl(unsigned char* v) { return v; }
152 inline const void* test_output_impl(signed char* v) { return v; }
153 template<class T> inline const void* test_output_impl(T volatile* v) { return const_cast<T*>(v); }
155 #if !defined( BOOST_NO_CXX11_NULLPTR )
156 inline const void* test_output_impl(std::nullptr_t) { return nullptr; }
160 template <typename T, typename U>
161 bool operator()(const T& t, const U& u) const { return t == u; }
162 static const char* op() { return "=="; }
166 template <typename T, typename U>
167 bool operator()(const T& t, const U& u) const { return t != u; }
168 static const char* op() { return "!="; }
172 template <typename T, typename U>
173 bool operator()(const T& t, const U& u) const { return t < u; }
174 static const char* op() { return "<"; }
178 template <typename T, typename U>
179 bool operator()(const T& t, const U& u) const { return t <= u; }
180 static const char* op() { return "<="; }
184 template <typename T, typename U>
185 bool operator()(const T& t, const U& u) const { return t > u; }
186 static const char* op() { return ">"; }
190 template <typename T, typename U>
191 bool operator()(const T& t, const U& u) const { return t >= u; }
192 static const char* op() { return ">="; }
195 template<class BinaryPredicate, class T, class U>
196 inline bool test_with_impl(BinaryPredicate pred, char const * expr1, char const * expr2,
197 char const * file, int line, char const * function,
198 T const & t, U const & u)
207 BOOST_LIGHTWEIGHT_TEST_OSTREAM
208 << file << "(" << line << "): test '" << expr1 << " " << pred.op() << " " << expr2
209 << "' ('" << test_output_impl(t) << "' " << pred.op() << " '" << test_output_impl(u)
210 << "') failed in function '" << function << "'" << std::endl;
211 ++test_results().errors();
216 inline bool test_cstr_eq_impl( char const * expr1, char const * expr2,
217 char const * file, int line, char const * function, char const * const t, char const * const u )
219 if( std::strcmp(t, u) == 0 )
226 BOOST_LIGHTWEIGHT_TEST_OSTREAM
227 << file << "(" << line << "): test '" << expr1 << " == " << expr2 << "' ('" << t
228 << "' == '" << u << "') failed in function '" << function << "'" << std::endl;
229 ++test_results().errors();
234 inline bool test_cstr_ne_impl( char const * expr1, char const * expr2,
235 char const * file, int line, char const * function, char const * const t, char const * const u )
237 if( std::strcmp(t, u) != 0 )
244 BOOST_LIGHTWEIGHT_TEST_OSTREAM
245 << file << "(" << line << "): test '" << expr1 << " != " << expr2 << "' ('" << t
246 << "' != '" << u << "') failed in function '" << function << "'" << std::endl;
247 ++test_results().errors();
252 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2>
253 bool test_all_eq_impl(FormattedOutputFunction& output,
254 char const * file, int line, char const * function,
255 InputIterator1 first_begin, InputIterator1 first_end,
256 InputIterator2 second_begin, InputIterator2 second_end)
258 InputIterator1 first_it = first_begin;
259 InputIterator2 second_it = second_begin;
260 typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
261 typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
262 std::size_t error_count = 0;
263 const std::size_t max_count = 8;
266 while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
273 if ((first_it == first_end) || (second_it == second_end))
277 if (error_count == 0)
279 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
281 else if (error_count >= max_count)
286 output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
292 } while (first_it != first_end);
294 first_index += std::distance(first_it, first_end);
295 second_index += std::distance(second_it, second_end);
296 if (first_index != second_index)
298 if (error_count == 0)
300 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
304 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
309 if (error_count == 0)
317 ++test_results().errors();
322 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2, typename BinaryPredicate>
323 bool test_all_with_impl(FormattedOutputFunction& output,
324 char const * file, int line, char const * function,
325 InputIterator1 first_begin, InputIterator1 first_end,
326 InputIterator2 second_begin, InputIterator2 second_end,
327 BinaryPredicate predicate)
329 InputIterator1 first_it = first_begin;
330 InputIterator2 second_it = second_begin;
331 typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
332 typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
333 std::size_t error_count = 0;
334 const std::size_t max_count = 8;
337 while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it))
344 if ((first_it == first_end) || (second_it == second_end))
348 if (error_count == 0)
350 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
352 else if (error_count >= max_count)
357 output << " [" << first_index << "]";
363 } while (first_it != first_end);
365 first_index += std::distance(first_it, first_end);
366 second_index += std::distance(second_it, second_end);
367 if (first_index != second_index)
369 if (error_count == 0)
371 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
375 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
380 if (error_count == 0)
388 ++test_results().errors();
393 #if defined(__clang__) && defined(__has_warning)
394 # if __has_warning("-Wsign-compare")
395 # pragma clang diagnostic pop
397 #elif defined(_MSC_VER)
398 # pragma warning(pop)
399 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
400 # pragma GCC diagnostic pop
403 } // namespace detail
405 inline int report_errors()
407 boost::detail::test_result& result = boost::detail::test_results();
410 int errors = result.errors();
414 BOOST_LIGHTWEIGHT_TEST_OSTREAM
415 << "No errors detected." << std::endl;
419 BOOST_LIGHTWEIGHT_TEST_OSTREAM
420 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
423 // `return report_errors();` from main only supports 8 bit exit codes
424 return errors < 256? errors: 255;
429 #define BOOST_TEST(expr) ( ::boost::detail::test_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, (expr)? true: false) )
430 #define BOOST_TEST_NOT(expr) BOOST_TEST(!(expr))
432 #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
434 #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_eq(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
435 #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_ne(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
437 #define BOOST_TEST_LT(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_lt(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
438 #define BOOST_TEST_LE(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_le(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
439 #define BOOST_TEST_GT(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_gt(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
440 #define BOOST_TEST_GE(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_ge(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
442 #define BOOST_TEST_CSTR_EQ(expr1,expr2) ( ::boost::detail::test_cstr_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
443 #define BOOST_TEST_CSTR_NE(expr1,expr2) ( ::boost::detail::test_cstr_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
445 #define BOOST_TEST_ALL_EQ(begin1, end1, begin2, end2) ( ::boost::detail::test_all_eq_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2) )
446 #define BOOST_TEST_ALL_WITH(begin1, end1, begin2, end2, predicate) ( ::boost::detail::test_all_with_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2, predicate) )
448 #ifndef BOOST_NO_EXCEPTIONS
449 #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
452 ::boost::detail::throw_failed_impl \
453 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
455 catch(EXCEP const&) { \
456 ::boost::detail::test_results(); \
459 ::boost::detail::throw_failed_impl \
460 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
464 #define BOOST_TEST_THROWS( EXPR, EXCEP )
467 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP