]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/core/lightweight_test.hpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / boost / core / lightweight_test.hpp
1 #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP
2 #define BOOST_CORE_LIGHTWEIGHT_TEST_HPP
3
4 // MS compatible compilers support #pragma once
5
6 #if defined(_MSC_VER)
7 # pragma once
8 #endif
9
10 //
11 // boost/core/lightweight_test.hpp - lightweight test library
12 //
13 // Copyright (c) 2002, 2009, 2014 Peter Dimov
14 // Copyright (2) Beman Dawes 2010, 2011
15 // Copyright (3) Ion Gaztanaga 2013
16 //
17 // Copyright 2018 Glen Joseph Fernandes
18 // (glenjofe@gmail.com)
19 //
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
23 //
24
25 #include <boost/current_function.hpp>
26 #include <boost/config.hpp>
27 #include <iostream>
28 #include <iterator>
29 #include <cstdlib>
30 #include <cstring>
31 #include <cstddef>
32
33 #if defined(_MSC_VER) && defined(_CPPLIB_VER) && defined(_DEBUG)
34 # include <crtdbg.h>
35 #endif
36
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
41 #endif
42
43 namespace boost
44 {
45
46 namespace detail
47 {
48
49 class test_result {
50 public:
51 test_result()
52 : report_(false)
53 , errors_(0) {
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);
57 #endif
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 );
62 #endif
63 }
64
65 ~test_result() {
66 if (!report_) {
67 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "main() should return report_errors()" << std::endl;
68 std::abort();
69 }
70 }
71
72 int& errors() {
73 return errors_;
74 }
75
76 void done() {
77 report_ = true;
78 }
79
80 private:
81 bool report_;
82 int errors_;
83 };
84
85 inline test_result& test_results()
86 {
87 static test_result instance;
88 return instance;
89 }
90
91 inline int& test_errors()
92 {
93 return test_results().errors();
94 }
95
96 inline bool test_impl(char const * expr, char const * file, int line, char const * function, bool v)
97 {
98 if( v )
99 {
100 test_results();
101 return true;
102 }
103 else
104 {
105 BOOST_LIGHTWEIGHT_TEST_OSTREAM
106 << file << "(" << line << "): test '" << expr << "' failed in function '"
107 << function << "'" << std::endl;
108 ++test_results().errors();
109 return false;
110 }
111 }
112
113 inline void error_impl(char const * msg, char const * file, int line, char const * function)
114 {
115 BOOST_LIGHTWEIGHT_TEST_OSTREAM
116 << file << "(" << line << "): " << msg << " in function '"
117 << function << "'" << std::endl;
118 ++test_results().errors();
119 }
120
121 inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
122 {
123 BOOST_LIGHTWEIGHT_TEST_OSTREAM
124 << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
125 << function << "'" << std::endl;
126 ++test_results().errors();
127 }
128
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"
136 # endif
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"
143 #endif
144
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); }
154
155 #if !defined( BOOST_NO_CXX11_NULLPTR )
156 inline const void* test_output_impl(std::nullptr_t) { return nullptr; }
157 #endif
158
159 struct lw_test_eq {
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 "=="; }
163 };
164
165 struct lw_test_ne {
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 "!="; }
169 };
170
171 struct lw_test_lt {
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 "<"; }
175 };
176
177 struct lw_test_le {
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 "<="; }
181 };
182
183 struct lw_test_gt {
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 ">"; }
187 };
188
189 struct lw_test_ge {
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 ">="; }
193 };
194
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)
199 {
200 if( pred(t, u) )
201 {
202 test_results();
203 return true;
204 }
205 else
206 {
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();
212 return false;
213 }
214 }
215
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 )
218 {
219 if( std::strcmp(t, u) == 0 )
220 {
221 test_results();
222 return true;
223 }
224 else
225 {
226 BOOST_LIGHTWEIGHT_TEST_OSTREAM
227 << file << "(" << line << "): test '" << expr1 << " == " << expr2 << "' ('" << t
228 << "' == '" << u << "') failed in function '" << function << "'" << std::endl;
229 ++test_results().errors();
230 return false;
231 }
232 }
233
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 )
236 {
237 if( std::strcmp(t, u) != 0 )
238 {
239 test_results();
240 return true;
241 }
242 else
243 {
244 BOOST_LIGHTWEIGHT_TEST_OSTREAM
245 << file << "(" << line << "): test '" << expr1 << " != " << expr2 << "' ('" << t
246 << "' != '" << u << "') failed in function '" << function << "'" << std::endl;
247 ++test_results().errors();
248 return false;
249 }
250 }
251
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)
257 {
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;
264 do
265 {
266 while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
267 {
268 ++first_it;
269 ++second_it;
270 ++first_index;
271 ++second_index;
272 }
273 if ((first_it == first_end) || (second_it == second_end))
274 {
275 break; // do-while
276 }
277 if (error_count == 0)
278 {
279 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
280 }
281 else if (error_count >= max_count)
282 {
283 output << " ...";
284 break;
285 }
286 output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
287 ++first_it;
288 ++second_it;
289 ++first_index;
290 ++second_index;
291 ++error_count;
292 } while (first_it != first_end);
293
294 first_index += std::distance(first_it, first_end);
295 second_index += std::distance(second_it, second_end);
296 if (first_index != second_index)
297 {
298 if (error_count == 0)
299 {
300 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
301 }
302 else
303 {
304 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
305 }
306 ++error_count;
307 }
308
309 if (error_count == 0)
310 {
311 test_results();
312 return true;
313 }
314 else
315 {
316 output << std::endl;
317 ++test_results().errors();
318 return false;
319 }
320 }
321
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)
328 {
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;
335 do
336 {
337 while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it))
338 {
339 ++first_it;
340 ++second_it;
341 ++first_index;
342 ++second_index;
343 }
344 if ((first_it == first_end) || (second_it == second_end))
345 {
346 break; // do-while
347 }
348 if (error_count == 0)
349 {
350 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
351 }
352 else if (error_count >= max_count)
353 {
354 output << " ...";
355 break;
356 }
357 output << " [" << first_index << "]";
358 ++first_it;
359 ++second_it;
360 ++first_index;
361 ++second_index;
362 ++error_count;
363 } while (first_it != first_end);
364
365 first_index += std::distance(first_it, first_end);
366 second_index += std::distance(second_it, second_end);
367 if (first_index != second_index)
368 {
369 if (error_count == 0)
370 {
371 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
372 }
373 else
374 {
375 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
376 }
377 ++error_count;
378 }
379
380 if (error_count == 0)
381 {
382 test_results();
383 return true;
384 }
385 else
386 {
387 output << std::endl;
388 ++test_results().errors();
389 return false;
390 }
391 }
392
393 #if defined(__clang__) && defined(__has_warning)
394 # if __has_warning("-Wsign-compare")
395 # pragma clang diagnostic pop
396 # endif
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
401 #endif
402
403 } // namespace detail
404
405 inline int report_errors()
406 {
407 boost::detail::test_result& result = boost::detail::test_results();
408 result.done();
409
410 int errors = result.errors();
411
412 if( errors == 0 )
413 {
414 BOOST_LIGHTWEIGHT_TEST_OSTREAM
415 << "No errors detected." << std::endl;
416 }
417 else
418 {
419 BOOST_LIGHTWEIGHT_TEST_OSTREAM
420 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
421 }
422
423 // `return report_errors();` from main only supports 8 bit exit codes
424 return errors < 256? errors: 255;
425 }
426
427 } // namespace boost
428
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))
431
432 #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
433
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) )
436
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) )
441
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) )
444
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) )
447
448 #ifndef BOOST_NO_EXCEPTIONS
449 #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
450 try { \
451 EXPR; \
452 ::boost::detail::throw_failed_impl \
453 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
454 } \
455 catch(EXCEP const&) { \
456 ::boost::detail::test_results(); \
457 } \
458 catch(...) { \
459 ::boost::detail::throw_failed_impl \
460 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
461 } \
462 //
463 #else
464 #define BOOST_TEST_THROWS( EXPR, EXCEP )
465 #endif
466
467 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP