SST/macro
equality.h
Go to the documentation of this file.
1 #ifndef EQUALITY_H
2 #define EQUALITY_H
3 
4 #include <vector>
5 #include <cmath>
6 
7 template <class T>
8 inline bool arrays_equal(size_t n, const T* test, const T* right);
9 
10 
11 template <class T>
13 {
14 
15  public:
16  static bool equals(size_t n, const T* test, const T* right) {
17  return arrays_equal<T>(n, test, right);
18  }
19 
20  static bool equals(const T& test, const T& right) {
21  return test == right;
22  }
23 
24 };
25 
26 
27 
28 template <> class TestEquals<float>
29 {
30 
31  public:
32  static float cutoff;
33 
34  static bool equals(float test, float right) {
35  // do a relative difference - normalize for large numbers
36  return (fabs(test - right) / fabs(right)) < cutoff;
37  }
38 
39  static bool equals(size_t n, const float* test, const float* right) {
40  return arrays_equal<float>(n, test, right);
41  }
42 
43 };
44 
45 
46 
47 template <> class TestEquals<double>
48 {
49 
50  public:
51  static double cutoff;
52 
53  static bool equals(double test, double right) {
54  // do a relative difference - normalize for large numbers
55  return (fabs(test - right) / fabs(right)) < cutoff;
56  }
57 
58 };
59 
60 template <class T> class TestEquals< std::vector<T> >
61 {
62 
63  public:
64  static bool equals(const std::vector<T>& test, const std::vector<T>& asserted) {
65  if (test.size() != asserted.size()) {
66  return false;
67  }
68 
69  typename std::vector<T>::const_iterator it, end = test.end();
70  typename std::vector<T>::const_iterator ia = asserted.begin();
71  for (it=test.begin(); it != end; ++ia, ++it) {
72  if ( !(TestEquals<T>::equals(*it, *ia)) ) {
73  return false;
74  }
75  }
76  return true;
77  }
78 };
79 
80 template <class T>
81 inline bool arrays_equal(size_t n, const T* test, const T* right)
82 {
83  for (size_t i=0; i < n; ++i) {
84  if ( !(TestEquals<T>::equals(test[i], right[i])) ) {
85  return false;
86  }
87  }
88  return true;
89 }
90 
91 
92 
93 
94 #endif // EQUALITY_H
95 
static bool equals(size_t n, const float *test, const float *right)
Definition: equality.h:39
static bool equals(size_t n, const T *test, const T *right)
Definition: equality.h:16
bool arrays_equal(size_t n, const T *test, const T *right)
Definition: equality.h:81
Variable< T > fabs(const Variable< T > &t)
Definition: variable.h:246
static double cutoff
Definition: equality.h:51
static bool equals(float test, float right)
Definition: equality.h:34
static bool equals(double test, double right)
Definition: equality.h:53
static float cutoff
Definition: equality.h:32
static bool equals(const T &test, const T &right)
Definition: equality.h:20
static bool equals(const std::vector< T > &test, const std::vector< T > &asserted)
Definition: equality.h:64