Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GTestUtils.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 // gtest includes
11 #include <gtest/gtest.h>
12 
13 struct CompareFloats {
14 public:
15  double tol_a, tol_r;
16 
17  CompareFloats(double tol_a_, double tol_r_) : tol_a(tol_a_), tol_r(tol_r_) {}
18 
19  template <typename ScalarType>
20  bool operator() (const ScalarType& a, const ScalarType& b) {
21  return std::abs(a-b) < tol_a + tol_r*std::abs(a);
22  }
23 };
24 
25 struct CompareFads {
26 public:
28 
29  CompareFads(double tol_a, double tol_r) : cmp(tol_a, tol_r) {}
30 
31  template <typename FadType1, typename FadType2>
32  bool operator() (const FadType1& a, const FadType2& b)
33  {
34  if (a.size() != b.size()) return false;
35  if (a.hasFastAccess() != b.hasFastAccess()) return false;
36  if (!cmp(a.val(), b.val())) return false;
37  for (int i=0; i<a.size(); ++i) {
38  if (!cmp(a.dx(i), b.dx(i))) return false;
39  if (!cmp(a.fastAccessDx(i), b.fastAccessDx(i))) return false;
40  }
41  return true;
42  }
43 
44  template <typename FadType1, typename FadType2>
45  ::testing::AssertionResult operator() (const char* a_expr, const char* b_expr,
46  const FadType1& a, const FadType2& b)
47  {
48  bool success = (*this)(a,b);
51  << "Fad's do not match!" << std::endl
52  << a_expr << " = " << a << std::endl
53  << b_expr << " = " << b << std::endl;
54  }
55 };
56 
58 public:
60 
61  CompareNestedFads(double tol_a, double tol_r) : cmp(tol_a, tol_r) {}
62 
63  template <typename FadType1, typename FadType2>
64  bool operator() (const FadType1& a, const FadType2& b)
65  {
66  if (a.size() != b.size()) return false;
67  if (a.hasFastAccess() != b.hasFastAccess()) return false;
68  if (!cmp(a.val(), b.val())) return false;
69  for (int i=0; i<a.size(); ++i) {
70  if (!cmp(a.dx(i), b.dx(i))) return false;
71  if (!cmp(a.fastAccessDx(i), b.fastAccessDx(i))) return false;
72  }
73  return true;
74  }
75 
76  template <typename FadType1, typename FadType2>
77  ::testing::AssertionResult operator() (const char* a_expr, const char* b_expr,
78  const FadType1& a, const FadType2& b)
79  {
80  bool success = (*this)(a,b);
83  << "Fad's do not match!" << std::endl
84  << a_expr << " = " << a << std::endl
85  << b_expr << " = " << b << std::endl;
86  }
87 };
88 
89 #define COMPARE_VALUES(a, b) \
90  ASSERT_PRED2(CompareFloats(this->tol_a, this->tol_r), a, b);
91 
92 #define COMPARE_FADS(a, b) \
93  ASSERT_PRED_FORMAT2(CompareFads(this->tol_a, this->tol_r), a, b);
94 
95 #define COMPARE_NESTED_FADS(a, b) \
96  ASSERT_PRED_FORMAT2(CompareNestedFads(this->tol_a, this->tol_r), a, b);
bool operator()(const FadType1 &a, const FadType2 &b)
Definition: GTestUtils.hpp:64
AssertionResult AssertionFailure()
Definition: gtest.cc:1200
abs(expr.val())
bool operator()(const ScalarType &a, const ScalarType &b)
Definition: GTestUtils.hpp:20
CompareFads cmp
Definition: GTestUtils.hpp:59
bool operator()(const FadType1 &a, const FadType2 &b)
Definition: GTestUtils.hpp:32
AssertionResult AssertionSuccess()
Definition: gtest.cc:1195
CompareFads(double tol_a, double tol_r)
Definition: GTestUtils.hpp:29
CompareFloats cmp
Definition: GTestUtils.hpp:27
CompareNestedFads(double tol_a, double tol_r)
Definition: GTestUtils.hpp:61
CompareFloats(double tol_a_, double tol_r_)
Definition: GTestUtils.hpp:17