fpc_op.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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
  8. //!@brief Floating point comparison with enhanced reporting
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_TOOLS_FPC_OP_HPP_050915GER
  11. #define BOOST_TEST_TOOLS_FPC_OP_HPP_050915GER
  12. // Boost.Test
  13. #include <boost/test/tools/assertion.hpp>
  14. #include <boost/test/tools/floating_point_comparison.hpp>
  15. #include <boost/test/tools/fpc_tolerance.hpp>
  16. // Boost
  17. #include <boost/type_traits/common_type.hpp>
  18. #include <boost/type_traits/is_arithmetic.hpp>
  19. #include <boost/utility/enable_if.hpp>
  20. #include <boost/test/detail/suppress_warnings.hpp>
  21. //____________________________________________________________________________//
  22. namespace boost {
  23. namespace test_tools {
  24. namespace assertion {
  25. namespace op {
  26. // ************************************************************************** //
  27. // ************** fpctraits ************** //
  28. // ************************************************************************** //
  29. // set of floating point comparison traits per comparison OP
  30. template<typename OP>
  31. struct fpctraits {
  32. // indicate if we should perform the operation with a "logical OR"
  33. // with the "equality under tolerance".
  34. static const bool equality_logical_disjunction = true;
  35. };
  36. template <typename Lhs, typename Rhs>
  37. struct fpctraits<op::LT<Lhs,Rhs> > {
  38. static const bool equality_logical_disjunction = false;
  39. };
  40. template <typename Lhs, typename Rhs>
  41. struct fpctraits<op::GT<Lhs,Rhs> > {
  42. static const bool equality_logical_disjunction = false;
  43. };
  44. //____________________________________________________________________________//
  45. // ************************************************************************** //
  46. // ************** set of overloads to select correct fpc algo ************** //
  47. // ************************************************************************** //
  48. // we really only care about EQ vs NE. All other comparisons use direct first
  49. // and then need EQ. For example a <= b (tolerance t) IFF a <= b OR a == b (tolerance t)
  50. template <typename FPT, typename Lhs, typename Rhs, typename OP>
  51. inline assertion_result
  52. compare_fpv( Lhs const& lhs, Rhs const& rhs, OP* cmp_operator)
  53. {
  54. bool result = cmp_operator->eval_direct(lhs, rhs);
  55. if(fpctraits<OP>::equality_logical_disjunction) {
  56. return result || compare_fpv<FPT>(lhs, rhs, (op::EQ<Lhs, Rhs>*)0);
  57. }
  58. return result && compare_fpv<FPT>(lhs, rhs, (op::NE<Lhs, Rhs>*)0);
  59. }
  60. //____________________________________________________________________________//
  61. template <typename FPT, typename Lhs, typename Rhs>
  62. inline assertion_result
  63. compare_fpv_near_zero( FPT const& fpv, op::EQ<Lhs,Rhs>* )
  64. {
  65. fpc::small_with_tolerance<FPT> P( fpc_tolerance<FPT>() );
  66. assertion_result ar( P( fpv ) );
  67. if( !ar )
  68. ar.message() << "Absolute value exceeds tolerance [|" << fpv << "| > "<< fpc_tolerance<FPT>() << ']';
  69. return ar;
  70. }
  71. //____________________________________________________________________________//
  72. template <typename FPT, typename Lhs, typename Rhs>
  73. inline assertion_result
  74. compare_fpv_near_zero( FPT const& fpv, op::NE<Lhs,Rhs>* )
  75. {
  76. fpc::small_with_tolerance<FPT> P( fpc_tolerance<FPT>() );
  77. assertion_result ar( !P( fpv ) );
  78. if( !ar )
  79. ar.message() << "Absolute value is within tolerance [|" << fpv << "| < "<< fpc_tolerance<FPT>() << ']';
  80. return ar;
  81. }
  82. //____________________________________________________________________________//
  83. template <typename FPT, typename Lhs, typename Rhs>
  84. inline assertion_result
  85. compare_fpv( Lhs const& lhs, Rhs const& rhs, op::EQ<Lhs,Rhs>* )
  86. {
  87. if( lhs == 0 ) {
  88. return compare_fpv_near_zero<FPT>( rhs, (op::EQ<Lhs,Rhs>*)0 );
  89. }
  90. else if( rhs == 0) {
  91. return compare_fpv_near_zero<FPT>( lhs, (op::EQ<Lhs,Rhs>*)0 );
  92. }
  93. else {
  94. fpc::close_at_tolerance<FPT> P( fpc_tolerance<FPT>(), fpc::FPC_STRONG );
  95. assertion_result ar( P( lhs, rhs ) );
  96. if( !ar )
  97. ar.message() << "Relative difference exceeds tolerance ["
  98. << P.tested_rel_diff() << " > " << P.fraction_tolerance() << ']';
  99. return ar;
  100. }
  101. }
  102. //____________________________________________________________________________//
  103. template <typename FPT, typename Lhs, typename Rhs>
  104. inline assertion_result
  105. compare_fpv( Lhs const& lhs, Rhs const& rhs, op::NE<Lhs,Rhs>* )
  106. {
  107. if( lhs == 0 ) {
  108. return compare_fpv_near_zero<FPT>( rhs, (op::NE<Lhs,Rhs>*)0 );
  109. }
  110. else if( rhs == 0 ) {
  111. return compare_fpv_near_zero<FPT>( lhs, (op::NE<Lhs,Rhs>*)0 );
  112. }
  113. else {
  114. fpc::close_at_tolerance<FPT> P( fpc_tolerance<FPT>(), fpc::FPC_WEAK );
  115. assertion_result ar( !P( lhs, rhs ) );
  116. if( !ar )
  117. ar.message() << "Relative difference is within tolerance ["
  118. << P.tested_rel_diff() << " < " << fpc_tolerance<FPT>() << ']';
  119. return ar;
  120. }
  121. }
  122. //____________________________________________________________________________//
  123. #define DEFINE_FPV_COMPARISON( oper, name, rev ) \
  124. template<typename Lhs,typename Rhs> \
  125. struct name<Lhs,Rhs,typename boost::enable_if_c< \
  126. (fpc::tolerance_based<Lhs>::value && \
  127. fpc::tolerance_based<Rhs>::value) || \
  128. (fpc::tolerance_based<Lhs>::value && \
  129. boost::is_arithmetic<Rhs>::value) || \
  130. (boost::is_arithmetic<Lhs>::value && \
  131. fpc::tolerance_based<Rhs>::value) \
  132. >::type> { \
  133. public: \
  134. typedef typename common_type<Lhs,Rhs>::type FPT; \
  135. typedef name<Lhs,Rhs> OP; \
  136. \
  137. typedef assertion_result result_type; \
  138. \
  139. static bool \
  140. eval_direct( Lhs const& lhs, Rhs const& rhs ) \
  141. { \
  142. return lhs oper rhs; \
  143. } \
  144. \
  145. static assertion_result \
  146. eval( Lhs const& lhs, Rhs const& rhs ) \
  147. { \
  148. if( fpc_tolerance<FPT>() == FPT(0) \
  149. || (std::numeric_limits<Lhs>::has_infinity \
  150. && (lhs == std::numeric_limits<Lhs>::infinity())) \
  151. || (std::numeric_limits<Rhs>::has_infinity \
  152. && (rhs == std::numeric_limits<Rhs>::infinity()))) \
  153. { \
  154. return eval_direct( lhs, rhs ); \
  155. } \
  156. \
  157. return compare_fpv<FPT>( lhs, rhs, (OP*)0 ); \
  158. } \
  159. \
  160. template<typename PrevExprType> \
  161. static void \
  162. report( std::ostream& ostr, \
  163. PrevExprType const& lhs, \
  164. Rhs const& rhs ) \
  165. { \
  166. lhs.report( ostr ); \
  167. ostr << revert() \
  168. << tt_detail::print_helper( rhs ); \
  169. } \
  170. \
  171. static char const* revert() \
  172. { return " " #rev " "; } \
  173. }; \
  174. /**/
  175. BOOST_TEST_FOR_EACH_COMP_OP( DEFINE_FPV_COMPARISON )
  176. #undef DEFINE_FPV_COMPARISON
  177. //____________________________________________________________________________//
  178. } // namespace op
  179. } // namespace assertion
  180. } // namespace test_tools
  181. } // namespace boost
  182. #include <boost/test/detail/enable_warnings.hpp>
  183. #endif // BOOST_TEST_TOOLS_FPC_OP_HPP_050915GER