Compare Eigen matrices in Google Test or Google Mock

后端 未结 3 2029
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 13:03

I was wondering if there is a good way to test two Eigen matrices for approximate equality using Google Test, or Google Mock.

Take the following test-case as a

3条回答
  •  失恋的感觉
    2021-02-14 13:23

    EXPECT_PRED2 from GoogleTest can be used for this.

    Under C++11 using a lambda works fine but looks unseemly:

      ASSERT_PRED2([](const MatrixXf &lhs, const MatrixXf &rhs) {
                      return lhs.isApprox(rhs, 1e-4);
                   },
                   C_expect, C_actual);
    

    If that fails, you get a print-out of the input arguments.

    Instead of using a lambda, a normal predicate function can be defined like this:

    bool MatrixEquality(const MatrixXf &lhs, const MatrixXf &rhs) {
      return lhs.isApprox(rhs, 1e-4);
    }
    
    TEST(Eigen, MatrixMultiplication) {
      ...
    
      ASSERT_PRED2(MatrixEquality, C_expected, C_actual);
    }
    

    The later version also works on pre-C++11.

提交回复
热议问题