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
All of Approx's member functions now accept strong typedefs
- Commit
a34c053f0a06f719f93f7e7a3ab4319cd6dd10b4- Author
- Martin Hořeňovský ·
- Parents
4cdb203ec3eb
Changes
include/internal/catch_approx.hpp | 40 ++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp index 940b641a..fc681436 100644 --- a/include/internal/catch_approx.hpp +++ b/include/internal/catch_approx.hpp @@ -40,16 +40,17 @@ namespace Detail { return Approx( 0 ); } - Approx operator()( double value ) { - Approx approx( value ); +#if defined(CATCH_CONFIG_CPP11_TYPE_TRAITS) + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + Approx operator()( T value ) { + Approx approx( static_cast<double>(value) ); approx.epsilon( m_epsilon ); approx.margin( m_margin ); approx.scale( m_scale ); return approx; } -#if defined(CATCH_CONFIG_CPP11_TYPE_TRAITS) - template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> explicit Approx( T value ): Approx(static_cast<double>(value)) {} @@ -100,7 +101,36 @@ namespace Detail { friend bool operator >= ( Approx const& lhs, T rhs ) { return lhs.m_value > double(rhs) || lhs == rhs; } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + Approx& epsilon( T newEpsilon ) { + m_epsilon = double(newEpsilon); + return *this; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + Approx& margin( T newMargin ) { + m_margin = double(newMargin); + return *this; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + Approx& scale( T newScale ) { + m_scale = double(newScale); + return *this; + } + #else + + Approx operator()( double value ) { + Approx approx( value ); + approx.epsilon( m_epsilon ); + approx.margin( m_margin ); + approx.scale( m_scale ); + return approx; + } + + friend bool operator == ( double lhs, Approx const& rhs ) { // Thanks to Richard Harris for his help refining this formula bool relativeOK = std::fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( std::fabs(lhs), std::fabs(rhs.m_value) ) ); @@ -137,7 +167,6 @@ namespace Detail { friend bool operator >= ( Approx const& lhs, double rhs ) { return lhs.m_value > rhs || lhs == rhs; } -#endif Approx& epsilon( double newEpsilon ) { m_epsilon = newEpsilon; @@ -153,6 +182,7 @@ namespace Detail { m_scale = newScale; return *this; } +#endif std::string toString() const { std::ostringstream oss;