Use gMock matchers in the problem and manifold tests Element-wise loops and norm comparisons in the problem, manifold, evaluator, covariance, minimizer and tiny solver tests report little beyond the failing line, and evaluation results are often unchecked. Compare matrices, vectors and containers using matchers that print the compared values and locate mismatches, and assert that evaluations succeed. Change-Id: I5d569c8dd66eab2d30425c81637ab96450613525
diff --git a/internal/ceres/covariance_test.cc b/internal/ceres/covariance_test.cc index 9af321a..a0a0db0 100644 --- a/internal/ceres/covariance_test.cc +++ b/internal/ceres/covariance_test.cc
@@ -39,6 +39,7 @@ #include <vector> #include "absl/log/log.h" +#include "absl/types/span.h" #include "ceres/autodiff_cost_function.h" #include "ceres/compressed_row_sparse_matrix.h" #include "ceres/cost_function.h" @@ -48,12 +49,16 @@ #include "ceres/manifold.h" #include "ceres/map_util.h" #include "ceres/problem_impl.h" +#include "ceres/test_util.h" #include "ceres/types.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres { namespace internal { +using ::testing::ElementsAreArray; + class UnaryCostFunction : public CostFunction { public: UnaryCostFunction(const int num_residuals, @@ -193,7 +198,7 @@ Covariance::Options options; CovarianceImpl covariance_impl(options); - EXPECT_TRUE( + ASSERT_TRUE( covariance_impl.ComputeCovarianceSparsity(covariance_blocks, &problem)); const CompressedRowSparseMatrix* crsm = covariance_impl.covariance_matrix(); @@ -202,17 +207,10 @@ EXPECT_EQ(crsm->num_cols(), 10); EXPECT_EQ(crsm->num_nonzeros(), 40); - const int* rows = crsm->rows(); - for (int r = 0; r < crsm->num_rows() + 1; ++r) { - EXPECT_EQ(rows[r], expected_rows[r]) - << r << " " << rows[r] << " " << expected_rows[r]; - } - - const int* cols = crsm->cols(); - for (int c = 0; c < crsm->num_nonzeros(); ++c) { - EXPECT_EQ(cols[c], expected_cols[c]) - << c << " " << cols[c] << " " << expected_cols[c]; - } + EXPECT_THAT(absl::MakeConstSpan(crsm->rows(), crsm->num_rows() + 1), + ElementsAreArray(expected_rows)); + EXPECT_THAT(absl::MakeConstSpan(crsm->cols(), crsm->num_nonzeros()), + ElementsAreArray(expected_cols)); } TEST(CovarianceImpl, ComputeCovarianceSparsityWithConstantParameterBlock) { @@ -274,7 +272,7 @@ Covariance::Options options; CovarianceImpl covariance_impl(options); - EXPECT_TRUE( + ASSERT_TRUE( covariance_impl.ComputeCovarianceSparsity(covariance_blocks, &problem)); const CompressedRowSparseMatrix* crsm = covariance_impl.covariance_matrix(); @@ -283,17 +281,10 @@ EXPECT_EQ(crsm->num_cols(), 7); EXPECT_EQ(crsm->num_nonzeros(), 25); - const int* rows = crsm->rows(); - for (int r = 0; r < crsm->num_rows() + 1; ++r) { - EXPECT_EQ(rows[r], expected_rows[r]) - << r << " " << rows[r] << " " << expected_rows[r]; - } - - const int* cols = crsm->cols(); - for (int c = 0; c < crsm->num_nonzeros(); ++c) { - EXPECT_EQ(cols[c], expected_cols[c]) - << c << " " << cols[c] << " " << expected_cols[c]; - } + EXPECT_THAT(absl::MakeConstSpan(crsm->rows(), crsm->num_rows() + 1), + ElementsAreArray(expected_rows)); + EXPECT_THAT(absl::MakeConstSpan(crsm->cols(), crsm->num_nonzeros()), + ElementsAreArray(expected_cols)); } TEST(CovarianceImpl, ComputeCovarianceSparsityWithFreeParameterBlock) { @@ -353,7 +344,7 @@ Covariance::Options options; CovarianceImpl covariance_impl(options); - EXPECT_TRUE( + ASSERT_TRUE( covariance_impl.ComputeCovarianceSparsity(covariance_blocks, &problem)); const CompressedRowSparseMatrix* crsm = covariance_impl.covariance_matrix(); @@ -362,17 +353,10 @@ EXPECT_EQ(crsm->num_cols(), 7); EXPECT_EQ(crsm->num_nonzeros(), 25); - const int* rows = crsm->rows(); - for (int r = 0; r < crsm->num_rows() + 1; ++r) { - EXPECT_EQ(rows[r], expected_rows[r]) - << r << " " << rows[r] << " " << expected_rows[r]; - } - - const int* cols = crsm->cols(); - for (int c = 0; c < crsm->num_nonzeros(); ++c) { - EXPECT_EQ(cols[c], expected_cols[c]) - << c << " " << cols[c] << " " << expected_cols[c]; - } + EXPECT_THAT(absl::MakeConstSpan(crsm->rows(), crsm->num_rows() + 1), + ElementsAreArray(expected_rows)); + EXPECT_THAT(absl::MakeConstSpan(crsm->cols(), crsm->num_nonzeros()), + ElementsAreArray(expected_cols)); } // x_plus_delta = delta * x; @@ -519,7 +503,7 @@ } Covariance covariance(options); - EXPECT_TRUE(covariance.Compute(covariance_blocks, &problem_)); + ASSERT_TRUE(covariance.Compute(covariance_blocks, &problem_)); for (auto& covariance_block : covariance_blocks) { const double* block1 = covariance_block.first; @@ -555,9 +539,9 @@ Matrix actual(row_end - row_begin, col_end - col_begin); if (lift_covariance_to_ambient_space) { - EXPECT_TRUE(covariance.GetCovarianceBlock(block1, block2, actual.data())); + ASSERT_TRUE(covariance.GetCovarianceBlock(block1, block2, actual.data())); } else { - EXPECT_TRUE(covariance.GetCovarianceBlockInTangentSpace( + ASSERT_TRUE(covariance.GetCovarianceBlockInTangentSpace( block1, block2, actual.data())); } @@ -566,22 +550,18 @@ dof = std::max(dof, bound.second.second); } ConstMatrixRef expected(expected_covariance, dof, dof); - double diff_norm = - (expected.block( - row_begin, col_begin, row_end - row_begin, col_end - col_begin) - - actual) - .norm(); - diff_norm /= (row_end - row_begin) * (col_end - col_begin); + // The tolerance applies to the norm of the difference averaged over the + // entries of the block. const double kTolerance = 1e-5; - EXPECT_NEAR(diff_norm, 0.0, kTolerance) + EXPECT_THAT( + actual, + NormNear( + expected.block( + row_begin, col_begin, row_end - row_begin, col_end - col_begin), + kTolerance * actual.size())) << "rows: " << row_begin << " " << row_end << " " - << "cols: " << col_begin << " " << col_end << " " - << "\n\n expected: \n " - << expected.block( - row_begin, col_begin, row_end - row_begin, col_end - col_begin) - << "\n\n actual: \n " << actual << "\n\n full expected: \n" - << expected; + << "cols: " << col_begin << " " << col_end; } double parameters_[6]; @@ -1244,22 +1224,22 @@ covariance_blocks.emplace_back(&x, &y); covariance_blocks.emplace_back(&y, &x); covariance_blocks.emplace_back(&y, &y); - EXPECT_TRUE(covariance.Compute(covariance_blocks, &problem)); + ASSERT_TRUE(covariance.Compute(covariance_blocks, &problem)); double value = -1; - covariance.GetCovarianceBlock(&x, &x, &value); + EXPECT_TRUE(covariance.GetCovarianceBlock(&x, &x, &value)); EXPECT_NEAR(value, 1.0, std::numeric_limits<double>::epsilon()); value = -1; - covariance.GetCovarianceBlock(&x, &y, &value); + EXPECT_TRUE(covariance.GetCovarianceBlock(&x, &y, &value)); EXPECT_NEAR(value, 0.0, std::numeric_limits<double>::epsilon()); value = -1; - covariance.GetCovarianceBlock(&y, &x, &value); + EXPECT_TRUE(covariance.GetCovarianceBlock(&y, &x, &value)); EXPECT_NEAR(value, 0.0, std::numeric_limits<double>::epsilon()); value = -1; - covariance.GetCovarianceBlock(&y, &y, &value); + EXPECT_TRUE(covariance.GetCovarianceBlock(&y, &y, &value)); EXPECT_NEAR(value, 0.0, std::numeric_limits<double>::epsilon()); } @@ -1279,20 +1259,20 @@ covariance_blocks.emplace_back(&x, &y); covariance_blocks.emplace_back(&y, &x); covariance_blocks.emplace_back(&y, &y); - EXPECT_TRUE(covariance.Compute(covariance_blocks, &problem)); + ASSERT_TRUE(covariance.Compute(covariance_blocks, &problem)); double value = -1; - covariance.GetCovarianceBlockInTangentSpace(&x, &x, &value); + EXPECT_TRUE(covariance.GetCovarianceBlockInTangentSpace(&x, &x, &value)); EXPECT_NEAR(value, 1.0, std::numeric_limits<double>::epsilon()); value = -1; // The following three calls, should not touch this value, since the // tangent space is of size zero - covariance.GetCovarianceBlockInTangentSpace(&x, &y, &value); + EXPECT_TRUE(covariance.GetCovarianceBlockInTangentSpace(&x, &y, &value)); EXPECT_EQ(value, -1); - covariance.GetCovarianceBlockInTangentSpace(&y, &x, &value); + EXPECT_TRUE(covariance.GetCovarianceBlockInTangentSpace(&y, &x, &value)); EXPECT_EQ(value, -1); - covariance.GetCovarianceBlockInTangentSpace(&y, &y, &value); + EXPECT_TRUE(covariance.GetCovarianceBlockInTangentSpace(&y, &y, &value)); EXPECT_EQ(value, -1); } @@ -1332,7 +1312,7 @@ sparse_linear_algebra_library_type; options.num_threads = num_threads; Covariance covariance(options); - EXPECT_TRUE(covariance.Compute(all_covariance_blocks_, &problem_)); + ASSERT_TRUE(covariance.Compute(all_covariance_blocks_, &problem_)); Matrix expected(parameter_block_size_, parameter_block_size_); Matrix actual(parameter_block_size_, parameter_block_size_); @@ -1343,24 +1323,18 @@ expected /= (i + 1.0) * (i + 1.0); double* block_i = parameters_.get() + i * parameter_block_size_; - covariance.GetCovarianceBlock(block_i, block_i, actual.data()); - EXPECT_NEAR((expected - actual).norm(), 0.0, kTolerance) - << "block: " << i << ", " << i << "\n" - << "expected: \n" - << expected << "\n" - << "actual: \n" - << actual; + ASSERT_TRUE( + covariance.GetCovarianceBlock(block_i, block_i, actual.data())); + EXPECT_THAT(actual, NormNear(expected, kTolerance)) + << "block: " << i << ", " << i; expected.setZero(); for (int j = i + 1; j < num_parameter_blocks_; ++j) { double* block_j = parameters_.get() + j * parameter_block_size_; - covariance.GetCovarianceBlock(block_i, block_j, actual.data()); - EXPECT_NEAR((expected - actual).norm(), 0.0, kTolerance) - << "block: " << i << ", " << j << "\n" - << "expected: \n" - << expected << "\n" - << "actual: \n" - << actual; + ASSERT_TRUE( + covariance.GetCovarianceBlock(block_i, block_j, actual.data())); + EXPECT_THAT(actual, NormNear(expected, kTolerance)) + << "block: " << i << ", " << j; } } }
diff --git a/internal/ceres/evaluator_test.cc b/internal/ceres/evaluator_test.cc index 820d485..12e717d 100644 --- a/internal/ceres/evaluator_test.cc +++ b/internal/ceres/evaluator_test.cc
@@ -50,12 +50,16 @@ #include "ceres/program.h" #include "ceres/sized_cost_function.h" #include "ceres/sparse_matrix.h" +#include "ceres/test_util.h" #include "ceres/types.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres { namespace internal { +using ::testing::ElementsAre; + // TODO(keir): Consider pushing this into a common test utils file. template <int kFactor, int kNumResiduals, int... Ns> class ParameterIgnoringCostFunction @@ -640,7 +644,7 @@ { double cost = -1; ASSERT_TRUE(evaluator->Evaluate(state, &cost, nullptr, nullptr, nullptr)); - EXPECT_EQ(48.5, cost); + EXPECT_EQ(cost, 48.5); } // Cost and residuals, no jacobian. @@ -648,9 +652,8 @@ double cost = -1; double residuals[2] = {-2, -2}; ASSERT_TRUE(evaluator->Evaluate(state, &cost, residuals, nullptr, nullptr)); - EXPECT_EQ(48.5, cost); - EXPECT_EQ(4, residuals[0]); - EXPECT_EQ(9, residuals[1]); + EXPECT_EQ(cost, 48.5); + EXPECT_THAT(residuals, ElementsAre(4, 9)); } // Cost, residuals, and jacobian. @@ -660,19 +663,15 @@ SetSparseMatrixConstant(jacobian.get(), -1); ASSERT_TRUE( evaluator->Evaluate(state, &cost, residuals, nullptr, jacobian.get())); - EXPECT_EQ(48.5, cost); - EXPECT_EQ(4, residuals[0]); - EXPECT_EQ(9, residuals[1]); + EXPECT_EQ(cost, 48.5); + EXPECT_THAT(residuals, ElementsAre(4, 9)); Matrix actual_jacobian; jacobian->ToDenseMatrix(&actual_jacobian); Matrix expected_jacobian(2, 2); expected_jacobian << 2 * state[0], 0, 0, 2 * state[1]; - EXPECT_TRUE((actual_jacobian.array() == expected_jacobian.array()).all()) - << "Actual:\n" - << actual_jacobian << "\nExpected:\n" - << expected_jacobian; + EXPECT_THAT(actual_jacobian, MatrixEq(expected_jacobian)); } }
diff --git a/internal/ceres/evaluator_test_utils.cc b/internal/ceres/evaluator_test_utils.cc index 6fa210b..7987d54 100644 --- a/internal/ceres/evaluator_test_utils.cc +++ b/internal/ceres/evaluator_test_utils.cc
@@ -34,6 +34,8 @@ #include "ceres/evaluator_test_utils.h" #include "ceres/eigen.h" +#include "ceres/test_util.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres::internal { @@ -48,18 +50,13 @@ const double* actual_residuals, const double* actual_gradient, const double* actual_jacobian) { - EXPECT_EQ(expected_cost, actual_cost); + EXPECT_EQ(actual_cost, expected_cost); if (expected_residuals != nullptr) { ConstVectorRef expected_residuals_vector(expected_residuals, expected_num_rows); ConstVectorRef actual_residuals_vector(actual_residuals, expected_num_rows); - EXPECT_TRUE( - (actual_residuals_vector.array() == expected_residuals_vector.array()) - .all()) - << "Actual:\n" - << actual_residuals_vector << "\nExpected:\n" - << expected_residuals_vector; + EXPECT_THAT(actual_residuals_vector, MatrixEq(expected_residuals_vector)); } if (expected_gradient != nullptr) { @@ -67,12 +64,7 @@ expected_num_cols); ConstVectorRef actual_gradient_vector(actual_gradient, expected_num_cols); - EXPECT_TRUE( - (actual_gradient_vector.array() == expected_gradient_vector.array()) - .all()) - << "Actual:\n" - << actual_gradient_vector.transpose() << "\nExpected:\n" - << expected_gradient_vector.transpose(); + EXPECT_THAT(actual_gradient_vector, MatrixEq(expected_gradient_vector)); } if (expected_jacobian != nullptr) { @@ -80,12 +72,7 @@ expected_jacobian, expected_num_rows, expected_num_cols); ConstMatrixRef actual_jacobian_matrix( actual_jacobian, expected_num_rows, expected_num_cols); - EXPECT_TRUE( - (actual_jacobian_matrix.array() == expected_jacobian_matrix.array()) - .all()) - << "Actual:\n" - << actual_jacobian_matrix << "\nExpected:\n" - << expected_jacobian_matrix; + EXPECT_THAT(actual_jacobian_matrix, MatrixEq(expected_jacobian_matrix)); } }
diff --git a/internal/ceres/gradient_problem_solver_test.cc b/internal/ceres/gradient_problem_solver_test.cc index 661a113..f8ce732 100644 --- a/internal/ceres/gradient_problem_solver_test.cc +++ b/internal/ceres/gradient_problem_solver_test.cc
@@ -33,10 +33,14 @@ #include <memory> #include "ceres/gradient_problem.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres::internal { +using ::testing::DoubleNear; +using ::testing::Each; + // Rosenbrock function; see http://en.wikipedia.org/wiki/Rosenbrock_function . class Rosenbrock : public ceres::FirstOrderFunction { public: @@ -67,8 +71,7 @@ ceres::Solve(options, problem, parameters, &summary); EXPECT_EQ(CONVERGENCE, summary.termination_type); - EXPECT_NEAR(1.0, parameters[0], expected_tolerance); - EXPECT_NEAR(1.0, parameters[1], expected_tolerance); + EXPECT_THAT(parameters, Each(DoubleNear(1.0, expected_tolerance))); } class QuadraticFunction : public ceres::FirstOrderFunction { @@ -113,9 +116,7 @@ ceres::Solve(options, problem, &x, &summary); num_iterations = summary.iterations.size() - 1; EXPECT_GT(num_iterations, 1); - for (double value : callback.x_values) { - EXPECT_EQ(50.0, value); - } + EXPECT_THAT(callback.x_values, Each(50.0)); // Second try: with updating x = 50.0; @@ -124,8 +125,9 @@ ceres::Solve(options, problem, &x, &summary); num_iterations = summary.iterations.size() - 1; EXPECT_GT(num_iterations, 1); - EXPECT_EQ(original_x, callback.x_values[0]); - EXPECT_NE(original_x, callback.x_values[1]); + ASSERT_GE(callback.x_values.size(), 2); + EXPECT_EQ(callback.x_values[0], original_x); + EXPECT_NE(callback.x_values[1], original_x); } } // namespace ceres::internal
diff --git a/internal/ceres/manifold_test.cc b/internal/ceres/manifold_test.cc index bf28aac..e1ca7c2 100644 --- a/internal/ceres/manifold_test.cc +++ b/internal/ceres/manifold_test.cc
@@ -46,6 +46,7 @@ #include "ceres/quaternion_manifold_test_utils.h" #include "ceres/rotation.h" #include "ceres/sphere_manifold.h" +#include "ceres/test_util.h" #include "ceres/types.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -105,10 +106,8 @@ Vector delta = Vector::Random(manifold.TangentSize()); Vector x_plus_delta = Vector::Zero(manifold.AmbientSize()); - manifold.Plus(x.data(), delta.data(), x_plus_delta.data()); - EXPECT_NEAR((x_plus_delta - x - delta).norm() / (x + delta).norm(), - 0.0, - kTolerance); + ASSERT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data())); + EXPECT_THAT(x_plus_delta, RelativeNormNear(x + delta, kTolerance)); EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance); } @@ -126,10 +125,8 @@ Vector delta = Vector::Random(manifold.TangentSize()); Vector x_plus_delta = Vector::Zero(manifold.AmbientSize()); - manifold.Plus(x.data(), delta.data(), x_plus_delta.data()); - EXPECT_NEAR((x_plus_delta - x - delta).norm() / (x + delta).norm(), - 0.0, - kTolerance); + ASSERT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data())); + EXPECT_THAT(x_plus_delta, RelativeNormNear(x + delta, kTolerance)); EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance); } @@ -143,10 +140,8 @@ Vector delta = Vector::Random(3); Vector x_plus_delta = Vector::Zero(3); - manifold.Plus(x.data(), delta.data(), x_plus_delta.data()); - EXPECT_NEAR((x_plus_delta - x - delta).norm() / (x + delta).norm(), - 0.0, - kTolerance); + ASSERT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data())); + EXPECT_THAT(x_plus_delta, RelativeNormNear(x + delta, kTolerance)); EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance); } } @@ -179,17 +174,16 @@ Vector delta = Vector::Random(kTangentSize); Vector x_plus_delta = Vector::Zero(kAmbientSize); - x_plus_delta.setZero(); - manifold_with_ith_parameter_constant.Plus( - x.data(), delta.data(), x_plus_delta.data()); - int k = 0; - for (int j = 0; j < kAmbientSize; ++j) { - if (j == i) { - EXPECT_EQ(x_plus_delta[j], x[j]); - } else { - EXPECT_EQ(x_plus_delta[j], x[j] + delta[k++]); + ASSERT_TRUE(manifold_with_ith_parameter_constant.Plus( + x.data(), delta.data(), x_plus_delta.data())); + Vector expected_x_plus_delta = x; + for (int j = 0, k = 0; j < kAmbientSize; ++j) { + if (j != i) { + expected_x_plus_delta[j] += delta[k++]; } } + EXPECT_THAT(x_plus_delta, MatrixEq(expected_x_plus_delta)) + << "constant parameter " << i; EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD( manifold_with_ith_parameter_constant, x, delta, y, kTolerance); @@ -292,9 +286,7 @@ ambient_cursor += manifold4.AmbientSize(); tangent_cursor += manifold4.TangentSize(); - for (int i = 0; i < x.size(); ++i) { - EXPECT_EQ(x_plus_delta[i], x_plus_delta_expected[i]); - } + EXPECT_THAT(x_plus_delta, MatrixEq(x_plus_delta_expected)); EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD( manifold, x, delta, x_plus_delta, kTolerance); @@ -646,8 +638,8 @@ double delta[1]{constants::pi / 4}; Eigen::Vector2d y = Eigen::Vector2d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta, y.data())); - const Eigen::Vector2d gtY(std::sqrt(2.0) / 2.0, std::sqrt(2.0) / 2.0); - EXPECT_LT((y - gtY).norm(), kTolerance); + const Eigen::Vector2d gtY(kHalfSqrt2, kHalfSqrt2); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -655,7 +647,7 @@ Eigen::Vector2d y = Eigen::Vector2d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta, y.data())); const Eigen::Vector2d gtY = Eigen::Vector2d::UnitX(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -663,7 +655,7 @@ Eigen::Vector2d y = Eigen::Vector2d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta, y.data())); const Eigen::Vector2d gtY = -Eigen::Vector2d::UnitY(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -671,7 +663,7 @@ Eigen::Vector2d y = Eigen::Vector2d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta, y.data())); const Eigen::Vector2d gtY = Eigen::Vector2d::UnitY(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } } @@ -684,7 +676,7 @@ Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = Eigen::Vector3d::UnitX(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -692,7 +684,7 @@ Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = -Eigen::Vector3d::UnitZ(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -700,7 +692,7 @@ Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = Eigen::Vector3d::UnitZ(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -708,7 +700,7 @@ Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = Eigen::Vector3d::UnitY(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -716,7 +708,7 @@ Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = -Eigen::Vector3d::UnitZ(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -724,7 +716,7 @@ Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = Eigen::Vector3d::UnitZ(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -732,8 +724,8 @@ Eigen::Vector2d(1, 1).normalized() * constants::pi / 2; Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); - const Eigen::Vector3d gtY(std::sqrt(2.0) / 2.0, std::sqrt(2.0) / 2.0, 0.0); - EXPECT_LT((y - gtY).norm(), kTolerance); + const Eigen::Vector3d gtY(kHalfSqrt2, kHalfSqrt2, 0.0); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -741,7 +733,7 @@ Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = -Eigen::Vector3d::UnitZ(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } } @@ -751,10 +743,10 @@ { double delta[1]; - const Eigen::Vector2d y(std::sqrt(2.0) / 2.0, std::sqrt(2.0) / 2.0); + const Eigen::Vector2d y(kHalfSqrt2, kHalfSqrt2); const double gtDelta{constants::pi / 4}; EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta)); - EXPECT_LT(std::abs(delta[0] - gtDelta), kTolerance); + EXPECT_NEAR(delta[0], gtDelta, kTolerance); } { @@ -762,7 +754,7 @@ const Eigen::Vector2d y(-1, 0); const double gtDelta{constants::pi}; EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta)); - EXPECT_LT(std::abs(delta[0] - gtDelta), kTolerance); + EXPECT_NEAR(delta[0], gtDelta, kTolerance); } } @@ -772,10 +764,10 @@ { Eigen::Vector2d delta; - const Eigen::Vector3d y(std::sqrt(2.0) / 2.0, 0.0, std::sqrt(2.0) / 2.0); + const Eigen::Vector3d y(kHalfSqrt2, 0.0, kHalfSqrt2); const Eigen::Vector2d gtDelta(constants::pi / 4, 0.0); EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta.data())); - EXPECT_LT((delta - gtDelta).norm(), kTolerance); + EXPECT_THAT(delta, NormNear(gtDelta, kTolerance)); } { @@ -783,7 +775,7 @@ const Eigen::Vector3d y(-1, 0, 0); const Eigen::Vector2d gtDelta(0.0, constants::pi); EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta.data())); - EXPECT_LT((delta - gtDelta).norm(), kTolerance); + EXPECT_THAT(delta, NormNear(gtDelta, kTolerance)); } } @@ -909,7 +901,7 @@ EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); Vector6d gtY; gtY << 2.0 * Vector3d::UnitY(), Vector3d::UnitX(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -918,7 +910,7 @@ EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); Vector6d gtY; gtY << 3.0 * Vector3d::UnitX(), Vector3d::UnitY(); - EXPECT_LT((y - gtY).norm(), kTolerance); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } { @@ -928,9 +920,8 @@ Vector6d y = Vector6d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); Vector6d gtY; - gtY << Vector3d(1.0, 2.0, 0.0), - Vector3d(std::sqrt(2.0) / 2.0, std::sqrt(2.0) / 2.0, 0.0); - EXPECT_LT((y - gtY).norm(), kTolerance); + gtY << Vector3d(1.0, 2.0, 0.0), Vector3d(kHalfSqrt2, kHalfSqrt2, 0.0); + EXPECT_THAT(y, NormNear(gtY, kTolerance)); } }
diff --git a/internal/ceres/minimizer_test.cc b/internal/ceres/minimizer_test.cc index 10fb9e5..be3dea4 100644 --- a/internal/ceres/minimizer_test.cc +++ b/internal/ceres/minimizer_test.cc
@@ -32,10 +32,13 @@ #include "ceres/iteration_callback.h" #include "ceres/solver.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres::internal { +using ::testing::ElementsAre; + class FakeIterationCallback : public IterationCallback { public: CallbackReturnType operator()(const IterationSummary& summary) final { @@ -52,10 +55,7 @@ solver_options.callbacks.push_back(&callback1); Minimizer::Options minimizer_options(solver_options); - ASSERT_EQ(2, minimizer_options.callbacks.size()); - - EXPECT_EQ(minimizer_options.callbacks[0], &callback0); - EXPECT_EQ(minimizer_options.callbacks[1], &callback1); + EXPECT_THAT(minimizer_options.callbacks, ElementsAre(&callback0, &callback1)); } class AbortingIterationCallback : public IterationCallback {
diff --git a/internal/ceres/parameter_block_test.cc b/internal/ceres/parameter_block_test.cc index 6706e11..c76c142 100644 --- a/internal/ceres/parameter_block_test.cc +++ b/internal/ceres/parameter_block_test.cc
@@ -32,11 +32,15 @@ #include "absl/log/log.h" #include "ceres/eigen.h" +#include "ceres/test_util.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres { namespace internal { +using ::testing::ElementsAre; + TEST(ParameterBlock, SetManifoldDiesOnSizeMismatch) { double x[3] = {1.0, 2.0, 3.0}; ParameterBlock parameter_block(x, 3, -1); @@ -94,20 +98,15 @@ // Ensure the manifold plus jacobian result is correctly computed. ConstMatrixRef manifold_jacobian(parameter_block.PlusJacobian(), 3, 2); - ASSERT_EQ(1.0, manifold_jacobian(0, 0)); - ASSERT_EQ(0.0, manifold_jacobian(0, 1)); - ASSERT_EQ(0.0, manifold_jacobian(1, 0)); - ASSERT_EQ(0.0, manifold_jacobian(1, 1)); - ASSERT_EQ(0.0, manifold_jacobian(2, 0)); - ASSERT_EQ(1.0, manifold_jacobian(2, 1)); + Matrix expected_jacobian(3, 2); + expected_jacobian << 1.0, 0.0, 0.0, 0.0, 0.0, 1.0; + EXPECT_THAT(manifold_jacobian, MatrixEq(expected_jacobian)); // Check that updating works as expected. double x_plus_delta[3]; double delta[2] = {0.5, 0.3}; - parameter_block.Plus(x, delta, x_plus_delta); - ASSERT_EQ(1.5, x_plus_delta[0]); - ASSERT_EQ(2.0, x_plus_delta[1]); - ASSERT_EQ(3.3, x_plus_delta[2]); + ASSERT_TRUE(parameter_block.Plus(x, delta, x_plus_delta)); + EXPECT_THAT(x_plus_delta, ElementsAre(1.5, 2.0, 3.3)); } struct TestManifold : public Manifold { @@ -156,9 +155,8 @@ double delta[2] = {0.2, 0.3}; double x_plus_delta[2]; - parameter_block.Plus(x, delta, x_plus_delta); - EXPECT_EQ(1.2, x_plus_delta[0]); - EXPECT_EQ(2.3, x_plus_delta[1]); + ASSERT_TRUE(parameter_block.Plus(x, delta, x_plus_delta)); + EXPECT_THAT(x_plus_delta, ElementsAre(1.2, 2.3)); } // Stops computing the plus_jacobian after the first time. @@ -241,9 +239,8 @@ parameter_block.SetUpperBound(0, 2.0); parameter_block.SetLowerBound(1, -1.0); double x_plus_delta[2]; - parameter_block.Plus(x, delta, x_plus_delta); - EXPECT_EQ(x_plus_delta[0], 2.0); - EXPECT_EQ(x_plus_delta[1], -1.0); + ASSERT_TRUE(parameter_block.Plus(x, delta, x_plus_delta)); + EXPECT_THAT(x_plus_delta, ElementsAre(2.0, -1.0)); } TEST(ParameterBlock, ResetManifoldToNull) {
diff --git a/internal/ceres/problem_test.cc b/internal/ceres/problem_test.cc index b06d647..eaad220 100644 --- a/internal/ceres/problem_test.cc +++ b/internal/ceres/problem_test.cc
@@ -31,11 +31,13 @@ #include "ceres/problem.h" +#include <limits> #include <memory> #include <string> #include <vector> #include "absl/log/check.h" +#include "absl/strings/str_format.h" #include "ceres/autodiff_cost_function.h" #include "ceres/casts.h" #include "ceres/cost_function.h" @@ -49,12 +51,21 @@ #include "ceres/program.h" #include "ceres/sized_cost_function.h" #include "ceres/sparse_matrix.h" +#include "ceres/test_util.h" #include "ceres/types.h" #include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres::internal { +using ::testing::Contains; +using ::testing::ElementsAre; +using ::testing::SizeIs; +using ::testing::UnorderedElementsAre; +using ::testing::UnorderedElementsAreArray; + +constexpr double kEpsilon = std::numeric_limits<double>::epsilon(); + // The following three classes are for the purposes of defining // function signatures. They have dummy Evaluate functions. @@ -336,8 +347,7 @@ problem.AddResidualBlock(cost_function, nullptr, x); EXPECT_EQ(problem.GetCostFunctionForResidualBlock(residual_block), cost_function); - EXPECT_TRUE(problem.GetLossFunctionForResidualBlock(residual_block) == - nullptr); + EXPECT_EQ(problem.GetLossFunctionForResidualBlock(residual_block), nullptr); } TEST(Problem, GetLossFunctionForResidualBlock) { @@ -428,14 +438,14 @@ double* values, ResidualBlock* residual_block) { ParameterBlock* parameter_block = FindOrDie(problem->parameter_map(), values); - EXPECT_TRUE(ContainsKey(*(parameter_block->mutable_residual_blocks()), - residual_block)); + EXPECT_THAT(*(parameter_block->mutable_residual_blocks()), + Contains(residual_block)); } void ExpectSize(double* values, int size) { ParameterBlock* parameter_block = FindOrDie(problem->parameter_map(), values); - EXPECT_EQ(size, parameter_block->mutable_residual_blocks()->size()); + EXPECT_THAT(*(parameter_block->mutable_residual_blocks()), SizeIs(size)); } // Degenerate case. @@ -570,7 +580,7 @@ Manifold* manifold = new EuclideanManifold<3>; problem.SetManifold(x, manifold); EXPECT_EQ(problem.GetManifold(x), manifold); - EXPECT_TRUE(problem.GetManifold(y) == nullptr); + EXPECT_EQ(problem.GetManifold(y), nullptr); } TEST(Problem, HasManifold) { @@ -634,17 +644,13 @@ std::vector<double*> parameter_blocks; problem.GetParameterBlocks(¶meter_blocks); - EXPECT_EQ(parameter_blocks.size(), 2); - EXPECT_NE(parameter_blocks[0], parameter_blocks[1]); - EXPECT_TRUE(parameter_blocks[0] == x || parameter_blocks[0] == y); - EXPECT_TRUE(parameter_blocks[1] == x || parameter_blocks[1] == y); + EXPECT_THAT(parameter_blocks, UnorderedElementsAre(x, y)); EXPECT_TRUE(problem.HasParameterBlock(x)); problem.RemoveParameterBlock(x); EXPECT_FALSE(problem.HasParameterBlock(x)); problem.GetParameterBlocks(¶meter_blocks); - EXPECT_EQ(parameter_blocks.size(), 1); - EXPECT_TRUE(parameter_blocks[0] == y); + EXPECT_THAT(parameter_blocks, ElementsAre(y)); } TEST(Problem, ParameterBlockQueryTest) { @@ -663,17 +669,13 @@ std::vector<double*> parameter_blocks; problem.GetParameterBlocks(¶meter_blocks); - EXPECT_EQ(parameter_blocks.size(), 2); - EXPECT_NE(parameter_blocks[0], parameter_blocks[1]); - EXPECT_TRUE(parameter_blocks[0] == x || parameter_blocks[0] == y); - EXPECT_TRUE(parameter_blocks[1] == x || parameter_blocks[1] == y); + EXPECT_THAT(parameter_blocks, UnorderedElementsAre(x, y)); EXPECT_TRUE(problem.HasParameterBlock(x)); problem.RemoveParameterBlock(x); EXPECT_FALSE(problem.HasParameterBlock(x)); problem.GetParameterBlocks(¶meter_blocks); - EXPECT_EQ(parameter_blocks.size(), 1); - EXPECT_TRUE(parameter_blocks[0] == y); + EXPECT_THAT(parameter_blocks, ElementsAre(y)); } TEST_P(DynamicProblem, RemoveParameterBlockWithNoResiduals) { @@ -831,9 +833,9 @@ ExpectParameterBlockContains(w, r_yzw, r_yw, r_zw, r_w); } else { // Otherwise, nothing. - EXPECT_TRUE(GetParameterBlock(0)->mutable_residual_blocks() == nullptr); - EXPECT_TRUE(GetParameterBlock(1)->mutable_residual_blocks() == nullptr); - EXPECT_TRUE(GetParameterBlock(2)->mutable_residual_blocks() == nullptr); + EXPECT_EQ(GetParameterBlock(0)->mutable_residual_blocks(), nullptr); + EXPECT_EQ(GetParameterBlock(1)->mutable_residual_blocks(), nullptr); + EXPECT_EQ(GetParameterBlock(2)->mutable_residual_blocks(), nullptr); } EXPECT_EQ(3, problem->NumParameterBlocks()); EXPECT_EQ(7, NumResidualBlocks()); @@ -977,37 +979,11 @@ problem->RemoveResidualBlock(r_y); } -// Check that a null-terminated array, a, has the same elements as b. -template <typename T> -void ExpectVectorContainsUnordered(const T* a, const std::vector<T>& b) { - // Compute the size of a. - int size = 0; - while (a[size]) { - ++size; - } - ASSERT_EQ(size, b.size()); - - // Sort a. - std::vector<T> a_sorted(size); - copy(a, a + size, a_sorted.begin()); - sort(a_sorted.begin(), a_sorted.end()); - - // Sort b. - std::vector<T> b_sorted(b); - sort(b_sorted.begin(), b_sorted.end()); - - // Compare. - for (int i = 0; i < size; ++i) { - EXPECT_EQ(a_sorted[i], b_sorted[i]); - } -} - -static void ExpectProblemHasResidualBlocks( - const ProblemImpl& problem, - const ResidualBlockId* expected_residual_blocks) { +static std::vector<ResidualBlockId> GetResidualBlocks( + const ProblemImpl& problem) { std::vector<ResidualBlockId> residual_blocks; problem.GetResidualBlocks(&residual_blocks); - ExpectVectorContainsUnordered(expected_residual_blocks, residual_blocks); + return residual_blocks; } TEST_P(DynamicProblem, GetXXXBlocksForYYYBlock) { @@ -1027,44 +1003,26 @@ CostFunction* cost_w = new UnaryCostFunction (1, 3); ResidualBlock* r_yzw = problem->AddResidualBlock(cost_yzw, nullptr, y, z, w); - { - ResidualBlockId expected_residuals[] = {r_yzw, nullptr}; - ExpectProblemHasResidualBlocks(*problem, expected_residuals); - } + EXPECT_THAT(GetResidualBlocks(*problem), + UnorderedElementsAre(r_yzw)); ResidualBlock* r_yz = problem->AddResidualBlock(cost_yz, nullptr, y, z); - { - ResidualBlockId expected_residuals[] = {r_yzw, r_yz, nullptr}; - ExpectProblemHasResidualBlocks(*problem, expected_residuals); - } + EXPECT_THAT(GetResidualBlocks(*problem), + UnorderedElementsAre(r_yzw, r_yz)); ResidualBlock* r_yw = problem->AddResidualBlock(cost_yw, nullptr, y, w); - { - ResidualBlock *expected_residuals[] = {r_yzw, r_yz, r_yw, nullptr}; - ExpectProblemHasResidualBlocks(*problem, expected_residuals); - } + EXPECT_THAT(GetResidualBlocks(*problem), + UnorderedElementsAre(r_yzw, r_yz, r_yw)); ResidualBlock* r_zw = problem->AddResidualBlock(cost_zw, nullptr, z, w); - { - ResidualBlock *expected_residuals[] = {r_yzw, r_yz, r_yw, r_zw, nullptr}; - ExpectProblemHasResidualBlocks(*problem, expected_residuals); - } + EXPECT_THAT(GetResidualBlocks(*problem), + UnorderedElementsAre(r_yzw, r_yz, r_yw, r_zw)); ResidualBlock* r_y = problem->AddResidualBlock(cost_y, nullptr, y); - { - ResidualBlock *expected_residuals[] = {r_yzw, r_yz, r_yw, r_zw, r_y, nullptr}; - ExpectProblemHasResidualBlocks(*problem, expected_residuals); - } + EXPECT_THAT(GetResidualBlocks(*problem), + UnorderedElementsAre(r_yzw, r_yz, r_yw, r_zw, r_y)); ResidualBlock* r_z = problem->AddResidualBlock(cost_z, nullptr, z); - { - ResidualBlock *expected_residuals[] = { - r_yzw, r_yz, r_yw, r_zw, r_y, r_z, nullptr - }; - ExpectProblemHasResidualBlocks(*problem, expected_residuals); - } + EXPECT_THAT(GetResidualBlocks(*problem), + UnorderedElementsAre(r_yzw, r_yz, r_yw, r_zw, r_y, r_z)); ResidualBlock* r_w = problem->AddResidualBlock(cost_w, nullptr, w); - { - ResidualBlock *expected_residuals[] = { - r_yzw, r_yz, r_yw, r_zw, r_y, r_z, r_w, nullptr - }; - ExpectProblemHasResidualBlocks(*problem, expected_residuals); - } + EXPECT_THAT(GetResidualBlocks(*problem), + UnorderedElementsAre(r_yzw, r_yz, r_yw, r_zw, r_y, r_z, r_w)); std::vector<double*> parameter_blocks; std::vector<ResidualBlockId> residual_blocks; @@ -1072,45 +1030,39 @@ // Check GetResidualBlocksForParameterBlock() for all parameter blocks. struct GetResidualBlocksForParameterBlockTestCase { double* parameter_block; - ResidualBlockId expected_residual_blocks[10]; + std::vector<ResidualBlockId> expected_residual_blocks; }; - GetResidualBlocksForParameterBlockTestCase get_residual_blocks_cases[] = { - { y, { r_yzw, r_yz, r_yw, r_y, nullptr} }, - { z, { r_yzw, r_yz, r_zw, r_z, nullptr} }, - { w, { r_yzw, r_yw, r_zw, r_w, nullptr} }, - { nullptr, { nullptr } } + const GetResidualBlocksForParameterBlockTestCase get_residual_blocks_cases[] = { + { y, { r_yzw, r_yz, r_yw, r_y } }, + { z, { r_yzw, r_yz, r_zw, r_z } }, + { w, { r_yzw, r_yw, r_zw, r_w } }, }; - for (int i = 0; get_residual_blocks_cases[i].parameter_block; ++i) { - problem->GetResidualBlocksForParameterBlock( - get_residual_blocks_cases[i].parameter_block, - &residual_blocks); - ExpectVectorContainsUnordered( - get_residual_blocks_cases[i].expected_residual_blocks, - residual_blocks); + for (const auto& test_case : get_residual_blocks_cases) { + problem->GetResidualBlocksForParameterBlock(test_case.parameter_block, + &residual_blocks); + EXPECT_THAT(residual_blocks, + UnorderedElementsAreArray(test_case.expected_residual_blocks)); } // Check GetParameterBlocksForResidualBlock() for all residual blocks. struct GetParameterBlocksForResidualBlockTestCase { ResidualBlockId residual_block; - double* expected_parameter_blocks[10]; + std::vector<double*> expected_parameter_blocks; }; - GetParameterBlocksForResidualBlockTestCase get_parameter_blocks_cases[] = { - { r_yzw, { y, z, w, nullptr } }, - { r_yz , { y, z, nullptr } }, - { r_yw , { y, w, nullptr } }, - { r_zw , { z, w, nullptr } }, - { r_y , { y, nullptr } }, - { r_z , { z, nullptr } }, - { r_w , { w, nullptr } }, - { nullptr, { nullptr } } + const GetParameterBlocksForResidualBlockTestCase get_parameter_blocks_cases[] = { + { r_yzw, { y, z, w } }, + { r_yz , { y, z } }, + { r_yw , { y, w } }, + { r_zw , { z, w } }, + { r_y , { y } }, + { r_z , { z } }, + { r_w , { w } }, }; - for (int i = 0; get_parameter_blocks_cases[i].residual_block; ++i) { - problem->GetParameterBlocksForResidualBlock( - get_parameter_blocks_cases[i].residual_block, - ¶meter_blocks); - ExpectVectorContainsUnordered( - get_parameter_blocks_cases[i].expected_parameter_blocks, - parameter_blocks); + for (const auto& test_case : get_parameter_blocks_cases) { + problem->GetParameterBlocksForResidualBlock(test_case.residual_block, + ¶meter_blocks); + EXPECT_THAT(parameter_blocks, + UnorderedElementsAreArray(test_case.expected_parameter_blocks)); } // clang-format on @@ -1125,10 +1077,11 @@ // r_i = i - (j + 1) * x_ij^2 template <int kNumResiduals, int kNumParameterBlocks> class QuadraticCostFunction : public CostFunction { + static_assert(kNumResiduals > 0); + static_assert(kNumParameterBlocks > 0); + public: QuadraticCostFunction() { - CHECK_GT(kNumResiduals, 0); - CHECK_GT(kNumParameterBlocks, 0); set_num_residuals(kNumResiduals); for (int i = 0; i < kNumParameterBlocks; ++i) { mutable_parameter_block_sizes()->push_back(kNumResiduals); @@ -1163,7 +1116,7 @@ // Convert a CRSMatrix to a dense Eigen matrix. static void CRSToDenseMatrix(const CRSMatrix& input, Matrix* output) { - ASSERT_TRUE(output != nullptr); + ASSERT_NE(output, nullptr); Matrix& m = *output; m.resize(input.num_rows, input.num_cols); m.setZero(); @@ -1213,7 +1166,7 @@ std::vector<double> gradient; CRSMatrix jacobian; - EXPECT_TRUE( + ASSERT_TRUE( problem_.Evaluate(options, &cost, expected_residuals != nullptr ? &residuals : nullptr, @@ -1221,11 +1174,11 @@ expected_jacobian != nullptr ? &jacobian : nullptr)); if (expected_residuals != nullptr) { - EXPECT_EQ(residuals.size(), expected_num_rows); + ASSERT_THAT(residuals, SizeIs(expected_num_rows)); } if (expected_gradient != nullptr) { - EXPECT_EQ(gradient.size(), expected_num_cols); + ASSERT_THAT(gradient, SizeIs(expected_num_cols)); } if (expected_jacobian != nullptr) { @@ -1253,6 +1206,10 @@ void CheckAllEvaluationCombinations(const Problem::EvaluateOptions& options, const ExpectedEvaluation& expected) { for (int i = 0; i < 8; ++i) { + SCOPED_TRACE(absl::StrFormat("residuals %d, gradient %d, jacobian %d", + i & 1, + (i & 2) >> 1, + (i & 4) >> 2)); EvaluateAndCompare(options, expected.num_rows, expected.num_cols, @@ -1661,18 +1618,9 @@ 0, std::numeric_limits<double>::epsilon()) << actual_cost; - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; - EXPECT_NEAR((expected_dfdx - actual_dfdx).norm() / actual_dfdx.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdx; - EXPECT_NEAR((expected_dfdy - actual_dfdy).norm() / actual_dfdy.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdy; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); + EXPECT_THAT(actual_dfdx, RelativeNormNear(expected_dfdx, kEpsilon)); + EXPECT_THAT(actual_dfdy, RelativeNormNear(expected_dfdy, kEpsilon)); } TEST_F(ProblemEvaluateResidualBlockTest, @@ -1729,10 +1677,7 @@ 0, std::numeric_limits<double>::epsilon()) << actual_cost; - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); } TEST_F(ProblemEvaluateResidualBlockTest, @@ -1760,14 +1705,8 @@ 0, std::numeric_limits<double>::epsilon()) << actual_cost; - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; - EXPECT_NEAR((expected_dfdx - actual_dfdx).norm() / actual_dfdx.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdx; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); + EXPECT_THAT(actual_dfdx, RelativeNormNear(expected_dfdx, kEpsilon)); } TEST_F(ProblemEvaluateResidualBlockTest, @@ -1784,10 +1723,7 @@ actual_f.data(), nullptr)); - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); } TEST_F(ProblemEvaluateResidualBlockTest, OneResidualBlockWithLossFunction) { @@ -1823,18 +1759,9 @@ 0, std::numeric_limits<double>::epsilon()) << actual_cost; - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; - EXPECT_NEAR((expected_dfdx - actual_dfdx).norm() / actual_dfdx.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdx; - EXPECT_NEAR((expected_dfdy - actual_dfdy).norm() / actual_dfdy.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdy; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); + EXPECT_THAT(actual_dfdx, RelativeNormNear(expected_dfdx, kEpsilon)); + EXPECT_THAT(actual_dfdy, RelativeNormNear(expected_dfdy, kEpsilon)); } TEST_F(ProblemEvaluateResidualBlockTest, @@ -1868,18 +1795,9 @@ 0, std::numeric_limits<double>::epsilon()) << actual_cost; - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; - EXPECT_NEAR((expected_dfdx - actual_dfdx).norm() / actual_dfdx.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdx; - EXPECT_NEAR((expected_dfdy - actual_dfdy).norm() / actual_dfdy.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdy; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); + EXPECT_THAT(actual_dfdx, RelativeNormNear(expected_dfdx, kEpsilon)); + EXPECT_THAT(actual_dfdy, RelativeNormNear(expected_dfdy, kEpsilon)); } TEST_F(ProblemEvaluateResidualBlockTest, OneResidualBlockWithOneManifold) { @@ -1911,18 +1829,9 @@ 0, std::numeric_limits<double>::epsilon()) << actual_cost; - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; - EXPECT_NEAR((expected_dfdx - actual_dfdx).norm() / actual_dfdx.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdx; - EXPECT_NEAR((expected_dfdy - actual_dfdy).norm() / actual_dfdy.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdy; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); + EXPECT_THAT(actual_dfdx, RelativeNormNear(expected_dfdx, kEpsilon)); + EXPECT_THAT(actual_dfdy, RelativeNormNear(expected_dfdy, kEpsilon)); } TEST_F(ProblemEvaluateResidualBlockTest, OneResidualBlockWithTwoManifolds) { @@ -1955,18 +1864,9 @@ 0, std::numeric_limits<double>::epsilon()) << actual_cost; - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; - EXPECT_NEAR((expected_dfdx - actual_dfdx).norm() / actual_dfdx.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdx; - EXPECT_NEAR((expected_dfdy - actual_dfdy).norm() / actual_dfdy.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdy; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); + EXPECT_THAT(actual_dfdx, RelativeNormNear(expected_dfdx, kEpsilon)); + EXPECT_THAT(actual_dfdy, RelativeNormNear(expected_dfdy, kEpsilon)); } TEST_F(ProblemEvaluateResidualBlockTest, @@ -2007,14 +1907,8 @@ 0, std::numeric_limits<double>::epsilon()) << actual_cost; - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; - EXPECT_NEAR((expected_dfdy - actual_dfdy).norm() / actual_dfdy.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdy; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); + EXPECT_THAT(actual_dfdy, RelativeNormNear(expected_dfdy, kEpsilon)); } TEST_F(ProblemEvaluateResidualBlockTest, @@ -2061,10 +1955,7 @@ 0, std::numeric_limits<double>::epsilon()) << actual_cost; - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); } TEST_F(ProblemEvaluateResidualBlockTest, @@ -2106,14 +1997,8 @@ 0, std::numeric_limits<double>::epsilon()) << actual_cost; - EXPECT_NEAR((expected_f - actual_f).norm() / actual_f.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_f; - EXPECT_NEAR((expected_dfdy - actual_dfdy).norm() / actual_dfdy.norm(), - 0, - std::numeric_limits<double>::epsilon()) - << actual_dfdy; + EXPECT_THAT(actual_f, RelativeNormNear(expected_f, kEpsilon)); + EXPECT_THAT(actual_dfdy, RelativeNormNear(expected_dfdy, kEpsilon)); } TEST(Problem, SetAndGetParameterLowerBound) {
diff --git a/internal/ceres/program_test.cc b/internal/ceres/program_test.cc index 9c51ff9..394242e 100644 --- a/internal/ceres/program_test.cc +++ b/internal/ceres/program_test.cc
@@ -41,12 +41,19 @@ #include "ceres/problem_impl.h" #include "ceres/residual_block.h" #include "ceres/sized_cost_function.h" +#include "ceres/test_util.h" #include "ceres/triplet_sparse_matrix.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres { namespace internal { +using ::testing::ElementsAre; +using ::testing::HasSubstr; +using ::testing::IsEmpty; +using ::testing::SizeIs; + // A cost function that simply returns its argument. class UnaryIdentityCostFunction : public SizedCostFunction<1, 1> { public: @@ -103,7 +110,7 @@ EXPECT_EQ(reduced_program->NumParameterBlocks(), 3); EXPECT_EQ(reduced_program->NumResidualBlocks(), 3); - EXPECT_EQ(removed_parameter_blocks.size(), 0); + EXPECT_THAT(removed_parameter_blocks, IsEmpty()); EXPECT_EQ(fixed_cost, 0.0); } @@ -124,8 +131,7 @@ EXPECT_EQ(reduced_program->NumParameterBlocks(), 0); EXPECT_EQ(reduced_program->NumResidualBlocks(), 0); - EXPECT_EQ(removed_parameter_blocks.size(), 1); - EXPECT_EQ(removed_parameter_blocks[0], &x); + EXPECT_THAT(removed_parameter_blocks, ElementsAre(&x)); EXPECT_EQ(fixed_cost, 9.0); } @@ -147,7 +153,7 @@ &removed_parameter_blocks, &fixed_cost, &message)); EXPECT_EQ(reduced_program->NumParameterBlocks(), 0); EXPECT_EQ(reduced_program->NumResidualBlocks(), 0); - EXPECT_EQ(removed_parameter_blocks.size(), 3); + EXPECT_THAT(removed_parameter_blocks, SizeIs(3)); EXPECT_EQ(fixed_cost, 0.0); } @@ -312,9 +318,7 @@ Matrix actual_dense_jacobian; actual_block_sparse_jacobian->ToDenseMatrix(&actual_dense_jacobian); - EXPECT_EQ(expected_dense_jacobian.rows(), actual_dense_jacobian.rows()); - EXPECT_EQ(expected_dense_jacobian.cols(), actual_dense_jacobian.cols()); - EXPECT_EQ((expected_dense_jacobian - actual_dense_jacobian).norm(), 0.0); + EXPECT_THAT(actual_dense_jacobian, MatrixEq(expected_dense_jacobian)); } INSTANTIATE_TEST_SUITE_P(AllColumns, BlockJacobianTest, ::testing::Range(0, 7)); @@ -381,7 +385,7 @@ Matrix actual_dense_jacobian; actual_block_sparse_jacobian->ToDenseMatrix(&actual_dense_jacobian); - EXPECT_EQ((expected_dense_jacobian - actual_dense_jacobian).norm(), 0.0); + EXPECT_THAT(actual_dense_jacobian, MatrixEq(expected_dense_jacobian)); } TEST(Program, ProblemHasNanParameterBlocks) { @@ -392,8 +396,7 @@ problem.AddResidualBlock(new MockCostFunctionBase<1, 2>(), nullptr, x); std::string error; EXPECT_FALSE(problem.program().ParameterBlocksAreFinite(&error)); - EXPECT_NE(error.find("has at least one invalid value"), std::string::npos) - << error; + EXPECT_THAT(error, HasSubstr("has at least one invalid value")) << error; } TEST(Program, InfeasibleParameterBlock) { @@ -404,7 +407,7 @@ problem.SetParameterUpperBound(x, 0, 1.0); std::string error; EXPECT_FALSE(problem.program().IsFeasible(&error)); - EXPECT_NE(error.find("infeasible bound"), std::string::npos) << error; + EXPECT_THAT(error, HasSubstr("infeasible bound")) << error; } TEST(Program, InfeasibleConstantParameterBlock) { @@ -416,7 +419,7 @@ problem.SetParameterBlockConstant(x); std::string error; EXPECT_FALSE(problem.program().IsFeasible(&error)); - EXPECT_NE(error.find("infeasible value"), std::string::npos) << error; + EXPECT_THAT(error, HasSubstr("infeasible value")) << error; } } // namespace internal
diff --git a/internal/ceres/residual_block_test.cc b/internal/ceres/residual_block_test.cc index f684487..b82d56f 100644 --- a/internal/ceres/residual_block_test.cc +++ b/internal/ceres/residual_block_test.cc
@@ -34,14 +34,20 @@ #include <string> #include <vector> +#include "absl/types/span.h" #include "ceres/eigen.h" #include "ceres/manifold.h" #include "ceres/parameter_block.h" #include "ceres/sized_cost_function.h" +#include "ceres/test_util.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres::internal { +using ::testing::Each; +using ::testing::ElementsAre; + // Trivial cost function that accepts three arguments. class TernaryCostFunction : public CostFunction { public: @@ -107,16 +113,15 @@ // Verify cost-only evaluation. double cost; - residual_block.Evaluate(true, &cost, nullptr, nullptr, scratch); - EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); + ASSERT_TRUE(residual_block.Evaluate(true, &cost, nullptr, nullptr, scratch)); + EXPECT_EQ(cost, 0.5 * (0 * 0 + 1 * 1 + 2 * 2)); // Verify cost and residual evaluation. double residuals[3]; - residual_block.Evaluate(true, &cost, residuals, nullptr, scratch); - EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); - EXPECT_EQ(0.0, residuals[0]); - EXPECT_EQ(1.0, residuals[1]); - EXPECT_EQ(2.0, residuals[2]); + ASSERT_TRUE( + residual_block.Evaluate(true, &cost, residuals, nullptr, scratch)); + EXPECT_EQ(cost, 0.5 * (0 * 0 + 1 * 1 + 2 * 2)); + EXPECT_THAT(residuals, ElementsAre(0.0, 1.0, 2.0)); // Verify cost, residual, and jacobian evaluation. cost = 0.0; @@ -133,15 +138,14 @@ double* jacobian_ptrs[3] = { jacobian_rx.data(), jacobian_ry.data(), jacobian_rz.data()}; - residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch); - EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); - EXPECT_EQ(0.0, residuals[0]); - EXPECT_EQ(1.0, residuals[1]); - EXPECT_EQ(2.0, residuals[2]); + ASSERT_TRUE( + residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch)); + EXPECT_EQ(cost, 0.5 * (0 * 0 + 1 * 1 + 2 * 2)); + EXPECT_THAT(residuals, ElementsAre(0.0, 1.0, 2.0)); - EXPECT_TRUE((jacobian_rx.array() == 0.0).all()) << "\n" << jacobian_rx; - EXPECT_TRUE((jacobian_ry.array() == 1.0).all()) << "\n" << jacobian_ry; - EXPECT_TRUE((jacobian_rz.array() == 2.0).all()) << "\n" << jacobian_rz; + EXPECT_THAT(absl::MakeConstSpan(jacobian_rx), Each(0.0)); + EXPECT_THAT(absl::MakeConstSpan(jacobian_ry), Each(1.0)); + EXPECT_THAT(absl::MakeConstSpan(jacobian_rz), Each(2.0)); // Verify cost, residual, and partial jacobian evaluation. cost = 0.0; @@ -152,16 +156,15 @@ jacobian_ptrs[1] = nullptr; // Don't compute the jacobian for y. - residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch); - EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); - EXPECT_EQ(0.0, residuals[0]); - EXPECT_EQ(1.0, residuals[1]); - EXPECT_EQ(2.0, residuals[2]); + ASSERT_TRUE( + residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch)); + EXPECT_EQ(cost, 0.5 * (0 * 0 + 1 * 1 + 2 * 2)); + EXPECT_THAT(residuals, ElementsAre(0.0, 1.0, 2.0)); // clang-format off - EXPECT_TRUE((jacobian_rx.array() == 0.0).all()) << "\n" << jacobian_rx; - EXPECT_TRUE((jacobian_ry.array() == -1.0).all()) << "\n" << jacobian_ry; - EXPECT_TRUE((jacobian_rz.array() == 2.0).all()) << "\n" << jacobian_rz; + EXPECT_THAT(absl::MakeConstSpan(jacobian_rx), Each(0.0)); + EXPECT_THAT(absl::MakeConstSpan(jacobian_ry), Each(-1.0)); + EXPECT_THAT(absl::MakeConstSpan(jacobian_rz), Each(2.0)); // clang-format on } @@ -244,16 +247,15 @@ // Verify cost-only evaluation. double cost; - residual_block.Evaluate(true, &cost, nullptr, nullptr, scratch); - EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); + ASSERT_TRUE(residual_block.Evaluate(true, &cost, nullptr, nullptr, scratch)); + EXPECT_EQ(cost, 0.5 * (0 * 0 + 1 * 1 + 2 * 2)); // Verify cost and residual evaluation. double residuals[3]; - residual_block.Evaluate(true, &cost, residuals, nullptr, scratch); - EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); - EXPECT_EQ(0.0, residuals[0]); - EXPECT_EQ(1.0, residuals[1]); - EXPECT_EQ(2.0, residuals[2]); + ASSERT_TRUE( + residual_block.Evaluate(true, &cost, residuals, nullptr, scratch)); + EXPECT_EQ(cost, 0.5 * (0 * 0 + 1 * 1 + 2 * 2)); + EXPECT_THAT(residuals, ElementsAre(0.0, 1.0, 2.0)); // Verify cost, residual, and jacobian evaluation. cost = 0.0; @@ -270,11 +272,10 @@ double* jacobian_ptrs[3] = { jacobian_rx.data(), jacobian_ry.data(), jacobian_rz.data()}; - residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch); - EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); - EXPECT_EQ(0.0, residuals[0]); - EXPECT_EQ(1.0, residuals[1]); - EXPECT_EQ(2.0, residuals[2]); + ASSERT_TRUE( + residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch)); + EXPECT_EQ(cost, 0.5 * (0 * 0 + 1 * 1 + 2 * 2)); + EXPECT_THAT(residuals, ElementsAre(0.0, 1.0, 2.0)); // clang-format off @@ -291,15 +292,9 @@ 0.0, 1.0, /* 2.0, */ 3.0, 0.0, 1.0, /* 2.0, */ 3.0; - EXPECT_EQ(expected_jacobian_rx, jacobian_rx) - << "\nExpected:\n" << expected_jacobian_rx - << "\nActual:\n" << jacobian_rx; - EXPECT_EQ(expected_jacobian_ry, jacobian_ry) - << "\nExpected:\n" << expected_jacobian_ry - << "\nActual:\n" << jacobian_ry; - EXPECT_EQ(expected_jacobian_rz, jacobian_rz) - << "\nExpected:\n " << expected_jacobian_rz - << "\nActual:\n" << jacobian_rz; + EXPECT_THAT(jacobian_rx, MatrixEq(expected_jacobian_rx)); + EXPECT_THAT(jacobian_ry, MatrixEq(expected_jacobian_ry)); + EXPECT_THAT(jacobian_rz, MatrixEq(expected_jacobian_rz)); // clang-format on @@ -312,15 +307,14 @@ jacobian_ptrs[1] = nullptr; // Don't compute the jacobian for y. - residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch); - EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); - EXPECT_EQ(0.0, residuals[0]); - EXPECT_EQ(1.0, residuals[1]); - EXPECT_EQ(2.0, residuals[2]); + ASSERT_TRUE( + residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch)); + EXPECT_EQ(cost, 0.5 * (0 * 0 + 1 * 1 + 2 * 2)); + EXPECT_THAT(residuals, ElementsAre(0.0, 1.0, 2.0)); - EXPECT_EQ(expected_jacobian_rx, jacobian_rx); - EXPECT_TRUE((jacobian_ry.array() == -1.0).all()) << "\n" << jacobian_ry; - EXPECT_EQ(expected_jacobian_rz, jacobian_rz); + EXPECT_THAT(jacobian_rx, MatrixEq(expected_jacobian_rx)); + EXPECT_THAT(absl::MakeConstSpan(jacobian_ry), Each(-1.0)); + EXPECT_THAT(jacobian_rz, MatrixEq(expected_jacobian_rz)); } } // namespace ceres::internal
diff --git a/internal/ceres/tiny_solver_autodiff_function_test.cc b/internal/ceres/tiny_solver_autodiff_function_test.cc index 546b49c..e29eccb 100644 --- a/internal/ceres/tiny_solver_autodiff_function_test.cc +++ b/internal/ceres/tiny_solver_autodiff_function_test.cc
@@ -34,12 +34,17 @@ #include <limits> #include "Eigen/Core" +#include "ceres/test_util.h" #include "ceres/tiny_solver.h" #include "ceres/tiny_solver_test_util.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres { +using internal::MatrixPointwise; +using ::testing::DoubleNear; + struct AutoDiffTestFunctor { template <typename T> bool operator()(const T* const parameters, T* residuals) const { @@ -69,29 +74,30 @@ // Check the case with cost-only evaluation. residuals.setConstant(555); // Arbitrary. - EXPECT_TRUE(f(&x(0), &residuals(0), nullptr)); - EXPECT_NEAR(3.0, residuals(0), kTolerance); - EXPECT_NEAR(2.0, residuals(1), kTolerance); + ASSERT_TRUE(f(&x(0), &residuals(0), nullptr)); + EXPECT_THAT( + residuals, + MatrixPointwise(DoubleNear(kTolerance), Eigen::Vector2d(3.0, 2.0))); // Check the case with cost and Jacobian evaluation. Eigen::Matrix<double, 2, 3> jacobian; residuals.setConstant(555); // Arbitrary. jacobian.setConstant(555); - EXPECT_TRUE(f(&x(0), &residuals(0), &jacobian(0, 0))); + ASSERT_TRUE(f(&x(0), &residuals(0), &jacobian(0, 0))); // Verify cost. - EXPECT_NEAR(3.0, residuals(0), kTolerance); - EXPECT_NEAR(2.0, residuals(1), kTolerance); + EXPECT_THAT( + residuals, + MatrixPointwise(DoubleNear(kTolerance), Eigen::Vector2d(3.0, 2.0))); - // Verify Jacobian Row 1. - EXPECT_NEAR(2.0, jacobian(0, 0), kTolerance); - EXPECT_NEAR(0.0, jacobian(0, 1), kTolerance); - EXPECT_NEAR(1.0, jacobian(0, 2), kTolerance); - - // Verify Jacobian row 2. - EXPECT_NEAR(0.0, jacobian(1, 0), kTolerance); - EXPECT_NEAR(4.0, jacobian(1, 1), kTolerance); - EXPECT_NEAR(6.0, jacobian(1, 2), kTolerance); + // Verify Jacobian. + Eigen::Matrix<double, 2, 3> expected_jacobian; + // clang-format off + expected_jacobian << 2.0, 0.0, 1.0, + 0.0, 4.0, 6.0; + // clang-format on + EXPECT_THAT(jacobian, + MatrixPointwise(DoubleNear(kTolerance), expected_jacobian)); } class DynamicResidualsFunctor {
diff --git a/internal/ceres/tiny_solver_cost_function_adapter_test.cc b/internal/ceres/tiny_solver_cost_function_adapter_test.cc index 1bf1ee7..b61aa35 100644 --- a/internal/ceres/tiny_solver_cost_function_adapter_test.cc +++ b/internal/ceres/tiny_solver_cost_function_adapter_test.cc
@@ -36,10 +36,14 @@ #include "Eigen/Core" #include "ceres/cost_function.h" #include "ceres/sized_cost_function.h" +#include "ceres/test_util.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres { +using internal::RelativeNormNear; + class CostFunction2x3 : public SizedCostFunction<2, 3> { bool Evaluate(double const* const* parameters, double* residuals, @@ -74,13 +78,14 @@ TinySolverCostFunctionAdapter<kNumResiduals, kNumParameters, kMaxResiduals, kMaxParameters>; CostFunctionAdapter cfa(*cost_function); - EXPECT_EQ(CostFunctionAdapter::NUM_RESIDUALS, kNumResiduals); - EXPECT_EQ(CostFunctionAdapter::NUM_PARAMETERS, kNumParameters); + static_assert(CostFunctionAdapter::NUM_RESIDUALS == kNumResiduals); + static_assert(CostFunctionAdapter::NUM_PARAMETERS == kNumParameters); EXPECT_EQ(cfa.NumResiduals(), 2); EXPECT_EQ(cfa.NumParameters(), 3); - Eigen::Matrix<double, 2, 1> actual_residuals, expected_residuals; + Eigen::Matrix<double, 2, 1> actual_residuals; + Eigen::Matrix<double, 2, 1> expected_residuals; Eigen::Matrix<double, 2, 3, Eigen::ColMajor> actual_jacobian; Eigen::Matrix<double, 2, 3, Eigen::RowMajor> expected_jacobian; @@ -88,33 +93,26 @@ double* parameters[1] = {xyz}; // Check that residual only evaluation works. - cost_function->Evaluate(parameters, expected_residuals.data(), nullptr); + ASSERT_TRUE( + cost_function->Evaluate(parameters, expected_residuals.data(), nullptr)); cfa(xyz, actual_residuals.data(), nullptr); - EXPECT_NEAR( - (expected_residuals - actual_residuals).norm() / actual_residuals.norm(), - 0.0, - std::numeric_limits<double>::epsilon()) - << "\nExpected residuals: " << expected_residuals.transpose() - << "\nActual residuals: " << actual_residuals.transpose(); + EXPECT_THAT(actual_residuals, + RelativeNormNear(expected_residuals, + std::numeric_limits<double>::epsilon())); // Check that residual and jacobian evaluation works. double* jacobians[1] = {expected_jacobian.data()}; - cost_function->Evaluate(parameters, expected_residuals.data(), jacobians); + ASSERT_TRUE(cost_function->Evaluate( + parameters, expected_residuals.data(), jacobians)); cfa(xyz, actual_residuals.data(), actual_jacobian.data()); - EXPECT_NEAR( - (expected_residuals - actual_residuals).norm() / actual_residuals.norm(), - 0.0, - std::numeric_limits<double>::epsilon()) - << "\nExpected residuals: " << expected_residuals.transpose() - << "\nActual residuals: " << actual_residuals.transpose(); + EXPECT_THAT(actual_residuals, + RelativeNormNear(expected_residuals, + std::numeric_limits<double>::epsilon())); - EXPECT_NEAR( - (expected_jacobian - actual_jacobian).norm() / actual_jacobian.norm(), - 0.0, - std::numeric_limits<double>::epsilon()) - << "\nExpected jacobian: " << expected_jacobian.transpose() - << "\nActual jacobian: " << actual_jacobian.transpose(); + EXPECT_THAT(actual_jacobian, + RelativeNormNear(expected_jacobian, + std::numeric_limits<double>::epsilon())); } TEST(TinySolverCostFunctionAdapter, StaticResidualsStaticParameterBlock) {
diff --git a/internal/ceres/trust_region_minimizer_test.cc b/internal/ceres/trust_region_minimizer_test.cc index 33a8f42..b5a071a 100644 --- a/internal/ceres/trust_region_minimizer_test.cc +++ b/internal/ceres/trust_region_minimizer_test.cc
@@ -51,10 +51,14 @@ #include "ceres/minimizer.h" #include "ceres/problem.h" #include "ceres/trust_region_strategy.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace ceres::internal { +using ::testing::DoubleNear; +using ::testing::Each; + // Templated Evaluator for Powell's function. The template parameters // indicate which of the four variables/columns of the jacobian are // active. This is equivalent to constructing a problem and using the @@ -260,10 +264,7 @@ minimizer.Minimize(minimizer_options, parameters, &summary); // The minimum is at x1 = x2 = x3 = x4 = 0. - EXPECT_NEAR(0.0, parameters[0], 0.001); - EXPECT_NEAR(0.0, parameters[1], 0.001); - EXPECT_NEAR(0.0, parameters[2], 0.001); - EXPECT_NEAR(0.0, parameters[3], 0.001); + EXPECT_THAT(parameters, Each(DoubleNear(0.0, 0.001))); } TEST(TrustRegionMinimizer, PowellsSingularFunctionUsingLevenbergMarquardt) {