forked from artivis/manif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheigen_gtest.h
296 lines (237 loc) · 9.12 KB
/
eigen_gtest.h
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#ifndef _MANIF_TEST_EIGEN_GTEST_H_
#define _MANIF_TEST_EIGEN_GTEST_H_
#include <gtest/gtest.h>
namespace manif {
namespace detail {
template <int... I> struct int_sequence
{
using type = int_sequence;
using value_type = int;
static constexpr unsigned int size() noexcept { return sizeof...(I); }
};
template <class, class, int> struct range_cat;
template <int... H, int... T, int Start>
struct range_cat<int_sequence<H...>, int_sequence<T...>, Start>
{
using type = int_sequence<H..., Start+T...>;
};
template <int Start, unsigned int N>
struct range_ : range_cat< typename range_<Start, N / 2>::type,
typename range_<Start, N - N / 2>::type,
N / 2 > { };
template <int Start> struct range_<Start, 1> { using type = int_sequence<Start>; };
template <int Start> struct range_<Start, 0> { using type = int_sequence<>; };
template <int End>
using make_int_sequence = typename range_<0, End + 1>::type;
template<typename F, class T, template <int...I> class S, int... I>
void call_for_each(F f, const T& t, const S<I...>&)
{
auto l = { (f(std::get<I>(/*std::forward<T>*/(t))), 0)... };
(void)(l);
}
template<typename F, template <typename...Ts> class C, typename... Ts>
void call_for_each(F&& f, const C<Ts...>& t)
{
call_for_each(std::forward<F>(f), t, make_int_sequence<sizeof...(Ts)-1>());
}
struct RowSizeGetter {
template <typename... Ts>
auto operator()(const Eigen::MatrixBase<Ts>&... ms)
-> decltype(std::make_tuple(ms.rows()...))
{ return std::make_tuple(ms.rows()...); }
static const char* dim() { constexpr static char dim_arr[] = "row"; return dim_arr; }
};
struct ColSizeGetter {
template <typename... Ts>
auto operator()(const Eigen::MatrixBase<Ts>&... ms)
-> decltype(std::make_tuple(ms.cols()...))
{ return std::make_tuple(ms.cols()...); }
static const char* dim() { constexpr static char dim_arr[] = "col"; return dim_arr; }
};
using EigenIndex = EIGEN_DEFAULT_DENSE_INDEX_TYPE;
} /* namespace detail */
/**
* @brief Gtest predicate function for matrice same dim.
* @param dim, the expected dim size
* @param ms, N Eigen::Matrix to be tested
* @note This function requires an extra template param helper DimGetter
* @see detail::RowSizeGetter
* @see detail::ColSizeGetter
* @see isEigenMatrixDimSize
* @see isEigenMatrixColSize
*/
template <typename DimGetter, typename... Ts>
inline ::testing::AssertionResult
isEigenMatrixDimSize(const detail::EigenIndex dim,
const Eigen::MatrixBase<Ts>&... ms)
{
static_assert(sizeof...(Ts)>=1, "No matrix passed !");
const auto sizes = DimGetter()(ms...);
bool result = true;
auto f = [&result, &dim](const detail::EigenIndex i){ result &= (dim == i);};
detail::call_for_each(f, sizes);
// cppcheck-suppress knownConditionTrueFalse
if (!result)
{
std::stringstream ss;
ss << dim;
auto p = [&ss, &dim](const detail::EigenIndex i)
{ ss << ((i==dim)?" == ":" != ") << i; };
detail::call_for_each(p, sizes);
return ::testing::AssertionFailure() << "Matrice have different "
<< DimGetter::dim()
<< " size ! " << ss.str();
}
return ::testing::AssertionSuccess();
}
/**
* @brief Gtest predicate function for testing expected matrice row dim.
* @note This is an helper function for isEigenMatrixDimSize
*/
template <typename... Ts>
inline ::testing::AssertionResult
isEigenMatrixRowSize(const detail::EigenIndex rows,
const Eigen::MatrixBase<Ts>&... ms)
{
return isEigenMatrixDimSize<detail::RowSizeGetter>(rows, ms...);
}
/**
* @brief Gtest predicate function for testing expected matrice col dim.
* @note This is an helper function for isEigenMatrixDimSize
*/
template <typename... Ts>
inline ::testing::AssertionResult
isEigenMatrixColSize(const detail::EigenIndex cols,
const Eigen::MatrixBase<Ts>&... ms)
{
return isEigenMatrixDimSize<detail::ColSizeGetter>(cols, ms...);
}
/**
* @brief Gtest predicate function for testing N matrice have the same row size.
*/
template <typename Derived, typename... Ts>
inline ::testing::AssertionResult
isEigenMatrixSameRowSize(const Eigen::MatrixBase<Derived>& m0,
const Eigen::MatrixBase<Ts>&... ms)
{
static_assert(sizeof...(Ts)>=1, "Only one matrix passed !\n"
"Please consider using isEigenMatrixRowSize instead.");
return isEigenMatrixRowSize(m0.rows(), ms...);
}
/**
* @brief Gtest predicate function for testing
* N matrice have the same col size.
*/
template <typename Derived, typename... Ts>
inline ::testing::AssertionResult
isEigenMatrixSameColSize(const Eigen::MatrixBase<Derived>& m0,
const Eigen::MatrixBase<Ts>&... ms)
{
static_assert(sizeof...(Ts)>=1, "Only one matrix passed !\n"
"Please consider using isEigenMatrixColSize instead.");
return isEigenMatrixColSize(m0.cols(), ms...);
}
/**
* @brief Gtest predicate function for testing
* N matrice have the same size.
*/
template <typename Derived, typename... Ts>
inline ::testing::AssertionResult
isEigenMatrixSameSize(const Eigen::MatrixBase<Derived>& m0,
const Eigen::MatrixBase<Ts>&... ms)
{
const ::testing::AssertionResult row_check =
isEigenMatrixSameRowSize(m0, ms...);
if (!row_check)
{
return row_check;
}
const ::testing::AssertionResult col_check =
isEigenMatrixSameColSize(m0, ms...);
if (!col_check)
{
return col_check;
}
return ::testing::AssertionSuccess();
}
/**
* @brief isZero() is not very suitable for comparing vectors which have norms
* significantly larger than 0, isApprox(), on the other hand, does not work
* with small norms.
* https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#ae8443357b808cd393be1b51974213f9c
*/
template <class _DerivedA, class _DerivedB>
inline ::testing::AssertionResult isEigenMatrixNear(const Eigen::MatrixBase<_DerivedA>& matrix_a,
const Eigen::MatrixBase<_DerivedB>& matrix_b,
const std::string& matrix_a_name = "matrix_a",
const std::string& matrix_b_name = "matrix_b",
double tolerance = 1e-8)
{
const ::testing::AssertionResult size_check =
isEigenMatrixSameSize(matrix_a, matrix_b);
if (!size_check)
{
return size_check;
}
bool result = false;
if (std::min(matrix_a.norm(), matrix_b.norm()) < tolerance)
{
result = (matrix_a - matrix_b).isZero(tolerance);
}
else
{
result = (matrix_a.isApprox(matrix_b, tolerance));
}
return (result ? ::testing::AssertionSuccess()
: ::testing::AssertionFailure()
<< matrix_a_name << " != " << matrix_b_name << "\n"
<< matrix_a_name << ":\n" << matrix_a << "\n"
<< matrix_b_name << ":\n" << matrix_b << "\n"
<< "diff:\n" << (matrix_a - matrix_b) << "\n");
}
} /* namespace manif */
#define __GET_4TH_ARG(arg1,arg2,arg3,arg4, ...) arg4
#define EXPECT_EIGEN_NEAR_DEFAULT_TOL(A,B) \
EXPECT_TRUE(manif::isEigenMatrixNear(A, B, #A, #B))
#define EXPECT_EIGEN_NEAR_TOL(A,B,tol) \
EXPECT_TRUE(manif::isEigenMatrixNear(A, B, #A, #B, tol))
#define __EXPECT_EIGEN_NEAR_CHOOSER(...) \
__GET_4TH_ARG(__VA_ARGS__, EXPECT_EIGEN_NEAR_TOL, \
EXPECT_EIGEN_NEAR_DEFAULT_TOL, )
#define EXPECT_EIGEN_NEAR(...) \
__EXPECT_EIGEN_NEAR_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
#define EXPECT_EIGEN_NOT_NEAR_DEFAULT_TOL(A,B) \
EXPECT_FALSE(manif::isEigenMatrixNear(A, B, #A, #B))
#define EXPECT_EIGEN_NOT_NEAR_TOL(A,B,tol) \
EXPECT_FALSE(manif::isEigenMatrixNear(A, B, #A, #B, tol))
#define __EXPECT_EIGEN_NOT_NEAR_CHOOSER(...) \
__GET_4TH_ARG(__VA_ARGS__, EXPECT_EIGEN_NOT_NEAR_TOL, \
EXPECT_EIGEN_NOT_NEAR_DEFAULT_TOL, )
#define EXPECT_EIGEN_NOT_NEAR(...) \
__EXPECT_EIGEN_NOT_NEAR_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
#define ASSERT_EIGEN_NEAR_DEFAULT_TOL(A,B) \
ASSERT_TRUE(manif::isEigenMatrixNear(A, B, #A, #B))
#define ASSERT_EIGEN_NEAR_TOL(A,B,tol) \
ASSERT_TRUE(manif::isEigenMatrixNear(A, B, #A, #B, tol))
#define __ASSERT_EIGEN_NEAR_CHOOSER(...) \
__GET_4TH_ARG(__VA_ARGS__, ASSERT_EIGEN_NEAR_TOL, \
ASSERT_EIGEN_NEAR_DEFAULT_TOL, )
#define ASSERT_EIGEN_NEAR(...) \
__ASSERT_EIGEN_NEAR_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
#define ASSERT_EIGEN_NOT_NEAR_DEFAULT_TOL(A,B) \
ASSERT_FALSE(manif::isEigenMatrixNear(A, B, #A, #B))
#define ASSERT_EIGEN_NOT_NEAR_TOL(A,B,tol) \
ASSERT_FALSE(manif::isEigenMatrixNear(A, B, #A, #B, tol))
#define __ASSERT_EIGEN_NOT_NEAR_CHOOSER(...) \
__GET_4TH_ARG(__VA_ARGS__, ASSERT_EIGEN_NOT_NEAR_TOL, \
ASSERT_EIGEN_NOT_NEAR_DEFAULT_TOL, )
#define ASSERT_EIGEN_NOT_NEAR(...) \
__ASSERT_EIGEN_NOT_NEAR_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
/*
* E.g
EXPECT_TRUE(isEigenMatrixSameSize(Eigen::Vector2d::Zero(),
Eigen::Vector2d::Zero(),
Eigen::Vector3d::Zero(),
Eigen::Vector4d::Zero()));
*/
#endif /* _MANIF_TEST_EIGEN_GTEST_H_ */