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 // IDE's like Visual Studio perform better if output goes to std::cout or
34 // some other stream, so allow user to configure output stream:
35 #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
36 # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
50 #if defined(_MSC_VER) && (_MSC_VER > 1310)
51 ::_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
57 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "main() should return report_errors()" << std::endl;
75 inline test_result& test_results()
77 static test_result instance;
81 inline int& test_errors()
83 return test_results().errors();
86 inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
88 BOOST_LIGHTWEIGHT_TEST_OSTREAM
89 << file << "(" << line << "): test '" << expr << "' failed in function '"
90 << function << "'" << std::endl;
91 ++test_results().errors();
94 inline void error_impl(char const * msg, char const * file, int line, char const * function)
96 BOOST_LIGHTWEIGHT_TEST_OSTREAM
97 << file << "(" << line << "): " << msg << " in function '"
98 << function << "'" << std::endl;
99 ++test_results().errors();
102 inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
104 BOOST_LIGHTWEIGHT_TEST_OSTREAM
105 << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
106 << function << "'" << std::endl;
107 ++test_results().errors();
110 // In the comparisons below, it is possible that T and U are signed and unsigned integer types, which generates warnings in some compilers.
111 // A cleaner fix would require common_type trait or some meta-programming, which would introduce a dependency on Boost.TypeTraits. To avoid
112 // the dependency we just disable the warnings.
113 #if defined(__clang__) && defined(__has_warning)
114 # if __has_warning("-Wsign-compare")
115 # pragma clang diagnostic push
116 # pragma clang diagnostic ignored "-Wsign-compare"
118 #elif defined(_MSC_VER)
119 # pragma warning(push)
120 # pragma warning(disable: 4389)
121 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
122 # pragma GCC diagnostic push
123 # pragma GCC diagnostic ignored "-Wsign-compare"
126 // specialize test output for char pointers to avoid printing as cstring
127 template <class T> inline const T& test_output_impl(const T& v) { return v; }
128 inline const void* test_output_impl(const char* v) { return v; }
129 inline const void* test_output_impl(const unsigned char* v) { return v; }
130 inline const void* test_output_impl(const signed char* v) { return v; }
131 inline const void* test_output_impl(char* v) { return v; }
132 inline const void* test_output_impl(unsigned char* v) { return v; }
133 inline const void* test_output_impl(signed char* v) { return v; }
134 template<class T> inline const void* test_output_impl(T volatile* v) { return const_cast<T*>(v); }
136 #if !defined( BOOST_NO_CXX11_NULLPTR )
137 inline const void* test_output_impl(std::nullptr_t) { return nullptr; }
141 template <typename T, typename U>
142 bool operator()(const T& t, const U& u) const { return t == u; }
143 static const char* op() { return "=="; }
147 template <typename T, typename U>
148 bool operator()(const T& t, const U& u) const { return t != u; }
149 static const char* op() { return "!="; }
153 template <typename T, typename U>
154 bool operator()(const T& t, const U& u) const { return t < u; }
155 static const char* op() { return "<"; }
159 template <typename T, typename U>
160 bool operator()(const T& t, const U& u) const { return t <= u; }
161 static const char* op() { return "<="; }
165 template <typename T, typename U>
166 bool operator()(const T& t, const U& u) const { return t > u; }
167 static const char* op() { return ">"; }
171 template <typename T, typename U>
172 bool operator()(const T& t, const U& u) const { return t >= u; }
173 static const char* op() { return ">="; }
176 template<class BinaryPredicate, class T, class U>
177 inline void test_with_impl(BinaryPredicate pred, char const * expr1, char const * expr2,
178 char const * file, int line, char const * function,
179 T const & t, U const & u)
187 BOOST_LIGHTWEIGHT_TEST_OSTREAM
188 << file << "(" << line << "): test '" << expr1 << " " << pred.op() << " " << expr2
189 << "' ('" << test_output_impl(t) << "' " << pred.op() << " '" << test_output_impl(u)
190 << "') failed in function '" << function << "'" << std::endl;
191 ++test_results().errors();
195 inline void test_cstr_eq_impl( char const * expr1, char const * expr2,
196 char const * file, int line, char const * function, char const * const t, char const * const u )
198 if( std::strcmp(t, u) == 0 )
204 BOOST_LIGHTWEIGHT_TEST_OSTREAM
205 << file << "(" << line << "): test '" << expr1 << " == " << expr2 << "' ('" << t
206 << "' == '" << u << "') failed in function '" << function << "'" << std::endl;
207 ++test_results().errors();
211 inline void test_cstr_ne_impl( char const * expr1, char const * expr2,
212 char const * file, int line, char const * function, char const * const t, char const * const u )
214 if( std::strcmp(t, u) != 0 )
220 BOOST_LIGHTWEIGHT_TEST_OSTREAM
221 << file << "(" << line << "): test '" << expr1 << " != " << expr2 << "' ('" << t
222 << "' != '" << u << "') failed in function '" << function << "'" << std::endl;
223 ++test_results().errors();
227 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2>
228 void test_all_eq_impl(FormattedOutputFunction& output,
229 char const * file, int line, char const * function,
230 InputIterator1 first_begin, InputIterator1 first_end,
231 InputIterator2 second_begin, InputIterator2 second_end)
233 InputIterator1 first_it = first_begin;
234 InputIterator2 second_it = second_begin;
235 typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
236 typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
237 std::size_t error_count = 0;
238 const std::size_t max_count = 8;
241 while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
248 if ((first_it == first_end) || (second_it == second_end))
252 if (error_count == 0)
254 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
256 else if (error_count >= max_count)
261 output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
267 } while (first_it != first_end);
269 first_index += std::distance(first_it, first_end);
270 second_index += std::distance(second_it, second_end);
271 if (first_index != second_index)
273 if (error_count == 0)
275 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
279 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
284 if (error_count == 0)
291 ++test_results().errors();
295 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2, typename BinaryPredicate>
296 void test_all_with_impl(FormattedOutputFunction& output,
297 char const * file, int line, char const * function,
298 InputIterator1 first_begin, InputIterator1 first_end,
299 InputIterator2 second_begin, InputIterator2 second_end,
300 BinaryPredicate predicate)
302 InputIterator1 first_it = first_begin;
303 InputIterator2 second_it = second_begin;
304 typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
305 typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
306 std::size_t error_count = 0;
307 const std::size_t max_count = 8;
310 while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it))
317 if ((first_it == first_end) || (second_it == second_end))
321 if (error_count == 0)
323 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
325 else if (error_count >= max_count)
330 output << " [" << first_index << "]";
336 } while (first_it != first_end);
338 first_index += std::distance(first_it, first_end);
339 second_index += std::distance(second_it, second_end);
340 if (first_index != second_index)
342 if (error_count == 0)
344 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
348 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
353 if (error_count == 0)
360 ++test_results().errors();
364 #if defined(__clang__) && defined(__has_warning)
365 # if __has_warning("-Wsign-compare")
366 # pragma clang diagnostic pop
368 #elif defined(_MSC_VER)
369 # pragma warning(pop)
370 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
371 # pragma GCC diagnostic pop
374 } // namespace detail
376 inline int report_errors()
378 boost::detail::test_result& result = boost::detail::test_results();
381 int errors = result.errors();
385 BOOST_LIGHTWEIGHT_TEST_OSTREAM
386 << "No errors detected." << std::endl;
390 BOOST_LIGHTWEIGHT_TEST_OSTREAM
391 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
394 // `return report_errors();` from main only supports 8 bit exit codes
395 return errors < 256? errors: 255;
400 #define BOOST_TEST(expr) ((expr)? (void)::boost::detail::test_results(): ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
401 #define BOOST_TEST_NOT(expr) BOOST_TEST(!(expr))
403 #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
405 #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) )
406 #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) )
408 #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) )
409 #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) )
410 #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) )
411 #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) )
413 #define BOOST_TEST_CSTR_EQ(expr1,expr2) ( ::boost::detail::test_cstr_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
414 #define BOOST_TEST_CSTR_NE(expr1,expr2) ( ::boost::detail::test_cstr_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
416 #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) )
417 #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) )
419 #ifndef BOOST_NO_EXCEPTIONS
420 #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
423 ::boost::detail::throw_failed_impl \
424 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
426 catch(EXCEP const&) { \
427 ::boost::detail::test_results(); \
430 ::boost::detail::throw_failed_impl \
431 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
435 #define BOOST_TEST_THROWS( EXPR, EXCEP )
438 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP