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)
6 // See http://www.boost.org/libs/test for the library home page.
9 //!@brief Floating point comparison with enhanced reporting
10 // ***************************************************************************
12 #ifndef BOOST_TEST_TOOLS_FPC_OP_HPP_050915GER
13 #define BOOST_TEST_TOOLS_FPC_OP_HPP_050915GER
16 #include <boost/test/tools/assertion.hpp>
18 #include <boost/test/tools/floating_point_comparison.hpp>
19 #include <boost/test/tools/fpc_tolerance.hpp>
22 #include <boost/type_traits/common_type.hpp>
23 #include <boost/utility/enable_if.hpp>
25 #include <boost/test/detail/suppress_warnings.hpp>
27 //____________________________________________________________________________//
30 namespace test_tools {
34 // ************************************************************************** //
35 // ************** fpctraits ************** //
36 // ************************************************************************** //
37 // set of floating point comparison traits per comparison OP
41 // indicate if we should perform the operation with a "logical OR"
42 // with the "equality under tolerance".
43 static const bool equality_logical_disjunction = true;
46 template <typename Lhs, typename Rhs>
47 struct fpctraits<op::LT<Lhs,Rhs> > {
48 static const bool equality_logical_disjunction = false;
51 template <typename Lhs, typename Rhs>
52 struct fpctraits<op::GT<Lhs,Rhs> > {
53 static const bool equality_logical_disjunction = false;
56 //____________________________________________________________________________//
58 // ************************************************************************** //
59 // ************** set of overloads to select correct fpc algo ************** //
60 // ************************************************************************** //
61 // we really only care about EQ vs NE. All other comparisons use direct first
62 // and then need EQ. For example a <= b (tolerance t) IFF a <= b OR a == b (tolerance t)
64 template <typename FPT, typename Lhs, typename Rhs, typename OP>
65 inline assertion_result
66 compare_fpv( Lhs const& lhs, Rhs const& rhs, OP* cmp_operator)
68 bool result = cmp_operator->eval_direct(lhs, rhs);
69 if(fpctraits<OP>::equality_logical_disjunction) {
70 return result || compare_fpv<FPT>(lhs, rhs, (op::EQ<Lhs, Rhs>*)0);
72 return result && compare_fpv<FPT>(lhs, rhs, (op::NE<Lhs, Rhs>*)0);
75 //____________________________________________________________________________//
77 template <typename FPT, typename Lhs, typename Rhs>
78 inline assertion_result
79 compare_fpv_near_zero( FPT const& fpv, op::EQ<Lhs,Rhs>* )
81 fpc::small_with_tolerance<FPT> P( fpc_tolerance<FPT>() );
83 assertion_result ar( P( fpv ) );
85 ar.message() << "Absolute value exceeds tolerance [|" << fpv << "| > "<< fpc_tolerance<FPT>() << ']';
90 //____________________________________________________________________________//
92 template <typename FPT, typename Lhs, typename Rhs>
93 inline assertion_result
94 compare_fpv_near_zero( FPT const& fpv, op::NE<Lhs,Rhs>* )
96 fpc::small_with_tolerance<FPT> P( fpc_tolerance<FPT>() );
98 assertion_result ar( !P( fpv ) );
100 ar.message() << "Absolute value is within tolerance [|" << fpv << "| < "<< fpc_tolerance<FPT>() << ']';
104 //____________________________________________________________________________//
106 template <typename FPT, typename Lhs, typename Rhs>
107 inline assertion_result
108 compare_fpv( Lhs const& lhs, Rhs const& rhs, op::EQ<Lhs,Rhs>* )
111 return compare_fpv_near_zero( rhs, (op::EQ<Lhs,Rhs>*)0 );
114 return compare_fpv_near_zero( lhs, (op::EQ<Lhs,Rhs>*)0 );
117 fpc::close_at_tolerance<FPT> P( fpc_tolerance<FPT>(), fpc::FPC_STRONG );
119 assertion_result ar( P( lhs, rhs ) );
121 ar.message() << "Relative difference exceeds tolerance ["
122 << P.tested_rel_diff() << " > " << P.fraction_tolerance() << ']';
127 //____________________________________________________________________________//
129 template <typename FPT, typename Lhs, typename Rhs>
130 inline assertion_result
131 compare_fpv( Lhs const& lhs, Rhs const& rhs, op::NE<Lhs,Rhs>* )
134 return compare_fpv_near_zero( rhs, (op::NE<Lhs,Rhs>*)0 );
136 else if( rhs == 0 ) {
137 return compare_fpv_near_zero( lhs, (op::NE<Lhs,Rhs>*)0 );
140 fpc::close_at_tolerance<FPT> P( fpc_tolerance<FPT>(), fpc::FPC_WEAK );
142 assertion_result ar( !P( lhs, rhs ) );
144 ar.message() << "Relative difference is within tolerance ["
145 << P.tested_rel_diff() << " < " << fpc_tolerance<FPT>() << ']';
151 //____________________________________________________________________________//
153 #define DEFINE_FPV_COMPARISON( oper, name, rev ) \
154 template<typename Lhs,typename Rhs> \
155 struct name<Lhs,Rhs,typename boost::enable_if_c< \
156 (fpc::tolerance_based<Lhs>::value && \
157 fpc::tolerance_based<Rhs>::value)>::type> { \
159 typedef typename common_type<Lhs,Rhs>::type FPT; \
160 typedef name<Lhs,Rhs> OP; \
162 typedef assertion_result result_type; \
165 eval_direct( Lhs const& lhs, Rhs const& rhs ) \
167 return lhs oper rhs; \
170 static assertion_result \
171 eval( Lhs const& lhs, Rhs const& rhs ) \
173 if( fpc_tolerance<FPT>() == FPT(0) ) \
175 return eval_direct( lhs, rhs ); \
178 return compare_fpv<FPT>( lhs, rhs, (OP*)0 ); \
181 template<typename PrevExprType> \
183 report( std::ostream& ostr, \
184 PrevExprType const& lhs, \
187 lhs.report( ostr ); \
189 << tt_detail::print_helper( rhs ); \
192 static char const* revert() \
193 { return " " #rev " "; } \
197 BOOST_TEST_FOR_EACH_COMP_OP( DEFINE_FPV_COMPARISON )
198 #undef DEFINE_FPV_COMPARISON
200 //____________________________________________________________________________//
203 } // namespace assertion
204 } // namespace test_tools
207 #include <boost/test/detail/enable_warnings.hpp>
209 #endif // BOOST_TEST_TOOLS_FPC_OP_HPP_050915GER