1// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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 <algorithm>
34#include <cmath>
35#include "ceres/file.h"
36#include "ceres/stringprintf.h"
37#include "gflags/gflags.h"
38#include "glog/logging.h"
39#include "gtest/gtest.h"
40
41DECLARE_string(test_srcdir);
42
43// This macro is used to inject additional path information specific
44// to the build system.
45
46#ifndef CERES_TEST_SRCDIR_SUFFIX
47#define CERES_TEST_SRCDIR_SUFFIX ""
48#endif
49
50namespace ceres {
51namespace internal {
52
53bool ExpectClose(double x, double y, double max_abs_relative_difference) {
54  double absolute_difference = fabs(x - y);
55  double relative_difference = absolute_difference / std::max(fabs(x), fabs(y));
56  if (x == 0 || y == 0) {
57    // If x or y is exactly zero, then relative difference doesn't have any
58    // meaning. Take the absolute difference instead.
59    relative_difference = absolute_difference;
60  }
61  if (relative_difference > max_abs_relative_difference) {
62    VLOG(1) << StringPrintf("x=%17g y=%17g abs=%17g rel=%17g",
63                            x, y, absolute_difference, relative_difference);
64  }
65
66  EXPECT_NEAR(relative_difference, 0.0, max_abs_relative_difference);
67  return relative_difference <= max_abs_relative_difference;
68}
69
70void ExpectArraysCloseUptoScale(int n,
71                                const double* p,
72                                const double* q,
73                                double tol) {
74  CHECK_GT(n, 0);
75  CHECK(p);
76  CHECK(q);
77
78  double p_max = 0;
79  double q_max = 0;
80  int p_i = 0;
81  int q_i = 0;
82
83  for (int i = 0; i < n; ++i) {
84    if (std::abs(p[i]) > p_max) {
85      p_max = std::abs(p[i]);
86      p_i = i;
87    }
88    if (std::abs(q[i]) > q_max) {
89      q_max = std::abs(q[i]);
90      q_i = i;
91    }
92  }
93
94  // If both arrays are all zeros, they are equal up to scale, but
95  // for testing purposes, that's more likely to be an error than
96  // a desired result.
97  CHECK_NE(p_max, 0.0);
98  CHECK_NE(q_max, 0.0);
99
100  for (int i = 0; i < n; ++i) {
101    double p_norm = p[i] / p[p_i];
102    double q_norm = q[i] / q[q_i];
103
104    EXPECT_NEAR(p_norm, q_norm, tol) << "i=" << i;
105  }
106}
107
108void ExpectArraysClose(int n,
109                       const double* p,
110                       const double* q,
111                       double tol) {
112  CHECK_GT(n, 0);
113  CHECK(p);
114  CHECK(q);
115
116  for (int i = 0; i < n; ++i) {
117    EXPECT_NEAR(p[i], q[i], tol) << "i=" << i;
118  }
119}
120
121string TestFileAbsolutePath(const string& filename) {
122  return JoinPath(FLAGS_test_srcdir + CERES_TEST_SRCDIR_SUFFIX,
123                  filename);
124}
125
126
127}  // namespace internal
128}  // namespace ceres
129