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))) << message <<
"\nNaN check failed at (" << r <<
"," << c <<
")\n" 44 <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B;
46 ASSERT_EQ(A(r,c),B(r,c)) << message <<
"\nEquality comparison failed at (" << r <<
"," << c <<
")\n" 47 <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B;
53 template<
typename M1,
typename M2,
typename T>
54 void assertNear(
const M1 & A,
const M2 & B, T tolerance, std::string
const & message =
"")
59 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";
60 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";
62 for(
int r = 0; r < A.rows(); r++)
64 for(
int c = 0; c < A.cols(); c++)
66 if (std::isnan(A(r,c))) {
67 ASSERT_TRUE(std::isnan(B(r,c))) << message <<
"\nNaN check failed at (" << r <<
"," << c <<
")\n" 68 <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B;
70 ASSERT_NEAR(A(r,c),B(r,c),tolerance) << message <<
"\nTolerance comparison failed at (" << r <<
"," << c <<
")\n" 71 <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B;
77 template<
typename M1,
typename M2,
typename T>
78 void expectNear(
const M1 & A,
const M2 & B, T tolerance, std::string
const & message =
"")
80 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";
81 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";
83 for(
int r = 0; r < A.rows(); r++)
85 for(
int c = 0; c < A.cols(); c++)
87 if (std::isnan(A(r,c))) {
88 EXPECT_TRUE(std::isnan(B(r,c))) << message <<
"\nNaN check failed at (" << r <<
"," << c <<
")\n" 89 <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B;
91 EXPECT_NEAR(A(r,c),B(r,c),tolerance) << message <<
"\nTolerance comparison failed at (" << r <<
"," << c <<
")\n" 92 <<
"\nMatrix A:\n" << A <<
"\nand matrix B\n" << B;
101 for(
int r = 0; r < A.rows(); r++)
103 for(
int c = 0; c < A.cols(); c++)
105 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;
110 inline bool compareRelative(
double a,
double b,
double percentTolerance,
double * percentError = NULL)
115 if( (fa < 1e-15 && fb < 1e-15) ||
116 (fa == 0.0 && fb < 1e-6) ||
117 (fb == 0.0 && fa < 1e-6) )
120 double diff = fabs(a - b)/std::max(fa,fb);
121 if(diff > percentTolerance * 1e-2)
124 *percentError = diff * 100.0;
130 #define ASSERT_DOUBLE_MX_EQ(A, B, PERCENT_TOLERANCE, MSG) \ 131 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"; \ 132 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"; \ 133 for(int r = 0; r < (A).rows(); r++) \ 135 for(int c = 0; c < (A).cols(); c++) \ 137 double percentError = 0.0; \ 138 ASSERT_TRUE(grid_map::compareRelative( (A)(r,c), (B)(r,c), PERCENT_TOLERANCE, &percentError)) \ 139 << MSG << "\nComparing:\n" \ 140 << #A << "(" << r << "," << c << ") = " << (A)(r,c) << std::endl \ 141 << #B << "(" << r << "," << c << ") = " << (B)(r,c) << std::endl \ 142 << "Error was " << percentError << "% > " << PERCENT_TOLERANCE << "%\n" \ 143 << "\nMatrix " << #A << ":\n" << A << "\nand matrix " << #B << "\n" << B; \ 147 #define ASSERT_DOUBLE_SPARSE_MX_EQ(A, B, PERCENT_TOLERANCE, MSG) \ 148 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"; \ 149 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"; \ 150 for(int r = 0; r < (A).rows(); r++) \ 152 for(int c = 0; c < (A).cols(); c++) \ 154 double percentError = 0.0; \ 155 ASSERT_TRUE(grid_map::compareRelative( (A).coeff(r,c), (B).coeff(r,c), PERCENT_TOLERANCE, &percentError)) \ 156 << MSG << "\nComparing:\n" \ 157 << #A << "(" << r << "," << c << ") = " << (A).coeff(r,c) << std::endl \ 158 << #B << "(" << r << "," << c << ") = " << (B).coeff(r,c) << std::endl \ 159 << "Error was " << percentError << "% > " << PERCENT_TOLERANCE << "%\n" \ 160 << "\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)