catch3
Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.
Clonegit clone https://git.progmasoft.com/catch3
Approx rework: default scale == 0, epsilon applies to Approx::value
- Commit
00af677577973758b3b35e1c94ab4142c45c3f67- Author
- Pfiffikus ·
- Committer
- Martin Hořeňovský
- Parents
ae21020640e6
Changes
include/internal/catch_approx.cpp | 2 +- include/internal/catch_approx.h | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/internal/catch_approx.cpp b/include/internal/catch_approx.cpp index ca3ac94a..826a1049 100644 --- a/include/internal/catch_approx.cpp +++ b/include/internal/catch_approx.cpp @@ -23,7 +23,7 @@ namespace Detail { Approx::Approx ( double value ) : m_epsilon( std::numeric_limits<float>::epsilon()*100 ), m_margin( 0.0 ), - m_scale( 1.0 ), + m_scale( 0.0 ), m_value( value ) {} diff --git a/include/internal/catch_approx.h b/include/internal/catch_approx.h index 69428c8a..8ca7f315 100644 --- a/include/internal/catch_approx.h +++ b/include/internal/catch_approx.h @@ -44,8 +44,9 @@ namespace Detail { friend bool operator == ( const T& lhs, Approx const& rhs ) { // Thanks to Richard Harris for his help refining this formula auto lhs_v = static_cast<double>(lhs); - bool relativeOK = std::fabs(lhs_v - rhs.m_value) < rhs.m_epsilon * (rhs.m_scale + - dmax(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::fabs(rhs.m_value) ); + if (relativeOK) { return true; } @@ -89,7 +90,11 @@ namespace Detail { template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> Approx& epsilon( T const& newEpsilon ) { - m_epsilon = static_cast<double>(newEpsilon); + double asDouble = static_cast<double>(newEpsilon); + CATCH_ENFORCE(asDouble >= 0 && asDouble <= 1.0, + "Invalid Approx::epsilon: " << m_epsilon << + ", Approx::epsilon has to be between 0 and 1"); + m_epsilon = asDouble; return *this; }