lightweight_test.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
  2. #define BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // boost/detail/lightweight_test.hpp - lightweight test library
  9. //
  10. // Copyright (c) 2002, 2009 Peter Dimov
  11. // Copyright (2) Beman Dawes 2010, 2011
  12. // Copyright (3) Ion Gaztanaga 2013
  13. //
  14. // Distributed under the Boost Software License, Version 1.0.
  15. // See accompanying file LICENSE_1_0.txt or copy at
  16. // http://www.boost.org/LICENSE_1_0.txt
  17. //
  18. // ---------------
  19. //
  20. // If expression is false increases the error count
  21. // and outputs a message containing 'expression'
  22. //
  23. // BOOST_TEST(expression)
  24. //
  25. // ---------------
  26. //
  27. // Increases error count and outputs a message containing 'message'
  28. //
  29. // BOOST_ERROR(message)
  30. //
  31. // ---------------
  32. //
  33. // If 'expr1' != 'expr2' increases the error count
  34. // and outputs a message containing both expressions
  35. //
  36. // BOOST_TEST_EQ(expr1, expr2)
  37. //
  38. // ---------------
  39. //
  40. // If 'expr1' == 'expr2' increases the error count
  41. // and outputs a message containing both expressions
  42. //
  43. // BOOST_TEST_NE(expr1, expr2)
  44. //
  45. // ---------------
  46. //
  47. // If BOOST_NO_EXCEPTIONS is NOT defined and if 'expr' does not
  48. // throw an exception of type 'excep', increases the error count
  49. // and outputs a message containing the expression.
  50. //
  51. // If BOOST_NO_EXCEPTIONS is defined, this macro expands to nothing
  52. // and 'expr' is not evaluated.
  53. //
  54. // BOOST_TEST_THROWS(expr, excep)
  55. //
  56. // ---------------
  57. //
  58. // Returns the error count
  59. //
  60. // int boost::report_errors()
  61. //
  62. #include <iostream>
  63. #include <boost/current_function.hpp>
  64. #include <boost/assert.hpp>
  65. #include <boost/detail/no_exceptions_support.hpp>
  66. // IDE's like Visual Studio perform better if output goes to std::cout or
  67. // some other stream, so allow user to configure output stream:
  68. #ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
  69. # define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
  70. #endif
  71. namespace boost
  72. {
  73. namespace detail
  74. {
  75. struct report_errors_reminder
  76. {
  77. bool called_report_errors_function;
  78. report_errors_reminder() : called_report_errors_function(false) {}
  79. ~report_errors_reminder()
  80. {
  81. BOOST_ASSERT(called_report_errors_function); // verify report_errors() was called
  82. }
  83. };
  84. inline report_errors_reminder& report_errors_remind()
  85. {
  86. static report_errors_reminder r;
  87. return r;
  88. }
  89. inline int & test_errors()
  90. {
  91. static int x = 0;
  92. report_errors_remind();
  93. return x;
  94. }
  95. inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
  96. {
  97. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  98. << file << "(" << line << "): test '" << expr << "' failed in function '"
  99. << function << "'" << std::endl;
  100. ++test_errors();
  101. }
  102. inline void error_impl(char const * msg, char const * file, int line, char const * function)
  103. {
  104. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  105. << file << "(" << line << "): " << msg << " in function '"
  106. << function << "'" << std::endl;
  107. ++test_errors();
  108. }
  109. inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
  110. {
  111. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  112. << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
  113. << function << "'" << std::endl;
  114. ++test_errors();
  115. }
  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 )
  118. {
  119. if( t == u )
  120. {
  121. }
  122. else
  123. {
  124. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  125. << file << "(" << line << "): test '" << expr1 << " == " << expr2
  126. << "' failed in function '" << function << "': "
  127. << "'" << t << "' != '" << u << "'" << std::endl;
  128. ++test_errors();
  129. }
  130. }
  131. template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2,
  132. char const * file, int line, char const * function, T const & t, U const & u )
  133. {
  134. if( t != u )
  135. {
  136. }
  137. else
  138. {
  139. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  140. << file << "(" << line << "): test '" << expr1 << " != " << expr2
  141. << "' failed in function '" << function << "': "
  142. << "'" << t << "' == '" << u << "'" << std::endl;
  143. ++test_errors();
  144. }
  145. }
  146. } // namespace detail
  147. inline int report_errors()
  148. {
  149. detail::report_errors_remind().called_report_errors_function = true;
  150. int errors = detail::test_errors();
  151. if( errors == 0 )
  152. {
  153. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  154. << "No errors detected." << std::endl;
  155. return 0;
  156. }
  157. else
  158. {
  159. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  160. << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
  161. return 1;
  162. }
  163. }
  164. } // namespace boost
  165. #define BOOST_TEST(expr) ((expr)? (void)0: ::boost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
  166. #define BOOST_ERROR(msg) ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
  167. #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  168. #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
  169. #ifndef BOOST_NO_EXCEPTIONS
  170. #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
  171. try { \
  172. EXPR; \
  173. ::boost::detail::throw_failed_impl \
  174. (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
  175. } \
  176. catch(EXCEP const&) { \
  177. } \
  178. catch(...) { \
  179. ::boost::detail::throw_failed_impl \
  180. (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
  181. } \
  182. //
  183. #else
  184. #define BOOST_TEST_THROWS( EXPR, EXCEP )
  185. #endif
  186. #endif // #ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED