]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/core/lightweight_test.hpp
update sources to v12.2.3
[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 // 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
20 //
21
22 #include <boost/core/no_exceptions_support.hpp>
23 #include <boost/assert.hpp>
24 #include <boost/current_function.hpp>
25 #include <iostream>
26 #include <iterator>
27 #include <cstring>
28 #include <cstddef>
29
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
34 #endif
35
36 namespace boost
37 {
38
39 namespace detail
40 {
41
42 struct report_errors_reminder
43 {
44 bool called_report_errors_function;
45
46 report_errors_reminder() : called_report_errors_function(false) {}
47
48 ~report_errors_reminder()
49 {
50 BOOST_ASSERT(called_report_errors_function); // verify report_errors() was called
51 }
52 };
53
54 inline report_errors_reminder& report_errors_remind()
55 {
56 static report_errors_reminder r;
57 return r;
58 }
59
60 inline int & test_errors()
61 {
62 static int x = 0;
63 report_errors_remind();
64 return x;
65 }
66
67 inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
68 {
69 BOOST_LIGHTWEIGHT_TEST_OSTREAM
70 << file << "(" << line << "): test '" << expr << "' failed in function '"
71 << function << "'" << std::endl;
72 ++test_errors();
73 }
74
75 inline void error_impl(char const * msg, char const * file, int line, char const * function)
76 {
77 BOOST_LIGHTWEIGHT_TEST_OSTREAM
78 << file << "(" << line << "): " << msg << " in function '"
79 << function << "'" << std::endl;
80 ++test_errors();
81 }
82
83 inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
84 {
85 BOOST_LIGHTWEIGHT_TEST_OSTREAM
86 << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
87 << function << "'" << std::endl;
88 ++test_errors();
89 }
90
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.
94 #if defined(_MSC_VER)
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"
101 # endif
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"
105 #endif
106
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); }
116
117 #if !defined( BOOST_NO_CXX11_NULLPTR )
118 inline const void* test_output_impl(std::nullptr_t) { return nullptr; }
119 #endif
120
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 )
123 {
124 if( t == u )
125 {
126 report_errors_remind();
127 }
128 else
129 {
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;
134 ++test_errors();
135 }
136 }
137
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 )
140 {
141 if( t != u )
142 {
143 report_errors_remind();
144 }
145 else
146 {
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;
151 ++test_errors();
152 }
153 }
154
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 )
157 {
158 if( t < u )
159 {
160 report_errors_remind();
161 }
162 else
163 {
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;
168 ++test_errors();
169 }
170 }
171
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 )
174 {
175 if( t <= u )
176 {
177 report_errors_remind();
178 }
179 else
180 {
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;
185 ++test_errors();
186 }
187 }
188
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 )
191 {
192 if( t > u )
193 {
194 report_errors_remind();
195 }
196 else
197 {
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;
202 ++test_errors();
203 }
204 }
205
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 )
208 {
209 if( t >= u )
210 {
211 report_errors_remind();
212 }
213 else
214 {
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;
219 ++test_errors();
220 }
221 }
222
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 )
225 {
226 if( std::strcmp(t, u) == 0 )
227 {
228 report_errors_remind();
229 }
230 else
231 {
232 BOOST_LIGHTWEIGHT_TEST_OSTREAM
233 << file << "(" << line << "): test '" << expr1 << " == " << expr2
234 << "' failed in function '" << function << "': "
235 << "'" << t << "' != '" << u << "'" << std::endl;
236 ++test_errors();
237 }
238 }
239
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 )
242 {
243 if( std::strcmp(t, u) != 0 )
244 {
245 report_errors_remind();
246 }
247 else
248 {
249 BOOST_LIGHTWEIGHT_TEST_OSTREAM
250 << file << "(" << line << "): test '" << expr1 << " == " << expr2
251 << "' failed in function '" << function << "': "
252 << "'" << t << "' == '" << u << "'" << std::endl;
253 ++test_errors();
254 }
255 }
256
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)
262 {
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;
269 do
270 {
271 while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
272 {
273 ++first_it;
274 ++second_it;
275 ++first_index;
276 ++second_index;
277 }
278 if ((first_it == first_end) || (second_it == second_end))
279 {
280 break; // do-while
281 }
282 if (error_count == 0)
283 {
284 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
285 }
286 else if (error_count >= max_count)
287 {
288 output << " ...";
289 break;
290 }
291 output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
292 ++first_it;
293 ++second_it;
294 ++first_index;
295 ++second_index;
296 ++error_count;
297 } while (first_it != first_end);
298
299 first_index += std::distance(first_it, first_end);
300 second_index += std::distance(second_it, second_end);
301 if (first_index != second_index)
302 {
303 if (error_count == 0)
304 {
305 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
306 }
307 else
308 {
309 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
310 }
311 ++error_count;
312 }
313
314 if (error_count == 0)
315 {
316 boost::detail::report_errors_remind();
317 }
318 else
319 {
320 output << std::endl;
321 ++boost::detail::test_errors();
322 }
323 }
324
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)
331 {
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;
338 do
339 {
340 while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it))
341 {
342 ++first_it;
343 ++second_it;
344 ++first_index;
345 ++second_index;
346 }
347 if ((first_it == first_end) || (second_it == second_end))
348 {
349 break; // do-while
350 }
351 if (error_count == 0)
352 {
353 output << file << "(" << line << "): Container contents differ in function '" << function << "':";
354 }
355 else if (error_count >= max_count)
356 {
357 output << " ...";
358 break;
359 }
360 output << " [" << first_index << "]";
361 ++first_it;
362 ++second_it;
363 ++first_index;
364 ++second_index;
365 ++error_count;
366 } while (first_it != first_end);
367
368 first_index += std::distance(first_it, first_end);
369 second_index += std::distance(second_it, second_end);
370 if (first_index != second_index)
371 {
372 if (error_count == 0)
373 {
374 output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
375 }
376 else
377 {
378 output << " [*] size(" << first_index << ") != size(" << second_index << ")";
379 }
380 ++error_count;
381 }
382
383 if (error_count == 0)
384 {
385 report_errors_remind();
386 }
387 else
388 {
389 output << std::endl;
390 ++test_errors();
391 }
392 }
393
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
399 # endif
400 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
401 # pragma GCC diagnostic pop
402 #endif
403
404 } // namespace detail
405
406 inline int report_errors()
407 {
408 boost::detail::report_errors_remind().called_report_errors_function = true;
409
410 int errors = boost::detail::test_errors();
411
412 if( errors == 0 )
413 {
414 BOOST_LIGHTWEIGHT_TEST_OSTREAM
415 << "No errors detected." << std::endl;
416 return 0;
417 }
418 else
419 {
420 BOOST_LIGHTWEIGHT_TEST_OSTREAM
421 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
422 return 1;
423 }
424 }
425
426 } // namespace boost
427
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))
430
431 #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
432
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) )
435
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) )
440
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) )
443
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) )
446
447 #ifndef BOOST_NO_EXCEPTIONS
448 #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
449 try { \
450 EXPR; \
451 ::boost::detail::throw_failed_impl \
452 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
453 } \
454 catch(EXCEP const&) { \
455 } \
456 catch(...) { \
457 ::boost::detail::throw_failed_impl \
458 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
459 } \
460 //
461 #else
462 #define BOOST_TEST_THROWS( EXPR, EXCEP )
463 #endif
464
465 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP