Imported Upstream version 1.72.0
[platform/upstream/boost.git] / 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 //  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
37 #endif
38
39 namespace boost
40 {
41
42 namespace detail
43 {
44
45 class test_result {
46 public:
47     test_result()
48         : report_(false)
49         , errors_(0) {
50 #if defined(_MSC_VER) && (_MSC_VER > 1310)
51         ::_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
52 #endif
53     }
54
55     ~test_result() {
56         if (!report_) {
57             BOOST_LIGHTWEIGHT_TEST_OSTREAM << "main() should return report_errors()" << std::endl;
58             std::abort();
59         }
60     }
61
62     int& errors() {
63         return errors_;
64     }
65
66     void done() {
67         report_ = true;
68     }
69
70 private:
71     bool report_;
72     int errors_;
73 };
74
75 inline test_result& test_results()
76 {
77     static test_result instance;
78     return instance;
79 }
80
81 inline int& test_errors()
82 {
83     return test_results().errors();
84 }
85
86 inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
87 {
88     BOOST_LIGHTWEIGHT_TEST_OSTREAM
89       << file << "(" << line << "): test '" << expr << "' failed in function '"
90       << function << "'" << std::endl;
91     ++test_results().errors();
92 }
93
94 inline void error_impl(char const * msg, char const * file, int line, char const * function)
95 {
96     BOOST_LIGHTWEIGHT_TEST_OSTREAM
97       << file << "(" << line << "): " << msg << " in function '"
98       << function << "'" << std::endl;
99     ++test_results().errors();
100 }
101
102 inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
103 {
104    BOOST_LIGHTWEIGHT_TEST_OSTREAM
105     << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
106     << function << "'" << std::endl;
107    ++test_results().errors();
108 }
109
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"
117 # endif
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"
124 #endif
125
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); }
135
136 #if !defined( BOOST_NO_CXX11_NULLPTR )
137 inline const void* test_output_impl(std::nullptr_t) { return nullptr; }
138 #endif
139
140 struct lw_test_eq {
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 "=="; }
144 };
145
146 struct lw_test_ne {
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 "!="; }
150 };
151
152 struct lw_test_lt {
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 "<"; }
156 };
157
158 struct lw_test_le {
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 "<="; }
162 };
163
164 struct lw_test_gt {
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 ">"; }
168 };
169
170 struct lw_test_ge {
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 ">="; }
174 };
175
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)
180 {
181     if( pred(t, u) )
182     {
183         test_results();
184     }
185     else
186     {
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();
192     }
193 }
194
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 )
197 {
198     if( std::strcmp(t, u) == 0 )
199     {
200         test_results();
201     }
202     else
203     {
204         BOOST_LIGHTWEIGHT_TEST_OSTREAM
205             << file << "(" << line << "): test '" << expr1 << " == " << expr2 << "' ('" << t
206             << "' == '" << u << "') failed in function '" << function << "'" << std::endl;
207         ++test_results().errors();
208     }
209 }
210
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 )
213 {
214     if( std::strcmp(t, u) != 0 )
215     {
216         test_results();
217     }
218     else
219     {
220         BOOST_LIGHTWEIGHT_TEST_OSTREAM
221             << file << "(" << line << "): test '" << expr1 << " != " << expr2 << "' ('" << t
222             << "' != '" << u << "') failed in function '" << function << "'" << std::endl;
223         ++test_results().errors();
224     }
225 }
226
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)
232 {
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;
239     do
240     {
241         while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
242         {
243             ++first_it;
244             ++second_it;
245             ++first_index;
246             ++second_index;
247         }
248         if ((first_it == first_end) || (second_it == second_end))
249         {
250             break; // do-while
251         }
252         if (error_count == 0)
253         {
254             output << file << "(" << line << "): Container contents differ in function '" << function << "':";
255         }
256         else if (error_count >= max_count)
257         {
258             output << " ...";
259             break;
260         }
261         output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
262         ++first_it;
263         ++second_it;
264         ++first_index;
265         ++second_index;
266         ++error_count;
267     } while (first_it != first_end);
268
269     first_index += std::distance(first_it, first_end);
270     second_index += std::distance(second_it, second_end);
271     if (first_index != second_index)
272     {
273         if (error_count == 0)
274         {
275             output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
276         }
277         else
278         {
279             output << " [*] size(" << first_index << ") != size(" << second_index << ")";
280         }
281         ++error_count;
282     }
283
284     if (error_count == 0)
285     {
286         test_results();
287     }
288     else
289     {
290         output << std::endl;
291         ++test_results().errors();
292     }
293 }
294
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)
301 {
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;
308     do
309     {
310         while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it))
311         {
312             ++first_it;
313             ++second_it;
314             ++first_index;
315             ++second_index;
316         }
317         if ((first_it == first_end) || (second_it == second_end))
318         {
319             break; // do-while
320         }
321         if (error_count == 0)
322         {
323             output << file << "(" << line << "): Container contents differ in function '" << function << "':";
324         }
325         else if (error_count >= max_count)
326         {
327             output << " ...";
328             break;
329         }
330         output << " [" << first_index << "]";
331         ++first_it;
332         ++second_it;
333         ++first_index;
334         ++second_index;
335         ++error_count;
336     } while (first_it != first_end);
337
338     first_index += std::distance(first_it, first_end);
339     second_index += std::distance(second_it, second_end);
340     if (first_index != second_index)
341     {
342         if (error_count == 0)
343         {
344             output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
345         }
346         else
347         {
348             output << " [*] size(" << first_index << ") != size(" << second_index << ")";
349         }
350         ++error_count;
351     }
352
353     if (error_count == 0)
354     {
355         test_results();
356     }
357     else
358     {
359         output << std::endl;
360         ++test_results().errors();
361     }
362 }
363
364 #if defined(__clang__) && defined(__has_warning)
365 # if __has_warning("-Wsign-compare")
366 #  pragma clang diagnostic pop
367 # endif
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
372 #endif
373
374 } // namespace detail
375
376 inline int report_errors()
377 {
378     boost::detail::test_result& result = boost::detail::test_results();
379     result.done();
380
381     int errors = result.errors();
382
383     if( errors == 0 )
384     {
385         BOOST_LIGHTWEIGHT_TEST_OSTREAM
386           << "No errors detected." << std::endl;
387     }
388     else
389     {
390         BOOST_LIGHTWEIGHT_TEST_OSTREAM
391           << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
392     }
393
394     // `return report_errors();` from main only supports 8 bit exit codes
395     return errors < 256? errors: 255;
396 }
397
398 } // namespace boost
399
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))
402
403 #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
404
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) )
407
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) )
412
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) )
415
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) )
418
419 #ifndef BOOST_NO_EXCEPTIONS
420    #define BOOST_TEST_THROWS( EXPR, EXCEP )                    \
421       try {                                                    \
422          EXPR;                                                 \
423          ::boost::detail::throw_failed_impl                    \
424          (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
425       }                                                        \
426       catch(EXCEP const&) {                                    \
427          ::boost::detail::test_results();                      \
428       }                                                        \
429       catch(...) {                                             \
430          ::boost::detail::throw_failed_impl                    \
431          (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
432       }                                                        \
433    //
434 #else
435    #define BOOST_TEST_THROWS( EXPR, EXCEP )
436 #endif
437
438 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP