xmscore  1.0
TestTools.h
Go to the documentation of this file.
1 #pragma once
2 //------------------------------------------------------------------------------
7 //------------------------------------------------------------------------------
8 
9 //----- Included files ---------------------------------------------------------
10 
11 // 3. Standard library headers
12 #include <sstream> // for sstream
13 #include <vector>
14 
15 // 4. External library headers
16 #ifdef CXX_TEST
17 #include <cxxtest/TestSuite.h>
18 #else
19 #error "Should only be included in builds with testing enabled."
20 #endif // ifdef CXX_TEST
21 
22 // 5. Shared code headers
23 #include <xmscore/points/ptsfwd.h>
24 #include <xmscore/stl/utility.h>
25 
26 namespace xms
27 {
28 //------------------------------------------------------------------------------
30 //------------------------------------------------------------------------------
31 #define _TS_ASSERT_EQUALS_AVEC(f, l, expected, length, actual) \
32  xms::ttAssertEqualsAVec(f, l, expected, length, actual)
33 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
36 #define TS_ASSERT_EQUALS_AVEC(expected, length, actual) \
37  _TS_ASSERT_EQUALS_AVEC(__FILE__, __LINE__, expected, length, actual)
38 
39 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
42 #define _TS_ASSERT_DELTA_VEC(f, l, a, b, delta) xms::ttAssertDeltaVec((f), (l), (a), (b), (delta))
43 //------------------------------------------------------------------------------
45 //------------------------------------------------------------------------------
46 #define TS_ASSERT_DELTA_VEC(a, b, delta) _TS_ASSERT_DELTA_VEC(__FILE__, __LINE__, a, b, delta)
47 
48 //------------------------------------------------------------------------------
51 //------------------------------------------------------------------------------
52 #define _TS_ASSERT_DELTA_VEC2D(f, l, a, b, delta) \
53  xms::ttAssertDeltaVec2D((f), (l), (a), (b), (delta))
54 //------------------------------------------------------------------------------
57 //------------------------------------------------------------------------------
58 #define TS_ASSERT_DELTA_VEC2D(a, b, delta) _TS_ASSERT_DELTA_VEC2D(__FILE__, __LINE__, a, b, delta)
59 
60 //------------------------------------------------------------------------------
62 //------------------------------------------------------------------------------
63 #define _TS_ASSERT_EQUALS_VEC2D(f, l, expected, actual) \
64  xms::ttAssertEqualsVec2D((f), (l), (expected), (actual))
65 //------------------------------------------------------------------------------
67 //------------------------------------------------------------------------------
68 #define TS_ASSERT_EQUALS_VEC2D(expected, actual) \
69  _TS_ASSERT_EQUALS_VEC2D(__FILE__, __LINE__, expected, actual)
70 
71 //------------------------------------------------------------------------------
74 //------------------------------------------------------------------------------
75 #define _TS_ASSERT_DELTA_VECPT3D(f, l, a, b, delta) ttAssertDeltaVecPt3d(f, l, a, b, delta)
76 //------------------------------------------------------------------------------
79 //------------------------------------------------------------------------------
80 #define TS_ASSERT_DELTA_VECPT3D(a, b, delta) \
81  _TS_ASSERT_DELTA_VECPT3D(__FILE__, __LINE__, a, b, delta)
82 //------------------------------------------------------------------------------
85 //------------------------------------------------------------------------------
86 #define TS_ASSERT_DELTA_VEC_MP3(a, b, delta) \
87  _TS_ASSERT_DELTA_VECPT3D(__FILE__, __LINE__, a, b, delta)
88 
89 //------------------------------------------------------------------------------
92 //------------------------------------------------------------------------------
93 #define _TS_ASSERT_DELTA_VECPT2D(f, l, a, b, delta) ttAssertDeltaVecPt2d(f, l, a, b, delta)
94 //------------------------------------------------------------------------------
97 //------------------------------------------------------------------------------
98 #define TS_ASSERT_DELTA_VECPT2D(a, b, delta) \
99  _TS_ASSERT_DELTA_VECPT2D(__FILE__, __LINE__, a, b, delta)
100 //------------------------------------------------------------------------------
103 //------------------------------------------------------------------------------
104 #define TS_ASSERT_DELTA_VEC_MP2(a, b, delta) \
105  _TS_ASSERT_DELTA_VECPT2D(__FILE__, __LINE__, a, b, delta)
106 
107 //------------------------------------------------------------------------------
109 //------------------------------------------------------------------------------
110 #define _TS_ASSERT_DELTA_PT3D(f, l, a, b, delta) ttAssertDeltaPt3d(f, l, a, b, delta)
111 //------------------------------------------------------------------------------
113 //------------------------------------------------------------------------------
114 #define _TS_ASSERT_DELTA_PT2D(f, l, a, b, delta) ttAssertDeltaPt2d(f, l, a, b, delta);
115 //------------------------------------------------------------------------------
117 //------------------------------------------------------------------------------
118 #define TS_ASSERT_DELTA_PT3D(a, b, delta) _TS_ASSERT_DELTA_PT3D(__FILE__, __LINE__, a, b, delta)
119 //------------------------------------------------------------------------------
121 //------------------------------------------------------------------------------
122 #define TS_ASSERT_DELTA_PT2D(a, b, delta) _TS_ASSERT_DELTA_PT2D(__FILE__, __LINE__, a, b, delta)
123 
124 //------------------------------------------------------------------------------
126 //------------------------------------------------------------------------------
127 #define _TS_ASSERT_TXT_FILES_EQUAL(f, l, a, b) xms::ttTextFilesEqual((f), (l), a, b)
128 //------------------------------------------------------------------------------
130 //------------------------------------------------------------------------------
131 #define TS_ASSERT_TXT_FILES_EQUAL(a, b) _TS_ASSERT_TXT_FILES_EQUAL(__FILE__, __LINE__, a, b)
132 
133 //------------------------------------------------------------------------------
135 //------------------------------------------------------------------------------
136 #define _TS_ASSERT_STREAMS_EQUAL(f, l, a, b) xms::ttStreamsEqual((f), (l), a, b)
137 //------------------------------------------------------------------------------
139 //------------------------------------------------------------------------------
140 #define TS_ASSERT_STREAMS_EQUAL(a, b) _TS_ASSERT_STREAMS_EQUAL(__FILE__, __LINE__, a, b)
141 
142 //------------------------------------------------------------------------------
144 //------------------------------------------------------------------------------
145 #define _TS_ASSERT_EQUALS_VEC(f, l, a, b) \
146  { \
147  if (a.size() != b.size()) \
148  { \
149  std::stringstream msg; \
150  msg << "Incorrect size Expecting size: " << a.size() << " Found size: " << b.size(); \
151  _TS_FAIL(f, l, msg.str().c_str()); \
152  } \
153  else \
154  { \
155  for (size_t iUnique = 0; iUnique < a.size(); ++iUnique) \
156  { \
157  if (a[iUnique] != b[iUnique]) \
158  { \
159  std::stringstream msg; \
160  msg << "Incorrect value at position : " << iUnique << " Expecting: " << a[iUnique] \
161  << " Found: " << b[iUnique]; \
162  _TS_FAIL(f, l, msg.str().c_str()); \
163  } \
164  } \
165  } \
166  \
167 }
168 //------------------------------------------------------------------------------
170 //------------------------------------------------------------------------------
171 #define TS_ASSERT_EQUALS_VEC(a, b) _TS_ASSERT_EQUALS_VEC(__FILE__, __LINE__, a, b)
172 
173 //------------------------------------------------------------------------------
175 //------------------------------------------------------------------------------
176 #define _TS_REQUIRE_NOT_NULL(f, l, pointer) \
177  if (pointer == NULL) \
178  { \
179  _TS_FAIL(f, l, "Unexpected NULL pointer."); \
180  return; \
181  }
182 //------------------------------------------------------------------------------
184 //------------------------------------------------------------------------------
185 #define TS_REQUIRE_NOT_NULL(pointer) _TS_REQUIRE_NOT_NULL(__FILE__, __LINE__, pointer)
186 
187 //------------------------------------------------------------------------------
189 //------------------------------------------------------------------------------
190 #define _TS_ASSERT_STACKED_ERRORS(f, l, expected) ::xms::ttAssertStackedErrors(f, l, expected)
191 //------------------------------------------------------------------------------
193 //------------------------------------------------------------------------------
194 #define TS_ASSERT_STACKED_ERRORS(expected) _TS_ASSERT_STACKED_ERRORS(__FILE__, __LINE__, expected)
195 //#endif // ifdef CXX_TEST
196 
197 //----- Structs / Classes ------------------------------------------------------
198 
202 {
203 public:
205  void SetDefault(int a_);
206  void ClearDefault();
207  int GetDefault();
208  void SetSkipping(bool a_);
209  bool GetSkipping();
210  bool DefaultValWasSet();
211 
212 private:
216 };
217 
218 //----- Function prototypes ----------------------------------------------------
219 
220 void ttByPassMessages(int a_defaultChoice);
221 bool ttSkippingMessages();
222 int ttByPassDefault();
225 void ttCheckXmLogForEachTest(bool a_check);
227 //------------------------------------------------------------------------------
235 //------------------------------------------------------------------------------
236 template <class _T, class _U, class _V>
237 bool ttEqualWithinTolerance(_T A, _U B, _V tolerance)
238 {
239  return (fabs((A) - (B)) <= (tolerance));
240 } // ttEqualWithinTolerance
241 //#ifdef CXX_TEST
242 void ttAssertStackedErrors(const char* a_file, int a_line, const std::string& a_expected);
243 void ttGetTestFilePaths(const std::string& a_path,
244  const std::string& a_fileBase,
245  const std::string& a_extension,
246  std::string& a_baseFilePath,
247  std::string& a_outFilePath);
248 void ttTextFilesEqual(const std::string& a_srcFile,
249  unsigned line,
250  const std::string& a_file1,
251  const std::string& a_file2);
252 
253 bool ttTextFilesEqual(const std::string& a_file1,
254  const std::string& a_file2,
255  std::string& a_message);
256 void ttStreamsEqual(const std::string& a_src,
257  unsigned int a_line,
258  std::istream& a_strm1,
259  std::istream& a_strm2);
260 bool ttEqualPointsXYZ(const Pt3d& a_pt1, const Pt3d& a_pt2, double a_tolerance);
261 bool ttEqualPointsXYZ(double a_x1,
262  double a_y1,
263  double a_z1,
264  double a_x2,
265  double a_y2,
266  double a_z2,
267  double a_tolerance);
268 bool ttEqualPointsXY(const Pt2d& a_pt1, const Pt2d& a_pt2, double a_tolerance);
269 bool ttEqualPointsXY(const Pt3d& a_pt1, const Pt3d& a_pt2, double a_tolerance);
270 bool ttEqualPointsXY(double a_x1, double a_y1, double a_x2, double a_y2, double a_tolerance);
271 //------------------------------------------------------------------------------
279 //------------------------------------------------------------------------------
280 template <typename T, typename U>
281 bool ttAssertDeltaVec(const char* f,
282  unsigned l,
283  const std::vector<T>& a,
284  const std::vector<T>& b,
285  U delta)
286 {
287  bool ok = true;
288  if (a.size() != b.size())
289  {
290  std::stringstream msg;
291  msg << "Incorrect size (" << a.size() << " != " << b.size() << ")";
292  _TS_FAIL(f, l, msg.str().c_str());
293  ok = false;
294  }
295  else
296  {
297  for (size_t i = 0; i < a.size(); ++i)
298  {
299  if (!ttEqualWithinTolerance(a.at(i), b.at(i), delta))
300  {
301  std::stringstream msg;
302  msg << "Incorrect value at position (" << i << "). "
303  << "(" << a.at(i) << " != " << b.at(i) << ")"
304  << " within delta (" << delta << ")";
305  _TS_FAIL(f, l, msg.str().c_str());
306  ok = false;
307  }
308  }
309  }
310  return ok;
311 } // ttAssertDeltaVec
312 //------------------------------------------------------------------------------
320 //------------------------------------------------------------------------------
321 template <typename T, typename U>
322 bool ttAssertDeltaVec2D(const char* f,
323  unsigned l,
324  const std::vector<std::vector<T>>& a,
325  const std::vector<std::vector<T>>& b,
326  U delta)
327 {
328  bool ok = true;
329  if (a.size() != b.size())
330  {
331  std::stringstream msg;
332  msg << "Incorrect size (" << a.size() << " != " << b.size() << ")";
333  _TS_FAIL(f, l, msg.str().c_str());
334  ok = false;
335  }
336  else
337  {
338  for (size_t i = 0; i < a.size(); ++i)
339  {
340  if (a.at(i).size() != b.at(i).size())
341  {
342  std::stringstream msg;
343  msg << "Incorrect size (" << a.at(i).size() << " != " << b.at(i).size() << ")";
344  _TS_FAIL(f, l, msg.str().c_str());
345  ok = false;
346  }
347  else
348  {
349  for (size_t j = 0; j < a.at(i).size(); ++j)
350  {
351  if (!ttEqualWithinTolerance(a.at(i).at(j), b.at(i).at(j), delta))
352  {
353  std::stringstream msg;
354  msg << "Incorrect value at position (" << i << ")(" << j << "). "
355  << "(" << a.at(i).at(j) << " != " << b.at(i).at(j) << ")"
356  << " within delta (" << delta << ")";
357  _TS_FAIL(f, l, msg.str().c_str());
358  ok = false;
359  }
360  }
361  }
362  }
363  }
364  return ok;
365 } // ttAssertDeltaVec2D
366 //------------------------------------------------------------------------------
373 //------------------------------------------------------------------------------
374 template <class T>
375 void ttAssertEqualsAVec(const char* a_file,
376  int a_line,
377  const T* a_expected,
378  size_t a_expectedLength,
379  const std::vector<T>& a_actual)
380 {
381  std::vector<T> expected(a_expected, a_expected + a_expectedLength);
382  _TS_ASSERT_EQUALS_VEC(a_file, a_line, expected, a_actual);
383 }
384 //------------------------------------------------------------------------------
391 //------------------------------------------------------------------------------
392 template <typename T>
393 bool ttAssertEqualsVec2D(const char* f,
394  unsigned l,
395  const std::vector<std::vector<T>>& a,
396  const std::vector<std::vector<T>>& b)
397 {
398  bool ok = true;
399  if (a.size() != b.size())
400  {
401  std::stringstream msg;
402  msg << "Incorrect size (" << a.size() << " != " << b.size() << ")";
403  _TS_FAIL(f, l, msg.str().c_str());
404  ok = false;
405  }
406  else
407  {
408  for (size_t i = 0; i < a.size(); ++i)
409  {
410  if (a.at(i).size() != b.at(i).size())
411  {
412  std::stringstream msg;
413  msg << "Incorrect size (" << a.at(i).size() << " != " << b.at(i).size() << ")";
414  _TS_FAIL(f, l, msg.str().c_str());
415  ok = false;
416  }
417  else
418  {
419  for (size_t j = 0; j < a.at(i).size(); ++j)
420  {
421  if (a.at(i).at(j) != b.at(i).at(j))
422  {
423  std::stringstream msg;
424  msg << "Incorrect value at position (" << i << ")(" << j << "). "
425  << "(" << a.at(i).at(j) << " != " << b.at(i).at(j) << ")";
426  _TS_FAIL(f, l, msg.str().c_str());
427  ok = false;
428  }
429  }
430  }
431  }
432  }
433  return ok;
434 } // ttAssertEqualsVec2D
435 //------------------------------------------------------------------------------
443 //------------------------------------------------------------------------------
444 template <class Pt>
445 void ttAssertDeltaPt3d(const char* a_file,
446  int a_line,
447  const Pt& a_pt1,
448  const Pt& a_pt2,
449  double a_delta)
450 {
451  if (!::xms::ttEqualPointsXYZ(a_pt1, a_pt2, a_delta))
452  {
453  std::stringstream msg;
454  msg << "(" << a_pt1 << "), != (" << a_pt2 << ") within delta (" << a_delta << ")";
455  _TS_FAIL(a_file, a_line, msg.str().c_str());
456  }
457 } // ttAssertDeltaPt3d
458 //------------------------------------------------------------------------------
466 //------------------------------------------------------------------------------
467 template <class Pt>
468 void ttAssertDeltaPt2d(const char* a_file,
469  int a_line,
470  const Pt& a_pt1,
471  const Pt& a_pt2,
472  double a_delta)
473 {
474  if (!::xms::ttEqualPointsXY(a_pt1, a_pt2, a_delta))
475  {
476  std::stringstream msg;
477  msg << "(" << a_pt1 << "), != (" << a_pt2 << ") within delta (" << a_delta << ")";
478  _TS_FAIL(a_file, a_line, msg.str().c_str());
479  }
480 } // ttAssertDeltaPt2d
481 //------------------------------------------------------------------------------
489 //------------------------------------------------------------------------------
490 template <class Pt1, class Pt2>
491 void ttAssertDeltaVecPt3d(const char* a_file,
492  int a_line,
493  const Pt1& a_pts1,
494  const Pt2& a_pts2,
495  double a_delta)
496 {
497  if (a_pts1.size() != a_pts2.size())
498  {
499  std::stringstream msg;
500  msg << "Incorrect size Expecting size: " << a_pts1.size() << " Found size: " << a_pts2.size();
501  _TS_FAIL(a_file, a_line, msg.str().c_str());
502  }
503  else
504  {
505  for (size_t i = 0; i < a_pts1.size(); ++i)
506  {
507  if (!::xms::ttEqualPointsXYZ(a_pts1.at(i).x, a_pts1.at(i).y, a_pts1.at(i).z, a_pts2.at(i).x,
508  a_pts2.at(i).y, a_pts2.at(i).z, a_delta))
509  {
510  std::stringstream msg;
511  msg << "Incorrect value at position : " << i << " Expecting: " << a_pts1.at(i)
512  << " Found: " << a_pts2.at(i);
513  _TS_FAIL(a_file, a_line, msg.str().c_str());
514  }
515  }
516  }
517 } // ttAssertDeltaVecPt3d
518 //------------------------------------------------------------------------------
526 //------------------------------------------------------------------------------
527 template <class Pt1, class Pt2>
528 void ttAssertDeltaVecPt2d(const char* a_file,
529  int a_line,
530  const Pt1& a_pts1,
531  const Pt2& a_pts2,
532  double a_delta)
533 {
534  if (a_pts1.size() != a_pts2.size())
535  {
536  std::stringstream msg;
537  msg << "Incorrect size Expecting size: " << a_pts1.size() << " Found size: " << a_pts2.size();
538  _TS_FAIL(a_file, a_line, msg.str().c_str());
539  }
540  else
541  {
542  for (size_t i = 0; i < a_pts1.size(); ++i)
543  {
544  if (!::xms::ttEqualPointsXY(a_pts1.at(i).x, a_pts1.at(i).y, a_pts2.at(i).x, a_pts2.at(i).y,
545  a_delta))
546  {
547  std::stringstream msg;
548  msg << "Incorrect value at position : " << i << " Expecting: " << a_pts1.at(i)
549  << " Found: " << a_pts2.at(i);
550  _TS_FAIL(a_file, a_line, msg.str().c_str());
551  }
552  }
553  }
554 } // ttAssertDeltaVecPt2d
555 //#endif // ifdef CXX_TEST
556 
557 } // namespace xms
ETestMessagingState & ttTestMessagingState()
Get the batch mode singleton.
Definition: TestTools.cpp:230
void ttCheckXmLogForEachTest(bool a_setting)
Set check of XM_LOG before and after each test.
Definition: TestTools.cpp:239
Include this small header instead of pt.h.
Class to allow running in batch mode without ui elements. Currently only used for testing but it can ...
Definition: TestTools.h:201
void ttAssertDeltaVecPt2d(const char *a_file, int a_line, const Pt1 &a_pts1, const Pt2 &a_pts2, double a_delta)
Assert two points are equal.
Definition: TestTools.h:528
bool m_defaultSet
Has a default result value been set?
Definition: TestTools.h:214
const T & at(unsigned int a) const
Definition: pt.h:753
void ttAssertEqualsAVec(const char *a_file, int a_line, const T *a_expected, size_t a_expectedLength, const std::vector< T > &a_actual)
Function for TS_ASSERT_EQUALS_AVEC.
Definition: TestTools.h:375
void ttGetTestFilePaths(const std::string &a_path, const std::string &a_fileBase, const std::string &a_extension, std::string &a_baseFilePath, std::string &a_outFilePath)
Get testing base and output file using the compiled for architecture. Checks for appended "MacOsX" fo...
Definition: TestTools.cpp:277
bool ttEqualPointsXY(double a_x1, double a_y1, double a_x2, double a_y2, double a_tolerance)
Returns true if the points are equal to within tolerance.
Definition: TestTools.cpp:478
bool ttEqualPointsXYZ(double a_x1, double a_y1, double a_z1, double a_x2, double a_y2, double a_z2, double a_tolerance)
Returns true if the points are equal to within tolerance.
Definition: TestTools.cpp:456
bool ttSkippingMessages()
Has ttByPassMessages been called without calling ttClearSkippingMessages.
Definition: TestTools.cpp:206
ETestMessagingState()
Constructor.
Definition: TestTools.cpp:137
void ClearDefault()
Change to no default value set.
Definition: TestTools.cpp:155
bool ttEqualPointsXYZ(const Pt3d &a_pt1, const Pt3d &a_pt2, double a_tolerance)
Returns true if the points are equal to within tolerance.
Definition: TestTools.cpp:441
bool ttEqualWithinTolerance(_T A, _U B, _V tolerance)
Tolerance function to match CxxTest::delta for older CxxTest. Could be replaced by CxxTest::doAssertD...
Definition: TestTools.h:237
void ttAssertDeltaPt2d(const char *a_file, int a_line, const Pt &a_pt1, const Pt &a_pt2, double a_delta)
Assert two points are equal.
Definition: TestTools.h:468
void ttByPassMessages(int a_defaultChoice)
Bypass the ui elements and just set return value.
Definition: TestTools.cpp:196
void ttClearSkippingMessages()
Turn off skipping ui elements.
Definition: TestTools.cpp:221
int GetDefault()
Get default value.
Definition: TestTools.cpp:163
bool ttEqualPointsXY(const Pt3d &a_pt1, const Pt3d &a_pt2, double a_tolerance)
Definition: TestTools.cpp:507
bool m_skippingMessages
Are messages being skipped?
Definition: TestTools.h:213
void SetSkipping(bool a_)
Set if skipping messages.
Definition: TestTools.cpp:171
2D Point template class
Definition: pt.h:82
void SetDefault(int a_)
Set default result value.
Definition: TestTools.cpp:147
bool ttAssertDeltaVec2D(const char *f, unsigned l, const std::vector< std::vector< T >> &a, const std::vector< std::vector< T >> &b, U delta)
Template function returning true or false to help debug tests.
Definition: TestTools.h:322
3d point template class
Definition: pt.h:791
bool ttAssertEqualsVec2D(const char *f, unsigned l, const std::vector< std::vector< T >> &a, const std::vector< std::vector< T >> &b)
Template function returning true or false to help debug tests.
Definition: TestTools.h:393
bool ttAssertDeltaVec(const char *f, unsigned l, const std::vector< T > &a, const std::vector< T > &b, U delta)
Template function returning true or false to help debug tests.
Definition: TestTools.h:281
int m_defaultRetValue
Default return value.
Definition: TestTools.h:215
bool ttTextFilesEqual(const std::string &a_file1, const std::string &a_file2, std::string &a_message)
Returns true if the two files are equal.
Definition: TestTools.cpp:324
void ttStreamsEqual(const std::string &a_src, unsigned int a_line, std::istream &a_strm1, std::istream &a_strm2)
Returns true if the two streams are equal.
Definition: TestTools.cpp:395
void ttAssertDeltaPt3d(const char *a_file, int a_line, const Pt &a_pt1, const Pt &a_pt2, double a_delta)
Assert two points are equal.
Definition: TestTools.h:445
bool DefaultValWasSet()
Was default value set (true) or has it been cleared?
Definition: TestTools.cpp:187
void ttAssertStackedErrors(const char *a_file, int a_line, const std::string &a_expected)
CXX Test assert that the stackable XM_LOG has the expected errors.
Definition: TestTools.cpp:257
void ttAssertDeltaVecPt3d(const char *a_file, int a_line, const Pt1 &a_pts1, const Pt2 &a_pts2, double a_delta)
Assert two points are equal.
Definition: TestTools.h:491
int ttByPassDefault()
Get the default choice when skipping messages.
Definition: TestTools.cpp:214
bool GetSkipping()
Get if skipping messages.
Definition: TestTools.cpp:179
#define _TS_ASSERT_EQUALS_VEC(f, l, a, b)
Used to compare to vectors and give useful output information.
Definition: TestTools.h:145