-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmatchers.h
More file actions
241 lines (221 loc) · 7.75 KB
/
matchers.h
File metadata and controls
241 lines (221 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright (C) 2020-2026 MEmilio
*
* Authors: Daniel Abele
*
* Contact: Martin J. Kuehn <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MIO_TESTS_MATCHERS_H
#define MIO_TESTS_MATCHERS_H
#include "memilio/config.h"
#include "memilio/utils/compiler_diagnostics.h"
#include "memilio/math/floating_point.h"
#include "memilio/io/io.h"
#include "gmock/gmock.h"
#ifdef MEMILIO_HAS_JSONCPP
#include "json/json.h"
namespace Json
{
void PrintTo(const Value& json, std::ostream* os);
} // namespace Json
std::string json_type_to_string(Json::ValueType t);
MATCHER_P(JsonEqual, expected_json, testing::PrintToString(expected_json))
{
auto match_rec = [&](auto&& match, const Json::Value& a, const Json::Value& b, std::string name) {
// first check if the types match
if (a.type() != b.type()) {
*result_listener << "type mismatch for " << name << ", expected " << json_type_to_string(a.type())
<< ", actual " << json_type_to_string(b.type());
return false;
}
// handle object types by recursively matching members
if (a.isObject()) {
for (auto& key : a.getMemberNames()) {
if (!b.isMember(key)) {
*result_listener << "missing key \"" << key << "\" in " << name;
return false;
}
if (!match(match, a[key], b[key], name + "[\"" + key + "\"]")) {
return false;
}
}
}
// handle arrays by recursively matching each item
else if (a.isArray()) {
if (a.size() != b.size()) {
*result_listener << "wrong number of items in " << name << ", expected " << a.size() << ", actual "
<< b.size();
return false;
}
for (Json::ArrayIndex i = 0; i < a.size(); ++i) {
if (!match(match, a[i], b[i], name + "[\"" + std::to_string(i) + "\"]")) {
return false;
}
}
}
// handle value types using Json::Value::operator==
else if (a != b) {
*result_listener << "value mismatch in " << name << ", expected " << testing::PrintToString(a)
<< ", actual " << testing::PrintToString(b);
return false;
}
return true;
};
return match_rec(match_rec, expected_json, arg, "Json::Value");
}
#endif //MEMILIO_HAS_JSONCPP
/**
* @brief overload gtest printer function for eigen matrices.
* @note see https://stackoverflow.com/questions/25146997/teach-google-test-how-to-print-eigen-matrix
*/
template <class M>
struct MatrixPrintWrap : public M {
friend void PrintTo(const MatrixPrintWrap& m, std::ostream* os)
{
if (m.rows() == 1) {
//print row vector inline
(*os) << m;
}
else if (m.cols() == 1) {
//print col vector inline transposed
(*os) << m.transpose() << " T";
}
else {
//print matrix on its own
(*os) << '\n' << m;
}
}
};
/**
* @brief wrap m for gtest printing
* returns a reference to the original object, no copying or moving, mind the lifetime!
*/
template <class M>
const MatrixPrintWrap<M>& print_wrap(const Eigen::EigenBase<M>& m)
{
return static_cast<const MatrixPrintWrap<M>&>(m);
}
/**
* gmock matcher, checks if each element of two eigen matrices are within tolerance.
* @param other matrix to compare
* @param rtol relative tolerance
* @param atol absolute tolerance
* @return matcher that accepts eigen matrix types
*/
MATCHER_P3(MatrixNear, other, rtol, atol,
"approx. equal to " + testing::PrintToString(print_wrap(other)) +
" (rtol = " + testing::PrintToString(rtol) + ", atol = " + testing::PrintToString(atol) + ")")
{
if (arg.rows() != other.rows() || arg.cols() != other.cols()) {
*result_listener << "different dimensions";
return false;
}
return ((arg - other).array().abs() <= (atol + rtol * other.array().abs())).all();
}
/**
* gmock matcher, checks if each element of two eigen matrices are close.
* @param other matrix to compare
* @return matcher that accepts eigen matrix types
*/
MATCHER_P(MatrixNear, other,
"approx. equal to " + testing::PrintToString(print_wrap(other)) + " (rtol = 1e-15, atol = 1e-15)")
{
mio::unused(result_listener);
return ((arg - other).array().abs() <= (1e-15 + 1e-15 * other.array().abs())).all();
}
/**
* gmock matcher, checks if two floating point values are almost equal.
* @param other value to compare
* @param rtol relative tolerance
* @param atol absolute tolerance
* @return matcher that accepts floating point values.
*/
MATCHER_P3(FloatingPointEqual, other, atol, rtol,
"approx. equal to " + testing::PrintToString(other) + " (rtol = " + testing::PrintToString(rtol) +
", atol = " + testing::PrintToString(atol) + ")")
{
mio::unused(result_listener);
return mio::floating_point_equal(arg, other, atol, rtol);
}
/**
* @brief overload gtest printer function for IOResult.
* @note see https://stackoverflow.com/questions/25146997/teach-google-test-how-to-print-eigen-matrix
*/
template <class T>
struct IOResultPrintWrap : public mio::IOResult<T> {
friend void PrintTo(const IOResultPrintWrap& m, std::ostream* os)
{
if (m) {
*os << "Success";
}
else {
*os << "Error: " << m.error().formatted_message();
}
}
};
/**
* @brief wrap an IOResult for gtest printing
* returns a reference to the original object, no copying or moving, mind the lifetime!
*/
template <class T>
const IOResultPrintWrap<T>& print_wrap(const mio::IOResult<T>& r)
{
return static_cast<const IOResultPrintWrap<T>&>(r);
}
/**
* gmock matcher for IOResult.
* The matcher succeeds if the IOResult represents success.
* @return matcher that checks an IOResult
*/
MATCHER(IsSuccess, std::string(negation ? "isn't" : "is") + " successful. ")
{
if (arg) {
return true;
}
*result_listener << arg.error().formatted_message();
return false;
}
/**
* gmock matcher for IOResult.
* The matcher succeeds if the IOResult represents failure with the specified status code.
* @return matcher that checks an IOResult
*/
MATCHER_P(IsFailure, status_code, std::string(negation ? "isn't" : "is") + " failure. ")
{
if (arg.error().code() == status_code) {
return true;
}
*result_listener << arg.error().formatted_message();
return false;
}
/**
* gmock matcher that checks whether the elements of a container are linearly spaced.
* @param b minimum value
* @param e maximum value
* @param num_points number of linearly spaced points in [b, e]
* @return matcher that accepts a stl container
*/
template <class T>
auto ElementsAreLinspace(T b, T e, size_t num_points)
{
assert(num_points >= 2);
std::vector<decltype(FloatingPointEqual(std::declval<T>(), std::declval<T>(), std::declval<T>()))> values;
auto step_size = (e - b) / (num_points - 1);
for (size_t i = 0; i < num_points; i++) {
values.push_back(FloatingPointEqual(b + i * step_size, 1e-15 * step_size, 1e-15));
}
return testing::ElementsAreArray(values);
}
#endif // MIO_TESTS_MATCHERS_H