Sequoia
Loading...
Searching...
No Matches
BinaryRelationships.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2021. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
19
20#include <cmath>
21#include <functional>
22
23namespace sequoia::testing
24{
25 namespace abs_detail
26 {
27 using std::abs;
28
29 template<class T, class NormType>
30 inline constexpr static bool has_abs{requires(const T& x) { { abs(x) } -> std::same_as<NormType>; }};
31 }
32
33 template<class T, class NormType>
34 inline constexpr static bool has_abs{abs_detail::has_abs<T, NormType>};
35
42 template<class Compare>
44 {
45 template<bool IsFinalMessage, class T>
46 [[nodiscard]]
47 static std::string reporter(final_message_constant<IsFinalMessage>, const Compare&, const T&, const T&) = delete;
48 };
49
54 template<std::totally_ordered ToleranceType>
56 {
57 ToleranceType m_Tol{};
58 public:
59 using tolerance_type = ToleranceType;
60
61 constexpr explicit within_tolerance(ToleranceType tol) : m_Tol{std::move(tol)} {};
62
63 [[nodiscard]]
64 constexpr tolerance_type tol() const noexcept
65 {
66 return m_Tol;
67 }
68
69 template<class T>
70 requires has_abs<T, ToleranceType>
71 [[nodiscard]]
72 constexpr bool operator()(const T& obtained, const T& prediction) const noexcept
73 {
74 using std::abs;
75 return abs(obtained - prediction) <= m_Tol;
76 }
77 };
78
79 template<class T>
81 {
82 template<bool IsFinalMessage, class ComparedType>
84 [[nodiscard]]
85 static std::string reporter(final_message_constant<IsFinalMessage>, const within_tolerance<T>& c, const ComparedType& obtained, const ComparedType& prediction)
86 {
87 return prediction_message(obtained, prediction).append(" +/- ").append(to_string(c.tol()));
88 }
89 };
90
91 template<>
92 struct failure_reporter<std::ranges::equal_to>
93 {
94 template<bool IsFinalMessage, class T>
95 requires (reportable<T> || !IsFinalMessage)
96 [[nodiscard]]
97 static std::string reporter(final_message_constant<IsFinalMessage>, const std::ranges::equal_to&, const T& obtained, const T& prediction)
98 {
99 return failure_message(final_message_constant<IsFinalMessage>{}, obtained, prediction);
100 }
101 };
102
103 template<class T>
104 [[nodiscard]]
105 std::string relational_failure_message(std::string symbol, const T& obtained, const T& prediction)
106 {
107 return prediction_message(to_string(obtained), symbol.append(" ").append(to_string(prediction)));
108 }
109
110 template<>
111 struct failure_reporter<std::ranges::less>
112 {
113 template<bool IsFinalMessage, class T>
114 requires reportable<T>
115 [[nodiscard]]
116 static std::string reporter(final_message_constant<IsFinalMessage>, const std::ranges::less&, const T& obtained, const T& prediction)
117 {
118 return relational_failure_message("<", obtained, prediction);
119 }
120 };
121
122 template<>
123 struct failure_reporter<std::ranges::less_equal>
124 {
125 template<bool IsFinalMessage, class T>
126 requires reportable<T>
127 [[nodiscard]]
128 static std::string reporter(final_message_constant<IsFinalMessage>, const std::ranges::less_equal&, const T& obtained, const T& prediction)
129 {
130 return relational_failure_message("<=", obtained, prediction);
131 }
132 };
133
134 template<>
135 struct failure_reporter<std::ranges::greater>
136 {
137 template<bool IsFinalMessage, class T>
138 requires reportable<T>
139 [[nodiscard]]
140 static std::string reporter(final_message_constant<IsFinalMessage>, const std::ranges::greater&, const T& obtained, const T& prediction)
141 {
142 return relational_failure_message(">", obtained, prediction);
143 }
144 };
145
146 template<>
147 struct failure_reporter<std::ranges::greater_equal>
148 {
149 template<bool IsFinalMessage, class T>
150 requires reportable<T>
151 [[nodiscard]]
152 static std::string reporter(final_message_constant<IsFinalMessage>, const std::ranges::greater_equal&, const T& obtained, const T& prediction)
153 {
154 return relational_failure_message(">=", obtained, prediction);
155 }
156 };
157}
A collection of functions for formatting test output.
std::string failure_message(is_final_message_t, T, T)
To prevent implicit conversions to bool.
Definition: Output.hpp:146
Definition: Output.hpp:186
Function object for performing comparisons within an absolute tolerance.
Definition: BinaryRelationships.hpp:56
Definition: Output.hpp:140
Specialize this struct template to provide custom reporting for comparisons performed with a binary o...
Definition: BinaryRelationships.hpp:44