10 #include <gtest/gtest.h> 20 Eigen::Matrix<double,N,N> U;
22 return U.transpose() * U + 5.0 * Eigen::Matrix<double,N,N>::Identity();
27 Eigen::MatrixXd U(N,N);
29 return U.transpose() * U + 5.0 * Eigen::MatrixXd::Identity(N,N);
32 template<
typename M1,
typename M2>
33 void assertEqual(
const M1 & A,
const M2 & B, std::string
const & message =
"")
35 ASSERT_EQ((
size_t)A.rows(),(size_t)B.rows()) << message <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B <<
"\nare not the same\n";
36 ASSERT_EQ((
size_t)A.cols(),(size_t)B.cols()) << message <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B <<
"\nare not the same\n";
38 for(
int r = 0; r < A.rows(); r++)
40 for(
int c = 0; c < A.cols(); c++)
42 if (std::isnan(A(r,c))) {
43 ASSERT_TRUE(std::isnan(B(r,c)));
45 ASSERT_EQ(A(r,c),B(r,c)) << message <<
"\nEquality comparison failed at (" << r <<
"," << c <<
")\n" 46 <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B;
52 template<
typename M1,
typename M2,
typename T>
53 void assertNear(
const M1 & A,
const M2 & B, T tolerance, std::string
const & message =
"")
58 ASSERT_EQ((
size_t)A.rows(),(size_t)B.rows()) << message <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B <<
"\nare not the same\n";
59 ASSERT_EQ((
size_t)A.cols(),(size_t)B.cols()) << message <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B <<
"\nare not the same\n";
61 for(
int r = 0; r < A.rows(); r++)
63 for(
int c = 0; c < A.cols(); c++)
65 if (std::isnan(A(r,c))) {
66 ASSERT_TRUE(std::isnan(B(r,c)));
68 ASSERT_NEAR(A(r,c),B(r,c),tolerance) << message <<
"\nTolerance comparison failed at (" << r <<
"," << c <<
")\n" 69 <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B;
75 template<
typename M1,
typename M2,
typename T>
76 void expectNear(
const M1 & A,
const M2 & B, T tolerance, std::string
const & message =
"")
78 EXPECT_EQ((
size_t)A.rows(),(size_t)B.rows()) << message <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B <<
"\nare not the same\n";
79 EXPECT_EQ((
size_t)A.cols(),(size_t)B.cols()) << message <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B <<
"\nare not the same\n";
81 for(
int r = 0; r < A.rows(); r++)
83 for(
int c = 0; c < A.cols(); c++)
85 if (std::isnan(A(r,c))) {
86 EXPECT_TRUE(std::isnan(B(r,c)));
88 EXPECT_NEAR(A(r,c),B(r,c),tolerance) << message <<
"\nTolerance comparison failed at (" << r <<
"," << c <<
")\n" 89 <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B;
98 for(
int r = 0; r < A.rows(); r++)
100 for(
int c = 0; c < A.cols(); c++)
102 ASSERT_TRUE(std::isfinite(A(r,c))) << std::endl <<
"Check for finite values failed at A(" << r <<
"," << c <<
"). Matrix A:" << std::endl << A << std::endl;
107 inline bool compareRelative(
double a,
double b,
double percentTolerance,
double * percentError = NULL)
112 if( (fa < 1e-15 && fb < 1e-15) ||
113 (fa == 0.0 && fb < 1e-6) ||
114 (fb == 0.0 && fa < 1e-6) )
117 double diff = fabs(a - b)/std::max(fa,fb);
118 if(diff > percentTolerance * 1e-2)
121 *percentError = diff * 100.0;
127 #define ASSERT_DOUBLE_MX_EQ(A, B, PERCENT_TOLERANCE, MSG) \ 128 ASSERT_EQ((size_t)(A).rows(), (size_t)(B).rows()) << MSG << "\nMatrix " << #A << ":\n" << A << "\nand matrix " << #B << "\n" << B << "\nare not the same size"; \ 129 ASSERT_EQ((size_t)(A).cols(), (size_t)(B).cols()) << MSG << "\nMatrix " << #A << ":\n" << A << "\nand matrix " << #B << "\n" << B << "\nare not the same size"; \ 130 for(int r = 0; r < (A).rows(); r++) \ 132 for(int c = 0; c < (A).cols(); c++) \ 134 double percentError = 0.0; \ 135 ASSERT_TRUE(grid_map::compareRelative( (A)(r,c), (B)(r,c), PERCENT_TOLERANCE, &percentError)) \ 136 << MSG << "\nComparing:\n" \ 137 << #A << "(" << r << "," << c << ") = " << (A)(r,c) << std::endl \ 138 << #B << "(" << r << "," << c << ") = " << (B)(r,c) << std::endl \ 139 << "Error was " << percentError << "% > " << PERCENT_TOLERANCE << "%\n" \ 140 << "\nMatrix " << #A << ":\n" << A << "\nand matrix " << #B << "\n" << B; \ 144 #define ASSERT_DOUBLE_SPARSE_MX_EQ(A, B, PERCENT_TOLERANCE, MSG) \ 145 ASSERT_EQ((size_t)(A).rows(), (size_t)(B).rows()) << MSG << "\nMatrix " << #A << ":\n" << A << "\nand matrix " << #B << "\n" << B << "\nare not the same size"; \ 146 ASSERT_EQ((size_t)(A).cols(), (size_t)(B).cols()) << MSG << "\nMatrix " << #A << ":\n" << A << "\nand matrix " << #B << "\n" << B << "\nare not the same size"; \ 147 for(int r = 0; r < (A).rows(); r++) \ 149 for(int c = 0; c < (A).cols(); c++) \ 151 double percentError = 0.0; \ 152 ASSERT_TRUE(grid_map::compareRelative( (A).coeff(r,c), (B).coeff(r,c), PERCENT_TOLERANCE, &percentError)) \ 153 << MSG << "\nComparing:\n" \ 154 << #A << "(" << r << "," << c << ") = " << (A).coeff(r,c) << std::endl \ 155 << #B << "(" << r << "," << c << ") = " << (B).coeff(r,c) << std::endl \ 156 << "Error was " << percentError << "% > " << PERCENT_TOLERANCE << "%\n" \ 157 << "\nMatrix " << #A << ":\n" << A << "\nand matrix " << #B << "\n" << B; \ Eigen::MatrixXd randomCovarianceXd(int N)
void assertNear(const M1 &A, const M2 &B, T tolerance, std::string const &message="")
void assertFinite(const M1 &A, std::string const &message="")
void assertEqual(const M1 &A, const M2 &B, std::string const &message="")
void expectNear(const M1 &A, const M2 &B, T tolerance, std::string const &message="")
Eigen::Matrix< double, N, N > randomCovariance()
bool compareRelative(double a, double b, double percentTolerance, double *percentError=NULL)