UTPP
Loading...
Searching...
No Matches
checks.h
Go to the documentation of this file.
1#pragma once
2/*
3 UTPP - A New Generation of UnitTest++
4 (c) Mircea Neacsu 2017-2024
5
6 See LICENSE file for full copyright information.
7*/
8
14
15#include <sstream>
16#include <vector>
17#include <array>
18#include <list>
19#include <string>
20#include <cmath>
21#include <cstring>
22#include <sys/stat.h>
23
29
37#ifdef CHECK
38#error Macro CHECK is already defined
39#endif
40#define CHECK(value) \
41 do \
42 { \
43 try { \
44 if (!UnitTest::Check(value)) \
45 UnitTest::ReportFailure (__FILE__, __LINE__, "Check failed: " #value);\
46 } \
47 catch (...) { \
48 UnitTest::ReportFailure (__FILE__, __LINE__, \
49 "Unhandled exception in CHECK(" #value ")"); \
50 } \
51 } while (0)
52
61#ifdef CHECK_EX
62#error Macro CHECK_EX is already defined
63#endif
64#define CHECK_EX(value, ...) \
65 do \
66 { \
67 try { \
68 if (!UnitTest::Check(value)){ \
69 char message[UnitTest::MAX_MESSAGE_SIZE]; \
70 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
71 UnitTest::ReportFailure (__FILE__, __LINE__, message); \
72 } \
73 } \
74 catch (...) { \
75 UnitTest::ReportFailure (__FILE__, __LINE__, \
76 "Unhandled exception in CHECK_EX(" #value ")"); \
77 } \
78 } while (0)
79
88#ifdef CHECK_EQUAL
89#error Macro CHECK_EQUAL is already defined
90#endif
91
92#define CHECK_EQUAL(expected, actual) \
93 do \
94 { \
95 try { \
96 std::string str__; \
97 if (!UnitTest::CheckEqual((expected), (actual), str__)) \
98 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
99 } \
100 catch (...) { \
101 UnitTest::ReportFailure (__FILE__, __LINE__, \
102 "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
103 } \
104 } while (0)
105
113#ifdef CHECK_NAN
114#error Macro CHECK_NAN is already defined
115#endif
116
117#define CHECK_NAN(value) \
118 do \
119 { \
120 try { \
121 if (!UnitTest::CheckNaN(value)) \
122 UnitTest::ReportFailure (__FILE__, __LINE__, "Check failed: " #value " is not NaN");\
123 } \
124 catch (...) { \
125 UnitTest::ReportFailure (__FILE__, __LINE__, \
126 "Unhandled exception in CHECK_NAN(" #value ")"); \
127 } \
128 } while (0)
129
140#ifdef CHECK_EQUAL_EX
141#error Macro CHECK_EQUAL_EX is already defined
142#endif
143#define CHECK_EQUAL_EX(expected, actual, ...) \
144 do \
145 { \
146 try { \
147 std::string str__; \
148 if (!UnitTest::CheckEqual((expected), (actual), str__)) \
149 { \
150 char message[UnitTest::MAX_MESSAGE_SIZE]; \
151 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
152 str__ += " - "; \
153 str__ += message; \
154 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
155 } \
156 } \
157 catch (...) { \
158 UnitTest::ReportFailure (__FILE__, __LINE__, \
159 "Unhandled exception in CHECK_EQUAL_EX(" #expected ", " #actual ")"); \
160 } \
161 } while (0)
162
176
177#ifdef CHECK_CLOSE
178#error Macro CHECK_CLOSE is already defined
179#endif
180#define CHECK_CLOSE(expected, actual,...) \
181 do \
182 { \
183 try { \
184 std::string str__; \
185 if (!UnitTest::CheckClose ((expected), (actual), (__VA_ARGS__+0), str__)) \
186 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
187 } \
188 catch (UnitTest::tolerance_not_set&) \
189 { \
190 throw UnitTest::test_abort (__FILE__, __LINE__, "UnitTest::default_tolerance not set"); \
191 } \
192 catch (...) { \
193 UnitTest::ReportFailure (__FILE__, __LINE__, \
194 "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \
195 } \
196 } while (0)
197
210
211#ifdef CHECK_CLOSE_EX
212#error Macro CHECK_CLOSE_EX is already defined
213#endif
214#define CHECK_CLOSE_EX(expected, actual, tolerance, ...) \
215 do \
216 { \
217 try { \
218 std::string str__; \
219 if (!UnitTest::CheckClose ((expected), (actual), (tolerance), str__)) \
220 { \
221 char message[UnitTest::MAX_MESSAGE_SIZE]; \
222 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
223 str__ += " - "; \
224 str__ += message; \
225 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
226 } \
227 } \
228 catch (UnitTest::tolerance_not_set&) \
229 { \
230 throw UnitTest::test_abort (__FILE__, __LINE__, "UnitTest::default_tolerance not set"); \
231 } \
232 catch (...) { \
233 UnitTest::ReportFailure (__FILE__, __LINE__, \
234 "Unhandled exception in CHECK_CLOSE_EX(" #expected ", " #actual ")"); \
235 } \
236 } while (0)
237
247#ifdef CHECK_ARRAY_EQUAL
248#error Macro CHECK_ARRAY_EQUAL is already defined
249#endif
250
251#define CHECK_ARRAY_EQUAL(expected, actual, count) \
252 do \
253 { \
254 try { \
255 std::string str__; \
256 if (!UnitTest::CheckArrayEqual ((expected), (actual), (count), str__)) \
257 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
258 } \
259 catch (...) { \
260 UnitTest::ReportFailure (__FILE__, __LINE__, \
261 "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \
262 } \
263 } while (0)
264
279#ifdef CHECK_ARRAY_CLOSE
280#error Macro CHECK_ARRAY_CLOSE is already defined
281#endif
282
283#define CHECK_ARRAY_CLOSE(expected, actual, count, ...) \
284 do \
285 { \
286 try { \
287 std::string str__; \
288 if (!UnitTest::CheckArrayClose ((expected), (actual), (count), (__VA_ARGS__+0), str__)) \
289 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
290 } \
291 catch (UnitTest::tolerance_not_set&) \
292 { \
293 throw UnitTest::test_abort (__FILE__, __LINE__, "UnitTest::default_tolerance not set"); \
294 } \
295 catch (...) { \
296 UnitTest::ReportFailure (__FILE__, __LINE__, \
297 "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
298 } \
299 } while (0)
300
311
312#ifdef CHECK_ARRAY2D_EQUAL
313#error Macro CHECK_ARRAY2D_EQUAL is already defined
314#endif
315
316#define CHECK_ARRAY2D_EQUAL(expected, actual, rows, columns) \
317 do \
318 { \
319 try { \
320 std::string str__; \
321 if (!UnitTest::CheckArray2DEqual ((expected), (actual), (rows), (columns), str__)) \
322 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
323 } \
324 catch (...) { \
325 UnitTest::ReportFailure (__FILE__, __LINE__, \
326 "Unhandled exception in CHECK_ARRAY2D_EQUAL(" #expected ", " #actual ")"); \
327 } \
328 } while (0)
329
330
346#ifdef CHECK_ARRAY2D_CLOSE
347#error Macro CHECK_ARRAY2D_CLOSE is already defined
348#endif
349
350#define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, ...) \
351 do \
352 { \
353 try { \
354 std::string str__; \
355 if (!UnitTest::CheckArray2DClose (expected, actual, rows, columns, (__VA_ARGS__+0), str__)) \
356 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
357 } \
358 catch (UnitTest::tolerance_not_set&) \
359 { \
360 throw UnitTest::test_abort (__FILE__, __LINE__, "UnitTest::default_tolerance not set"); \
361 } \
362 catch (...) { \
363 UnitTest::ReportFailure (__FILE__, __LINE__, \
364 "Unhandled exception in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")"); \
365 } \
366 } while (0)
367
376#ifdef CHECK_THROW
377#error Macro CHECK_THROW is already defined
378#endif
379#define CHECK_THROW(expr, except) \
380 do \
381 { \
382 bool caught_ = false; \
383 try { (expr); } \
384 catch (const except& ) { caught_ = true; } \
385 catch (...) { \
386 UnitTest::ReportFailure (__FILE__, __LINE__, \
387 "Unexpected exception in CHECK_THROW"); \
388 } \
389 if (!caught_) \
390 UnitTest::ReportFailure (__FILE__, __LINE__, \
391 "Expected exception: \"" #except "\", not thrown"); \
392 } while(0)
393
404#ifdef CHECK_THROW_EX
405#error Macro CHECK_THROW_EX is already defined
406#endif
407#define CHECK_THROW_EX(expr, except, ...) \
408 do \
409 { \
410 bool caught_ = false; \
411 try { (expr); } \
412 catch (const except& ) { caught_ = true; } \
413 catch (...) { \
414 UnitTest::ReportFailure (__FILE__, __LINE__, \
415 "Unexpected exception in CHECK_THROW_EX"); \
416 } \
417 if (!caught_) { \
418 std::string str__{"Expected exception: \"" #except "\", not thrown"}; \
419 char message[UnitTest::MAX_MESSAGE_SIZE]; \
420 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
421 str__ += " - "; \
422 str__ += message; \
423 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
424 } \
425 } while(0)
426
437
438#ifdef CHECK_THROW_EQUAL
439#error Macro CHECK_THROW_EQUAL is already defined
440#endif
441#define CHECK_THROW_EQUAL(expr, value, except) \
442 do \
443 { \
444 bool caught_ = false; \
445 try { expr; } \
446 catch (const except& actual) { \
447 caught_ = true; \
448 std::string str__; \
449 if (!UnitTest::CheckEqual(value, actual, str__)) \
450 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
451 } \
452 catch (...) { \
453 UnitTest::ReportFailure (__FILE__, __LINE__, \
454 "Unexpected exception in CHECK_THROW_EQUAL"); \
455 } \
456 if (!caught_) \
457 UnitTest::ReportFailure (__FILE__, __LINE__, \
458 "Expected exception: \"" #except "\", not thrown"); \
459 } while(0)
460
472
473#ifdef CHECK_THROW_EQUAL_EX
474#error Macro CHECK_THROW_EQUAL_EX is already defined
475#endif
476#define CHECK_THROW_EQUAL_EX(expr, value, except, ...) \
477 do \
478 { \
479 bool caught_ = false; \
480 try { expr; } \
481 catch (const except& actual) { \
482 caught_ = true; \
483 std::string str__; \
484 if (!UnitTest::CheckEqual(value, actual, str__)) \
485 { \
486 char message[UnitTest::MAX_MESSAGE_SIZE]; \
487 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
488 str__ += " - "; \
489 str__ += message; \
490 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
491 } \
492 } \
493 catch (...) { \
494 UnitTest::ReportFailure (__FILE__, __LINE__, \
495 "Unexpected exception in CHECK_THROW_EQUAL"); \
496 } \
497 if (!caught_) \
498 { \
499 std::string str__{ "Expected exception: \"" #except "\", not thrown" }; \
500 char message[UnitTest::MAX_MESSAGE_SIZE]; \
501 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
502 str__ += " - "; \
503 str__ += message; \
504 UnitTest::ReportFailure (__FILE__, __LINE__, \
505 "Expected exception: \"" #except "\", not thrown"); \
506 } \
507 } while(0)
508
517#ifdef CHECK_FILE_EQUAL
518#error Macro CHECK_FILE_EQUAL is already defined
519#endif
520#define CHECK_FILE_EQUAL(expected, actual) \
521 do \
522 { \
523 try { \
524 std::string str__; \
525 if (!UnitTest::CheckFileEqual((expected), (actual), str__)) \
526 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
527 } \
528 catch (...) { \
529 UnitTest::ReportFailure (__FILE__, __LINE__, \
530 "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
531 } \
532 } while (0)
533
541#ifdef FAILURE
542#error Macro FAILURE is already defined
543#endif
544#define FAILURE(...) \
545 do \
546 { \
547 char message[UnitTest::MAX_MESSAGE_SIZE]; \
548 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
549 UnitTest::ReportFailure (__FILE__, __LINE__, message); \
550 } while (0)
551
553
554namespace UnitTest {
555
563#if UTPP_CPP_LANG < 201703L
564extern double default_tolerance;
565#else
566inline double default_tolerance = 0;
567#endif
568
569//------------------ Check functions -----------------------------------------
570
577
578template <typename Value>
579bool Check (Value const value)
580{
581 return (bool)value;
582}
583
590template <typename Value>
591bool CheckNaN (Value const value)
592{
593 return std::isnan(value);
594}
595
605template <typename expected_T, typename actual_T>
606bool CheckEqual (const expected_T& expected, const actual_T& actual, std::string& msg)
607{
608 if (!(expected == actual))
609 {
610 std::stringstream stream;
611 stream << "Expected " << expected << " but was " << actual;
612 msg = stream.str ();
613 return false;
614 }
615 else
616 msg.clear ();
617 return true;
618}
619
620template <typename expected_T, typename actual_T>
621bool CheckEqual (const expected_T* expected, const actual_T* actual, std::string& msg)
622{
623 if (!(*expected == *actual))
624 {
625 std::stringstream stream;
626 stream << "Expected " << *expected << " but was " << *actual;
627 msg = stream.str ();
628 return false;
629 }
630 else
631 msg.clear ();
632 return true;
633}
634
635
647template <typename expected_T, typename actual_T>
648inline
649bool CheckEqual (const std::vector<expected_T>& expected, const std::vector<actual_T>& actual, std::string& msg)
650{
651 if (expected != actual)
652 {
653 std::stringstream stream;
654 stream << "Expected [ ";
655 for (auto& p : expected)
656 stream << p << " ";
657
658 stream << "] but was [ ";
659 for (auto& p : actual)
660 stream << p << " ";
661
662 stream << "]";
663 msg = stream.str ();
664 return false;
665 }
666 else
667 msg.clear ();
668 return true;
669}
670
682template <typename expected_T, typename actual_T, size_t N>
683inline
684bool CheckEqual (const std::array<expected_T,N>& expected, const std::array<actual_T,N>& actual, std::string& msg)
685{
686 if (expected != actual)
687 {
688 std::stringstream stream;
689 stream << "Expected [ ";
690 for (size_t i = 0; i < N; ++i)
691 stream << expected[i] << " ";
692
693 stream << "] but was [ ";
694 for (size_t i = 0; i < N; ++i)
695 stream << actual[i] << " ";
696
697 stream << "]";
698 msg = stream.str ();
699 return false;
700 }
701 else
702 msg.clear ();
703 return true;
704}
705
717template <typename expected_T, typename actual_T>
718inline
719bool CheckEqual (const std::list<expected_T>& expected, const std::list<actual_T>& actual, std::string& msg)
720{
721 if (expected != actual)
722 {
723 std::stringstream stream;
724 stream << "Expected ( ";
725 for (auto const& x : expected)
726 stream << x << " ";
727
728 stream << ") but was ( ";
729 for (auto const& x : actual)
730 stream << x << " ";
731
732 stream << ")";
733 msg = stream.str ();
734 return false;
735 }
736 else
737 msg.clear ();
738 return true;
739}
740
741
745#if __WCHAR_MAX__ > 0x10000 //4-bytes wchar_t
746inline
747std::string to_utf8 (const std::wstring& ws)
748{
749 std::string out;
750 auto in = ws.cbegin ();
751 while (in != ws.end ())
752 {
753 unsigned int c = (unsigned int)*in++;
754 if (c < 0x7f)
755 out.push_back ((char)c);
756 else if (c < 0x7ff)
757 {
758 out.push_back (0xC0 | (c >> 6));
759 out.push_back (0x80 | (c & 0x3f));
760 }
761 else if (c < 0xffff)
762 {
763 out.push_back (0xE0 | (c >> 12));
764 out.push_back (0x80 | ((c >> 6) & 0x3f));
765 out.push_back (0x80 | (c & 0x3f));
766 }
767 else
768 {
769 out.push_back (0xF0 | (c >> 18));
770 out.push_back (0x80 | ((c >> 12) & 0x3f));
771 out.push_back (0x80 | ((c >> 6) & 0x3f));
772 out.push_back (0x80 | (c & 0x3f));
773 }
774 }
775 return out;
776}
777#else //2-bytes wchar_t
778inline
779std::string to_utf8 (const std::wstring& ws)
780{
781 std::string out;
782 auto in = ws.cbegin ();
783 while (in != ws.end ())
784 {
785 unsigned int c1 = (unsigned int)*in++;
786 if (c1 < 0xD800 || c1 > 0xe000)
787 {
788 if (c1 < 0x7f)
789 out.push_back ((char)c1);
790 else if (c1 < 0x7ff)
791 {
792 out.push_back (char(0xC0 | (c1 >> 6)));
793 out.push_back (char(0x80 | (c1 & 0x3f)));
794 }
795 else
796 {
797 out.push_back (char(0xE0 | (c1 >> 12)));
798 out.push_back (char(0x80 | ((c1 >> 6) & 0x3f)));
799 out.push_back (char(0x80 | (c1 & 0x3f)));
800 }
801 }
802 else if (in != ws.end ())
803 {
804 unsigned int c2 = (unsigned int)*in++;
805 if (c1 > 0xdbff || c2 < 0xdc00)
806 break; // invalid high/low surrogates order
807
808 c1 &= 0x3ff;
809 c2 &= 0x3ff;
810
811 unsigned int c = ((c1 << 10) | c2) + 0x10000;
812
813 out.push_back (char(0xF0 | (c >> 18)));
814 out.push_back (char(0x80 | ((c >> 12) & 0x3f)));
815 out.push_back (char(0x80 | ((c >> 6) & 0x3f)));
816 out.push_back (char(0x80 | (c & 0x3f)));
817 }
818 else
819 break; //malformed input; just bail out
820 }
821 return out;
822}
823#endif
834inline
835bool CheckEqual (const std::wstring expected, const std::wstring actual,
836 std::string& msg)
837{
838 if (expected != actual)
839 {
840 std::stringstream stream;
841 auto u8exp = to_utf8 (expected);
842 auto u8act = to_utf8 (actual);
843 stream << "Expected \'" << u8exp << "\' but was \'" << u8act << "\'";
844 msg = stream.str ();
845 return false;
846 }
847 else
848 msg.clear ();
849 return true;
850}
851
862inline
863bool CheckEqual (const wchar_t *expected, const wchar_t *actual,
864 std::string &msg)
865{
866 if (wcscmp (expected, actual))
867 {
868 std::stringstream stream;
869 std::string u8exp = to_utf8 (expected);
870 std::string u8act = to_utf8 (actual);
871 stream << "Expected \'" << u8exp << "\' but was \'" << u8act << "\'";
872 msg = stream.str ();
873 return false;
874 }
875 else
876 msg.clear ();
877 return true;
878}
879
880inline
881bool CheckEqual(const wchar_t* expected, const std::wstring actual,
882 std::string& msg)
883{
884 return CheckEqual(std::wstring(expected), actual, msg);
885}
886
887inline
888bool CheckEqual(const std::wstring expected, const wchar_t* actual,
889 std::string& msg)
890{
891 return CheckEqual(expected, std::wstring(actual), msg);
892}
893
894inline
895bool CheckEqual(const std::wstring expected, wchar_t* actual,
896 std::string& msg)
897{
898 return CheckEqual(expected, std::wstring(actual), msg);
899}
900
901inline
902bool CheckEqual (wchar_t *expected, wchar_t *actual, std::string &msg)
903{
904 return CheckEqual (const_cast<const wchar_t *> (expected), const_cast<const wchar_t *> (actual), msg);
905}
906
907inline
908bool CheckEqual (const wchar_t *expected, wchar_t *actual, std::string &msg)
909{
910 return CheckEqual (expected, const_cast<const wchar_t *> (actual), msg);
911}
912
913inline
914bool CheckEqual (wchar_t *expected, const wchar_t *actual, std::string &msg)
915{
916 return CheckEqual (const_cast<const wchar_t *> (expected), actual, msg);
917}
918
920
929inline
930bool CheckEqual (const char* expected, const char* actual, std::string& msg)
931{
932 if (strcmp (expected, actual))
933 {
934 std::stringstream stream;
935 stream << "Expected \'" << expected << "\' but was \'" << actual << "\'";
936 msg = stream.str ();
937 return false;
938 }
939 return true;
940}
941
942inline
943bool CheckEqual (char* expected, char* actual, std::string& msg)
944{
945 return CheckEqual (const_cast<const char *>(expected), const_cast<const char*>(actual), msg);
946}
947
948inline
949bool CheckEqual (const char* expected, char* actual, std::string& msg)
950{
951 return CheckEqual (const_cast<const char *>(expected), const_cast<const char*>(actual), msg);
952}
953
954inline
955bool CheckEqual (char* expected, const char* actual, std::string& msg)
956{
957 return CheckEqual (const_cast<const char *>(expected), const_cast<const char*>(actual), msg);
958}
959
960
961#if UTPP_CPP_LANG > 201703L
962#if defined(__cpp_char8_t)
967inline
968bool CheckEqual (const char8_t* expected, const std::string& actual, std::string& msg)
969{
970 return CheckEqual ((const char*)expected, actual, msg);
971}
972
973inline
974bool CheckEqual (const std::string& expected, const char8_t* actual, std::string& msg)
975{
976 return CheckEqual (expected, (const char*)actual, msg);
977}
978
979inline
980bool CheckEqual (const char8_t* expected, const char8_t* actual, std::string& msg)
981{
982 return CheckEqual ((const char*)expected, (const char*)actual, msg);
983}
985#endif
986
990inline
991bool CheckEqual (char32_t expected, char32_t actual, std::string& msg)
992{
993 if (expected != actual)
994 {
995 std::stringstream stream;
996 stream << std::hex << "Expected (char32_t)U\'\\x" << static_cast<uint32_t>(expected)
997 << "\' but was (char32_t)U\'\\x" << static_cast<uint32_t>(actual) << "\'";
998 msg = stream.str ();
999 return false;
1000 }
1001 return true;
1002}
1003#endif
1004
1020template <typename expected_T, typename actual_T>
1021bool isClose (const expected_T& expected, const actual_T& actual, double tolerance)
1022{
1023 if (tolerance == 0)
1024 {
1027 tolerance = UnitTest::default_tolerance;
1028 }
1029 return fabs ((double)(actual - expected)) <= tolerance;
1030}
1031
1042template <typename expected_T, typename actual_T>
1043bool CheckClose (const expected_T& expected, const actual_T& actual, double tolerance,
1044 std::string& msg)
1045{
1046 if (!isClose(actual, expected, tolerance))
1047 {
1048 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1049 int prec = (int)(1 - log10 (fail_tol));
1050 std::stringstream stream;
1051 stream.precision (prec);
1052 stream.setf (std::ios::fixed);
1053 stream << "Expected " << expected << " +/- " << fail_tol << " but was " << actual;
1054 msg = stream.str ();
1055 return false;
1056 }
1057 else
1058 msg.clear ();
1059 return true;
1060}
1061
1070template <typename expected_T, typename actual_T>
1071bool Equal1D (const expected_T& expected, const actual_T& actual, size_t count)
1072{
1073 for (size_t i = 0; i < count; ++i)
1074 if (expected[i] != actual[i])
1075 return false;
1076 return true;
1077}
1078
1088template <typename expected_T, typename actual_T>
1089bool CheckArrayEqual (const expected_T& expected, const actual_T& actual,
1090 size_t count, std::string& msg)
1091{
1092 if (!Equal1D (expected, actual, count))
1093 {
1094 std::stringstream stream;
1095 stream << "Expected [ ";
1096 for (size_t i = 0; i < count; ++i)
1097 stream << expected[i] << " ";
1098
1099 stream << "] but was [ ";
1100 for (size_t i = 0; i < count; ++i)
1101 stream << actual[i] << " ";
1102
1103 stream << "]";
1104 msg = stream.str ();
1105 return false;
1106 }
1107 return true;
1108}
1109
1123template <typename expected_T, typename actual_T>
1124bool isClose1D (const expected_T& expected, const actual_T& actual, size_t count, double tolerance)
1125{
1126 for (size_t i = 0; i < count; ++i)
1127 {
1128 if (!isClose(expected[i], actual[i], tolerance))
1129 return false;
1130 }
1131 return true;
1132}
1133
1134
1147template <typename expected_T, typename actual_T>
1148bool CheckArrayClose (const expected_T& expected, const actual_T& actual, size_t count,
1149 double tolerance, std::string& msg)
1150{
1151 if (!isClose1D (expected, actual, count, tolerance))
1152 {
1153 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1154 int prec = (int)(1 - log10 (fail_tol));
1155
1156 std::stringstream stream;
1157 stream.precision (prec);
1158 stream.setf (std::ios::fixed);
1159 stream << "Expected [ ";
1160 for (size_t i = 0; i < count; ++i)
1161 stream << expected[i] << " ";
1162
1163 stream << "] +/- " << fail_tol << " but was [ ";
1164 for (size_t i = 0; i < count; ++i)
1165 stream << actual[i] << " ";
1166 stream << "]";
1167 msg = stream.str ();
1168 return false;
1169 }
1170 return true;
1171}
1172
1182template <>
1183inline
1184bool CheckEqual<void, void>(const void* expected, const void* actual, std::string& msg)
1185{
1186 if (!(expected == actual)) {
1187 std::stringstream stream;
1188 stream << "Expected " << expected << " but was " << actual;
1189 msg = stream.str();
1190 return false;
1191 } else
1192 msg.clear();
1193 return true;
1194}
1195
1206template <typename expected_T, typename actual_T>
1207bool CheckClose (const std::vector<expected_T>& expected, const std::vector<actual_T>& actual, double tolerance,
1208 std::string& msg)
1209{
1210 if (expected.size () != actual.size ()
1211 || !isClose1D (&expected[0], &actual[0], expected.size(), tolerance))
1212 {
1213 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1214 int prec = (int)(1 - log10 (fail_tol));
1215 std::stringstream stream;
1216 stream.precision (prec);
1217 stream.setf (std::ios::fixed);
1218 stream << "Expected [ ";
1219 for (auto& p : expected)
1220 stream << p << " ";
1221
1222 stream << "] +/- " << fail_tol << " but was [ ";
1223 for (auto& p : actual)
1224 stream << p << " ";
1225 stream << "]";
1226 msg = stream.str ();
1227 return false;
1228 }
1229 return true;
1230}
1231
1242template <typename expected_T, typename actual_T, size_t N>
1243bool CheckClose (const std::array<expected_T, N>& expected, const std::array<actual_T, N>& actual, double tolerance,
1244 std::string& msg)
1245{
1246 if (!isClose1D (&expected[0], &actual[0], N, tolerance))
1247 {
1248 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1249 int prec = (int)(1 - log10 (fail_tol));
1250 std::stringstream stream;
1251 stream.precision (prec);
1252 stream.setf (std::ios::fixed);
1253 stream << "Expected [ ";
1254 for (auto& p : expected)
1255 stream << p << " ";
1256
1257 stream << "] +/- " << fail_tol << " but was [ ";
1258 for (auto& p : actual)
1259 stream << p << " ";
1260 stream << "]";
1261 msg = stream.str ();
1262 return false;
1263 }
1264 return true;
1265}
1266
1276template <typename expected_T, typename actual_T>
1277bool Equal2D (const expected_T& expected, const actual_T& actual, size_t rows, size_t columns)
1278{
1279 for (size_t i = 0; i < rows; ++i)
1280 if (!Equal1D (expected[i], actual[i], columns))
1281 return false;
1282 return true;
1283}
1284
1295template <typename expected_T, typename actual_T>
1296bool CheckArray2DEqual (const expected_T& expected, const actual_T& actual,
1297 size_t rows, size_t columns, std::string& msg)
1298{
1299 if (!Equal2D (expected, actual, rows, columns))
1300 {
1301 std::stringstream stream;
1302 size_t i, j;
1303 stream << "Expected [\n";
1304 for (i = 0; i < rows; ++i)
1305 {
1306 stream << " [";
1307 for (j = 0; j < columns; ++j)
1308 stream << expected[i][j] << " ";
1309 stream << "]\n";
1310 }
1311
1312 stream << "] but was [\n";
1313 for (i = 0; i < rows; ++i)
1314 {
1315 stream << " [";
1316 for (j = 0; j < columns; ++j)
1317 stream << actual[i][j] << " ";
1318 stream << "]\n";
1319 }
1320 stream << "]";
1321 msg = stream.str ();
1322 return false;
1323 }
1324 return true;
1325}
1326
1337template <typename expected_T, typename actual_T>
1338bool isClose2D (const expected_T& expected, const actual_T& actual, size_t rows, size_t columns,
1339 double tolerance)
1340{
1341 for (size_t i = 0; i < rows; ++i)
1342 if (!isClose1D (expected[i], actual[i], columns, tolerance))
1343 return false;
1344 return true;
1345}
1346
1359template <typename expected_T, typename actual_T>
1360bool CheckArray2DClose (const expected_T& expected, const actual_T& actual,
1361 size_t rows, size_t columns, double tolerance, std::string& msg)
1362{
1363 if (!isClose2D (expected, actual, rows, columns, tolerance))
1364 {
1365 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1366 int prec = (int)(1 - log10 (fail_tol));
1367 std::stringstream stream;
1368 stream.precision (prec);
1369 stream.setf (std::ios::fixed);
1370 stream << "Expected [\n";
1371 size_t i, j;
1372 for (i = 0; i < rows; ++i)
1373 {
1374 stream << " [ ";
1375 for (j = 0; j < columns; ++j)
1376 stream << expected[i][j] << " ";
1377 stream << "]\n";
1378 }
1379
1380 stream << "] +/- " << fail_tol << " but was [\n";
1381 for (i = 0; i < rows; ++i)
1382 {
1383 stream << " [ ";
1384 for (j = 0; j < columns; ++j)
1385 stream << actual[i][j] << " ";
1386 stream << "]\n";
1387 }
1388 stream << "]";
1389 msg = stream.str ();
1390 return false;
1391 }
1392 msg.clear ();
1393 return true;
1394}
1395
1406inline
1407bool CheckFileEqual (const std::string& ref, const std::string& actual, std::string& message)
1408{
1409 struct stat st1, st2;
1410 std::ostringstream buf;
1411
1412 stat (ref.c_str(), &st1);
1413 stat (actual.c_str(), &st2);
1414 if (st1.st_size != st2.st_size)
1415 {
1416 buf << "Size is different (" << st1.st_size << " vs " << st2.st_size
1417 << ") while comparing " << ref << " and " << actual;
1418 message = buf.str();
1419 return false;
1420 }
1421
1422 FILE* f1, * f2;
1423 f1 = fopen (ref.c_str(), "r");
1424 f2 = fopen (actual.c_str(), "r");
1425 if (!f1 || !f2)
1426 {
1427 if (f1) fclose (f1);
1428 if (f2) fclose (f2);
1429 buf << "Failed to open files while comparing "
1430 << ref << " and " << actual;
1431 message = buf.str();
1432 return false; //something wrong with files
1433 }
1434
1435 size_t ln = 0;
1436 bool ok = true;
1437 char ln1[1024], ln2[1024];
1438 while (ok)
1439 {
1440 ln++;
1441 if (fgets (ln1, sizeof (ln1), f1)
1442 && fgets (ln2, sizeof (ln2), f2))
1443 ok = !strcmp (ln1, ln2);
1444 else
1445 break;
1446 }
1447 fclose (f1);
1448 fclose (f2);
1449 if (!ok)
1450 {
1451 char* p1, * p2;
1452 int off;
1453 for (off = 0, p1 = ln1, p2 = ln2;
1454 *p1 && *p2 && *p1 == *p2;
1455 p1++, p2++, off++)
1456 ;
1457 buf << "Difference at line " << ln << " position " << off
1458 << " while comparing " << ref << " and " << actual;
1459 message = buf.str();
1460 }
1461 else
1462 message.clear ();
1463 return ok;
1464}
1465
1466} //end namespace
1467
1471
1475#define EXPECT_TRUE(x) CHECK (x)
1476
1481#define EXPECT_FALSE(x) CHECK (!(x))
1482
1487#define EXPECT_EQ(A, B) CHECK_EQUAL (B, A)
1488
1492#define EXPECT_NE(A, B) \
1493 do \
1494 { \
1495 try { \
1496 std::string str__; \
1497 if (UnitTest::CheckEqual ((A), (B), str__)) \
1498 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
1499 } \
1500 catch (...) { \
1501 UnitTest::ReportFailure (__FILE__, __LINE__, \
1502 "Unhandled exception in EXPECT_NE(" #A ", " #B ")"); \
1503 } \
1504 } while (0)
1505
1509#define EXPECT_GE(A, B) CHECK ((A) >= (B))
1510
1514#define EXPECT_GT(A, B) CHECK ((A) > (B))
1515
1519#define EXPECT_LE(A, B) CHECK ((A) <= (B))
1520
1524#define EXPECT_LT(A, B) CHECK ((A) < (B))
1525
1529#define EXPECT_NEAR(A, B, tol) CHECK_CLOSE(B, A, tol)
1530
1534#define EXPECT_THROW(expr, except) CHECK_THROW(expr, except)
1535
1539#define ASSERT_TRUE(expr) ABORT (!(expr))
1540
1544#define ASSERT_FALSE(expr) ABORT (expr)
1545
1549#define ASSERT_EQ(e1, e2) \
1550 do \
1551 { \
1552 std::string str__; \
1553 if (!UnitTest::CheckEqual((e1), (e2), str__)) \
1554 { \
1555 std::stringstream stream__; \
1556 stream__ << (e1) << " and " << (e2) << " should be equal"; \
1557 throw UnitTest::test_abort (__FILE__, __LINE__, str__.c_str()); \
1558 } \
1559 } while (0)
1560
1564#define ASSERT_NE(e1, e2) \
1565 do \
1566 { \
1567 std::string str__; \
1568 if (UnitTest::CheckEqual ((e1), (e2), str__)) \
1569 { \
1570 std::stringstream stream__; \
1571 stream__ << (e1) << " and " << (e2) << " should be different"; \
1572 throw UnitTest::test_abort (__FILE__, __LINE__, stream__.str ().c_str ());\
1573 } \
1574 } while (0)
1575
1579#define ASSERT_GE(e1, e2) ABORT ((e1) < (e2))
1580
1584#define ASSERT_GT(e1, e2) ABORT ((e1) <= (e2))
1585
1589#define ASSERT_LE(e1, e2) ABORT ((e1) > (e2))
1590
1594#define ASSERT_LT(e1, e2) ABORT ((e1) >= (e2))
1596
bool CheckNaN(Value const value)
Definition checks.h:591
double default_tolerance
std::string to_utf8(const std::wstring &ws)
Definition checks.h:779
bool CheckArray2DClose(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns, double tolerance, std::string &msg)
Definition checks.h:1360
bool CheckClose(const expected_T &expected, const actual_T &actual, double tolerance, std::string &msg)
Definition checks.h:1043
bool Check(Value const value)
Definition checks.h:579
bool isClose1D(const expected_T &expected, const actual_T &actual, size_t count, double tolerance)
Definition checks.h:1124
bool CheckArrayClose(const expected_T &expected, const actual_T &actual, size_t count, double tolerance, std::string &msg)
Definition checks.h:1148
bool CheckArrayEqual(const expected_T &expected, const actual_T &actual, size_t count, std::string &msg)
Definition checks.h:1089
bool CheckArray2DEqual(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns, std::string &msg)
Definition checks.h:1296
bool CheckEqual(const expected_T &expected, const actual_T &actual, std::string &msg)
Definition checks.h:606
bool Equal1D(const expected_T &expected, const actual_T &actual, size_t count)
Definition checks.h:1071
bool isClose2D(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns, double tolerance)
Definition checks.h:1338
bool CheckFileEqual(const std::string &ref, const std::string &actual, std::string &message)
Definition checks.h:1407
bool Equal2D(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns)
Definition checks.h:1277
bool isClose(const expected_T &expected, const actual_T &actual, double tolerance)
Definition checks.h:1021
Definition utpp.h:481