Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r50357 - in trunk/boost/test: . impl
From: gennadiy.rozental_at_[hidden]
Date: 2008-12-22 02:59:28


Author: rogeeff
Date: 2008-12-22 02:59:27 EST (Mon, 22 Dec 2008)
New Revision: 50357
URL: http://svn.boost.org/trac/boost/changeset/50357

Log:
Update FP comparison tools:
1. Allow different types for lhs and rhs
2. Report difference if any
Text files modified:
   trunk/boost/test/floating_point_comparison.hpp | 44 +++++++++++++++++++++++++++++----------
   trunk/boost/test/impl/test_tools.ipp | 14 +++++-------
   2 files changed, 38 insertions(+), 20 deletions(-)

Modified: trunk/boost/test/floating_point_comparison.hpp
==============================================================================
--- trunk/boost/test/floating_point_comparison.hpp (original)
+++ trunk/boost/test/floating_point_comparison.hpp 2008-12-22 02:59:27 EST (Mon, 22 Dec 2008)
@@ -15,11 +15,15 @@
 #ifndef BOOST_TEST_FLOATING_POINT_COMPARISON_HPP_071894GER
 #define BOOST_TEST_FLOATING_POINT_COMPARISON_HPP_071894GER
 
-#include <boost/limits.hpp> // for std::numeric_limits
-
 // Boost.Test
 #include <boost/test/detail/global_typedef.hpp>
 #include <boost/test/utils/class_properties.hpp>
+#include <boost/test/predicate_result.hpp>
+
+// Boost
+#include <boost/limits.hpp> // for std::numeric_limits
+#include <boost/numeric/conversion/conversion_traits.hpp> // for numeric::conversion_traits
+#include <boost/static_assert.hpp>
 
 #include <boost/test/detail/suppress_warnings.hpp>
 
@@ -168,28 +172,38 @@
                                     floating_point_comparison_type fpc_type = FPC_STRONG )
     : p_fraction_tolerance( tt_detail::fpt_abs( static_cast<FPT>(0.01)*tolerance.m_value ) )
     , p_strong_or_weak( fpc_type == FPC_STRONG )
+ , m_report_modifier( 100. )
     {}
     template<typename ToleranceBaseType>
     explicit close_at_tolerance( fraction_tolerance_t<ToleranceBaseType> tolerance,
                                     floating_point_comparison_type fpc_type = FPC_STRONG )
     : p_fraction_tolerance( tt_detail::fpt_abs( tolerance.m_value ) )
     , p_strong_or_weak( fpc_type == FPC_STRONG )
+ , m_report_modifier( 1. )
     {}
 
- bool operator()( FPT left, FPT right ) const
+ predicate_result operator()( FPT left, FPT right ) const
     {
         FPT diff = tt_detail::fpt_abs( left - right );
         FPT d1 = tt_detail::safe_fpt_division( diff, tt_detail::fpt_abs( right ) );
         FPT d2 = tt_detail::safe_fpt_division( diff, tt_detail::fpt_abs( left ) );
         
- return p_strong_or_weak
- ? (d1 <= p_fraction_tolerance.get() && d2 <= p_fraction_tolerance.get())
- : (d1 <= p_fraction_tolerance.get() || d2 <= p_fraction_tolerance.get());
+ predicate_result res( p_strong_or_weak
+ ? (d1 <= p_fraction_tolerance.get() && d2 <= p_fraction_tolerance.get())
+ : (d1 <= p_fraction_tolerance.get() || d2 <= p_fraction_tolerance.get()) );
+
+ if( !res )
+ res.message() << (( d1 <= p_fraction_tolerance.get() ? d2 : d1 ) * m_report_modifier);
+
+ return res;
     }
 
     // Public properties
     readonly_property<FPT> p_fraction_tolerance;
     readonly_property<bool> p_strong_or_weak;
+private:
+ // Data members
+ FPT m_report_modifier;
 };
 
 //____________________________________________________________________________//
@@ -202,20 +216,26 @@
     // Public typedefs
     typedef bool result_type;
 
- template<typename FPT, typename ToleranceBaseType>
- bool
- operator()( FPT left, FPT right, percent_tolerance_t<ToleranceBaseType> tolerance,
+ template<typename FPT1, typename FPT2, typename ToleranceBaseType>
+ predicate_result
+ operator()( FPT1 left, FPT2 right, percent_tolerance_t<ToleranceBaseType> tolerance,
                 floating_point_comparison_type fpc_type = FPC_STRONG )
     {
+ typedef typename numeric::conversion_traits<FPT1,FPT2>::supertype FPT;
+ BOOST_STATIC_ASSERT( !is_integral<FPT>::value );
+
         close_at_tolerance<FPT> pred( tolerance, fpc_type );
 
         return pred( left, right );
     }
- template<typename FPT, typename ToleranceBaseType>
- bool
- operator()( FPT left, FPT right, fraction_tolerance_t<ToleranceBaseType> tolerance,
+ template<typename FPT1, typename FPT2, typename ToleranceBaseType>
+ predicate_result
+ operator()( FPT1 left, FPT2 right, fraction_tolerance_t<ToleranceBaseType> tolerance,
                 floating_point_comparison_type fpc_type = FPC_STRONG )
     {
+ typedef typename numeric::conversion_traits<FPT1,FPT2>::supertype FPT;
+ BOOST_STATIC_ASSERT( !is_integral<FPT>::value );
+
         close_at_tolerance<FPT> pred( tolerance, fpc_type );
 
         return pred( left, right );

Modified: trunk/boost/test/impl/test_tools.ipp
==============================================================================
--- trunk/boost/test/impl/test_tools.ipp (original)
+++ trunk/boost/test/impl/test_tools.ipp 2008-12-22 02:59:27 EST (Mon, 22 Dec 2008)
@@ -227,18 +227,16 @@
 
         unit_test_log << unit_test::log::begin( file_name, line_num ) << ll;
 
- unit_test_log << "difference between " << arg1_descr << "{" << *arg1_val << "}"
- << " and " << arg2_descr << "{" << *arg2_val << "}"
- << ( tl == PASS ? " doesn't exceed " : " exceeds " )
+ unit_test_log << "difference{" << pr.message() << (ct == CHECK_CLOSE ? "%" : "")
+ << "} between " << arg1_descr << "{" << *arg1_val
+ << "} and " << arg2_descr << "{" << *arg2_val
+ << ( tl == PASS ? "} doesn't exceed " : "} exceeds " )
                       << *toler_val;
         if( ct == CHECK_CLOSE )
             unit_test_log << "%";
 
         va_end( args );
         
- if( !pr.has_empty_message() )
- unit_test_log << ". " << pr.message();
-
         unit_test_log << unit_test::log::end();
         break;
     }
@@ -451,7 +449,7 @@
         m_pimpl->m_pattern.open( pattern_file_name.begin(), m );
 
         BOOST_WARN_MESSAGE( m_pimpl->m_pattern.is_open(),
- "Couldn't open pattern file " << pattern_file_name
+ "Can't open pattern file " << pattern_file_name
                                 << " for " << (match_or_save ? "reading" : "writing") );
     }
 
@@ -528,7 +526,7 @@
 
     if( !m_pimpl->m_pattern.is_open() ) {
         result = false;
- result.message() << "Pattern file could not be opened!";
+ result.message() << "Pattern file can't be opened!";
     }
     else {
         if( m_pimpl->m_match_or_save ) {


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk