Skip to content

Commit

Permalink
Merge pull request ethz-asl#34 from ethz-asl/equal
Browse files Browse the repository at this point in the history
Add actual equality
  • Loading branch information
simonlynen committed Mar 30, 2015
2 parents e6aa645 + a1b6f49 commit a251998
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/eigen-checks/gtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
#include <eigen-checks/internal/gtest-equal.h>

#define EIGEN_MATRIX_EQUAL(MatrixA, MatrixB) \
eigen_checks::internal::MatricesNear(MatrixA, #MatrixA, MatrixB, #MatrixB, \
eigen_checks::internal::MatricesEqual(MatrixA, #MatrixA, MatrixB, #MatrixB, \
static_cast<typename std::remove_reference< \
decltype(MatrixA)>::type::Scalar>(0.0), "0.0")

#define EIGEN_MATRIX_EQUAL_DOUBLE(MatrixA, MatrixB) \
eigen_checks::internal::MatricesNear(MatrixA, #MatrixA, MatrixB, #MatrixB, \
eigen_checks::internal::MatricesEqual(MatrixA, #MatrixA, MatrixB, #MatrixB, \
static_cast<typename std::remove_reference< \
decltype(MatrixA)>::type::Scalar>( \
eigen_checks::internal::kDefaultPrecision), "Floating point precision")
Expand Down
35 changes: 35 additions & 0 deletions include/eigen-checks/internal/gtest-equal.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,41 @@ namespace eigen_checks {
namespace internal {
constexpr double kDefaultPrecision = 1e-10;

template<typename LeftMat, typename RightMat>
::testing::AssertionResult MatricesEqual(const LeftMat& A,
const std::string& name_lhs,
const RightMat& B,
const std::string& name_rhs,
double threshold,
const std::string& name_threshold) {
if (A.rows() != B.rows() || A.cols() != B.cols()) {
return ::testing::AssertionFailure()
<< "Matrix size mismatch: "
<< A.rows() << "x" << A.cols() << " (" << name_lhs << ") != "
<< B.rows() << "x" << B.cols() << " (" << name_rhs << ")";
}

bool success = true;
std::string message;
for (int i = 0; i < A.rows(); ++i) {
for (int j = 0; j < A.cols(); ++j) {
double Aij = A(i, j);
double Bij = B(i, j);
if (std::abs(Aij - Bij) > threshold) {
success = false;
message +=
"\n Mismatch at [" + std::to_string(i) + "," + std::to_string(j) +
"] : " + std::to_string(Aij) + " != " + std::to_string(Bij) +
" (" + name_threshold + " = " + std::to_string(threshold) + ")";
}
}
}

return success ?
::testing::AssertionSuccess() :
::testing::AssertionFailure() << message << std::endl;
}

template<typename LHSMatrix, typename RHSMatrix>
::testing::AssertionResult MatricesNear(
const Eigen::MatrixBase<LHSMatrix>& lhs,
Expand Down

0 comments on commit a251998

Please sign in to comment.