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 //
31 // Utility functions useful for testing.
32
33 #include "ceres/test_util.h"
34
35 #include <algorithm>
36 #include <cmath>
37
38 #include "ceres/file.h"
39 #include "ceres/stringprintf.h"
40 #include "ceres/types.h"
41 #include "gflags/gflags.h"
42 #include "glog/logging.h"
43 #include "gtest/gtest.h"
44
45 DECLARE_string(test_srcdir);
46
47 // This macro is used to inject additional path information specific
48 // to the build system.
49
50 #ifndef CERES_TEST_SRCDIR_SUFFIX
51 #define CERES_TEST_SRCDIR_SUFFIX ""
52 #endif
53
54 namespace ceres {
55 namespace internal {
56
ExpectClose(double x,double y,double max_abs_relative_difference)57 bool ExpectClose(double x, double y, double max_abs_relative_difference) {
58 if (std::isinf(x) && std::isinf(y)) {
59 EXPECT_EQ(std::signbit(x), std::signbit(y));
60 return true;
61 }
62
63 if (std::isnan(x) && std::isnan(y)) {
64 return true;
65 }
66
67 double absolute_difference = fabs(x - y);
68 double relative_difference = absolute_difference / std::max(fabs(x), fabs(y));
69 if (x == 0 || y == 0) {
70 // If x or y is exactly zero, then relative difference doesn't have any
71 // meaning. Take the absolute difference instead.
72 relative_difference = absolute_difference;
73 }
74 if (relative_difference > max_abs_relative_difference) {
75 VLOG(1) << StringPrintf("x=%17g y=%17g abs=%17g rel=%17g",
76 x,
77 y,
78 absolute_difference,
79 relative_difference);
80 }
81
82 EXPECT_NEAR(relative_difference, 0.0, max_abs_relative_difference);
83 return relative_difference <= max_abs_relative_difference;
84 }
85
ExpectArraysCloseUptoScale(int n,const double * p,const double * q,double tol)86 void ExpectArraysCloseUptoScale(int n,
87 const double* p,
88 const double* q,
89 double tol) {
90 CHECK_GT(n, 0);
91 CHECK(p);
92 CHECK(q);
93
94 double p_max = 0;
95 double q_max = 0;
96 int p_i = 0;
97 int q_i = 0;
98
99 for (int i = 0; i < n; ++i) {
100 if (std::abs(p[i]) > p_max) {
101 p_max = std::abs(p[i]);
102 p_i = i;
103 }
104 if (std::abs(q[i]) > q_max) {
105 q_max = std::abs(q[i]);
106 q_i = i;
107 }
108 }
109
110 // If both arrays are all zeros, they are equal up to scale, but
111 // for testing purposes, that's more likely to be an error than
112 // a desired result.
113 CHECK_NE(p_max, 0.0);
114 CHECK_NE(q_max, 0.0);
115
116 for (int i = 0; i < n; ++i) {
117 double p_norm = p[i] / p[p_i];
118 double q_norm = q[i] / q[q_i];
119
120 EXPECT_NEAR(p_norm, q_norm, tol) << "i=" << i;
121 }
122 }
123
ExpectArraysClose(int n,const double * p,const double * q,double tol)124 void ExpectArraysClose(int n, const double* p, const double* q, double tol) {
125 CHECK_GT(n, 0);
126 CHECK(p);
127 CHECK(q);
128
129 for (int i = 0; i < n; ++i) {
130 EXPECT_TRUE(ExpectClose(p[i], q[i], tol)) << "p[" << i << "]" << p[i] << " "
131 << "q[" << i << "]" << q[i] << " "
132 << "tol: " << tol;
133 }
134 }
135
TestFileAbsolutePath(const std::string & filename)136 std::string TestFileAbsolutePath(const std::string& filename) {
137 return JoinPath(FLAGS_test_srcdir + CERES_TEST_SRCDIR_SUFFIX, filename);
138 }
139
ToString(const Solver::Options & options)140 std::string ToString(const Solver::Options& options) {
141 return StringPrintf("(%s, %s, %s, %s, %d)",
142 LinearSolverTypeToString(options.linear_solver_type),
143 SparseLinearAlgebraLibraryTypeToString(
144 options.sparse_linear_algebra_library_type),
145 options.linear_solver_ordering ? "USER" : "AUTOMATIC",
146 PreconditionerTypeToString(options.preconditioner_type),
147 options.num_threads);
148 }
149
150 } // namespace internal
151 } // namespace ceres
152