impl.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision: 74248 $
  10. //
  11. // Description : implementation details for old toolbox
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_TOOLS_OLD_IMPL_HPP_012705GER
  14. #define BOOST_TEST_TOOLS_OLD_IMPL_HPP_012705GER
  15. // Boost.Test
  16. #include <boost/test/unit_test_log.hpp>
  17. #include <boost/test/tools/assertion_result.hpp>
  18. #include <boost/test/tools/floating_point_comparison.hpp>
  19. #include <boost/test/tools/detail/fwd.hpp>
  20. #include <boost/test/tools/detail/print_helper.hpp>
  21. // Boost
  22. #include <boost/limits.hpp>
  23. #include <boost/numeric/conversion/conversion_traits.hpp> // for numeric::conversion_traits
  24. #include <boost/type_traits/is_array.hpp>
  25. #include <boost/preprocessor/repetition/repeat.hpp>
  26. #include <boost/preprocessor/arithmetic/add.hpp>
  27. // STL
  28. #include <cstddef> // for std::size_t
  29. #include <climits> // for CHAR_BIT
  30. #include <boost/test/detail/suppress_warnings.hpp>
  31. //____________________________________________________________________________//
  32. namespace boost {
  33. namespace test_tools {
  34. namespace tt_detail {
  35. // ************************************************************************** //
  36. // ************** old TOOLBOX Implementation ************** //
  37. // ************************************************************************** //
  38. // This function adds level of indirection, but it makes sure we evaluate predicate
  39. // arguments only once
  40. #ifndef BOOST_TEST_PROD
  41. #define TEMPL_PARAMS( z, m, dummy ) , typename BOOST_JOIN( Arg, m )
  42. #define FUNC_PARAMS( z, m, dummy ) \
  43. , BOOST_JOIN( Arg, m ) const& BOOST_JOIN( arg, m ) \
  44. , char const* BOOST_JOIN( BOOST_JOIN( arg, m ), _descr ) \
  45. /**/
  46. #define PRED_PARAMS( z, m, dummy ) BOOST_PP_COMMA_IF( m ) BOOST_JOIN( arg, m )
  47. #define ARG_INFO( z, m, dummy ) \
  48. , BOOST_JOIN( BOOST_JOIN( arg, m ), _descr ) \
  49. , &static_cast<const unit_test::lazy_ostream&>(unit_test::lazy_ostream::instance() \
  50. << ::boost::test_tools::tt_detail::print_helper( BOOST_JOIN( arg, m ) )) \
  51. /**/
  52. #define IMPL_FRWD( z, n, dummy ) \
  53. template<typename Pred \
  54. BOOST_PP_REPEAT_ ## z( BOOST_PP_ADD( n, 1 ), TEMPL_PARAMS, _ )> \
  55. inline bool \
  56. check_frwd( Pred P, unit_test::lazy_ostream const& assertion_descr, \
  57. const_string file_name, std::size_t line_num, \
  58. tool_level tl, check_type ct \
  59. BOOST_PP_REPEAT_ ## z( BOOST_PP_ADD( n, 1 ), FUNC_PARAMS, _ ) \
  60. ) \
  61. { \
  62. return \
  63. report_assertion( P( BOOST_PP_REPEAT_ ## z(BOOST_PP_ADD(n, 1), PRED_PARAMS,_) ),\
  64. assertion_descr, file_name, line_num, tl, ct, \
  65. BOOST_PP_ADD( n, 1 ) \
  66. BOOST_PP_REPEAT_ ## z( BOOST_PP_ADD( n, 1 ), ARG_INFO, _ ) \
  67. ); \
  68. } \
  69. /**/
  70. #ifndef BOOST_TEST_MAX_PREDICATE_ARITY
  71. #define BOOST_TEST_MAX_PREDICATE_ARITY 5
  72. #endif
  73. BOOST_PP_REPEAT( BOOST_TEST_MAX_PREDICATE_ARITY, IMPL_FRWD, _ )
  74. #undef TEMPL_PARAMS
  75. #undef FUNC_PARAMS
  76. #undef PRED_INFO
  77. #undef ARG_INFO
  78. #undef IMPL_FRWD
  79. #endif
  80. //____________________________________________________________________________//
  81. template <class Left, class Right>
  82. inline assertion_result equal_impl( Left const& left, Right const& right )
  83. {
  84. return left == right;
  85. }
  86. //____________________________________________________________________________//
  87. inline assertion_result equal_impl( char* left, char const* right ) { return equal_impl( static_cast<char const*>(left), static_cast<char const*>(right) ); }
  88. inline assertion_result equal_impl( char const* left, char* right ) { return equal_impl( static_cast<char const*>(left), static_cast<char const*>(right) ); }
  89. inline assertion_result equal_impl( char* left, char* right ) { return equal_impl( static_cast<char const*>(left), static_cast<char const*>(right) ); }
  90. #if !defined( BOOST_NO_CWCHAR )
  91. BOOST_TEST_DECL assertion_result equal_impl( wchar_t const* left, wchar_t const* right );
  92. inline assertion_result equal_impl( wchar_t* left, wchar_t const* right ) { return equal_impl( static_cast<wchar_t const*>(left), static_cast<wchar_t const*>(right) ); }
  93. inline assertion_result equal_impl( wchar_t const* left, wchar_t* right ) { return equal_impl( static_cast<wchar_t const*>(left), static_cast<wchar_t const*>(right) ); }
  94. inline assertion_result equal_impl( wchar_t* left, wchar_t* right ) { return equal_impl( static_cast<wchar_t const*>(left), static_cast<wchar_t const*>(right) ); }
  95. #endif
  96. //____________________________________________________________________________//
  97. struct equal_impl_frwd {
  98. template <typename Left, typename Right>
  99. inline assertion_result
  100. call_impl( Left const& left, Right const& right, mpl::false_ ) const
  101. {
  102. return equal_impl( left, right );
  103. }
  104. template <typename Left, typename Right>
  105. inline assertion_result
  106. call_impl( Left const& left, Right const& right, mpl::true_ ) const
  107. {
  108. return (*this)( right, &left[0] );
  109. }
  110. template <typename Left, typename Right>
  111. inline assertion_result
  112. operator()( Left const& left, Right const& right ) const
  113. {
  114. typedef typename is_array<Left>::type left_is_array;
  115. return call_impl( left, right, left_is_array() );
  116. }
  117. };
  118. //____________________________________________________________________________//
  119. struct ne_impl {
  120. template <class Left, class Right>
  121. assertion_result operator()( Left const& left, Right const& right )
  122. {
  123. return !equal_impl_frwd()( left, right );
  124. }
  125. };
  126. //____________________________________________________________________________//
  127. struct lt_impl {
  128. template <class Left, class Right>
  129. assertion_result operator()( Left const& left, Right const& right )
  130. {
  131. return left < right;
  132. }
  133. };
  134. //____________________________________________________________________________//
  135. struct le_impl {
  136. template <class Left, class Right>
  137. assertion_result operator()( Left const& left, Right const& right )
  138. {
  139. return left <= right;
  140. }
  141. };
  142. //____________________________________________________________________________//
  143. struct gt_impl {
  144. template <class Left, class Right>
  145. assertion_result operator()( Left const& left, Right const& right )
  146. {
  147. return left > right;
  148. }
  149. };
  150. //____________________________________________________________________________//
  151. struct ge_impl {
  152. template <class Left, class Right>
  153. assertion_result operator()( Left const& left, Right const& right )
  154. {
  155. return left >= right;
  156. }
  157. };
  158. //____________________________________________________________________________//
  159. struct equal_coll_impl {
  160. template <typename Left, typename Right>
  161. assertion_result operator()( Left left_begin, Left left_end, Right right_begin, Right right_end )
  162. {
  163. assertion_result pr( true );
  164. std::size_t pos = 0;
  165. for( ; left_begin != left_end && right_begin != right_end; ++left_begin, ++right_begin, ++pos ) {
  166. if( *left_begin != *right_begin ) {
  167. pr = false;
  168. pr.message() << "\nMismatch at position " << pos << ": "
  169. << ::boost::test_tools::tt_detail::print_helper(*left_begin)
  170. << " != "
  171. << ::boost::test_tools::tt_detail::print_helper(*right_begin);
  172. }
  173. }
  174. if( left_begin != left_end ) {
  175. std::size_t r_size = pos;
  176. while( left_begin != left_end ) {
  177. ++pos;
  178. ++left_begin;
  179. }
  180. pr = false;
  181. pr.message() << "\nCollections size mismatch: " << pos << " != " << r_size;
  182. }
  183. if( right_begin != right_end ) {
  184. std::size_t l_size = pos;
  185. while( right_begin != right_end ) {
  186. ++pos;
  187. ++right_begin;
  188. }
  189. pr = false;
  190. pr.message() << "\nCollections size mismatch: " << l_size << " != " << pos;
  191. }
  192. return pr;
  193. }
  194. };
  195. //____________________________________________________________________________//
  196. struct bitwise_equal_impl {
  197. template <class Left, class Right>
  198. assertion_result operator()( Left const& left, Right const& right )
  199. {
  200. assertion_result pr( true );
  201. std::size_t left_bit_size = sizeof(Left)*CHAR_BIT;
  202. std::size_t right_bit_size = sizeof(Right)*CHAR_BIT;
  203. static Left const leftOne( 1 );
  204. static Right const rightOne( 1 );
  205. std::size_t total_bits = left_bit_size < right_bit_size ? left_bit_size : right_bit_size;
  206. for( std::size_t counter = 0; counter < total_bits; ++counter ) {
  207. if( ( left & ( leftOne << counter ) ) != ( right & ( rightOne << counter ) ) ) {
  208. pr = false;
  209. pr.message() << "\nMismatch at position " << counter;
  210. }
  211. }
  212. if( left_bit_size != right_bit_size ) {
  213. pr = false;
  214. pr.message() << "\nOperands bit sizes mismatch: " << left_bit_size << " != " << right_bit_size;
  215. }
  216. return pr;
  217. }
  218. };
  219. //____________________________________________________________________________//
  220. template<typename FPT1, typename FPT2>
  221. struct comp_supertype {
  222. // deduce "better" type from types of arguments being compared
  223. // if one type is floating and the second integral we use floating type and
  224. // value of integral type is promoted to the floating. The same for float and double
  225. // But we don't want to compare two values of integral types using this tool.
  226. typedef typename numeric::conversion_traits<FPT1,FPT2>::supertype type;
  227. BOOST_STATIC_ASSERT_MSG( !is_integral<type>::value, "Only floating-point types can be compared!");
  228. };
  229. } // namespace tt_detail
  230. namespace fpc = math::fpc;
  231. // ************************************************************************** //
  232. // ************** check_is_close ************** //
  233. // ************************************************************************** //
  234. struct BOOST_TEST_DECL check_is_close_t {
  235. // Public typedefs
  236. typedef assertion_result result_type;
  237. template<typename FPT1, typename FPT2, typename ToleranceType>
  238. assertion_result
  239. operator()( FPT1 left, FPT2 right, ToleranceType tolerance ) const
  240. {
  241. fpc::close_at_tolerance<typename tt_detail::comp_supertype<FPT1,FPT2>::type> pred( tolerance, fpc::FPC_STRONG );
  242. assertion_result ar( pred( left, right ) );
  243. if( !ar )
  244. ar.message() << pred.tested_rel_diff();
  245. return ar;
  246. }
  247. };
  248. //____________________________________________________________________________//
  249. template<typename FPT1, typename FPT2, typename ToleranceType>
  250. inline assertion_result
  251. check_is_close( FPT1 left, FPT2 right, ToleranceType tolerance )
  252. {
  253. return check_is_close_t()( left, right, tolerance );
  254. }
  255. //____________________________________________________________________________//
  256. // ************************************************************************** //
  257. // ************** check_is_small ************** //
  258. // ************************************************************************** //
  259. struct BOOST_TEST_DECL check_is_small_t {
  260. // Public typedefs
  261. typedef bool result_type;
  262. template<typename FPT>
  263. bool
  264. operator()( FPT fpv, FPT tolerance ) const
  265. {
  266. return fpc::is_small( fpv, tolerance );
  267. }
  268. };
  269. //____________________________________________________________________________//
  270. template<typename FPT>
  271. inline bool
  272. check_is_small( FPT fpv, FPT tolerance )
  273. {
  274. return fpc::is_small( fpv, tolerance );
  275. }
  276. //____________________________________________________________________________//
  277. } // namespace test_tools
  278. } // namespace boost
  279. #include <boost/test/detail/enable_warnings.hpp>
  280. #endif // BOOST_TEST_TOOLS_OLD_IMPL_HPP_012705GER