-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathUnitTestSupport.hpp
More file actions
executable file
·580 lines (525 loc) · 40.3 KB
/
UnitTestSupport.hpp
File metadata and controls
executable file
·580 lines (525 loc) · 40.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* ============================================================================
* Copyright (c) 2009-2016 BlueQuartz Software, LLC
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of BlueQuartz Software, the US Air Force, nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The code contained herein was partially funded by the followig contracts:
* United States Air Force Prime Contract FA8650-07-D-5800
* United States Air Force Prime Contract FA8650-10-D-5210
* United States Prime Contract Navy N00173-07-C-2068
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
#pragma once
//-- C Includes
#include <assert.h>
//-- C++ Includes
#include <iostream>
#include <sstream>
#include <string.h>
#include <string>
#define NUM_COLS 120
#include <QtCore/QString>
#include <QtCore/QTextStream>
#include "SIMPLib/DataContainers/DataArrayPath.h"
namespace SIMPL
{
namespace unittest
{
static std::string CurrentMethod("");
static int numTestsPass = 0;
static int numTestFailed = 0;
static int numTests = 0;
static char TestMessage[NUM_COLS + 1];
static const char Passed[6] = {'P', 'A', 'S', 'S', 'E', 'D'};
static const char Failed[6] = {'F', 'A', 'I', 'L', 'E', 'D'};
static int SizeOfPassed = 6;
static int SizeOfFailed = 6;
} // namespace unittest
} // namespace SIMPL
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
QTextStream& operator<<(QTextStream& out, const DataArrayPath& v)
{
out << v.getDataContainerName() << "|" << v.getAttributeMatrixName() << "|" << v.getDataArrayName();
return out;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
class TestException : public std::exception
{
public:
/**
* @brief
* @param what
* @param file
* @param lineNumber
*/
TestException(const std::string& what, const std::string& filename, int lineNumber)
: m_Message(what)
, m_FileName(filename)
, m_LineNumber(lineNumber)
{
updateWhat();
}
/**
* @brief Copy Constructor
*/
TestException(const TestException& te)
{
m_Message = (&te)->getMessage();
m_FileName = te.getFileName();
m_LineNumber = te.getLineNumber();
updateWhat();
}
virtual ~TestException() throw()
{
}
/**
* @brief Over ride from base class
*/
virtual const char* what() const throw()
{
return m_What;
}
void setMessage(const std::string& m)
{
m_Message = m;
updateWhat();
}
std::string getMessage()
{
return m_Message;
}
std::string getMessage() const
{
return m_Message;
}
void setFileName(const std::string& fn)
{
m_FileName = fn;
updateWhat();
}
std::string getFileName()
{
return m_FileName;
}
std::string getFileName() const
{
return m_FileName;
}
void setLineNumber(int ln)
{
m_LineNumber = ln;
updateWhat();
}
int getLineNumber()
{
return m_LineNumber;
}
int getLineNumber() const
{
return m_LineNumber;
}
protected:
TestException()
{
updateWhat();
}
void updateWhat()
{
std::stringstream ss;
ss << " Reason: " << m_Message << std::endl;
ss << " File: " << m_FileName << std::endl;
ss << " Line: " << m_LineNumber;
::memset(m_What, 0, 2048);
::memcpy(m_What, ss.str().c_str(), ss.str().size());
m_What[2047] = 0; // Make sure we nullptr terminate no matter what.
}
private:
std::string m_Message;
std::string m_FileName;
int m_LineNumber;
char m_What[2048];
void operator=(const TestException&); // Move assignment Not Implemented
};
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void TestPassed(const std::string& test)
{
::memset(SIMPL::unittest::TestMessage, ' ', NUM_COLS); // Splat Spaces across the entire message
SIMPL::unittest::TestMessage[NUM_COLS] = 0; // Make sure it is null terminated
std::string::size_type size = NUM_COLS - SIMPL::unittest::SizeOfPassed;
::strncpy(&(SIMPL::unittest::TestMessage[size]), SIMPL::unittest::Passed, SIMPL::unittest::SizeOfPassed);
if(test.length() < size)
{
::strncpy(SIMPL::unittest::TestMessage, test.c_str(), test.length());
}
else
{
::strncpy(SIMPL::unittest::TestMessage, test.substr(0, size).c_str(), size);
}
SIMPL::unittest::TestMessage[NUM_COLS] = 0; // Make sure it is null terminated
std::cout << SIMPL::unittest::TestMessage << std::endl;
SIMPL::unittest::numTestsPass++;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void TestFailed(const std::string& test)
{
::memset(SIMPL::unittest::TestMessage, ' ', NUM_COLS); // Splat Spaces across the entire message
SIMPL::unittest::TestMessage[NUM_COLS] = 0; // Make sure it is null terminated
std::string::size_type size = NUM_COLS - SIMPL::unittest::SizeOfFailed;
::strncpy(&(SIMPL::unittest::TestMessage[size]), SIMPL::unittest::Failed, SIMPL::unittest::SizeOfFailed);
if(test.length() < size)
{
::strncpy(SIMPL::unittest::TestMessage, test.c_str(), test.length());
}
else
{
::strncpy(SIMPL::unittest::TestMessage, test.substr(0, size).c_str(), size);
}
SIMPL::unittest::TestMessage[NUM_COLS] = 0; // Make sure it is null terminated
std::cout << SIMPL::unittest::TestMessage << std::endl;
SIMPL::unittest::numTestFailed++;
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
#define INFINITYCHECK 1
#define SIGNCHECK 1
#ifdef INFINITYCHECK
inline bool IsInfinite(float* A)
{
const int kInfAsInt = 0x7F800000;
int* comp = reinterpret_cast<int*>(A);
// An infinity has an exponent of 255 (shift left 23 positions) and
// a zero mantissa. There are two infinities - positive and negative.
if((*comp & 0x7FFFFFFF) == kInfAsInt)
{
return true;
}
return false;
}
#endif
#ifdef NANCHECK
inline bool IsNan(float A)
{
// A NAN has an exponent of 255 (shifted left 23 positions) and
// a non-zero mantissa.
int exp = *(int*)&A & 0x7F800000;
int mantissa = *(int*)&A & 0x007FFFFF;
if(exp == 0x7F800000 && mantissa != 0)
{
return true;
}
return false;
}
#endif
#ifdef SIGNCHECK
inline int Sign(float* A)
{
int* comp = reinterpret_cast<int*>(A);
// The sign bit of a number is the high bit.
return *comp & 0x80000000;
}
#endif
bool AlmostEqualUlpsFinal(float* A, float* B, int maxUlps)
{
// There are several optional checks that you can do, depending
// on what behavior you want from your floating point comparisons.
// These checks should not be necessary and they are included
// mainly for completeness.
#ifdef INFINITYCHECK
// If A or B are infinity (positive or negative) then
// only return true if they are exactly equal to each other -
// that is, if they are both infinities of the same sign.
// This check is only needed if you will be generating
// infinities and you don't want them 'close' to numbers
// near FLT_MAX.
if(IsInfinite(A) || IsInfinite(B))
{
return *A == *B;
}
#endif
#ifdef NANCHECK
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves.
// This check is only needed if you will be generating NANs
// and you use a maxUlps greater than 4 million or you want to
// ensure that a NAN does not equal itself.
if(IsNan(A) || IsNan(B))
{
return false;
}
#endif
#ifdef SIGNCHECK
// After adjusting floats so their representations are lexicographically
// ordered as twos-complement integers a very small positive number
// will compare as 'close' to a very small negative number. If this is
// not desireable, and if you are on a platform that supports
// subnormals (which is the only place the problem can show up) then
// you need this check.
// The check for A == B is because zero and negative zero have different
// signs but are equal to each other.
if(Sign(A) != Sign(B))
{
return *A == *B;
}
#endif
int* aInt = reinterpret_cast<int*>(A);
// Make aInt lexicographically ordered as a twos-complement int
if(*aInt < 0)
{
*aInt = 0x80000000 - *aInt;
}
// Make bInt lexicographically ordered as a twos-complement int
int* bInt = reinterpret_cast<int*>(B);
if(*bInt < 0)
{
*bInt = 0x80000000 - *bInt;
}
// Now we can compare aInt and bInt to find out how far apart A and B
// are.
int intDiff = abs(*aInt - *bInt);
if(intDiff <= maxUlps)
{
return true;
}
return false;
}
// -----------------------------------------------------------------------------
// Developer Used Macros
// -----------------------------------------------------------------------------
#define DREAM3D_TEST_FAILED(P) \
{ \
DREAM3D_TEST_THROW_EXCEPTION(s) \
}
#define DREAM3D_REQUIRE(P) \
{ \
bool b = (P); \
if((b) == (false)) \
{ \
std::string s("Your test required the following\n '"); \
s = s.append(#P).append("'\n but this condition was not met."); \
DREAM3D_TEST_THROW_EXCEPTION(s) \
} \
}
#define DREAM3D_REQUIRED(L, Q, R) \
{ \
QString buf; \
QTextStream ss(&buf); \
bool b = (L Q R); \
if((b) == (false)) \
{ \
ss << "Your test required the following\n '"; \
ss << #L << " " << #Q << " " << #R << "' but this condition was not met.\n"; \
ss << " " << #L << " = " << L << "\n"; \
ss << " " << #R << " = " << R << "\n"; \
DREAM3D_TEST_THROW_EXCEPTION(buf.toStdString()) \
} \
}
#define DREAM3D_REQUIRED_PTR(L, Q, P) \
{ \
QString buf; \
QTextStream ss(&buf); \
bool b = (L Q P); \
if((b) == (false)) \
{ \
ss << "Your test required the following\n '"; \
ss << #L << " " << #Q << " " << #P << "' but this condition was not met.\n"; \
ss << " " << #L << " = " << L << "\n"; \
ss << " " << #P << " = \n"; \
DREAM3D_TEST_THROW_EXCEPTION(buf.toStdString()) \
} \
}
#define DREAM3D_REQUIRE_NE(L, R) \
if((L) == (R)) \
{ \
QString buf; \
QTextStream ss(&buf); \
ss << "Your test required the following\n '"; \
ss << #L << " != " << #R << "'\n but this condition was not met.\n"; \
ss << " " << L << "==" << R; \
DREAM3D_TEST_THROW_EXCEPTION(buf.toStdString()) \
}
#define DREAM3D_REQUIRE_EQUAL(L, R) \
if((L) != (R)) \
{ \
QString buf; \
QTextStream ss(&buf); \
ss << "Your test required the following\n '"; \
ss << #L << " == " << #R << "'\n but this condition was not met.\n"; \
ss << " " << L << "==" << R; \
DREAM3D_TEST_THROW_EXCEPTION(buf.toStdString()) \
}
#define DREAM3D_COMPARE_FLOATS(L, R, Ulps) \
if(false == AlmostEqualUlpsFinal((L), (R), Ulps)) \
{ \
QString buf; \
QTextStream ss(&buf); \
ss << "Your test required the following\n '"; \
ss << "AlmostEqualUlpsFinal(" << #L << ", " << #R << ", " << #Ulps << "'\n but this condition was not met with MaxUlps=" << Ulps << "\n"; \
ss << " " << L << "==" << R; \
DREAM3D_TEST_THROW_EXCEPTION(buf.toStdString()) \
}
#define DREAM3D_TEST_POINTER(L, Q, R) \
{ \
QString buf; \
QTextStream ss(&buf); \
bool b = (L Q R); \
if((b) == (false)) \
{ \
ss << "Your test required the following\n '"; \
ss << #L << " " << #Q << " " << #R << "' but this condition was not met.\n"; \
ss << " " << #L << " = "; \
if(L) \
{ \
ss << reinterpret_cast<void*>(L); \
} \
else \
{ \
ss << "Left side was nullptr"; \
} \
ss << "\n"; \
ss << " " << #R << " = "; \
if(R) \
{ \
ss << reinterpret_cast<void*>(R); \
} \
else \
{ \
ss << "Right Side was nullptr"; \
} \
ss << "\n"; \
DREAM3D_TEST_THROW_EXCEPTION(buf.toStdString()) \
} \
}
#define DREAM3D_REQUIRE_VALID_POINTER(L) \
{ \
QString buf; \
QTextStream ss(&buf); \
if(L == nullptr) \
{ \
ss << "Your test requires\n '"; \
ss << #L << "' != nullptr' but this condition was not met.\n"; \
ss << "\n"; \
DREAM3D_TEST_THROW_EXCEPTION(buf.toStdString()) \
} \
}
#define DREAM3D_REQUIRE_NULL_POINTER(L) \
{ \
QString buf; \
QTextStream ss(&buf); \
if(L != nullptr) \
{ \
ss << "Your test requires\n '"; \
ss << #L << " == nullptr' but this condition was not met.\n"; \
ss << "\n"; \
DREAM3D_TEST_THROW_EXCEPTION(buf.toStdString()) \
} \
}
// -----------------------------------------------------------------------------
// Private Macros. The Normal developer should NOT be using these.
// -----------------------------------------------------------------------------
#define DREAM3D_TEST_THROW_EXCEPTION(P) throw TestException(P, __FILE__, __LINE__);
#define DREAM3D_ASSERT(P) assert((P));
#define DREAM3D_ENTER_TEST(test) \
SIMPL::unittest::CurrentMethod = #test; \
SIMPL::unittest::numTests++;
#define DREAM3D_LEAVE_TEST(test) \
TestPassed(#test); \
SIMPL::unittest::CurrentMethod = "";
#define DREAM3D_REGISTER_TEST(test) \
try \
{ \
DREAM3D_ENTER_TEST(test); \
test; \
DREAM3D_LEAVE_TEST(test) \
} catch(TestException & e) \
{ \
TestFailed(SIMPL::unittest::CurrentMethod); \
std::cout << e.what() << std::endl; \
err = EXIT_FAILURE; \
}
#define PRINT_TEST_SUMMARY() \
std::cout << "Test Summary:" << std::endl; \
std::cout << " Tests Passed: " << SIMPL::unittest::numTestsPass << std::endl; \
std::cout << " Tests Failed: " << SIMPL::unittest::numTestFailed << std::endl; \
std::cout << " Total Tests: " << SIMPL::unittest::numTests << std::endl; \
if(SIMPL::unittest::numTestFailed > 0) \
{ \
err = EXIT_FAILURE; \
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
template <typename T, typename K> void require_equal(T l, const QString& L, K r, const QString& R, const QString file = "", int line = 0)
{
if(l != r)
{
QString buf;
QTextStream ss(&buf);
ss << "Your test required the following\n '";
ss << L << " == " << R << "'\n but this condition was not met.\n";
ss << " " << l << "==" << r;
// DREAM3D_TEST_THROW_EXCEPTION( buf.toStdString() )
throw TestException(buf.toStdString(), file.toStdString(), line);
}
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
template <typename T, typename K> void require_less_than(T l, const QString& L, K r, const QString& R, const QString file = "", int line = 0)
{
if(l >= r)
{
QString buf;
QTextStream ss(&buf);
ss << "Your test required the following\n '";
ss << L << " < " << R << "'\n but this condition was not met.\n";
ss << " " << l << "==" << r;
throw TestException(buf.toStdString(), file.toStdString(), line);
}
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
template <typename T, typename K> void require_greater_than(T l, const QString& L, K r, const QString& R, const QString file = "", int line = 0)
{
if(l <= r)
{
QString buf;
QTextStream ss(&buf);
ss << "Your test required the following\n '";
ss << L << " < " << R << "'\n but this condition was not met.\n";
ss << " " << l << "==" << r;
throw TestException(buf.toStdString(), file.toStdString(), line);
}
}