blob: e43eb35a0005b23ad1fc1efae01374330f18aa5a [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>
35#include "ceres/internal/port.h"
36#include "ceres/problem.h"
37#include "ceres/solver.h"
38#include "ceres/stringprintf.h"
39#include "gtest/gtest.h"
40
41namespace ceres {
42namespace internal {
43
44// Expects that x and y have a relative difference of no more than
45// max_abs_relative_difference. If either x or y is zero, then the relative
46// difference is interpreted as an absolute difference.
47bool ExpectClose(double x, double y, double max_abs_relative_difference);
48
49// Expects that for all i = 1,.., n - 1
50//
51// |p[i] - q[i]| / max(|p[i]|, |q[i]|) < tolerance
52void ExpectArraysClose(int n,
53 const double* p,
54 const double* q,
55 double tolerance);
56
57// Expects that for all i = 1,.., n - 1
58//
59// |p[i] / max_norm_p - q[i] / max_norm_q| < tolerance
60//
61// where max_norm_p and max_norm_q are the max norms of the arrays p
62// and q respectively.
63void ExpectArraysCloseUptoScale(int n,
64 const double* p,
65 const double* q,
66 double tolerance);
67
68// Construct a fully qualified path for the test file depending on the
69// local build/testing environment.
70std::string TestFileAbsolutePath(const std::string& filename);
71
72std::string ToString(const Solver::Options& options);
73
74// A templated test fixture, that is used for testing Ceres end to end
75// by computing a solution to the problem for a given solver
76// configuration and comparing it to a reference solver configuration.
77//
78// It is assumed that the SystemTestProblem has an Solver::Options
79// struct that contains the reference Solver configuration.
80template <typename SystemTestProblem>
81class SystemTest : public ::testing::Test {
82 protected:
83 virtual void SetUp() {
84 SystemTestProblem system_test_problem;
85 SolveAndEvaluateFinalResiduals(
86 *system_test_problem.mutable_solver_options(),
87 system_test_problem.mutable_problem(),
88 &expected_final_residuals_);
89 }
90
91 void RunSolverForConfigAndExpectResidualsMatch(const Solver::Options& options,
92 Problem* problem) {
93 std::vector<double> final_residuals;
94 SolveAndEvaluateFinalResiduals(options, problem, &final_residuals);
95
96 // We compare solutions by comparing their residual vectors. We do
97 // not compare parameter vectors because it is much more brittle
98 // and error prone to do so, since the same problem can have
99 // nearly the same residuals at two completely different positions
100 // in parameter space.
101 CHECK_EQ(expected_final_residuals_.size(), final_residuals.size());
102 for (int i = 0; i < final_residuals.size(); ++i) {
103 EXPECT_NEAR(final_residuals[i],
104 expected_final_residuals_[i],
105 SystemTestProblem::kResidualTolerance)
106 << "Not close enough residual:" << i;
107 }
108 }
109
110 void SolveAndEvaluateFinalResiduals(const Solver::Options& options,
111 Problem* problem,
112 std::vector<double>* final_residuals) {
113 Solver::Summary summary;
114 Solve(options, problem, &summary);
115 CHECK_NE(summary.termination_type, ceres::FAILURE);
116 problem->Evaluate(Problem::EvaluateOptions(),
117 nullptr,
118 final_residuals,
119 nullptr,
120 nullptr);
121 }
122
123 std::vector<double> expected_final_residuals_;
124};
125
126} // namespace internal
127} // namespace ceres
128
129#endif // CERES_INTERNAL_TEST_UTIL_H_