blob: 95aaa55579fbece1d53cf904fe0437bd79a3f877 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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 Schuh3de38b02024-06-25 18:25:10 -070036#include "ceres/internal/disable_warnings.h"
37#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080038#include "ceres/problem.h"
39#include "ceres/solver.h"
40#include "ceres/stringprintf.h"
41#include "gtest/gtest.h"
42
43namespace ceres {
44namespace internal {
45
46// Expects that x and y have a relative difference of no more than
47// max_abs_relative_difference. If either x or y is zero, then the relative
48// difference is interpreted as an absolute difference.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080049// 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.
Austin Schuh3de38b02024-06-25 18:25:10 -070051CERES_NO_EXPORT 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 Schuh3de38b02024-06-25 18:25:10 -070058CERES_NO_EXPORT 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 Schuh3de38b02024-06-25 18:25:10 -070069CERES_NO_EXPORT 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 Schuh3de38b02024-06-25 18:25:10 -070076CERES_NO_EXPORT std::string TestFileAbsolutePath(const std::string& filename);
Austin Schuh70cc9552019-01-21 19:46:48 -080077
Austin Schuh3de38b02024-06-25 18:25:10 -070078CERES_NO_EXPORT std::string ToString(const Solver::Options& options);
Austin Schuh70cc9552019-01-21 19:46:48 -080079
80// A templated test fixture, that is used for testing Ceres end to end
81// by computing a solution to the problem for a given solver
82// configuration and comparing it to a reference solver configuration.
83//
84// It is assumed that the SystemTestProblem has an Solver::Options
85// struct that contains the reference Solver configuration.
86template <typename SystemTestProblem>
Austin Schuh3de38b02024-06-25 18:25:10 -070087class CERES_NO_EXPORT SystemTest : public ::testing::Test {
Austin Schuh70cc9552019-01-21 19:46:48 -080088 protected:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080089 void SetUp() final {
Austin Schuh70cc9552019-01-21 19:46:48 -080090 SystemTestProblem system_test_problem;
91 SolveAndEvaluateFinalResiduals(
92 *system_test_problem.mutable_solver_options(),
93 system_test_problem.mutable_problem(),
94 &expected_final_residuals_);
95 }
96
97 void RunSolverForConfigAndExpectResidualsMatch(const Solver::Options& options,
98 Problem* problem) {
99 std::vector<double> final_residuals;
100 SolveAndEvaluateFinalResiduals(options, problem, &final_residuals);
101
102 // We compare solutions by comparing their residual vectors. We do
103 // not compare parameter vectors because it is much more brittle
104 // and error prone to do so, since the same problem can have
105 // nearly the same residuals at two completely different positions
106 // in parameter space.
107 CHECK_EQ(expected_final_residuals_.size(), final_residuals.size());
108 for (int i = 0; i < final_residuals.size(); ++i) {
109 EXPECT_NEAR(final_residuals[i],
110 expected_final_residuals_[i],
111 SystemTestProblem::kResidualTolerance)
112 << "Not close enough residual:" << i;
113 }
114 }
115
116 void SolveAndEvaluateFinalResiduals(const Solver::Options& options,
117 Problem* problem,
118 std::vector<double>* final_residuals) {
119 Solver::Summary summary;
120 Solve(options, problem, &summary);
121 CHECK_NE(summary.termination_type, ceres::FAILURE);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800122 problem->Evaluate(
123 Problem::EvaluateOptions(), nullptr, final_residuals, nullptr, nullptr);
Austin Schuh70cc9552019-01-21 19:46:48 -0800124 }
125
126 std::vector<double> expected_final_residuals_;
127};
128
129} // namespace internal
130} // namespace ceres
131
Austin Schuh3de38b02024-06-25 18:25:10 -0700132#include "ceres/internal/reenable_warnings.h"
133
Austin Schuh70cc9552019-01-21 19:46:48 -0800134#endif // CERES_INTERNAL_TEST_UTIL_H_