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>
34 #if defined(_MSC_VER) && defined(_CPPLIB_VER) && defined(_DEBUG)
38 // IDE's like Visual Studio perform better if output goes to std::cout or
39 // some other stream, so allow user to configure output stream:
40 #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
41 # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
55 #if defined(_MSC_VER) && (_MSC_VER > 1310)
56 // disable message boxes on assert(), abort()
57 ::_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
59 #if defined(_MSC_VER) && defined(_CPPLIB_VER) && defined(_DEBUG)
60 // disable message boxes on iterator debugging violations
61 _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
62 _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDERR );
68 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "main() should return report_errors()" << std::endl;
86 inline test_result& test_results()
88 static test_result instance;
92 inline int& test_errors()
94 return test_results().errors();
97 inline bool test_impl(char const * expr, char const * file, int line, char const * function, bool v)
106 BOOST_LIGHTWEIGHT_TEST_OSTREAM
107 << file << "(" << line << "): test '" << expr << "' failed in function '"
108 << function << "'" << std::endl;
109 ++test_results().errors();
114 inline void error_impl(char const * msg, char const * file, int line, char const * function)
116 BOOST_LIGHTWEIGHT_TEST_OSTREAM
117 << file << "(" << line << "): " << msg << " in function '"
118 << function << "'" << std::endl;
119 ++test_results().errors();
122 inline void throw_failed_impl(const char* expr, char const * excep, char const * file, int line, char const * function)
124 BOOST_LIGHTWEIGHT_TEST_OSTREAM
125 << file << "(" << line << "): expression '" << expr << "' did not throw exception '" << excep << "' in function '"
126 << function << "'" << std::endl;
127 ++test_results().errors();
130 inline void no_throw_failed_impl(const char* expr, const char* file, int line, const char* function)
132 BOOST_LIGHTWEIGHT_TEST_OSTREAM
133 << file << "(" << line << "): expression '" << expr << "' threw an exception in function '"
134 << function << "'" << std::endl;
135 ++test_results().errors();
138 inline void no_throw_failed_impl(const char* expr, const char* what, const char* file, int line, const char* function)
140 BOOST_LIGHTWEIGHT_TEST_OSTREAM
141 << file << "(" << line << "): expression '" << expr << "' threw an exception in function '"
142 << function << "': " << what << std::endl;
143 ++test_results().errors();
146 // In the comparisons below, it is possible that T and U are signed and unsigned integer types, which generates warnings in some compilers.
147 // A cleaner fix would require common_type trait or some meta-programming, which would introduce a dependency on Boost.TypeTraits. To avoid
148 // the dependency we just disable the warnings.
149 #if defined(__clang__) && defined(__has_warning)
150 # if __has_warning("-Wsign-compare")
151 # pragma clang diagnostic push
152 # pragma clang diagnostic ignored "-Wsign-compare"
154 #elif defined(_MSC_VER)
155 # pragma warning(push)
156 # pragma warning(disable: 4389)
157 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
158 # pragma GCC diagnostic push
159 # pragma GCC diagnostic ignored "-Wsign-compare"
162 // specialize test output for char pointers to avoid printing as cstring
163 template <class T> inline const T& test_output_impl(const T& v) { return v; }
164 inline const void* test_output_impl(const char* v) { return v; }
165 inline const void* test_output_impl(const unsigned char* v) { return v; }
166 inline const void* test_output_impl(const signed char* v) { return v; }
167 inline const void* test_output_impl(char* v) { return v; }
168 inline const void* test_output_impl(unsigned char* v) { return v; }
169 inline const void* test_output_impl(signed char* v) { return v; }
170 template<class T> inline const void* test_output_impl(T volatile* v) { return const_cast<T*>(v); }
172 #if !defined( BOOST_NO_CXX11_NULLPTR )
173 inline const void* test_output_impl(std::nullptr_t) { return nullptr; }
180 template <typename T, typename U>
181 bool operator()(const T& t, const U& u) const { return t == u; }
186 template <typename T, typename U>
187 bool operator()(const T& t, const U& u) const { return t != u; }
192 template <typename T, typename U>
193 bool operator()(const T& t, const U& u) const { return t < u; }
198 template <typename T, typename U>
199 bool operator()(const T& t, const U& u) const { return t <= u; }
204 template <typename T, typename U>
205 bool operator()(const T& t, const U& u) const { return t > u; }
210 template <typename T, typename U>
211 bool operator()(const T& t, const U& u) const { return t >= u; }
214 // lwt_predicate_name
216 template<class T> char const * lwt_predicate_name( T const& )
221 inline char const * lwt_predicate_name( lw_test_eq const& )
226 inline char const * lwt_predicate_name( lw_test_ne const& )
231 inline char const * lwt_predicate_name( lw_test_lt const& )
236 inline char const * lwt_predicate_name( lw_test_le const& )
241 inline char const * lwt_predicate_name( lw_test_gt const& )
246 inline char const * lwt_predicate_name( lw_test_ge const& )
253 template<class BinaryPredicate, class T, class U>
254 inline bool test_with_impl(BinaryPredicate pred, char const * expr1, char const * expr2,
255 char const * file, int line, char const * function,
256 T const & t, U const & u)
265 BOOST_LIGHTWEIGHT_TEST_OSTREAM
266 << file << "(" << line << "): test '" << expr1 << " " << lwt_predicate_name(pred) << " " << expr2
267 << "' ('" << test_output_impl(t) << "' " << lwt_predicate_name(pred) << " '" << test_output_impl(u)
268 << "') failed in function '" << function << "'" << std::endl;
269 ++test_results().errors();
274 inline bool test_cstr_eq_impl( char const * expr1, char const * expr2,
275 char const * file, int line, char const * function, char const * const t, char const * const u )
277 if( std::strcmp(t, u) == 0 )
284 BOOST_LIGHTWEIGHT_TEST_OSTREAM
285 << file << "(" << line << "): test '" << expr1 << " == " << expr2 << "' ('" << t
286 << "' == '" << u << "') failed in function '" << function << "'" << std::endl;
287 ++test_results().errors();
292 inline bool test_cstr_ne_impl( char const * expr1, char const * expr2,
293 char const * file, int line, char const * function, char const * const t, char const * const u )
295 if( std::strcmp(t, u) != 0 )
302 BOOST_LIGHTWEIGHT_TEST_OSTREAM
303 << file << "(" << line << "): test '" << expr1 << " != " << expr2 << "' ('" << t
304 << "' != '" << u << "') failed in function '" << function << "'" << std::endl;
305 ++test_results().errors();
310 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2>
311 bool test_all_eq_impl(FormattedOutputFunction& output,
312 char const * file, int line, char const * function,
313 InputIterator1 first_begin, InputIterator1 first_end,
314 InputIterator2 second_begin, InputIterator2 second_end)
316 InputIterator1 first_it = first_begin;
317 InputIterator2 second_it = second_begin;
318 typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
319 typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
320 std::size_t error_count = 0;
321 const std::size_t max_count = 8;
324 while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
331 if ((first_it == first_end) || (second_it == second_end))
335 if (error_count == 0)
337 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
339 else if (error_count >= max_count)
344 output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
350 } while (first_it != first_end);
352 first_index += std::distance(first_it, first_end);
353 second_index += std::distance(second_it, second_end);
354 if (first_index != second_index)
356 if (error_count == 0)
358 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
362 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
367 if (error_count == 0)
375 ++test_results().errors();
380 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2, typename BinaryPredicate>
381 bool test_all_with_impl(FormattedOutputFunction& output,
382 char const * file, int line, char const * function,
383 InputIterator1 first_begin, InputIterator1 first_end,
384 InputIterator2 second_begin, InputIterator2 second_end,
385 BinaryPredicate predicate)
387 InputIterator1 first_it = first_begin;
388 InputIterator2 second_it = second_begin;
389 typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
390 typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
391 std::size_t error_count = 0;
392 const std::size_t max_count = 8;
395 while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it))
402 if ((first_it == first_end) || (second_it == second_end))
406 if (error_count == 0)
408 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
410 else if (error_count >= max_count)
415 output << " [" << first_index << "]";
421 } while (first_it != first_end);
423 first_index += std::distance(first_it, first_end);
424 second_index += std::distance(second_it, second_end);
425 if (first_index != second_index)
427 if (error_count == 0)
429 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
433 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
438 if (error_count == 0)
446 ++test_results().errors();
451 #if defined(__clang__) && defined(__has_warning)
452 # if __has_warning("-Wsign-compare")
453 # pragma clang diagnostic pop
455 #elif defined(_MSC_VER)
456 # pragma warning(pop)
457 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
458 # pragma GCC diagnostic pop
461 } // namespace detail
463 inline int report_errors()
465 boost::detail::test_result& result = boost::detail::test_results();
468 int errors = result.errors();
472 BOOST_LIGHTWEIGHT_TEST_OSTREAM
473 << "No errors detected." << std::endl;
477 BOOST_LIGHTWEIGHT_TEST_OSTREAM
478 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
481 // `return report_errors();` from main only supports 8 bit exit codes
482 return errors < 256? errors: 255;
487 #define BOOST_TEST(expr) ( ::boost::detail::test_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, (expr)? true: false) )
488 #define BOOST_TEST_NOT(expr) BOOST_TEST(!(expr))
490 #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
492 #define BOOST_TEST_WITH(expr1,expr2,predicate) ( ::boost::detail::test_with_impl(predicate, #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
494 #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) )
495 #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) )
497 #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) )
498 #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) )
499 #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) )
500 #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) )
502 #define BOOST_TEST_CSTR_EQ(expr1,expr2) ( ::boost::detail::test_cstr_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
503 #define BOOST_TEST_CSTR_NE(expr1,expr2) ( ::boost::detail::test_cstr_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
505 #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) )
506 #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) )
508 #ifndef BOOST_NO_EXCEPTIONS
509 #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
512 ::boost::detail::throw_failed_impl \
513 (#EXPR, #EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
515 catch(EXCEP const&) { \
516 ::boost::detail::test_results(); \
519 ::boost::detail::throw_failed_impl \
520 (#EXPR, #EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
524 #define BOOST_TEST_THROWS( EXPR, EXCEP )
527 #ifndef BOOST_NO_EXCEPTIONS
528 # define BOOST_TEST_NO_THROW(EXPR) \
531 } catch (const std::exception& e) { \
532 ::boost::detail::no_throw_failed_impl \
533 (#EXPR, e.what(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
535 ::boost::detail::no_throw_failed_impl \
536 (#EXPR, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
540 # define BOOST_TEST_NO_THROW(EXPR) { EXPR; }
543 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP