testlib.h
Go to the documentation of this file.
1 #ifndef _testlib_h_
2 #define _testlib_h_
3 
4 #include <iostream>
5 #include <iomanip>
6 #include <sstream>
7 #include <string>
8 #include <vector>
9 #include <map>
10 
11 class TVector3;
12 
13 extern unsigned int iTest;
14 extern double eps;
15 
16 int GetPrecision();
17 int GetFieldWidth();
18 
19 void plan(int n);
20 void noplan();
21 std::string todoMsg(const std::string& name);
22 std::string todoMsg();
23 unsigned int& GetTestNo();
24 
25 void diag(const std::string& msg);
26 void diag(const std::ostringstream& msg);
27 
28 void pass(const std::string& name);
29 void fail(const std::string& name);
30 
31 bool ok(bool okay);
32 bool ok(bool okay, const std::string& name);
33 
34 bool is(const double a, const double b);
35 bool is(const double a, const double b, const std::string& name);
36 bool is(const float a, const float b);
37 bool is(const float a, const float b, const std::string& name);
38 bool isDiag(const double a, const double b, const std::string& name);
39 bool isDiag(const double a, const double b);
40 bool isEpsDiag(const TVector3& a, const TVector3& b, const double thisEps, const std::string& name);
41 
42 bool isEps(const double a, const double b, const double thisEps, const std::string& name);
43 bool isEpsDiag(const double a, const double b, const double thisEps, const std::string& name);
44 bool isRelEpsDiag(const double a, const double b, const double thisEps, const std::string& name);
45 
46 bool isnot(const double a, const double b);
47 bool isnot(const double a, const double b, const std::string& name);
48 bool isnot(const float a, const float b);
49 bool isnot(const float a, const float b, const std::string& name);
50 
51 #define BOTHFINITE(a,b) (isfinite(a) && isfinite(b))
52 #define BOTHNAN(a,b) (std::isnan(a) && std::isnan(b))
53 #define BOTHINFINITY(a,b) (isinf(a) == isinf(b) && isinf(a))
54 #define BOTHFINITE_OREQ(a,b) (BOTHNAN(a,b) || BOTHINFINITY(a,b) || BOTHFINITE(a,b))
55 #define SAMENONFINITE(a,b) (BOTHNAN(a,b) || BOTHINFINITY(a,b))
56 
57 template<class T>
58 bool
59 is(const T& a, const T& b)
60 {
61  return is(a, b, std::string(""));
62 }
63 
64 template<class T>
65 bool
66 is(const T& a, const T& b, const std::string& name)
67 {
68  bool retval = true;
69  if (!(a == b)) {
70  std::cout << "not ";
71  retval = false;
72  }
73  std::cout << "ok " << GetTestNo()++ << todoMsg(name) << std::endl;
74  return retval;
75 }
76 
77 
78 template<class T>
79 bool
80 isDiag(const T& a, const T& b)
81 { return isDiag(a, b, std::string("")); }
82 
83 template<class T>
84 bool
85 isDiag(const T& a, const T& b, const std::string& name)
86 {
87  bool retval = true;
88  if (!(a == b)) {
89  std::cout << "not ";
90  retval = false;
91  }
92  std::cout << "ok " << GetTestNo()++ << todoMsg(name);
93  if (retval == false) {
94  std::ostringstream s;
95  //s << "Difference: test='" << a << "' vs. ref='" << b << "'";
96  s << ", Difference: test="
97  << std::setprecision(GetPrecision()) << std::setw(GetFieldWidth()) << a
98  << " vs. ref="
99  << std::setprecision(GetPrecision()) << std::setw(GetFieldWidth()) << b;
100  diag(s);
101  }
102  else
103  std::cout << std::endl;
104  return retval;
105 }
106 
107 
108 template<class T>
109 bool
110 is(const std::vector<T>& a, const std::vector<T>& b, const std::string& name)
111 {
112  bool retval = true;
113  const unsigned int na = a.size();
114  const unsigned int nb = b.size();
115 
116  std::string nName = name + std::string(" (vector size)");
117  if (!is(na, nb, nName))
118  return false;
119 
120  unsigned int i = 0;
121  for (i = 0; i < na; ++i) {
122  if (!(a[i] == b[i])) {
123  retval = false;
124  break;
125  }
126  }
127 
128  if (retval == false) {
129  std::cout << "not ";
130  }
131  std::cout << "ok " << GetTestNo()++ << todoMsg(name) << std::endl;
132  return retval;
133 }
134 
135 template<class T>
136 bool
137 isDiag(const std::vector<T>& a, const std::vector<T>& b, const std::string& name)
138 {
139  bool retval = true;
140  const unsigned int na = a.size();
141  const unsigned int nb = b.size();
142 
143  std::string nName = name + std::string(" (vector size)");
144  if (!isDiag(na, nb, nName))
145  return false;
146 
147  unsigned int i = 0;
148  for (i = 0; i < na; ++i) {
149  if (!(a[i] == b[i])) {
150  retval = false;
151  break;
152  }
153  }
154 
155  if (retval == false) {
156  std::cout << "not ";
157  }
158  std::cout << "ok " << GetTestNo()++ << todoMsg(name);
159 
160  if (retval == false) {
161  std::ostringstream s;
162  //s << "Difference at index " << i << ": test='" << a[i] << "' vs. ref='" << b[i] << "'";
163  s << "Difference at index " << i
164  << ": test='"
165  << std::setprecision(GetPrecision()) << std::setw(GetFieldWidth()) << a[i]
166  << "' vs. ref='"
167  << std::setprecision(GetPrecision()) << std::setw(GetFieldWidth()) << b[i]
168  << "'";
169  diag(s);
170  }
171  else
172  std::cout << std::endl;
173  return retval;
174 }
175 
176 
177 bool is(const std::vector<double>& a, const std::vector<double>& b, const std::string& name);
178 bool isDiag(const std::vector<double>& a, const std::vector<double>& b, const std::string& name);
179 bool isEpsDiag(const std::vector<double>& a, const std::vector<double>& b, const double thisEps, const std::string& name);
180 bool isRelEpsDiag(const std::vector<double>& a, const std::vector<double>& b, const double thisEps, const std::string& name);
181 
182 template<class T>
183 bool
184 isnot(const T& a, const T& b)
185 {
186  return isnot(a, b, std::string(""));
187 }
188 
189 template<class T>
190 bool
191 isnot(const T& a, const T& b, const std::string name)
192 {
193  bool retval = true;
194  if (a == b) {
195  std::cout << "not ";
196  retval = false;
197  }
198  std::cout << "ok " << GetTestNo()++ << todoMsg(name) << std::endl;
199  return retval;
200 }
201 
202 template<class T>
203 bool
204 isnotDiag(const T& a, const T& b)
205 {
206  return isnotDiag(a, b, std::string(""));
207 }
208 
209 template<class T>
210 bool
211 isnotDiag(const T& a, const T& b, const std::string& name)
212 {
213  bool retval = true;
214  if (a == b) {
215  std::cout << "not ";
216  retval = false;
217  }
218  std::cout << "ok " << GetTestNo()++ << todoMsg(name);
219  if (retval == false) {
220  std::ostringstream s;
221  s << "Unexpectectly equivalent: test='"
222  << std::setprecision(GetPrecision()) << std::setw(GetFieldWidth()) << a
223  << "' vs. ref='"
224  << std::setprecision(GetPrecision()) << std::setw(GetFieldWidth()) << b << "'";
225  diag(s);
226  }
227  else
228  std::cout << std::endl;
229  return retval;
230 }
231 
232 
233 class ToDo {
234 public:
235  ToDo(const std::string reason);
236  ~ToDo();
237  std::string GetReason() { return fReason; }
238 private:
239  std::string fReason;
240  int fMarker;
241 };
242 
243 
244 class Planner {
245 public:
246  Planner() : fDoPrint(false) {}
247  ~Planner();
248  void SetDoPrint() { fDoPrint = true; }
249 private:
250  bool fDoPrint;
251 };
252 
253 bool fexists(const std::string& filename);
254 
255 #endif
std::string todoMsg(const std::string &name)
Definition: testlib.cc:362
bool fDoPrint
Definition: testlib.h:250
bool isnot(const double a, const double b)
Definition: testlib.cc:142
Planner()
Definition: testlib.h:246
bool is(const double a, const double b)
Definition: testlib.cc:113
bool isnotDiag(const T &a, const T &b)
Definition: testlib.h:204
void SetDoPrint()
Definition: testlib.h:248
std::string GetReason()
Definition: testlib.h:237
void diag(const std::string &msg)
Definition: testlib.cc:59
bool ok(bool okay)
Definition: testlib.cc:89
bool isRelEpsDiag(const double a, const double b, const double thisEps, const std::string &name)
Definition: testlib.cc:240
int GetFieldWidth()
Definition: testlib.cc:29
bool isEps(const double a, const double b, const double thisEps, const std::string &name)
Definition: testlib.cc:188
bool fexists(const std::string &filename)
Definition: testlib.cc:393
void fail(const std::string &name)
Definition: testlib.cc:83
int fMarker
Definition: testlib.h:240
unsigned int & GetTestNo()
Definition: testlib.cc:408
void noplan()
Definition: testlib.cc:53
constexpr double s
Definition: AugerUnits.h:163
Definition: testlib.h:233
bool isEpsDiag(const double a, const double b, const double thisEps, const std::string &name)
Definition: testlib.cc:210
double eps
~ToDo()
Definition: testlib.cc:386
~Planner()
Definition: testlib.cc:33
unsigned int iTest
void plan(int n)
Definition: testlib.cc:41
ToDo(const std::string reason)
Definition: testlib.cc:379
bool isDiag(const double a, const double b, const std::string &name)
Definition: testlib.cc:232
char * filename
Definition: dump1090.h:266
std::string fReason
Definition: testlib.h:239
int GetPrecision()
Definition: testlib.cc:28
void pass(const std::string &name)
Definition: testlib.cc:77

, generated on Tue Sep 26 2023.