mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-22 21:36:11 +01:00
597fca3c89
It was only used for std::max<double> within Approx, so we now have ::Catch::Detail::max(double, double) that is used instead.
124 lines
4.6 KiB
C++
124 lines
4.6 KiB
C++
/*
|
|
* Created by Phil on 28/04/2011.
|
|
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
|
|
*
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
*/
|
|
#ifndef TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
|
|
#define TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
|
|
|
|
#include "catch_tostring.h"
|
|
|
|
#include <cmath>
|
|
|
|
#include <type_traits>
|
|
|
|
namespace Catch {
|
|
namespace Detail {
|
|
|
|
double max(double lhs, double rhs);
|
|
|
|
class Approx {
|
|
public:
|
|
explicit Approx ( double value );
|
|
|
|
static Approx custom();
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
|
Approx operator()( T const& value ) {
|
|
Approx approx( static_cast<double>(value) );
|
|
approx.epsilon( m_epsilon );
|
|
approx.margin( m_margin );
|
|
approx.scale( m_scale );
|
|
return approx;
|
|
}
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
|
explicit Approx( T const& value ): Approx(static_cast<double>(value))
|
|
{}
|
|
|
|
|
|
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 = static_cast<double>(lhs);
|
|
bool relativeOK = std::fabs(lhs_v - rhs.m_value) < rhs.m_epsilon * (rhs.m_scale + (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>
|
|
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 const& 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 const& rhs ) {
|
|
return !operator==( rhs, lhs );
|
|
}
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
|
friend bool operator <= ( T const& lhs, Approx const& rhs ) {
|
|
return static_cast<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 const& rhs ) {
|
|
return lhs.m_value < static_cast<double>(rhs) || lhs == rhs;
|
|
}
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
|
friend bool operator >= ( T const& lhs, Approx const& rhs ) {
|
|
return static_cast<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 const& rhs ) {
|
|
return lhs.m_value > static_cast<double>(rhs) || lhs == rhs;
|
|
}
|
|
|
|
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);
|
|
return *this;
|
|
}
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
|
Approx& margin( T const& newMargin ) {
|
|
m_margin = static_cast<double>(newMargin);
|
|
return *this;
|
|
}
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
|
Approx& scale( T const& newScale ) {
|
|
m_scale = static_cast<double>(newScale);
|
|
return *this;
|
|
}
|
|
|
|
std::string toString() const;
|
|
|
|
private:
|
|
double m_epsilon;
|
|
double m_margin;
|
|
double m_scale;
|
|
double m_value;
|
|
};
|
|
}
|
|
|
|
template<>
|
|
struct StringMaker<Catch::Detail::Approx> {
|
|
static std::string convert(Catch::Detail::Approx const& value);
|
|
};
|
|
|
|
} // end namespace Catch
|
|
|
|
#endif // TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
|