fpc_op.hpp 8.5 KB

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