lightweight_test.hpp 6.8 KB

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