blob: c33c69cd09a52372f4825e6ba69c5189f83471db [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// 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
31#ifndef CERES_INTERNAL_TEST_UTIL_H_
32#define CERES_INTERNAL_TEST_UTIL_H_
33
34#include <string>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080035
Austin Schuh70cc9552019-01-21 19:46:48 -080036#include "ceres/internal/port.h"
37#include "ceres/problem.h"
38#include "ceres/solver.h"
39#include "ceres/stringprintf.h"
40#include "gtest/gtest.h"
41
42namespace ceres {
43namespace internal {
44
45// Expects that x and y have a relative difference of no more than
46// max_abs_relative_difference. If either x or y is zero, then the relative
47// difference is interpreted as an absolute difference.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080048//
49// If x and y have the same non-finite value (inf or nan) we treat them as being
50// close. In such a case no error is thrown and true is returned.
51CERES_EXPORT_INTERNAL bool ExpectClose(double x,
52 double y,
53 double max_abs_relative_difference);
Austin Schuh70cc9552019-01-21 19:46:48 -080054
55// Expects that for all i = 1,.., n - 1
56//
57// |p[i] - q[i]| / max(|p[i]|, |q[i]|) < tolerance
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080058CERES_EXPORT_INTERNAL void ExpectArraysClose(int n,
59 const double* p,
60 const double* q,
61 double tolerance);
Austin Schuh70cc9552019-01-21 19:46:48 -080062
63// Expects that for all i = 1,.., n - 1
64//
65// |p[i] / max_norm_p - q[i] / max_norm_q| < tolerance
66//
67// where max_norm_p and max_norm_q are the max norms of the arrays p
68// and q respectively.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080069CERES_EXPORT_INTERNAL void ExpectArraysCloseUptoScale(int n,
70 const double* p,
71 const double* q,
72 double tolerance);
Austin Schuh70cc9552019-01-21 19:46:48 -080073
74// Construct a fully qualified path for the test file depending on the
75// local build/testing environment.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080076CERES_EXPORT_INTERNAL std::string TestFileAbsolutePath(
77 const std::string& filename);
Austin Schuh70cc9552019-01-21 19:46:48 -080078
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080079CERES_EXPORT_INTERNAL std::string ToString(const Solver::Options& options);
Austin Schuh70cc9552019-01-21 19:46:48 -080080
81// A templated test fixture, that is used for testing Ceres end to end
82// by computing a solution to the problem for a given solver
83// configuration and comparing it to a reference solver configuration.
84//
85// It is assumed that the SystemTestProblem has an Solver::Options
86// struct that contains the reference Solver configuration.
87template <typename SystemTestProblem>
88class SystemTest : public ::testing::Test {
89 protected:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080090 void SetUp() final {
Austin Schuh70cc9552019-01-21 19:46:48 -080091 SystemTestProblem system_test_problem;
92 SolveAndEvaluateFinalResiduals(
93 *system_test_problem.mutable_solver_options(),
94 system_test_problem.mutable_problem(),
95 &expected_final_residuals_);
96 }
97
98 void RunSolverForConfigAndExpectResidualsMatch(const Solver::Options& options,
99 Problem* problem) {
100 std::vector<double> final_residuals;
101 SolveAndEvaluateFinalResiduals(options, problem, &final_residuals);
102
103 // We compare solutions by comparing their residual vectors. We do
104 // not compare parameter vectors because it is much more brittle
105 // and error prone to do so, since the same problem can have
106 // nearly the same residuals at two completely different positions
107 // in parameter space.
108 CHECK_EQ(expected_final_residuals_.size(), final_residuals.size());
109 for (int i = 0; i < final_residuals.size(); ++i) {
110 EXPECT_NEAR(final_residuals[i],
111 expected_final_residuals_[i],
112 SystemTestProblem::kResidualTolerance)
113 << "Not close enough residual:" << i;
114 }
115 }
116
117 void SolveAndEvaluateFinalResiduals(const Solver::Options& options,
118 Problem* problem,
119 std::vector<double>* final_residuals) {
120 Solver::Summary summary;
121 Solve(options, problem, &summary);
122 CHECK_NE(summary.termination_type, ceres::FAILURE);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800123 problem->Evaluate(
124 Problem::EvaluateOptions(), nullptr, final_residuals, nullptr, nullptr);
Austin Schuh70cc9552019-01-21 19:46:48 -0800125 }
126
127 std::vector<double> expected_final_residuals_;
128};
129
130} // namespace internal
131} // namespace ceres
132
133#endif // CERES_INTERNAL_TEST_UTIL_H_