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