diff options
| author | Jonathan Coe | 2016-05-12 19:18:44 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2017-01-26 18:44:03 +0100 |
| commit | c06afe438e261deac5658935a86cc9168ae4ed43 (patch) | |
| tree | 2c5dcd6537eb40f01ba1e41d399cdb533b4b88b3 /include | |
| parent | 73872207db8b729f46e2991039eb808fdf6ebc53 (diff) | |
| download | catch3-c06afe438e261deac5658935a86cc9168ae4ed43.tar.gz | |
Add support for comparison of Approx with strong typedefs
closes #652
Diffstat (limited to 'include')
| -rw-r--r-- | include/internal/catch_approx.hpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp index 163d2680..87d40fe9 100644 --- a/include/internal/catch_approx.hpp +++ b/include/internal/catch_approx.hpp @@ -13,6 +13,10 @@ #include <cmath> #include <limits> +#if defined(CATCH_CPP11_OR_GREATER) +#include <type_traits> +#endif + namespace Catch { namespace Detail { @@ -41,6 +45,53 @@ namespace Detail { return approx; } +#if defined(CATCH_CPP11_OR_GREATER) + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + 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 fabs( lhs_v - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs_v), fabs(rhs.m_value) ) ); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator == ( Approx const& lhs, const T& rhs ) { + return operator==( rhs, lhs ); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator != ( T lhs, Approx const& rhs ) { + return !operator==( lhs, rhs ); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator != ( Approx const& lhs, T rhs ) { + return !operator==( rhs, lhs ); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator <= ( T lhs, Approx const& rhs ) + { + return double(lhs) < rhs.m_value || lhs == rhs; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + 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> + friend bool operator >= ( T lhs, Approx const& rhs ) + { + return double(lhs) > rhs.m_value || lhs == rhs; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator >= ( Approx const& lhs, T rhs ) + { + return lhs.m_value > double(rhs) || lhs == rhs; + } +#else friend bool operator == ( double lhs, Approx const& rhs ) { // Thanks to Richard Harris for his help refining this formula return fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs), fabs(rhs.m_value) ) ); @@ -77,6 +128,7 @@ namespace Detail { { return lhs.m_value > rhs || lhs == rhs; } +#endif Approx& epsilon( double newEpsilon ) { m_epsilon = newEpsilon; |