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
15
namespace
{
16
17
// Performs equivalent check of std::fabs(lhs - rhs) <= margin
18
// But without the subtraction to allow for INFINITY in comparison
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
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
35
Approx
Approx::custom
() {
36
return
Approx
( 0 );
37
}
38
39
Approx
Approx::operator-
()
const
{
40
auto
temp(*
this
);
41
temp.m_value = -temp.m_value;
42
return
temp;
43
}
44
45
46
std::string
Approx::toString
()
const
{
47
ReusableStringStream
rss;
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
74
namespace
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
83
std::string
StringMaker<Catch::Detail::Approx>::convert
(
Catch::Detail::Approx
const
&
value
) {
84
return
value
.toString();
85
}
86
87
}
// end namespace Catch
catch_approx.h
catch_enforce.h
CATCH_ENFORCE
#define CATCH_ENFORCE(condition, msg)
Definition
catch_enforce.h:38
Catch::Detail::Approx
Definition
catch.hpp:2739
Catch::Detail::Approx::Approx
Approx(double value)
Definition
catch_approx.cpp:28
Catch::Detail::Approx::toString
std::string toString() const
Definition
catch_approx.cpp:46
Catch::Detail::Approx::operator-
Approx operator-() const
Definition
catch_approx.cpp:39
Catch::Detail::Approx::custom
static Approx custom()
Definition
catch_approx.cpp:35
Catch::ReusableStringStream
Definition
catch.hpp:1150
Catch::ReusableStringStream::str
auto str() const -> std::string
Definition
catch_stream.cpp:174
Catch::Detail::stringify
std::string stringify(const T &e)
Definition
catch_tostring.h:128
Catch
Definition
231-Cfg-OutputStreams.cpp:32
std
Definition
name.hpp:106
value
#define value
Definition
pkcs11.h:157
Catch::StringMaker::convert
static std::enable_if<::Catch::Detail::IsStreamInsertable< Fake >::value, std::string >::type convert(const Fake &value)
Definition
catch_tostring.h:103
libraries
sys-vm
external
Catch2
include
internal
catch_approx.cpp
Generated by
1.12.0