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/core/no_exceptions_support.hpp>
23 #include <boost/assert.hpp>
24 #include <boost/current_function.hpp>
30 // IDE's like Visual Studio perform better if output goes to std::cout or
31 // some other stream, so allow user to configure output stream:
32 #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
33 # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
42 struct report_errors_reminder
44 bool called_report_errors_function;
46 report_errors_reminder() : called_report_errors_function(false) {}
48 ~report_errors_reminder()
50 BOOST_ASSERT(called_report_errors_function); // verify report_errors() was called
54 inline report_errors_reminder& report_errors_remind()
56 static report_errors_reminder r;
60 inline int & test_errors()
63 report_errors_remind();
67 inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
69 BOOST_LIGHTWEIGHT_TEST_OSTREAM
70 << file << "(" << line << "): test '" << expr << "' failed in function '"
71 << function << "'" << std::endl;
75 inline void error_impl(char const * msg, char const * file, int line, char const * function)
77 BOOST_LIGHTWEIGHT_TEST_OSTREAM
78 << file << "(" << line << "): " << msg << " in function '"
79 << function << "'" << std::endl;
83 inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
85 BOOST_LIGHTWEIGHT_TEST_OSTREAM
86 << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
87 << function << "'" << std::endl;
91 // In the comparisons below, it is possible that T and U are signed and unsigned integer types, which generates warnings in some compilers.
92 // A cleaner fix would require common_type trait or some meta-programming, which would introduce a dependency on Boost.TypeTraits. To avoid
93 // the dependency we just disable the warnings.
95 # pragma warning(push)
96 # pragma warning(disable: 4389)
97 #elif defined(__clang__) && defined(__has_warning)
98 # if __has_warning("-Wsign-compare")
99 # pragma clang diagnostic push
100 # pragma clang diagnostic ignored "-Wsign-compare"
102 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
103 # pragma GCC diagnostic push
104 # pragma GCC diagnostic ignored "-Wsign-compare"
107 // specialize test output for char pointers to avoid printing as cstring
108 template <class T> inline const T& test_output_impl(const T& v) { return v; }
109 inline const void* test_output_impl(const char* v) { return v; }
110 inline const void* test_output_impl(const unsigned char* v) { return v; }
111 inline const void* test_output_impl(const signed char* v) { return v; }
112 inline const void* test_output_impl(char* v) { return v; }
113 inline const void* test_output_impl(unsigned char* v) { return v; }
114 inline const void* test_output_impl(signed char* v) { return v; }
115 template<class T> inline const void* test_output_impl(T volatile* v) { return const_cast<T*>(v); }
117 #if !defined( BOOST_NO_CXX11_NULLPTR )
118 inline const void* test_output_impl(std::nullptr_t) { return nullptr; }
121 template<class T, class U> inline void test_eq_impl( char const * expr1, char const * expr2,
122 char const * file, int line, char const * function, T const & t, U const & u )
126 report_errors_remind();
130 BOOST_LIGHTWEIGHT_TEST_OSTREAM
131 << file << "(" << line << "): test '" << expr1 << " == " << expr2
132 << "' failed in function '" << function << "': "
133 << "'" << test_output_impl(t) << "' != '" << test_output_impl(u) << "'" << std::endl;
138 template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2,
139 char const * file, int line, char const * function, T const & t, U const & u )
143 report_errors_remind();
147 BOOST_LIGHTWEIGHT_TEST_OSTREAM
148 << file << "(" << line << "): test '" << expr1 << " != " << expr2
149 << "' failed in function '" << function << "': "
150 << "'" << test_output_impl(t) << "' == '" << test_output_impl(u) << "'" << std::endl;
155 template<class T, class U> inline void test_lt_impl( char const * expr1, char const * expr2,
156 char const * file, int line, char const * function, T const & t, U const & u )
160 report_errors_remind();
164 BOOST_LIGHTWEIGHT_TEST_OSTREAM
165 << file << "(" << line << "): test '" << expr1 << " < " << expr2
166 << "' failed in function '" << function << "': "
167 << "'" << test_output_impl(t) << "' >= '" << test_output_impl(u) << "'" << std::endl;
172 template<class T, class U> inline void test_le_impl( char const * expr1, char const * expr2,
173 char const * file, int line, char const * function, T const & t, U const & u )
177 report_errors_remind();
181 BOOST_LIGHTWEIGHT_TEST_OSTREAM
182 << file << "(" << line << "): test '" << expr1 << " <= " << expr2
183 << "' failed in function '" << function << "': "
184 << "'" << test_output_impl(t) << "' > '" << test_output_impl(u) << "'" << std::endl;
189 template<class T, class U> inline void test_gt_impl( char const * expr1, char const * expr2,
190 char const * file, int line, char const * function, T const & t, U const & u )
194 report_errors_remind();
198 BOOST_LIGHTWEIGHT_TEST_OSTREAM
199 << file << "(" << line << "): test '" << expr1 << " > " << expr2
200 << "' failed in function '" << function << "': "
201 << "'" << test_output_impl(t) << "' <= '" << test_output_impl(u) << "'" << std::endl;
206 template<class T, class U> inline void test_ge_impl( char const * expr1, char const * expr2,
207 char const * file, int line, char const * function, T const & t, U const & u )
211 report_errors_remind();
215 BOOST_LIGHTWEIGHT_TEST_OSTREAM
216 << file << "(" << line << "): test '" << expr1 << " >= " << expr2
217 << "' failed in function '" << function << "': "
218 << "'" << test_output_impl(t) << "' < '" << test_output_impl(u) << "'" << std::endl;
223 inline void test_cstr_eq_impl( char const * expr1, char const * expr2,
224 char const * file, int line, char const * function, char const * const t, char const * const u )
226 if( std::strcmp(t, u) == 0 )
228 report_errors_remind();
232 BOOST_LIGHTWEIGHT_TEST_OSTREAM
233 << file << "(" << line << "): test '" << expr1 << " == " << expr2
234 << "' failed in function '" << function << "': "
235 << "'" << t << "' != '" << u << "'" << std::endl;
240 inline void test_cstr_ne_impl( char const * expr1, char const * expr2,
241 char const * file, int line, char const * function, char const * const t, char const * const u )
243 if( std::strcmp(t, u) != 0 )
245 report_errors_remind();
249 BOOST_LIGHTWEIGHT_TEST_OSTREAM
250 << file << "(" << line << "): test '" << expr1 << " == " << expr2
251 << "' failed in function '" << function << "': "
252 << "'" << t << "' == '" << u << "'" << std::endl;
257 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2>
258 void test_all_eq_impl(FormattedOutputFunction& output,
259 char const * file, int line, char const * function,
260 InputIterator1 first_begin, InputIterator1 first_end,
261 InputIterator2 second_begin, InputIterator2 second_end)
263 InputIterator1 first_it = first_begin;
264 InputIterator2 second_it = second_begin;
265 typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
266 typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
267 std::size_t error_count = 0;
268 const std::size_t max_count = 8;
271 while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
278 if ((first_it == first_end) || (second_it == second_end))
282 if (error_count == 0)
284 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
286 else if (error_count >= max_count)
291 output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
297 } while (first_it != first_end);
299 first_index += std::distance(first_it, first_end);
300 second_index += std::distance(second_it, second_end);
301 if (first_index != second_index)
303 if (error_count == 0)
305 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
309 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
314 if (error_count == 0)
316 boost::detail::report_errors_remind();
321 ++boost::detail::test_errors();
325 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2, typename BinaryPredicate>
326 void test_all_with_impl(FormattedOutputFunction& output,
327 char const * file, int line, char const * function,
328 InputIterator1 first_begin, InputIterator1 first_end,
329 InputIterator2 second_begin, InputIterator2 second_end,
330 BinaryPredicate predicate)
332 InputIterator1 first_it = first_begin;
333 InputIterator2 second_it = second_begin;
334 typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
335 typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
336 std::size_t error_count = 0;
337 const std::size_t max_count = 8;
340 while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it))
347 if ((first_it == first_end) || (second_it == second_end))
351 if (error_count == 0)
353 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
355 else if (error_count >= max_count)
360 output << " [" << first_index << "]";
366 } while (first_it != first_end);
368 first_index += std::distance(first_it, first_end);
369 second_index += std::distance(second_it, second_end);
370 if (first_index != second_index)
372 if (error_count == 0)
374 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
378 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
383 if (error_count == 0)
385 report_errors_remind();
394 #if defined(_MSC_VER)
395 # pragma warning(pop)
396 #elif defined(__clang__) && defined(__has_warning)
397 # if __has_warning("-Wsign-compare")
398 # pragma clang diagnostic pop
400 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
401 # pragma GCC diagnostic pop
404 } // namespace detail
406 inline int report_errors()
408 boost::detail::report_errors_remind().called_report_errors_function = true;
410 int errors = boost::detail::test_errors();
414 BOOST_LIGHTWEIGHT_TEST_OSTREAM
415 << "No errors detected." << std::endl;
420 BOOST_LIGHTWEIGHT_TEST_OSTREAM
421 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
428 #define BOOST_TEST(expr) ((expr)? (void)0: ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
429 #define BOOST_TEST_NOT(expr) BOOST_TEST(!(expr))
431 #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
433 #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
434 #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
436 #define BOOST_TEST_LT(expr1,expr2) ( ::boost::detail::test_lt_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
437 #define BOOST_TEST_LE(expr1,expr2) ( ::boost::detail::test_le_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
438 #define BOOST_TEST_GT(expr1,expr2) ( ::boost::detail::test_gt_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
439 #define BOOST_TEST_GE(expr1,expr2) ( ::boost::detail::test_ge_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
441 #define BOOST_TEST_CSTR_EQ(expr1,expr2) ( ::boost::detail::test_cstr_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
442 #define BOOST_TEST_CSTR_NE(expr1,expr2) ( ::boost::detail::test_cstr_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
444 #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) )
445 #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) )
447 #ifndef BOOST_NO_EXCEPTIONS
448 #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
451 ::boost::detail::throw_failed_impl \
452 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
454 catch(EXCEP const&) { \
457 ::boost::detail::throw_failed_impl \
458 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
462 #define BOOST_TEST_THROWS( EXPR, EXCEP )
465 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP