18
19
20
30#include <gtest/gtest.h>
37
38
39
42
43
44
45
46
47
48
49
50
51
52template <
typename X,
typename Y>
53::testing::AssertionResult
complex_are_close(X
const &x, Y
const &y,
double precision = 1.e-10) {
55 if (abs(x - y) < precision)
56 return ::testing::AssertionSuccess();
58 return ::testing::AssertionFailure() <<
"abs(x - y) = " << abs(x - y) <<
"\n x = " << x <<
"\n y = " << y;
62#define EXPECT_COMPLEX_NEAR(X, ...) EXPECT_TRUE
(complex_are_close(X, __VA_ARGS__))
65
66
67
68
69
70
71
72
73
74template <
typename X,
typename Y>
76 if (x.shape() != y.shape())
77 return ::testing::AssertionFailure() <<
"Comparing two arrays of different size "
78 <<
"\n X = " << x <<
"\n Y = " << y;
80 return ::testing::AssertionSuccess();
82 return ::testing::AssertionFailure() <<
"Arrays have different elements\n X = " << x <<
"\n Y = " << y;
86#define EXPECT_EQ_ARRAY(X, Y) EXPECT_TRUE
(array_are_equal(X, Y));
89#define EXPECT_ARRAY_EQ(X, Y) EXPECT_TRUE
(array_are_equal(X, Y));
92
93
94
95
96
97
98
99
100
101
102
103template <
typename X,
typename Y>
104::testing::AssertionResult
array_are_close(X
const &x, Y
const &y,
double precision = 1.e-10) {
105 nda::array<nda::get_value_t<X>, nda::get_rank<X>> x_reg = x;
106 nda::array<nda::get_value_t<X>, nda::get_rank<X>> y_reg = y;
109 if (x_reg.shape() != y_reg.shape())
110 return ::testing::AssertionFailure() <<
"Comparing two arrays of different size "
111 <<
"\n X = " << x_reg <<
"\n Y = " << y_reg;
114 if (x_reg.size() == 0)
return ::testing::AssertionSuccess();
117 const auto maxdiff = max_element(abs(make_regular(x_reg - y_reg)));
118 if (maxdiff < precision)
119 return ::testing::AssertionSuccess();
121 return ::testing::AssertionFailure() <<
"max_element(abs(X - Y)) = " << maxdiff <<
"\n X = " << x_reg <<
"\n Y = " << y_reg;
125#define EXPECT_ARRAY_NEAR(X, ...) EXPECT_TRUE
(array_are_close(X, __VA_ARGS__))
128
129
130
131
132
133
134
137 nda::array<nda::get_value_t<X>, nda::get_rank<X>> x_reg = x;
139 constexpr double eps = 1.e-10;
140 const auto max = max_element(abs(x_reg));
141 if (x_reg.size() == 0 || max < eps)
142 return ::testing::AssertionSuccess();
144 return ::testing::AssertionFailure() <<
"max_element(abs(X)) = " << max <<
"\n X = " << x_reg;
148#define EXPECT_ARRAY_ZERO(X) EXPECT_TRUE
(array_almost_zero(X))
151
152
153
154
155
156
157
158
159
160template <
typename X,
typename Y>
162 double precision = 1.e-12;
164 if (abs(x - y) > precision)
165 return ::testing::AssertionFailure() <<
"X = " << x <<
" and Y = " << y <<
" are different. \n Difference is: " << abs(x - y);
166 return ::testing::AssertionSuccess();
170#define EXPECT_CLOSE(X, Y) EXPECT_TRUE
(generic_are_near(X, Y));
::testing::AssertionResult array_almost_zero(X const &x)
Check that an array/view is close to zero, i.e. that its largest absolute element is less than 1e-10.
::testing::AssertionResult generic_are_near(X const &x, Y const &y)
Check that that two generic objects are close, i.e. that their absolute difference is less than 1e-12...
::testing::AssertionResult array_are_close(X const &x, Y const &y, double precision=1.e-10)
Check that two arrays/views are close, i.e. that they have the same shape and that the largest elemen...
::testing::AssertionResult array_are_equal(X const &x, Y const &y)
Check that two arrays/views are equal, i.e. that they have the same shape and the same elements.
::testing::AssertionResult complex_are_close(X const &x, Y const &y, double precision=1.e-10)
Check the absolute difference of two (complex) numbers.