Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorMartin Hořeňovský2017-02-27 13:12:13 +0100
committerMartin Hořeňovský2017-02-27 13:12:13 +0100
commitd5c623b3b69d9ecbd9bc6d48d5be4ded5a216770 (patch)
tree54c5d4c373afec5f89caa897abd7496aad0ad1d0 /include
parent061a18303635e4101309ae874a09f0f4bf4477ad (diff)
parent3523c39f44c855c1d6f0d7ef3d3a3ea12d4a8987 (diff)
downloadcatch3-d5c623b3b69d9ecbd9bc6d48d5be4ded5a216770.tar.gz
Merge branch 'develop' of https://github.com/CNugteren/Catch
This adds an optional absolute margin to the Approx checks.
Diffstat (limited to 'include')
-rw-r--r--include/internal/catch_approx.hpp21
1 files changed, 19 insertions, 2 deletions
diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp
index 23f42371..5d6df1d7 100644
--- a/include/internal/catch_approx.hpp
+++ b/include/internal/catch_approx.hpp
@@ -24,12 +24,14 @@ namespace Detail {
public:
explicit Approx ( double value )
: m_epsilon( std::numeric_limits<float>::epsilon()*100 ),
+ m_margin( 0.0 ),
m_scale( 1.0 ),
m_value( value )
{}
Approx( Approx const& other )
: m_epsilon( other.m_epsilon ),
+ m_margin( other.m_margin ),
m_scale( other.m_scale ),
m_value( other.m_value )
{}
@@ -41,6 +43,7 @@ namespace Detail {
Approx operator()( double value ) {
Approx approx( value );
approx.epsilon( m_epsilon );
+ approx.margin( m_margin );
approx.scale( m_scale );
return approx;
}
@@ -50,7 +53,11 @@ namespace Detail {
friend bool operator == ( const T& lhs, Approx const& rhs ) {
// Thanks to Richard Harris for his help refining this formula
auto lhs_v = double(lhs);
- return std::fabs( lhs_v - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( std::fabs(lhs_v), std::fabs(rhs.m_value) ) );
+ bool relativeOK = std::fabs(lhs_v - rhs.m_value) < rhs.m_epsilon * (rhs.m_scale + (std::max)(std::fabs(lhs_v), std::fabs(rhs.m_value)));
+ if (relativeOK) {
+ return true;
+ }
+ return std::fabs(lhs_v - rhs.m_value) < rhs.m_margin;
}
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
@@ -94,7 +101,11 @@ namespace Detail {
#else
friend bool operator == ( double lhs, Approx const& rhs ) {
// Thanks to Richard Harris for his help refining this formula
- return std::fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( std::fabs(lhs), std::fabs(rhs.m_value) ) );
+ bool relativeOK = std::fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( std::fabs(lhs), std::fabs(rhs.m_value) ) );
+ if (relativeOK) {
+ return true;
+ }
+ return std::fabs(lhs - rhs.m_value) < rhs.m_margin;
}
friend bool operator == ( Approx const& lhs, double rhs ) {
@@ -135,6 +146,11 @@ namespace Detail {
return *this;
}
+ Approx& margin( double newMargin ) {
+ m_margin = newMargin;
+ return *this;
+ }
+
Approx& scale( double newScale ) {
m_scale = newScale;
return *this;
@@ -148,6 +164,7 @@ namespace Detail {
private:
double m_epsilon;
+ double m_margin;
double m_scale;
double m_value;
};