testlib.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <sstream>
4 #include <string>
5 #include <map>
6 #include <sys/stat.h>
7 
8 #include <TPRegexp.h>
9 #include <TString.h>
10 
11 #include <cmath>
12 
13 #include <TVector3.h>
14 
15 using namespace std;
16 
17 static unsigned int iTest = 1;
18 static unsigned int gPrecision = 12;
19 static unsigned int nTests = 0;
20 static double eps = 1.e-10;
21 
22 static unsigned int gCurToDoMarker = 0;
23 class ToDo;
24 static std::map<int, ToDo*> gToDoMarkers;
25 
26 #include "testlib.h"
27 
28 int GetPrecision() { return gPrecision; }
29 int GetFieldWidth() { return gPrecision + 3; }
30 
32 
34 {
35  if (fDoPrint) {
36  cout << "1.." << iTest-1 << endl;
37  }
38 }
39 
40 void
41 plan(int n)
42 {
43  nTests = n;
44  if (nTests != 0) {
45  cout << "1.." << nTests << endl;
46  }
47  else {
49  }
50 }
51 
52 void
54 {
56 }
57 
58 void
59 diag(const std::string& msg)
60 {
61  TString s(msg.c_str());
62 
63  TPRegexp repl("^");
64  repl.Substitute(s, "# ", "mg");
65  TPRegexp repl2("\\n$");
66  repl.Substitute(s, "", "");
67  cout << s.Data() << endl;
68 }
69 
70 void
71 diag(const std::ostringstream& msg)
72 {
73  diag(msg.str());
74 }
75 
76 void
77 pass(const std::string& name)
78 {
79  cout << "ok " << iTest++ << todoMsg(name) << endl;
80 }
81 
82 void
83 fail(const std::string& name)
84 {
85  cout << "not ok " << iTest++ << todoMsg(name) << endl;
86 }
87 
88 bool
89 ok(bool okay)
90 {
91  bool retval = true;
92  if (!okay) {
93  cout << "not ";
94  retval = false;
95  }
96  cout << "ok " << iTest++ << todoMsg() << endl;
97  return retval;
98 }
99 
100 bool
101 ok(bool okay, const std::string& name)
102 {
103  bool retval = true;
104  if (!okay) {
105  cout << "not ";
106  retval = false;
107  }
108  cout << "ok " << iTest++ << todoMsg(name) << endl;
109  return retval;
110 }
111 
112 bool
113 is(const double a, const double b)
114 {
115  return is(a, b, string(""));
116 }
117 
118 bool
119 is(const double a, const double b, const std::string& name)
120 {
121  bool retval = true;
122  const double thisEps = eps;
123  if (!( SAMENONFINITE(a,b) || (a+thisEps > b && a-thisEps < b) )) {
124  cout << "not ";
125  retval = false;
126  }
127  cout << "ok " << iTest++ << todoMsg(name);
128  if (retval == false) {
129  ostringstream s;
130  s << ", Difference: test="
131  << setprecision(GetPrecision()) << setw(GetFieldWidth()) << a
132  << " vs. ref="
133  << setprecision(GetPrecision()) << setw(GetFieldWidth()) << b;
134  diag(s);
135  }
136  else
137  cout << endl;
138  return retval;
139 }
140 
141 bool
142 isnot(const double a, const double b)
143 {
144  return isnot(a, b, string(""));
145 }
146 
147 bool
148 isnot(const double a, const double b, const std::string& name)
149 {
150  bool retval = true;
151  if (BOTHINFINITY(a,b) || BOTHNAN(a,b) || ((a+eps > b) && (a-eps < b))) {
152  cout << "not ";
153  retval = false;
154  }
155  cout << "ok " << iTest++ << todoMsg(name);
156  if (retval == false) {
157  ostringstream s;
158  s << ", Unexpectedly equivalent: test="
159  << setprecision(GetPrecision()) << setw(GetFieldWidth()) << a
160  << " vs. ref="
161  << setprecision(GetPrecision()) << setw(GetFieldWidth()) << b;
162  diag(s);
163  }
164  else
165  cout << endl;
166  return retval;
167 }
168 
169 
170 
171 bool
172 is(const float a, const float b)
173 { return is((double)a, (double)b); }
174 
175 bool
176 is(const float a, const float b, const std::string& name)
177 { return is((double)a, (double)b, name); }
178 
179 bool
180 isnot(const float a, const float b)
181 { return isnot((double)a, (double)b); }
182 
183 bool
184 isnot(const float a, const float b, const std::string& name)
185 { return isnot((double)a, (double)b, name); }
186 
187 bool
188 isEps(const double a, const double b, const double thisEps, const std::string& name)
189 {
190  bool retval = true;
191  if (!( SAMENONFINITE(a,b) || (a+thisEps > b && a-thisEps < b) )) {
192  cout << "not ";
193  retval = false;
194  }
195  cout << "ok " << iTest++ << todoMsg(name);
196  if (retval == false) {
197  ostringstream s;
198  s << ", Difference: test="
199  << setprecision(GetPrecision()) << setw(GetFieldWidth()) << a
200  << " vs. ref="
201  << setprecision(GetPrecision()) << setw(GetFieldWidth()) << b;
202  diag(s);
203  }
204  else
205  cout << endl;
206  return retval;
207 }
208 
209 bool
210 isEpsDiag(const double a, const double b, const double thisEps, const std::string& name)
211 {
212  bool retval = true;
213  if (!( SAMENONFINITE(a,b) || (a+thisEps > b && a-thisEps < b) )) {
214  cout << "not ";
215  retval = false;
216  }
217  cout << "ok " << iTest++ << todoMsg(name);
218  if (retval == false) {
219  ostringstream s;
220  s << ", Difference: test="
221  << setprecision(GetPrecision()) << setw(GetFieldWidth()) << a
222  << " vs. ref="
223  << setprecision(GetPrecision()) << setw(GetFieldWidth()) << b;
224  diag(s);
225  }
226  else
227  cout << endl;
228  return retval;
229 }
230 
231 bool
232 isDiag(const double a, const double b, const std::string& name)
233 { return isEpsDiag(a, b, eps, name); }
234 
235 bool
236 isDiag(const double a, const double b)
237 { return isEpsDiag(a, b, eps, string("")); }
238 
239 bool
240 isRelEpsDiag(const double a, const double b, const double thisEps, const std::string& name)
241 {
242  return isEpsDiag(a, b, (fabs(a+b) == 0. ? 1.e-12 : fabs(a+b)*thisEps/2.), name);
243 }
244 
245 bool
246 isDiag(const std::vector<double>& a, const std::vector<double>& b, const std::string& name)
247 {
248  return isEpsDiag(a, b, eps, name);
249 }
250 
251 bool
252 isEpsDiag(const std::vector<double>& a, const std::vector<double>& b, const double thisEps, const std::string& name)
253 {
254  bool retval = true;
255  const unsigned int na = a.size();
256  const unsigned int nb = b.size();
257 
258  std::string nName = name + std::string(" (vector size)");
259  if (!isDiag(na, nb, nName))
260  return false;
261 
262  unsigned int i = 0;
263  for (i = 0; i < na; ++i) {
264  if (!( SAMENONFINITE(a[i],b[i]) || (a[i]+thisEps > b[i] && a[i]-thisEps < b[i]) )) {
265  retval = false;
266  break;
267  }
268  }
269 
270  if (retval == false) {
271  std::cout << "not ";
272  }
273  std::cout << "ok " << GetTestNo()++ << todoMsg(name);
274 
275  if (retval == false) {
276  std::ostringstream s;
277  //s << "Difference at index " << i << ": test='" << a[i] << "' vs. ref='" << b[i] << "'";
278  s << ", Difference at index " << i
279  << ": test='"
280  << std::setprecision(GetPrecision()) << std::setw(GetFieldWidth()) << a[i]
281  << "' vs. ref='"
282  << std::setprecision(GetPrecision()) << std::setw(GetFieldWidth()) << b[i]
283  << "'";
284  diag(s);
285  }
286  else
287  cout << endl;
288  return retval;
289 }
290 
291 bool
292 isRelEpsDiag(const std::vector<double>& a, const std::vector<double>& b, const double thisEps, const std::string& name)
293 {
294  bool retval = true;
295  const unsigned int na = a.size();
296  const unsigned int nb = b.size();
297 
298  std::string nName = name + std::string(" (vector size)");
299  if (!isDiag(na, nb, nName))
300  return false;
301 
302  unsigned int i = 0;
303  for (i = 0; i < na; ++i) {
304  const double teps = fabs(a[i]+b[i]) == 0. ? 1.e-12 : fabs(a[i]+b[i])*thisEps/2.;
305  if (!( SAMENONFINITE(a[i],b[i]) || (a[i]+teps > b[i] && a[i]-teps < b[i]) )) {
306  retval = false;
307  break;
308  }
309  }
310 
311  if (retval == false) {
312  std::cout << "not ";
313  }
314  std::cout << "ok " << GetTestNo()++ << todoMsg(name);
315 
316  if (retval == false) {
317  std::ostringstream s;
318  //s << "Difference at index " << i << ": test='" << a[i] << "' vs. ref='" << b[i] << "'";
319  s << ", Difference at index " << i
320  << ": test='"
321  << std::setprecision(GetPrecision()) << std::setw(GetFieldWidth()) << a[i]
322  << "' vs. ref='"
323  << std::setprecision(GetPrecision()) << std::setw(GetFieldWidth()) << b[i]
324  << "'";
325  diag(s);
326  }
327  else
328  cout << endl;
329  return retval;
330 }
331 
332 
333 bool
334 is(const std::vector<double>& a, const std::vector<double>& b, const std::string& name)
335 {
336  bool retval = true;
337  const unsigned int na = a.size();
338  const unsigned int nb = b.size();
339  const double thisEps = eps;
340 
341  std::string nName = name + std::string(" (vector size)");
342  if (!is(na, nb, nName))
343  return false;
344 
345  unsigned int i = 0;
346  for (i = 0; i < na; ++i) {
347  if (!( SAMENONFINITE(a[i],b[i]) || (a[i]+thisEps > b[i] && a[i]-thisEps < b[i]) )) {
348  retval = false;
349  break;
350  }
351  }
352 
353  if (retval == false) {
354  std::cout << "not ";
355  }
356  std::cout << "ok " << GetTestNo()++ << todoMsg(name) << std::endl;
357  return retval;
358 }
359 
360 
361 std::string
362 todoMsg(const std::string& name)
363 {
364  if (gToDoMarkers.empty()) {
365  if (name.length() == 0)
366  return string("");
367  return string(" - ") + name;
368  }
369  return string(" # TODO & SKIP ") + gToDoMarkers.rbegin()->second->GetReason()
370  + string(" (testname: ") + name + string(")");
371 }
372 
373 std::string
375 {
376  return todoMsg(string(""));
377 }
378 
379 ToDo::ToDo(const std::string reason)
380  : fReason(reason)
381 {
383  gToDoMarkers[fMarker] = this;
384 }
385 
387 {
388  gToDoMarkers.erase(fMarker);
389 }
390 
391 
392 bool
393 fexists(const std::string& filename)
394 {
395  bool okay = false;
396  struct stat buffer;
397  if (stat(filename.c_str(), &buffer)) {
398  cout << "not ";
399  }
400  else
401  okay = true;
402 
403  cout << "ok " << iTest++ << todoMsg(string("File ") + filename + string(" exists")) << endl;
404  return okay;
405 }
406 
407 unsigned int&
409 {
410  return iTest;
411 }
412 
413 bool
414 isEpsDiag(const TVector3& a, const TVector3& b, const double thisEps, const std::string& name)
415 {
416  bool retval = fabs(a.x() - b.x()) < thisEps && fabs(a.y() - b.y()) < thisEps && fabs(a.z() - b.z()) < thisEps;
417  if (!retval) {
418  std::cout << "(" << a.x() << ", " << a.y() << ", " << a.z() << ") != "
419  << "(" << b.x() << ", " << b.y() << ", " << b.z() << "), "
420  << "not ";
421  retval = false;
422  }
423  std::cout << "ok " << GetTestNo()++ << todoMsg(name);
424 
425  if (!retval) {
426  ostringstream s;
427  s << ", Diference (" << a.x() - b.x() << ", " << a.y() - b.y() << ", " << a.z() - b.z() << ") != ";
428  diag(s);
429  retval = false;
430  }
431  else
432  cout << endl;
433 
434  return retval;
435 }
436 
std::string todoMsg(const std::string &name)
Definition: testlib.cc:362
bool isnot(const double a, const double b)
Definition: testlib.cc:142
static unsigned int iTest
Definition: testlib.cc:17
bool is(const double a, const double b)
Definition: testlib.cc:113
void SetDoPrint()
Definition: testlib.h:248
void diag(const std::string &msg)
Definition: testlib.cc:59
static unsigned int gPrecision
Definition: testlib.cc:18
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
static double eps
Definition: testlib.cc:20
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
static unsigned int gCurToDoMarker
Definition: testlib.cc:22
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
static std::map< int, ToDo * > gToDoMarkers
Definition: testlib.cc:23
~ToDo()
Definition: testlib.cc:386
#define SAMENONFINITE(a, b)
Definition: testlib.h:55
~Planner()
Definition: testlib.cc:33
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
static unsigned int nTests
Definition: testlib.cc:19
char * filename
Definition: dump1090.h:266
static Planner gPlanner
Definition: testlib.cc:31
std::string fReason
Definition: testlib.h:239
int GetPrecision()
Definition: testlib.cc:28
#define BOTHINFINITY(a, b)
Definition: testlib.h:53
void pass(const std::string &name)
Definition: testlib.cc:77
#define BOTHNAN(a, b)
Definition: testlib.h:52

, generated on Tue Sep 26 2023.