mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-22 21:36:11 +01:00
Merge branch 'develop' of https://github.com/CNugteren/Catch
This adds an optional absolute margin to the Approx checks.
This commit is contained in:
commit
d5c623b3b6
@ -24,12 +24,14 @@ namespace Detail {
|
|||||||
public:
|
public:
|
||||||
explicit Approx ( double value )
|
explicit Approx ( double value )
|
||||||
: m_epsilon( std::numeric_limits<float>::epsilon()*100 ),
|
: m_epsilon( std::numeric_limits<float>::epsilon()*100 ),
|
||||||
|
m_margin( 0.0 ),
|
||||||
m_scale( 1.0 ),
|
m_scale( 1.0 ),
|
||||||
m_value( value )
|
m_value( value )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Approx( Approx const& other )
|
Approx( Approx const& other )
|
||||||
: m_epsilon( other.m_epsilon ),
|
: m_epsilon( other.m_epsilon ),
|
||||||
|
m_margin( other.m_margin ),
|
||||||
m_scale( other.m_scale ),
|
m_scale( other.m_scale ),
|
||||||
m_value( other.m_value )
|
m_value( other.m_value )
|
||||||
{}
|
{}
|
||||||
@ -41,6 +43,7 @@ namespace Detail {
|
|||||||
Approx operator()( double value ) {
|
Approx operator()( double value ) {
|
||||||
Approx approx( value );
|
Approx approx( value );
|
||||||
approx.epsilon( m_epsilon );
|
approx.epsilon( m_epsilon );
|
||||||
|
approx.margin( m_margin );
|
||||||
approx.scale( m_scale );
|
approx.scale( m_scale );
|
||||||
return approx;
|
return approx;
|
||||||
}
|
}
|
||||||
@ -50,7 +53,11 @@ namespace Detail {
|
|||||||
friend bool operator == ( const T& lhs, Approx const& rhs ) {
|
friend bool operator == ( const T& lhs, Approx const& rhs ) {
|
||||||
// Thanks to Richard Harris for his help refining this formula
|
// Thanks to Richard Harris for his help refining this formula
|
||||||
auto lhs_v = double(lhs);
|
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>
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
||||||
@ -94,7 +101,11 @@ namespace Detail {
|
|||||||
#else
|
#else
|
||||||
friend bool operator == ( double lhs, Approx const& rhs ) {
|
friend bool operator == ( double lhs, Approx const& rhs ) {
|
||||||
// Thanks to Richard Harris for his help refining this formula
|
// 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 ) {
|
friend bool operator == ( Approx const& lhs, double rhs ) {
|
||||||
@ -135,6 +146,11 @@ namespace Detail {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Approx& margin( double newMargin ) {
|
||||||
|
m_margin = newMargin;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
Approx& scale( double newScale ) {
|
Approx& scale( double newScale ) {
|
||||||
m_scale = newScale;
|
m_scale = newScale;
|
||||||
return *this;
|
return *this;
|
||||||
@ -148,6 +164,7 @@ namespace Detail {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
double m_epsilon;
|
double m_epsilon;
|
||||||
|
double m_margin;
|
||||||
double m_scale;
|
double m_scale;
|
||||||
double m_value;
|
double m_value;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user