2017-07-19 10:01:06 +02:00
|
|
|
/*
|
|
|
|
* Created by Martin on 19/07/2017.
|
|
|
|
* Copyright 2017 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)
|
|
|
|
*/
|
|
|
|
|
2017-09-07 12:24:33 +02:00
|
|
|
#include "catch_approx.h"
|
2018-09-03 10:03:47 +02:00
|
|
|
#include "catch_enforce.h"
|
2017-07-19 10:01:06 +02:00
|
|
|
|
2017-11-01 07:30:11 +01:00
|
|
|
#include <cmath>
|
2017-07-19 10:01:06 +02:00
|
|
|
#include <limits>
|
|
|
|
|
2017-11-01 07:30:11 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Performs equivalent check of std::fabs(lhs - rhs) <= margin
|
|
|
|
// But without the subtraction to allow for INFINITY in comparison
|
|
|
|
bool marginComparison(double lhs, double rhs, double margin) {
|
|
|
|
return (lhs + margin >= rhs) && (rhs + margin >= lhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:01:06 +02:00
|
|
|
namespace Catch {
|
|
|
|
namespace Detail {
|
|
|
|
|
|
|
|
Approx::Approx ( double value )
|
|
|
|
: m_epsilon( std::numeric_limits<float>::epsilon()*100 ),
|
|
|
|
m_margin( 0.0 ),
|
2017-10-26 09:19:57 +02:00
|
|
|
m_scale( 0.0 ),
|
2017-07-19 10:01:06 +02:00
|
|
|
m_value( value )
|
|
|
|
{}
|
|
|
|
|
|
|
|
Approx Approx::custom() {
|
|
|
|
return Approx( 0 );
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:42:40 +02:00
|
|
|
Approx Approx::operator-() const {
|
|
|
|
auto temp(*this);
|
|
|
|
temp.m_value = -temp.m_value;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-19 10:01:06 +02:00
|
|
|
std::string Approx::toString() const {
|
2017-11-07 19:01:10 +01:00
|
|
|
ReusableStringStream rss;
|
|
|
|
rss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )";
|
|
|
|
return rss.str();
|
2017-07-19 10:01:06 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 07:30:11 +01:00
|
|
|
bool Approx::equalityComparisonImpl(const double other) const {
|
|
|
|
// First try with fixed margin, then compute margin based on epsilon, scale and Approx's value
|
|
|
|
// Thanks to Richard Harris for his help refining the scaled margin value
|
|
|
|
return marginComparison(m_value, other, m_margin) || marginComparison(m_value, other, m_epsilon * (m_scale + std::fabs(m_value)));
|
|
|
|
}
|
|
|
|
|
2019-01-13 16:54:23 +01:00
|
|
|
void Approx::setMargin(double newMargin) {
|
|
|
|
CATCH_ENFORCE(newMargin >= 0,
|
|
|
|
"Invalid Approx::margin: " << newMargin << '.'
|
2018-09-03 10:03:47 +02:00
|
|
|
<< " Approx::Margin has to be non-negative.");
|
2019-01-13 16:54:23 +01:00
|
|
|
m_margin = newMargin;
|
2018-09-03 10:15:49 +02:00
|
|
|
}
|
|
|
|
|
2019-01-13 16:54:23 +01:00
|
|
|
void Approx::setEpsilon(double newEpsilon) {
|
|
|
|
CATCH_ENFORCE(newEpsilon >= 0 && newEpsilon <= 1.0,
|
|
|
|
"Invalid Approx::epsilon: " << newEpsilon << '.'
|
2018-09-03 10:03:47 +02:00
|
|
|
<< " Approx::epsilon has to be in [0, 1]");
|
2019-01-13 16:54:23 +01:00
|
|
|
m_epsilon = newEpsilon;
|
2018-09-03 10:15:49 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 10:01:06 +02:00
|
|
|
} // end namespace Detail
|
|
|
|
|
2018-05-21 15:42:40 +02:00
|
|
|
namespace literals {
|
|
|
|
Detail::Approx operator "" _a(long double val) {
|
|
|
|
return Detail::Approx(val);
|
|
|
|
}
|
|
|
|
Detail::Approx operator "" _a(unsigned long long val) {
|
|
|
|
return Detail::Approx(val);
|
|
|
|
}
|
|
|
|
} // end namespace literals
|
|
|
|
|
2017-07-19 10:01:06 +02:00
|
|
|
std::string StringMaker<Catch::Detail::Approx>::convert(Catch::Detail::Approx const& value) {
|
|
|
|
return value.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace Catch
|