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
23 #include <boost/assert.hpp>
24 #include <boost/current_function.hpp>
25 #include <boost/core/no_exceptions_support.hpp>
29 // IDE's like Visual Studio perform better if output goes to std::cout or
30 // some other stream, so allow user to configure output stream:
31 #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
32 # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
41 struct report_errors_reminder
43 bool called_report_errors_function;
45 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 inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
84 BOOST_LIGHTWEIGHT_TEST_OSTREAM
85 << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
86 << function << "'" << std::endl;
90 // In the comparisons below, it is possible that T and U are signed and unsigned integer types, which generates warnings in some compilers.
91 // A cleaner fix would require common_type trait or some meta-programming, which would introduce a dependency on Boost.TypeTraits. To avoid
92 // the dependency we just disable the warnings.
94 # pragma warning(push)
95 # pragma warning(disable: 4389)
96 #elif defined(__clang__) && defined(__has_warning)
97 # if __has_warning("-Wsign-compare")
98 # pragma clang diagnostic push
99 # pragma clang diagnostic ignored "-Wsign-compare"
101 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
102 # pragma GCC diagnostic push
103 # pragma GCC diagnostic ignored "-Wsign-compare"
106 // specialize test output for char pointers to avoid printing as cstring
107 template <class T> inline const T& test_output_impl(const T& v) { return v; }
108 inline const void* test_output_impl(const char* v) { return v; }
109 inline const void* test_output_impl(const unsigned char* v) { return v; }
110 inline const void* test_output_impl(const signed char* v) { return v; }
111 inline const void* test_output_impl(char* v) { return v; }
112 inline const void* test_output_impl(unsigned char* v) { return v; }
113 inline const void* test_output_impl(signed char* v) { return v; }
114 template<class T> inline const void* test_output_impl(T volatile* v) { return const_cast<T*>(v); }
116 template<class T, class U> inline void test_eq_impl( char const * expr1, char const * expr2,
117 char const * file, int line, char const * function, T const & t, U const & u )
121 report_errors_remind();
125 BOOST_LIGHTWEIGHT_TEST_OSTREAM
126 << file << "(" << line << "): test '" << expr1 << " == " << expr2
127 << "' failed in function '" << function << "': "
128 << "'" << test_output_impl(t) << "' != '" << test_output_impl(u) << "'" << std::endl;
133 template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2,
134 char const * file, int line, char const * function, T const & t, U const & u )
138 report_errors_remind();
142 BOOST_LIGHTWEIGHT_TEST_OSTREAM
143 << file << "(" << line << "): test '" << expr1 << " != " << expr2
144 << "' failed in function '" << function << "': "
145 << "'" << test_output_impl(t) << "' == '" << test_output_impl(u) << "'" << std::endl;
150 inline void test_cstr_eq_impl( char const * expr1, char const * expr2,
151 char const * file, int line, char const * function, char const * const t, char const * const u )
153 if( std::strcmp(t, u) == 0 )
155 report_errors_remind();
159 BOOST_LIGHTWEIGHT_TEST_OSTREAM
160 << file << "(" << line << "): test '" << expr1 << " == " << expr2
161 << "' failed in function '" << function << "': "
162 << "'" << t << "' != '" << u << "'" << std::endl;
167 inline void test_cstr_ne_impl( char const * expr1, char const * expr2,
168 char const * file, int line, char const * function, char const * const t, char const * const u )
170 if( std::strcmp(t, u) != 0 )
172 report_errors_remind();
176 BOOST_LIGHTWEIGHT_TEST_OSTREAM
177 << file << "(" << line << "): test '" << expr1 << " == " << expr2
178 << "' failed in function '" << function << "': "
179 << "'" << t << "' == '" << u << "'" << std::endl;
184 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2>
185 void test_all_eq_impl(FormattedOutputFunction& output,
186 char const * file, int line, char const * function,
187 InputIterator1 first_begin, InputIterator1 first_end,
188 InputIterator2 second_begin, InputIterator2 second_end)
190 InputIterator1 first_it = first_begin;
191 InputIterator2 second_it = second_begin;
192 typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
193 typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
194 std::size_t error_count = 0;
195 const std::size_t max_count = 8;
198 while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
205 if ((first_it == first_end) || (second_it == second_end))
209 if (error_count == 0)
211 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
213 else if (error_count >= max_count)
218 output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
224 } while (first_it != first_end);
226 first_index += std::distance(first_it, first_end);
227 second_index += std::distance(second_it, second_end);
228 if (first_index != second_index)
230 if (error_count == 0)
232 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
236 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
241 if (error_count == 0)
243 boost::detail::report_errors_remind();
248 ++boost::detail::test_errors();
252 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2, typename BinaryPredicate>
253 void test_all_with_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,
257 BinaryPredicate predicate)
259 InputIterator1 first_it = first_begin;
260 InputIterator2 second_it = second_begin;
261 typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
262 typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
263 std::size_t error_count = 0;
264 const std::size_t max_count = 8;
267 while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it))
274 if ((first_it == first_end) || (second_it == second_end))
278 if (error_count == 0)
280 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
282 else if (error_count >= max_count)
287 output << " [" << first_index << "]";
293 } while (first_it != first_end);
295 first_index += std::distance(first_it, first_end);
296 second_index += std::distance(second_it, second_end);
297 if (first_index != second_index)
299 if (error_count == 0)
301 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
305 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
310 if (error_count == 0)
312 report_errors_remind();
321 #if defined(_MSC_VER)
322 # pragma warning(pop)
323 #elif defined(__clang__) && defined(__has_warning)
324 # if __has_warning("-Wsign-compare")
325 # pragma clang diagnostic pop
327 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
328 # pragma GCC diagnostic pop
331 } // namespace detail
333 inline int report_errors()
335 boost::detail::report_errors_remind().called_report_errors_function = true;
337 int errors = boost::detail::test_errors();
341 BOOST_LIGHTWEIGHT_TEST_OSTREAM
342 << "No errors detected." << std::endl;
347 BOOST_LIGHTWEIGHT_TEST_OSTREAM
348 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
355 #define BOOST_TEST(expr) ((expr)? (void)0: ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
356 #define BOOST_TEST_NOT(expr) BOOST_TEST(!(expr))
358 #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
360 #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
361 #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
363 #define BOOST_TEST_CSTR_EQ(expr1,expr2) ( ::boost::detail::test_cstr_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
364 #define BOOST_TEST_CSTR_NE(expr1,expr2) ( ::boost::detail::test_cstr_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
366 #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) )
367 #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) )
369 #ifndef BOOST_NO_EXCEPTIONS
370 #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
373 ::boost::detail::throw_failed_impl \
374 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
376 catch(EXCEP const&) { \
379 ::boost::detail::throw_failed_impl \
380 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
384 #define BOOST_TEST_THROWS( EXPR, EXCEP )
387 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP