Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
catch_approx.cpp
Go to the documentation of this file.
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
15namespace {
16
17// Performs equivalent check of std::fabs(lhs - rhs) <= margin
18// But without the subtraction to allow for INFINITY in comparison
19bool marginComparison(double lhs, double rhs, double margin) {
20 return (lhs + margin >= rhs) && (rhs + margin >= lhs);
21}
22
23}
24
25namespace Catch {
26namespace Detail {
27
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
36 return Approx( 0 );
37 }
38
40 auto temp(*this);
41 temp.m_value = -temp.m_value;
42 return temp;
43 }
44
45
46 std::string Approx::toString() const {
48 rss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )";
49 return rss.str();
50 }
51
52 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
58 void Approx::setMargin(double newMargin) {
59 CATCH_ENFORCE(newMargin >= 0,
60 "Invalid Approx::margin: " << newMargin << '.'
61 << " Approx::Margin has to be non-negative.");
62 m_margin = newMargin;
63 }
64
65 void Approx::setEpsilon(double newEpsilon) {
66 CATCH_ENFORCE(newEpsilon >= 0 && newEpsilon <= 1.0,
67 "Invalid Approx::epsilon: " << newEpsilon << '.'
68 << " Approx::epsilon has to be in [0, 1]");
69 m_epsilon = newEpsilon;
70 }
71
72} // end namespace Detail
73
74namespace literals {
75 Detail::Approx operator "" _a(long double val) {
76 return Detail::Approx(val);
77 }
78 Detail::Approx operator "" _a(unsigned long long val) {
79 return Detail::Approx(val);
80 }
81} // end namespace literals
82
84 return value.toString();
85}
86
87} // end namespace Catch
#define CATCH_ENFORCE(condition, msg)
std::string toString() const
Approx operator-() const
static Approx custom()
auto str() const -> std::string
std::string stringify(const T &e)
Definition name.hpp:106
#define value
Definition pkcs11.h:157
static std::enable_if<::Catch::Detail::IsStreamInsertable< Fake >::value, std::string >::type convert(const Fake &value)