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 //  ([email protected])
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 <exception>
28 #include <iostream>
29 #include <iterator>
30 #include <string>
31 #include <cstdlib>
32 #include <cstring>
33 #include <cstddef>
34 #include <cctype>
35 #include <cstdio>
36 
37 #if defined(_MSC_VER) && defined(_CPPLIB_VER) && defined(_DEBUG)
38 # include <crtdbg.h>
39 #endif
40 
41 //  IDE's like Visual Studio perform better if output goes to std::cout or
42 //  some other stream, so allow user to configure output stream:
43 #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
44 # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
45 #endif
46 
47 namespace boost
48 {
49 
50 namespace detail
51 {
52 
53 class test_result {
54 public:
test_result()55     test_result()
56         : report_(false)
57         , errors_(0) {
58 #if defined(_MSC_VER) && (_MSC_VER > 1310)
59         // disable message boxes on assert(), abort()
60         ::_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
61 #endif
62 #if defined(_MSC_VER) && defined(_CPPLIB_VER) && defined(_DEBUG)
63         // disable message boxes on iterator debugging violations
64         _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
65         _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDERR );
66 #endif
67     }
68 
~test_result()69     ~test_result() {
70         if (!report_) {
71             BOOST_LIGHTWEIGHT_TEST_OSTREAM << "main() should return report_errors()" << std::endl;
72             std::abort();
73         }
74     }
75 
errors()76     int& errors() {
77         return errors_;
78     }
79 
done()80     void done() {
81         report_ = true;
82     }
83 
84 private:
85     bool report_;
86     int errors_;
87 };
88 
test_results()89 inline test_result& test_results()
90 {
91     static test_result instance;
92     return instance;
93 }
94 
test_errors()95 inline int& test_errors()
96 {
97     return test_results().errors();
98 }
99 
test_impl(char const * expr,char const * file,int line,char const * function,bool v)100 inline bool test_impl(char const * expr, char const * file, int line, char const * function, bool v)
101 {
102     if( v )
103     {
104         test_results();
105         return true;
106     }
107     else
108     {
109         BOOST_LIGHTWEIGHT_TEST_OSTREAM
110           << file << "(" << line << "): test '" << expr << "' failed in function '"
111           << function << "'" << std::endl;
112         ++test_results().errors();
113         return false;
114     }
115 }
116 
error_impl(char const * msg,char const * file,int line,char const * function)117 inline void error_impl(char const * msg, char const * file, int line, char const * function)
118 {
119     BOOST_LIGHTWEIGHT_TEST_OSTREAM
120       << file << "(" << line << "): " << msg << " in function '"
121       << function << "'" << std::endl;
122     ++test_results().errors();
123 }
124 
throw_failed_impl(const char * expr,char const * excep,char const * file,int line,char const * function)125 inline void throw_failed_impl(const char* expr, char const * excep, char const * file, int line, char const * function)
126 {
127    BOOST_LIGHTWEIGHT_TEST_OSTREAM
128     << file << "(" << line << "): expression '" << expr << "' did not throw exception '" << excep << "' in function '"
129     << function << "'" << std::endl;
130    ++test_results().errors();
131 }
132 
no_throw_failed_impl(const char * expr,const char * file,int line,const char * function)133 inline void no_throw_failed_impl(const char* expr, const char* file, int line, const char* function)
134 {
135     BOOST_LIGHTWEIGHT_TEST_OSTREAM
136         << file << "(" << line << "): expression '" << expr << "' threw an exception in function '"
137         << function << "'" << std::endl;
138    ++test_results().errors();
139 }
140 
no_throw_failed_impl(const char * expr,const char * what,const char * file,int line,const char * function)141 inline void no_throw_failed_impl(const char* expr, const char* what, const char* file, int line, const char* function)
142 {
143     BOOST_LIGHTWEIGHT_TEST_OSTREAM
144         << file << "(" << line << "): expression '" << expr << "' threw an exception in function '"
145         << function << "': " << what << std::endl;
146    ++test_results().errors();
147 }
148 
149 // In the comparisons below, it is possible that T and U are signed and unsigned integer types, which generates warnings in some compilers.
150 // A cleaner fix would require common_type trait or some meta-programming, which would introduce a dependency on Boost.TypeTraits. To avoid
151 // the dependency we just disable the warnings.
152 #if defined(__clang__) && defined(__has_warning)
153 # if __has_warning("-Wsign-compare")
154 #  pragma clang diagnostic push
155 #  pragma clang diagnostic ignored "-Wsign-compare"
156 # endif
157 #elif defined(_MSC_VER)
158 # pragma warning(push)
159 # pragma warning(disable: 4389)
160 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
161 # pragma GCC diagnostic push
162 # pragma GCC diagnostic ignored "-Wsign-compare"
163 #endif
164 
165 // specialize test output for char pointers to avoid printing as cstring
test_output_impl(const T & v)166 template <class T> inline const T& test_output_impl(const T& v) { return v; }
test_output_impl(const char * v)167 inline const void* test_output_impl(const char* v) { return v; }
test_output_impl(const unsigned char * v)168 inline const void* test_output_impl(const unsigned char* v) { return v; }
test_output_impl(const signed char * v)169 inline const void* test_output_impl(const signed char* v) { return v; }
test_output_impl(char * v)170 inline const void* test_output_impl(char* v) { return v; }
test_output_impl(unsigned char * v)171 inline const void* test_output_impl(unsigned char* v) { return v; }
test_output_impl(signed char * v)172 inline const void* test_output_impl(signed char* v) { return v; }
test_output_impl(T volatile * v)173 template<class T> inline const void* test_output_impl(T volatile* v) { return const_cast<T*>(v); }
174 
175 #if !defined( BOOST_NO_CXX11_NULLPTR )
test_output_impl(std::nullptr_t)176 inline const void* test_output_impl(std::nullptr_t) { return nullptr; }
177 #endif
178 
179 // print chars as numeric
180 
test_output_impl(signed char const & v)181 inline int test_output_impl( signed char const& v ) { return v; }
test_output_impl(unsigned char const & v)182 inline unsigned test_output_impl( unsigned char const& v ) { return v; }
183 
test_output_impl(wchar_t const & v)184 inline unsigned long test_output_impl( wchar_t const& v ) { return v; }
185 
186 #if !defined( BOOST_NO_CXX11_CHAR16_T )
test_output_impl(char16_t const & v)187 inline unsigned long test_output_impl( char16_t const& v ) { return v; }
188 #endif
189 
190 #if !defined( BOOST_NO_CXX11_CHAR32_T )
test_output_impl(char32_t const & v)191 inline unsigned long test_output_impl( char32_t const& v ) { return v; }
192 #endif
193 
194 #if defined(_MSC_VER)
195 #pragma warning(push)
196 #pragma warning(disable: 4996)
197 #endif
198 
test_output_impl(char const & v)199 inline std::string test_output_impl( char const& v )
200 {
201     if( std::isprint( static_cast<unsigned char>( v ) ) )
202     {
203         return std::string( 1, v );
204     }
205     else
206     {
207         char buffer[ 8 ];
208         std::sprintf( buffer, "\\x%02X", static_cast<unsigned char>( v ) );
209 
210         return buffer;
211     }
212 }
213 
214 #if defined(_MSC_VER)
215 #pragma warning(pop)
216 #endif
217 
218 // predicates
219 
220 struct lw_test_eq
221 {
222     template <typename T, typename U>
operator ()boost::detail::lw_test_eq223     bool operator()(const T& t, const U& u) const { return t == u; }
224 };
225 
226 struct lw_test_ne
227 {
228     template <typename T, typename U>
operator ()boost::detail::lw_test_ne229     bool operator()(const T& t, const U& u) const { return t != u; }
230 };
231 
232 struct lw_test_lt
233 {
234     template <typename T, typename U>
operator ()boost::detail::lw_test_lt235     bool operator()(const T& t, const U& u) const { return t < u; }
236 };
237 
238 struct lw_test_le
239 {
240     template <typename T, typename U>
operator ()boost::detail::lw_test_le241     bool operator()(const T& t, const U& u) const { return t <= u; }
242 };
243 
244 struct lw_test_gt
245 {
246     template <typename T, typename U>
operator ()boost::detail::lw_test_gt247     bool operator()(const T& t, const U& u) const { return t > u; }
248 };
249 
250 struct lw_test_ge
251 {
252     template <typename T, typename U>
operator ()boost::detail::lw_test_ge253     bool operator()(const T& t, const U& u) const { return t >= u; }
254 };
255 
256 // lwt_predicate_name
257 
lwt_predicate_name(T const &)258 template<class T> char const * lwt_predicate_name( T const& )
259 {
260     return "~=";
261 }
262 
lwt_predicate_name(lw_test_eq const &)263 inline char const * lwt_predicate_name( lw_test_eq const& )
264 {
265     return "==";
266 }
267 
lwt_predicate_name(lw_test_ne const &)268 inline char const * lwt_predicate_name( lw_test_ne const& )
269 {
270     return "!=";
271 }
272 
lwt_predicate_name(lw_test_lt const &)273 inline char const * lwt_predicate_name( lw_test_lt const& )
274 {
275     return "<";
276 }
277 
lwt_predicate_name(lw_test_le const &)278 inline char const * lwt_predicate_name( lw_test_le const& )
279 {
280     return "<=";
281 }
282 
lwt_predicate_name(lw_test_gt const &)283 inline char const * lwt_predicate_name( lw_test_gt const& )
284 {
285     return ">";
286 }
287 
lwt_predicate_name(lw_test_ge const &)288 inline char const * lwt_predicate_name( lw_test_ge const& )
289 {
290     return ">=";
291 }
292 
293 //
294 
295 template<class BinaryPredicate, class T, class U>
test_with_impl(BinaryPredicate pred,char const * expr1,char const * expr2,char const * file,int line,char const * function,T const & t,U const & u)296 inline bool test_with_impl(BinaryPredicate pred, char const * expr1, char const * expr2,
297                            char const * file, int line, char const * function,
298                            T const & t, U const & u)
299 {
300     if( pred(t, u) )
301     {
302         test_results();
303         return true;
304     }
305     else
306     {
307         BOOST_LIGHTWEIGHT_TEST_OSTREAM
308             << file << "(" << line << "): test '" << expr1 << " " << lwt_predicate_name(pred) << " " << expr2
309             << "' ('" << test_output_impl(t) << "' " << lwt_predicate_name(pred) << " '" << test_output_impl(u)
310             << "') failed in function '" << function << "'" << std::endl;
311         ++test_results().errors();
312         return false;
313     }
314 }
315 
test_cstr_eq_impl(char const * expr1,char const * expr2,char const * file,int line,char const * function,char const * const t,char const * const u)316 inline bool test_cstr_eq_impl( char const * expr1, char const * expr2,
317   char const * file, int line, char const * function, char const * const t, char const * const u )
318 {
319     if( std::strcmp(t, u) == 0 )
320     {
321         test_results();
322         return true;
323     }
324     else
325     {
326         BOOST_LIGHTWEIGHT_TEST_OSTREAM
327             << file << "(" << line << "): test '" << expr1 << " == " << expr2 << "' ('" << t
328             << "' == '" << u << "') failed in function '" << function << "'" << std::endl;
329         ++test_results().errors();
330         return false;
331     }
332 }
333 
test_cstr_ne_impl(char const * expr1,char const * expr2,char const * file,int line,char const * function,char const * const t,char const * const u)334 inline bool test_cstr_ne_impl( char const * expr1, char const * expr2,
335   char const * file, int line, char const * function, char const * const t, char const * const u )
336 {
337     if( std::strcmp(t, u) != 0 )
338     {
339         test_results();
340         return true;
341     }
342     else
343     {
344         BOOST_LIGHTWEIGHT_TEST_OSTREAM
345             << file << "(" << line << "): test '" << expr1 << " != " << expr2 << "' ('" << t
346             << "' != '" << u << "') failed in function '" << function << "'" << std::endl;
347         ++test_results().errors();
348         return false;
349     }
350 }
351 
352 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2>
test_all_eq_impl(FormattedOutputFunction & output,char const * file,int line,char const * function,InputIterator1 first_begin,InputIterator1 first_end,InputIterator2 second_begin,InputIterator2 second_end)353 bool test_all_eq_impl(FormattedOutputFunction& output,
354                       char const * file, int line, char const * function,
355                       InputIterator1 first_begin, InputIterator1 first_end,
356                       InputIterator2 second_begin, InputIterator2 second_end)
357 {
358     InputIterator1 first_it = first_begin;
359     InputIterator2 second_it = second_begin;
360     typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
361     typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
362     std::size_t error_count = 0;
363     const std::size_t max_count = 8;
364     do
365     {
366         while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
367         {
368             ++first_it;
369             ++second_it;
370             ++first_index;
371             ++second_index;
372         }
373         if ((first_it == first_end) || (second_it == second_end))
374         {
375             break; // do-while
376         }
377         if (error_count == 0)
378         {
379             output << file << "(" << line << "): Container contents differ in function '" << function << "':";
380         }
381         else if (error_count >= max_count)
382         {
383             output << " ...";
384             break;
385         }
386         output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
387         ++first_it;
388         ++second_it;
389         ++first_index;
390         ++second_index;
391         ++error_count;
392     } while (first_it != first_end);
393 
394     first_index += std::distance(first_it, first_end);
395     second_index += std::distance(second_it, second_end);
396     if (first_index != second_index)
397     {
398         if (error_count == 0)
399         {
400             output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
401         }
402         else
403         {
404             output << " [*] size(" << first_index << ") != size(" << second_index << ")";
405         }
406         ++error_count;
407     }
408 
409     if (error_count == 0)
410     {
411         test_results();
412         return true;
413     }
414     else
415     {
416         output << std::endl;
417         ++test_results().errors();
418         return false;
419     }
420 }
421 
422 template<class FormattedOutputFunction, class InputIterator1, class InputIterator2, typename BinaryPredicate>
test_all_with_impl(FormattedOutputFunction & output,char const * file,int line,char const * function,InputIterator1 first_begin,InputIterator1 first_end,InputIterator2 second_begin,InputIterator2 second_end,BinaryPredicate predicate)423 bool test_all_with_impl(FormattedOutputFunction& output,
424                         char const * file, int line, char const * function,
425                         InputIterator1 first_begin, InputIterator1 first_end,
426                         InputIterator2 second_begin, InputIterator2 second_end,
427                         BinaryPredicate predicate)
428 {
429     InputIterator1 first_it = first_begin;
430     InputIterator2 second_it = second_begin;
431     typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
432     typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
433     std::size_t error_count = 0;
434     const std::size_t max_count = 8;
435     do
436     {
437         while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it))
438         {
439             ++first_it;
440             ++second_it;
441             ++first_index;
442             ++second_index;
443         }
444         if ((first_it == first_end) || (second_it == second_end))
445         {
446             break; // do-while
447         }
448         if (error_count == 0)
449         {
450             output << file << "(" << line << "): Container contents differ in function '" << function << "':";
451         }
452         else if (error_count >= max_count)
453         {
454             output << " ...";
455             break;
456         }
457         output << " [" << first_index << "]";
458         ++first_it;
459         ++second_it;
460         ++first_index;
461         ++second_index;
462         ++error_count;
463     } while (first_it != first_end);
464 
465     first_index += std::distance(first_it, first_end);
466     second_index += std::distance(second_it, second_end);
467     if (first_index != second_index)
468     {
469         if (error_count == 0)
470         {
471             output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")";
472         }
473         else
474         {
475             output << " [*] size(" << first_index << ") != size(" << second_index << ")";
476         }
477         ++error_count;
478     }
479 
480     if (error_count == 0)
481     {
482         test_results();
483         return true;
484     }
485     else
486     {
487         output << std::endl;
488         ++test_results().errors();
489         return false;
490     }
491 }
492 
493 #if defined(__clang__) && defined(__has_warning)
494 # if __has_warning("-Wsign-compare")
495 #  pragma clang diagnostic pop
496 # endif
497 #elif defined(_MSC_VER)
498 # pragma warning(pop)
499 #elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
500 # pragma GCC diagnostic pop
501 #endif
502 
503 } // namespace detail
504 
report_errors()505 inline int report_errors()
506 {
507     boost::detail::test_result& result = boost::detail::test_results();
508     result.done();
509 
510     int errors = result.errors();
511 
512     if( errors == 0 )
513     {
514         BOOST_LIGHTWEIGHT_TEST_OSTREAM
515           << "No errors detected." << std::endl;
516     }
517     else
518     {
519         BOOST_LIGHTWEIGHT_TEST_OSTREAM
520           << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
521     }
522 
523     // `return report_errors();` from main only supports 8 bit exit codes
524     return errors < 256? errors: 255;
525 }
526 
527 } // namespace boost
528 
529 #define BOOST_TEST(expr) ( ::boost::detail::test_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, (expr)? true: false) )
530 #define BOOST_TEST_NOT(expr) BOOST_TEST(!(expr))
531 
532 #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
533 
534 #define BOOST_TEST_WITH(expr1,expr2,predicate) ( ::boost::detail::test_with_impl(predicate, #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
535 
536 #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) )
537 #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) )
538 
539 #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) )
540 #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) )
541 #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) )
542 #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) )
543 
544 #define BOOST_TEST_CSTR_EQ(expr1,expr2) ( ::boost::detail::test_cstr_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
545 #define BOOST_TEST_CSTR_NE(expr1,expr2) ( ::boost::detail::test_cstr_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
546 
547 #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) )
548 #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) )
549 
550 #ifndef BOOST_NO_EXCEPTIONS
551    #define BOOST_TEST_THROWS( EXPR, EXCEP )                           \
552       try {                                                           \
553          EXPR;                                                        \
554          ::boost::detail::throw_failed_impl                           \
555          (#EXPR, #EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
556       }                                                               \
557       catch(EXCEP const&) {                                           \
558          ::boost::detail::test_results();                             \
559       }                                                               \
560       catch(...) {                                                    \
561          ::boost::detail::throw_failed_impl                           \
562          (#EXPR, #EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
563       }                                                               \
564    //
565 #else
566    #define BOOST_TEST_THROWS( EXPR, EXCEP )
567 #endif
568 
569 #ifndef BOOST_NO_EXCEPTIONS
570 #  define BOOST_TEST_NO_THROW(EXPR)                                    \
571     try {                                                              \
572         EXPR;                                                          \
573     } catch (const std::exception& e) {                                \
574         ::boost::detail::no_throw_failed_impl                          \
575         (#EXPR, e.what(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
576     } catch (...) {                                                    \
577         ::boost::detail::no_throw_failed_impl                          \
578         (#EXPR, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION);           \
579     }
580     //
581 #else
582 #  define BOOST_TEST_NO_THROW(EXPR) { EXPR; }
583 #endif
584 
585 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_HPP
586