OpenWalker Project
Documentation of the ROS Packages
gtest_utilities.h
Go to the documentation of this file.
1 
34 #ifndef OPEN_WALKER_CORE_GTEST_UTILITIES_H
35 #define OPEN_WALKER_CORE_GTEST_UTILITIES_H
36 
37 #include <gtest/gtest.h>
38 #include <Eigen/Dense>
39 #include <geometry_msgs/Quaternion.h>
40 #include <geometry_msgs/Point.h>
41 #include <geometry_msgs/Vector3.h>
42 #include <tf/LinearMath/Matrix3x3.h>
43 #include <tf/LinearMath/Quaternion.h>
44 
45 namespace ow_test
46 {
47 
48 // testing precision
49 const double PRECISION = 1e-7;
50 
51 // test Eigen::Matrix m1 and m2 for acceptable error bounds
52 template <typename _DerivedA,typename _DerivedB>
53 ::testing::AssertionResult eigenMatrixNear(const Eigen::MatrixBase<_DerivedA>& m1, const std::string m1_name,
54  const Eigen::MatrixBase<_DerivedB>& m2, const std::string m2_name,
55  double prec = PRECISION)
56 {
57  if(m1.cols() != m2.cols())
58  {
59  return ::testing::AssertionFailure()
60  << "Differnt number of cols for " << m1_name << " and " << m2_name << ":\n"
61  << m1_name << ".cols()" << m1.cols() << ".\n"
62  << m2_name << ".cols()" << m2.cols() << ".\n";
63  }
64  if(m2.rows()!= m2.rows())
65  {
66  return ::testing::AssertionFailure()
67  << "Differnt number of rows for " << m1_name << " and " << m2_name << ":\n"
68  << m1_name << ".rows()" << m1.rows() << ".\n"
69  << m2_name << ".rows()" << m2.rows() << ".\n";
70  }
71  if(m1.isApprox(m2,prec))
72  return ::testing::AssertionSuccess();
73  else
74  {
75  ::testing::AssertionResult res(false);
76  res << "Detla between Eigen::Matrix " << m1_name << " and Eigen::Matrix " << m2_name << " detected:\n";
77 
78  for (int j = 0; j < m1.cols(); ++j)
79  {
80  for (int i = 0; i < m1.rows(); ++i)
81  {
82  double delta = std::abs(m1(i,j) - m2(i,j));
83  if(delta > prec) {
84  res << "Detla at entry (" << i << "," << j << "):" << "\n";
85  res << m1_name << "(" << i << "," << j << ")=" << m1(i,j) << ".\n";
86  res << m2_name << "(" << i << "," << j << ")=" << m2(i,j) << ".\n";
87  res << "delta=" << delta << ", prec=" << prec << ".\n";
88  }
89  }
90  }
91  return res;
92  }
93  return ::testing::AssertionFailure();
94 }
95 
96 // test Eigen::Quaternion Q_a and Q_b for acceptable error bounds
97 // note: compair RotationMatrix (unique)
98 template <typename _DerivedA, typename _DerivedB>
99 ::testing::AssertionResult eigenQuaternionNear(const Eigen::QuaternionBase<_DerivedA>& Q_a, const std::string q_a_name,
100  const Eigen::QuaternionBase<_DerivedB>& Q_b, const std::string q_b_name,
101  double prec = PRECISION)
102 {
103  typedef typename Eigen::QuaternionBase<_DerivedA>::Matrix3 Matrix3A;
104  typedef typename Eigen::QuaternionBase<_DerivedB>::Matrix3 Matrix3B;
105 
106  Matrix3A R_a = Q_a.toRotationMatrix();
107  Matrix3B R_b = Q_b.toRotationMatrix();
108 
109  ::testing::AssertionResult res = eigenMatrixNear(R_a, q_a_name, R_b, q_b_name);
110  if(!res)
111  {
112  ::testing::AssertionResult res_wrapped(false);
113  res_wrapped << "Detla between Eigen::Quaternion " << q_a_name << " and Eigen::Quaternion " << q_b_name << " detected:\n";
114  res_wrapped << res.message();
115  return res_wrapped;
116  }
117  return res;
118 }
119 
120 }
121 
122 #endif // OPEN_WALKER_CORE_GTEST_UTILITIES_H
Definition: gtest_utilities.h:45