1 /*
2 * Created by Martin on 07/11/2017.
3 *
4 * Distributed under the Boost Software License, Version 1.0. (See accompanying
5 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 */
7
8 #include "catch_matchers_floating.h"
9 #include "catch_enforce.h"
10 #include "catch_polyfills.hpp"
11 #include "catch_to_string.hpp"
12 #include "catch_tostring.h"
13
14 #include <algorithm>
15 #include <cmath>
16 #include <cstdlib>
17 #include <cstdint>
18 #include <cstring>
19 #include <sstream>
20 #include <type_traits>
21 #include <iomanip>
22 #include <limits>
23
24
25 namespace Catch {
26 namespace {
27
convert(float f)28 int32_t convert(float f) {
29 static_assert(sizeof(float) == sizeof(int32_t), "Important ULP matcher assumption violated");
30 int32_t i;
31 std::memcpy(&i, &f, sizeof(f));
32 return i;
33 }
34
convert(double d)35 int64_t convert(double d) {
36 static_assert(sizeof(double) == sizeof(int64_t), "Important ULP matcher assumption violated");
37 int64_t i;
38 std::memcpy(&i, &d, sizeof(d));
39 return i;
40 }
41
42 template <typename FP>
almostEqualUlps(FP lhs,FP rhs,uint64_t maxUlpDiff)43 bool almostEqualUlps(FP lhs, FP rhs, uint64_t maxUlpDiff) {
44 // Comparison with NaN should always be false.
45 // This way we can rule it out before getting into the ugly details
46 if (Catch::isnan(lhs) || Catch::isnan(rhs)) {
47 return false;
48 }
49
50 auto lc = convert(lhs);
51 auto rc = convert(rhs);
52
53 if ((lc < 0) != (rc < 0)) {
54 // Potentially we can have +0 and -0
55 return lhs == rhs;
56 }
57
58 // static cast as a workaround for IBM XLC
59 auto ulpDiff = std::abs(static_cast<FP>(lc - rc));
60 return static_cast<uint64_t>(ulpDiff) <= maxUlpDiff;
61 }
62
63 #if defined(CATCH_CONFIG_GLOBAL_NEXTAFTER)
64
nextafter(float x,float y)65 float nextafter(float x, float y) {
66 return ::nextafterf(x, y);
67 }
68
nextafter(double x,double y)69 double nextafter(double x, double y) {
70 return ::nextafter(x, y);
71 }
72
73 #endif // ^^^ CATCH_CONFIG_GLOBAL_NEXTAFTER ^^^
74
75 template <typename FP>
step(FP start,FP direction,uint64_t steps)76 FP step(FP start, FP direction, uint64_t steps) {
77 for (uint64_t i = 0; i < steps; ++i) {
78 #if defined(CATCH_CONFIG_GLOBAL_NEXTAFTER)
79 start = Catch::nextafter(start, direction);
80 #else
81 start = std::nextafter(start, direction);
82 #endif
83 }
84 return start;
85 }
86
87 // Performs equivalent check of std::fabs(lhs - rhs) <= margin
88 // But without the subtraction to allow for INFINITY in comparison
marginComparison(double lhs,double rhs,double margin)89 bool marginComparison(double lhs, double rhs, double margin) {
90 return (lhs + margin >= rhs) && (rhs + margin >= lhs);
91 }
92
93 template <typename FloatingPoint>
write(std::ostream & out,FloatingPoint num)94 void write(std::ostream& out, FloatingPoint num) {
95 out << std::scientific
96 << std::setprecision(std::numeric_limits<FloatingPoint>::max_digits10 - 1)
97 << num;
98 }
99
100 } // end anonymous namespace
101
102 namespace Matchers {
103 namespace Floating {
104
105 enum class FloatingPointKind : uint8_t {
106 Float,
107 Double
108 };
109
110
WithinAbsMatcher(double target,double margin)111 WithinAbsMatcher::WithinAbsMatcher(double target, double margin)
112 :m_target{ target }, m_margin{ margin } {
113 CATCH_ENFORCE(margin >= 0, "Invalid margin: " << margin << '.'
114 << " Margin has to be non-negative.");
115 }
116
117 // Performs equivalent check of std::fabs(lhs - rhs) <= margin
118 // But without the subtraction to allow for INFINITY in comparison
match(double const & matchee) const119 bool WithinAbsMatcher::match(double const& matchee) const {
120 return (matchee + m_margin >= m_target) && (m_target + m_margin >= matchee);
121 }
122
describe() const123 std::string WithinAbsMatcher::describe() const {
124 return "is within " + ::Catch::Detail::stringify(m_margin) + " of " + ::Catch::Detail::stringify(m_target);
125 }
126
127
WithinUlpsMatcher(double target,uint64_t ulps,FloatingPointKind baseType)128 WithinUlpsMatcher::WithinUlpsMatcher(double target, uint64_t ulps, FloatingPointKind baseType)
129 :m_target{ target }, m_ulps{ ulps }, m_type{ baseType } {
130 CATCH_ENFORCE(m_type == FloatingPointKind::Double
131 || m_ulps < (std::numeric_limits<uint32_t>::max)(),
132 "Provided ULP is impossibly large for a float comparison.");
133 }
134
135 #if defined(__clang__)
136 #pragma clang diagnostic push
137 // Clang <3.5 reports on the default branch in the switch below
138 #pragma clang diagnostic ignored "-Wunreachable-code"
139 #endif
140
match(double const & matchee) const141 bool WithinUlpsMatcher::match(double const& matchee) const {
142 switch (m_type) {
143 case FloatingPointKind::Float:
144 return almostEqualUlps<float>(static_cast<float>(matchee), static_cast<float>(m_target), m_ulps);
145 case FloatingPointKind::Double:
146 return almostEqualUlps<double>(matchee, m_target, m_ulps);
147 default:
148 CATCH_INTERNAL_ERROR( "Unknown FloatingPointKind value" );
149 }
150 }
151
152 #if defined(__clang__)
153 #pragma clang diagnostic pop
154 #endif
155
describe() const156 std::string WithinUlpsMatcher::describe() const {
157 std::stringstream ret;
158
159 ret << "is within " << m_ulps << " ULPs of ";
160
161 if (m_type == FloatingPointKind::Float) {
162 write(ret, static_cast<float>(m_target));
163 ret << 'f';
164 } else {
165 write(ret, m_target);
166 }
167
168 ret << " ([";
169 if (m_type == FloatingPointKind::Double) {
170 write(ret, step(m_target, static_cast<double>(-INFINITY), m_ulps));
171 ret << ", ";
172 write(ret, step(m_target, static_cast<double>( INFINITY), m_ulps));
173 } else {
174 // We have to cast INFINITY to float because of MinGW, see #1782
175 write(ret, step(static_cast<float>(m_target), static_cast<float>(-INFINITY), m_ulps));
176 ret << ", ";
177 write(ret, step(static_cast<float>(m_target), static_cast<float>( INFINITY), m_ulps));
178 }
179 ret << "])";
180
181 return ret.str();
182 }
183
WithinRelMatcher(double target,double epsilon)184 WithinRelMatcher::WithinRelMatcher(double target, double epsilon):
185 m_target(target),
186 m_epsilon(epsilon){
187 CATCH_ENFORCE(m_epsilon >= 0., "Relative comparison with epsilon < 0 does not make sense.");
188 CATCH_ENFORCE(m_epsilon < 1., "Relative comparison with epsilon >= 1 does not make sense.");
189 }
190
match(double const & matchee) const191 bool WithinRelMatcher::match(double const& matchee) const {
192 const auto relMargin = m_epsilon * (std::max)(std::fabs(matchee), std::fabs(m_target));
193 return marginComparison(matchee, m_target,
194 std::isinf(relMargin)? 0 : relMargin);
195 }
196
describe() const197 std::string WithinRelMatcher::describe() const {
198 Catch::ReusableStringStream sstr;
199 sstr << "and " << m_target << " are within " << m_epsilon * 100. << "% of each other";
200 return sstr.str();
201 }
202
203 }// namespace Floating
204
205
206
WithinULP(double target,uint64_t maxUlpDiff)207 Floating::WithinUlpsMatcher WithinULP(double target, uint64_t maxUlpDiff) {
208 return Floating::WithinUlpsMatcher(target, maxUlpDiff, Floating::FloatingPointKind::Double);
209 }
210
WithinULP(float target,uint64_t maxUlpDiff)211 Floating::WithinUlpsMatcher WithinULP(float target, uint64_t maxUlpDiff) {
212 return Floating::WithinUlpsMatcher(target, maxUlpDiff, Floating::FloatingPointKind::Float);
213 }
214
WithinAbs(double target,double margin)215 Floating::WithinAbsMatcher WithinAbs(double target, double margin) {
216 return Floating::WithinAbsMatcher(target, margin);
217 }
218
WithinRel(double target,double eps)219 Floating::WithinRelMatcher WithinRel(double target, double eps) {
220 return Floating::WithinRelMatcher(target, eps);
221 }
222
WithinRel(double target)223 Floating::WithinRelMatcher WithinRel(double target) {
224 return Floating::WithinRelMatcher(target, std::numeric_limits<double>::epsilon() * 100);
225 }
226
WithinRel(float target,float eps)227 Floating::WithinRelMatcher WithinRel(float target, float eps) {
228 return Floating::WithinRelMatcher(target, eps);
229 }
230
WithinRel(float target)231 Floating::WithinRelMatcher WithinRel(float target) {
232 return Floating::WithinRelMatcher(target, std::numeric_limits<float>::epsilon() * 100);
233 }
234
235
236 } // namespace Matchers
237 } // namespace Catch
238