test_util.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: keir@google.com (Keir Mierle)
  30. #ifndef CERES_INTERNAL_TEST_UTIL_H_
  31. #define CERES_INTERNAL_TEST_UTIL_H_
  32. #include <string>
  33. #include "ceres/internal/disable_warnings.h"
  34. #include "ceres/internal/export.h"
  35. #include "ceres/problem.h"
  36. #include "ceres/solver.h"
  37. #include "ceres/stringprintf.h"
  38. #include "gtest/gtest.h"
  39. namespace ceres {
  40. namespace internal {
  41. // Expects that x and y have a relative difference of no more than
  42. // max_abs_relative_difference. If either x or y is zero, then the relative
  43. // difference is interpreted as an absolute difference.
  44. // If x and y have the same non-finite value (inf or nan) we treat them as being
  45. // close. In such a case no error is thrown and true is returned.
  46. CERES_NO_EXPORT bool ExpectClose(double x,
  47. double y,
  48. double max_abs_relative_difference);
  49. // Expects that for all i = 1,.., n - 1
  50. //
  51. // |p[i] - q[i]| / max(|p[i]|, |q[i]|) < tolerance
  52. CERES_NO_EXPORT void ExpectArraysClose(int n,
  53. const double* p,
  54. const double* q,
  55. double tolerance);
  56. // Expects that for all i = 1,.., n - 1
  57. //
  58. // |p[i] / max_norm_p - q[i] / max_norm_q| < tolerance
  59. //
  60. // where max_norm_p and max_norm_q are the max norms of the arrays p
  61. // and q respectively.
  62. CERES_NO_EXPORT void ExpectArraysCloseUptoScale(int n,
  63. const double* p,
  64. const double* q,
  65. double tolerance);
  66. // Construct a fully qualified path for the test file depending on the
  67. // local build/testing environment.
  68. CERES_NO_EXPORT std::string TestFileAbsolutePath(const std::string& filename);
  69. CERES_NO_EXPORT std::string ToString(const Solver::Options& options);
  70. // A templated test fixture, that is used for testing Ceres end to end
  71. // by computing a solution to the problem for a given solver
  72. // configuration and comparing it to a reference solver configuration.
  73. //
  74. // It is assumed that the SystemTestProblem has an Solver::Options
  75. // struct that contains the reference Solver configuration.
  76. template <typename SystemTestProblem>
  77. class CERES_NO_EXPORT SystemTest : public ::testing::Test {
  78. protected:
  79. void SetUp() final {
  80. SystemTestProblem system_test_problem;
  81. SolveAndEvaluateFinalResiduals(
  82. *system_test_problem.mutable_solver_options(),
  83. system_test_problem.mutable_problem(),
  84. &expected_final_residuals_);
  85. }
  86. void RunSolverForConfigAndExpectResidualsMatch(const Solver::Options& options,
  87. Problem* problem) {
  88. std::vector<double> final_residuals;
  89. SolveAndEvaluateFinalResiduals(options, problem, &final_residuals);
  90. // We compare solutions by comparing their residual vectors. We do
  91. // not compare parameter vectors because it is much more brittle
  92. // and error prone to do so, since the same problem can have
  93. // nearly the same residuals at two completely different positions
  94. // in parameter space.
  95. CHECK_EQ(expected_final_residuals_.size(), final_residuals.size());
  96. for (int i = 0; i < final_residuals.size(); ++i) {
  97. EXPECT_NEAR(final_residuals[i],
  98. expected_final_residuals_[i],
  99. SystemTestProblem::kResidualTolerance)
  100. << "Not close enough residual:" << i;
  101. }
  102. }
  103. void SolveAndEvaluateFinalResiduals(const Solver::Options& options,
  104. Problem* problem,
  105. std::vector<double>* final_residuals) {
  106. Solver::Summary summary;
  107. Solve(options, problem, &summary);
  108. CHECK_NE(summary.termination_type, ceres::FAILURE);
  109. problem->Evaluate(
  110. Problem::EvaluateOptions(), nullptr, final_residuals, nullptr, nullptr);
  111. }
  112. std::vector<double> expected_final_residuals_;
  113. };
  114. } // namespace internal
  115. } // namespace ceres
  116. #include "ceres/internal/reenable_warnings.h"
  117. #endif // CERES_INTERNAL_TEST_UTIL_H_