Sacado Package Browser (Single Doxygen Collection)  Version of the Day
GTestUtils.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 // gtest includes
31 #include <gtest/gtest.h>
32 
33 struct CompareFloats {
34 public:
35  double tol_a, tol_r;
36 
37  CompareFloats(double tol_a_, double tol_r_) : tol_a(tol_a_), tol_r(tol_r_) {}
38 
39  template <typename ScalarType>
40  bool operator() (const ScalarType& a, const ScalarType& b) {
41  return std::abs(a-b) < tol_a + tol_r*std::abs(a);
42  }
43 };
44 
45 struct CompareFads {
46 public:
48 
49  CompareFads(double tol_a, double tol_r) : cmp(tol_a, tol_r) {}
50 
51  template <typename FadType1, typename FadType2>
52  bool operator() (const FadType1& a, const FadType2& b)
53  {
54  if (a.size() != b.size()) return false;
55  if (a.hasFastAccess() != b.hasFastAccess()) return false;
56  if (!cmp(a.val(), b.val())) return false;
57  for (int i=0; i<a.size(); ++i) {
58  if (!cmp(a.dx(i), b.dx(i))) return false;
59  if (!cmp(a.fastAccessDx(i), b.fastAccessDx(i))) return false;
60  }
61  return true;
62  }
63 
64  template <typename FadType1, typename FadType2>
65  ::testing::AssertionResult operator() (const char* a_expr, const char* b_expr,
66  const FadType1& a, const FadType2& b)
67  {
68  bool success = (*this)(a,b);
71  << "Fad's do not match!" << std::endl
72  << a_expr << " = " << a << std::endl
73  << b_expr << " = " << b << std::endl;
74  }
75 };
76 
78 public:
80 
81  CompareNestedFads(double tol_a, double tol_r) : cmp(tol_a, tol_r) {}
82 
83  template <typename FadType1, typename FadType2>
84  bool operator() (const FadType1& a, const FadType2& b)
85  {
86  if (a.size() != b.size()) return false;
87  if (a.hasFastAccess() != b.hasFastAccess()) return false;
88  if (!cmp(a.val(), b.val())) return false;
89  for (int i=0; i<a.size(); ++i) {
90  if (!cmp(a.dx(i), b.dx(i))) return false;
91  if (!cmp(a.fastAccessDx(i), b.fastAccessDx(i))) return false;
92  }
93  return true;
94  }
95 
96  template <typename FadType1, typename FadType2>
97  ::testing::AssertionResult operator() (const char* a_expr, const char* b_expr,
98  const FadType1& a, const FadType2& b)
99  {
100  bool success = (*this)(a,b);
101  if (success) return ::testing::AssertionSuccess();
103  << "Fad's do not match!" << std::endl
104  << a_expr << " = " << a << std::endl
105  << b_expr << " = " << b << std::endl;
106  }
107 };
108 
109 #define COMPARE_VALUES(a, b) \
110  ASSERT_PRED2(CompareFloats(this->tol_a, this->tol_r), a, b);
111 
112 #define COMPARE_FADS(a, b) \
113  ASSERT_PRED_FORMAT2(CompareFads(this->tol_a, this->tol_r), a, b);
114 
115 #define COMPARE_NESTED_FADS(a, b) \
116  ASSERT_PRED_FORMAT2(CompareNestedFads(this->tol_a, this->tol_r), a, b);
bool operator()(const FadType1 &a, const FadType2 &b)
Definition: GTestUtils.hpp:84
AssertionResult AssertionFailure()
Definition: gtest.cc:1200
abs(expr.val())
bool operator()(const ScalarType &a, const ScalarType &b)
Definition: GTestUtils.hpp:40
CompareFads cmp
Definition: GTestUtils.hpp:79
bool operator()(const FadType1 &a, const FadType2 &b)
Definition: GTestUtils.hpp:52
AssertionResult AssertionSuccess()
Definition: gtest.cc:1195
CompareFads(double tol_a, double tol_r)
Definition: GTestUtils.hpp:49
CompareFloats cmp
Definition: GTestUtils.hpp:47
CompareNestedFads(double tol_a, double tol_r)
Definition: GTestUtils.hpp:81
CompareFloats(double tol_a_, double tol_r_)
Definition: GTestUtils.hpp:37