2011-04-28 09:03:28 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2014-04-23 07:51:58 +02:00
|
|
|
#include "catch_tostring.h"
|
2011-04-28 09:03:28 +02:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
2016-05-12 20:18:44 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
2012-05-15 08:42:26 +02:00
|
|
|
namespace Catch {
|
|
|
|
namespace Detail {
|
|
|
|
|
2017-08-17 15:34:00 +02:00
|
|
|
double max(double lhs, double rhs);
|
|
|
|
|
2012-05-15 08:42:26 +02:00
|
|
|
class Approx {
|
|
|
|
public:
|
2017-07-19 10:01:06 +02:00
|
|
|
explicit Approx ( double value );
|
2012-05-15 08:42:26 +02:00
|
|
|
|
2017-07-19 10:01:06 +02:00
|
|
|
static Approx custom();
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-04-24 22:01:45 +02:00
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
2017-07-27 10:45:39 +02:00
|
|
|
Approx operator()( T const& value ) {
|
2017-04-24 22:01:45 +02:00
|
|
|
Approx approx( static_cast<double>(value) );
|
2012-05-15 08:42:26 +02:00
|
|
|
approx.epsilon( m_epsilon );
|
2015-11-12 15:07:20 +01:00
|
|
|
approx.margin( m_margin );
|
2012-05-15 08:42:26 +02:00
|
|
|
approx.scale( m_scale );
|
|
|
|
return approx;
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-04-04 15:19:15 +02:00
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
2017-07-27 10:45:39 +02:00
|
|
|
explicit Approx( T const& value ): Approx(static_cast<double>(value))
|
2017-04-04 15:19:15 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2016-05-12 20:18:44 +02:00
|
|
|
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
|
2017-07-27 10:45:39 +02:00
|
|
|
auto lhs_v = static_cast<double>(lhs);
|
2017-08-17 15:34:00 +02:00
|
|
|
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)));
|
2017-02-27 13:12:13 +01:00
|
|
|
if (relativeOK) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return std::fabs(lhs_v - rhs.m_value) < rhs.m_margin;
|
2016-05-12 20:18:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
2017-07-27 10:45:39 +02:00
|
|
|
friend bool operator != ( T const& lhs, Approx const& rhs ) {
|
2016-05-12 20:18:44 +02:00
|
|
|
return !operator==( lhs, rhs );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
2017-07-27 10:45:39 +02:00
|
|
|
friend bool operator != ( Approx const& lhs, T const& rhs ) {
|
2016-05-12 20:18:44 +02:00
|
|
|
return !operator==( rhs, lhs );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
2017-07-27 10:45:39 +02:00
|
|
|
friend bool operator <= ( T const& lhs, Approx const& rhs ) {
|
|
|
|
return static_cast<double>(lhs) < rhs.m_value || lhs == rhs;
|
2016-05-12 20:18:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
2017-07-27 10:45:39 +02:00
|
|
|
friend bool operator <= ( Approx const& lhs, T const& rhs ) {
|
|
|
|
return lhs.m_value < static_cast<double>(rhs) || lhs == rhs;
|
2016-05-12 20:18:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
2017-07-27 10:45:39 +02:00
|
|
|
friend bool operator >= ( T const& lhs, Approx const& rhs ) {
|
|
|
|
return static_cast<double>(lhs) > rhs.m_value || lhs == rhs;
|
2016-05-12 20:18:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
2017-07-27 10:45:39 +02:00
|
|
|
friend bool operator >= ( Approx const& lhs, T const& rhs ) {
|
|
|
|
return lhs.m_value > static_cast<double>(rhs) || lhs == rhs;
|
2016-05-12 20:18:44 +02:00
|
|
|
}
|
2017-04-24 22:01:45 +02:00
|
|
|
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
2017-07-27 10:45:39 +02:00
|
|
|
Approx& epsilon( T const& newEpsilon ) {
|
|
|
|
m_epsilon = static_cast<double>(newEpsilon);
|
2017-04-24 22:01:45 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
2017-07-27 10:45:39 +02:00
|
|
|
Approx& margin( T const& newMargin ) {
|
|
|
|
m_margin = static_cast<double>(newMargin);
|
2017-04-24 22:01:45 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
|
2017-07-27 10:45:39 +02:00
|
|
|
Approx& scale( T const& newScale ) {
|
|
|
|
m_scale = static_cast<double>(newScale);
|
2017-04-24 22:01:45 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:01:06 +02:00
|
|
|
std::string toString() const;
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-15 08:42:26 +02:00
|
|
|
private:
|
|
|
|
double m_epsilon;
|
2015-11-12 15:07:20 +01:00
|
|
|
double m_margin;
|
2012-05-15 08:42:26 +02:00
|
|
|
double m_scale;
|
|
|
|
double m_value;
|
|
|
|
};
|
|
|
|
}
|
2011-04-28 09:20:47 +02:00
|
|
|
|
2012-05-15 08:42:26 +02:00
|
|
|
template<>
|
2017-05-02 23:51:03 +02:00
|
|
|
struct StringMaker<Catch::Detail::Approx> {
|
2017-07-19 10:01:06 +02:00
|
|
|
static std::string convert(Catch::Detail::Approx const& value);
|
2017-05-02 23:51:03 +02:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2011-04-28 09:03:28 +02:00
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
|