1 /*
2 * Created by Martin on 19/07/2017.
3 * Copyright 2017 Two Blue Cubes Ltd. All rights reserved.
4 *
5 * Distributed under the Boost Software License, Version 1.0. (See accompanying
6 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 */
8
9 #include "catch_approx.h"
10 #include "catch_enforce.h"
11
12 #include <cmath>
13 #include <limits>
14
15 namespace {
16
17 // Performs equivalent check of std::fabs(lhs - rhs) <= margin
18 // But without the subtraction to allow for INFINITY in comparison
marginComparison(double lhs,double rhs,double margin)19 bool marginComparison(double lhs, double rhs, double margin) {
20 return (lhs + margin >= rhs) && (rhs + margin >= lhs);
21 }
22
23 }
24
25 namespace Catch {
26 namespace Detail {
27
Approx(double value)28 Approx::Approx ( double value )
29 : m_epsilon( std::numeric_limits<float>::epsilon()*100 ),
30 m_margin( 0.0 ),
31 m_scale( 0.0 ),
32 m_value( value )
33 {}
34
custom()35 Approx Approx::custom() {
36 return Approx( 0 );
37 }
38
operator -() const39 Approx Approx::operator-() const {
40 auto temp(*this);
41 temp.m_value = -temp.m_value;
42 return temp;
43 }
44
45
toString() const46 std::string Approx::toString() const {
47 ReusableStringStream rss;
48 rss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )";
49 return rss.str();
50 }
51
equalityComparisonImpl(const double other) const52 bool Approx::equalityComparisonImpl(const double other) const {
53 // First try with fixed margin, then compute margin based on epsilon, scale and Approx's value
54 // Thanks to Richard Harris for his help refining the scaled margin value
55 return marginComparison(m_value, other, m_margin) || marginComparison(m_value, other, m_epsilon * (m_scale + std::fabs(m_value)));
56 }
57
setMargin(double margin)58 void Approx::setMargin(double margin) {
59 CATCH_ENFORCE(margin >= 0,
60 "Invalid Approx::margin: " << margin << '.'
61 << " Approx::Margin has to be non-negative.");
62 m_margin = margin;
63 }
64
setEpsilon(double epsilon)65 void Approx::setEpsilon(double epsilon) {
66 CATCH_ENFORCE(epsilon >= 0 && epsilon <= 1.0,
67 "Invalid Approx::epsilon: " << epsilon << '.'
68 << " Approx::epsilon has to be in [0, 1]");
69 m_epsilon = epsilon;
70 }
71
72 } // end namespace Detail
73
74 namespace literals {
operator ""_a(long double val)75 Detail::Approx operator "" _a(long double val) {
76 return Detail::Approx(val);
77 }
operator ""_a(unsigned long long val)78 Detail::Approx operator "" _a(unsigned long long val) {
79 return Detail::Approx(val);
80 }
81 } // end namespace literals
82
convert(Catch::Detail::Approx const & value)83 std::string StringMaker<Catch::Detail::Approx>::convert(Catch::Detail::Approx const& value) {
84 return value.toString();
85 }
86
87 } // end namespace Catch
88