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
16#if !defined(_CRT_SECURE_NO_WARNINGS)
17#define _CRT_SECURE_NO_WARNINGS
18#endif
20
21#include <sstream>
22#include <vector>
23#include <array>
24#include <list>
25#include <string>
26#include <cmath>
27#include <cstring>
28
33
40#ifdef CHECK
41#error Macro CHECK is already defined
42#endif
43#define CHECK(value) \
44 do \
45 { \
46 try { \
47 if (!UnitTest::Check(value)) \
48 UnitTest::ReportFailure (__FILE__, __LINE__, "Check failed: " #value);\
49 } \
50 catch (...) { \
51 UnitTest::ReportFailure (__FILE__, __LINE__, \
52 "Unhandled exception in CHECK(" #value ")"); \
53 } \
54 } while (0)
55
62#ifdef CHECK_EX
63#error Macro CHECK_EX is already defined
64#endif
65#define CHECK_EX(value, ...) \
66 do \
67 { \
68 try { \
69 if (!UnitTest::Check(value)){ \
70 char message[UnitTest::MAX_MESSAGE_SIZE]; \
71 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
72 UnitTest::ReportFailure (__FILE__, __LINE__, message); \
73 } \
74 } \
75 catch (...) { \
76 UnitTest::ReportFailure (__FILE__, __LINE__, \
77 "Unhandled exception in CHECK_EX(" #value ")"); \
78 } \
79 } while (0)
80
87#ifdef CHECK_EQUAL
88#error Macro CHECK_EQUAL is already defined
89#endif
90
91#define CHECK_EQUAL(expected, actual) \
92 do \
93 { \
94 try { \
95 std::string str__; \
96 if (!UnitTest::CheckEqual((expected), (actual), str__)) \
97 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
98 } \
99 catch (...) { \
100 UnitTest::ReportFailure (__FILE__, __LINE__, \
101 "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
102 } \
103 } while (0)
104
111#ifdef CHECK_NAN
112#error Macro CHECK_NAN is already defined
113#endif
114
115#define CHECK_NAN(value) \
116 do \
117 { \
118 try { \
119 if (!UnitTest::CheckNaN(value)) \
120 UnitTest::ReportFailure (__FILE__, __LINE__, "Check failed: " #value " is not NaN");\
121 } \
122 catch (...) { \
123 UnitTest::ReportFailure (__FILE__, __LINE__, \
124 "Unhandled exception in CHECK_NAN(" #value ")"); \
125 } \
126 } while (0)
127
135#ifdef CHECK_EQUAL_EX
136#error Macro CHECK_EQUAL_EX is already defined
137#endif
138#define CHECK_EQUAL_EX(expected, actual, ...) \
139 do \
140 { \
141 try { \
142 std::string str__; \
143 if (!UnitTest::CheckEqual((expected), (actual), str__)) \
144 { \
145 char message[UnitTest::MAX_MESSAGE_SIZE]; \
146 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
147 str__ += " - "; \
148 str__ += message; \
149 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
150 } \
151 } \
152 catch (...) { \
153 UnitTest::ReportFailure (__FILE__, __LINE__, \
154 "Unhandled exception in CHECK_EQUAL_EX(" #expected ", " #actual ")"); \
155 } \
156 } while (0)
157
165
166#ifdef CHECK_CLOSE
167#error Macro CHECK_CLOSE is already defined
168#endif
169#define CHECK_CLOSE(expected, actual,...) \
170 do \
171 { \
172 try { \
173 std::string str__; \
174 if (!UnitTest::CheckClose ((expected), (actual), (__VA_ARGS__+0), str__)) \
175 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
176 } \
177 catch (UnitTest::tolerance_not_set&) \
178 { \
179 throw UnitTest::test_abort (__FILE__, __LINE__, "UnitTest::default_tolerance not set"); \
180 } \
181 catch (...) { \
182 UnitTest::ReportFailure (__FILE__, __LINE__, \
183 "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \
184 } \
185 } while (0)
186
194
195#ifdef CHECK_CLOSE_EX
196#error Macro CHECK_CLOSE_EX is already defined
197#endif
198#define CHECK_CLOSE_EX(expected, actual, tolerance, ...) \
199 do \
200 { \
201 try { \
202 std::string str__; \
203 if (!UnitTest::CheckClose ((expected), (actual), (tolerance), str__)) \
204 { \
205 char message[UnitTest::MAX_MESSAGE_SIZE]; \
206 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
207 str__ += " - "; \
208 str__ += message; \
209 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
210 } \
211 } \
212 catch (UnitTest::tolerance_not_set&) \
213 { \
214 throw UnitTest::test_abort (__FILE__, __LINE__, "UnitTest::default_tolerance not set"); \
215 } \
216 catch (...) { \
217 UnitTest::ReportFailure (__FILE__, __LINE__, \
218 "Unhandled exception in CHECK_CLOSE_EX(" #expected ", " #actual ")"); \
219 } \
220 } while (0)
221
228#ifdef CHECK_ARRAY_EQUAL
229#error Macro CHECK_ARRAY_EQUAL is already defined
230#endif
231
232#define CHECK_ARRAY_EQUAL(expected, actual, count) \
233 do \
234 { \
235 try { \
236 std::string str__; \
237 if (!UnitTest::CheckArrayEqual ((expected), (actual), (count), str__)) \
238 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
239 } \
240 catch (...) { \
241 UnitTest::ReportFailure (__FILE__, __LINE__, \
242 "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \
243 } \
244 } while (0)
245
253#ifdef CHECK_ARRAY_CLOSE
254#error Macro CHECK_ARRAY_CLOSE is already defined
255#endif
256
257#define CHECK_ARRAY_CLOSE(expected, actual, count, ...) \
258 do \
259 { \
260 try { \
261 std::string str__; \
262 if (!UnitTest::CheckArrayClose ((expected), (actual), (count), (__VA_ARGS__+0), str__)) \
263 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
264 } \
265 catch (UnitTest::tolerance_not_set&) \
266 { \
267 throw UnitTest::test_abort (__FILE__, __LINE__, "UnitTest::default_tolerance not set"); \
268 } \
269 catch (...) { \
270 UnitTest::ReportFailure (__FILE__, __LINE__, \
271 "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
272 } \
273 } while (0)
274
281
282#ifdef CHECK_ARRAY2D_EQUAL
283#error Macro CHECK_ARRAY2D_EQUAL is already defined
284#endif
285
286#define CHECK_ARRAY2D_EQUAL(expected, actual, rows, columns) \
287 do \
288 { \
289 try { \
290 std::string str__; \
291 if (!UnitTest::CheckArray2DEqual ((expected), (actual), (rows), (columns), str__)) \
292 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
293 } \
294 catch (...) { \
295 UnitTest::ReportFailure (__FILE__, __LINE__, \
296 "Unhandled exception in CHECK_ARRAY2D_EQUAL(" #expected ", " #actual ")"); \
297 } \
298 } while (0)
299
300
308#ifdef CHECK_ARRAY2D_CLOSE
309#error Macro CHECK_ARRAY2D_CLOSE is already defined
310#endif
311
312#define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, ...) \
313 do \
314 { \
315 try { \
316 std::string str__; \
317 if (!UnitTest::CheckArray2DClose (expected, actual, rows, columns, (__VA_ARGS__+0), str__)) \
318 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
319 } \
320 catch (UnitTest::tolerance_not_set&) \
321 { \
322 throw UnitTest::test_abort (__FILE__, __LINE__, "UnitTest::default_tolerance not set"); \
323 } \
324 catch (...) { \
325 UnitTest::ReportFailure (__FILE__, __LINE__, \
326 "Unhandled exception in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")"); \
327 } \
328 } while (0)
329
338#ifdef CHECK_THROW
339#error Macro CHECK_THROW is already defined
340#endif
341#define CHECK_THROW(expr, except) \
342 do \
343 { \
344 bool caught_ = false; \
345 try { (expr); } \
346 catch (const except& ) { caught_ = true; } \
347 catch (...) { \
348 UnitTest::ReportFailure (__FILE__, __LINE__, \
349 "Unexpected exception in CHECK_THROW"); \
350 } \
351 if (!caught_) \
352 UnitTest::ReportFailure (__FILE__, __LINE__, \
353 "Expected exception: \"" #except "\", not thrown"); \
354 } while(0)
355
364#ifdef CHECK_THROW_EX
365#error Macro CHECK_THROW_EX is already defined
366#endif
367#define CHECK_THROW_EX(expr, except, ...) \
368 do \
369 { \
370 bool caught_ = false; \
371 try { (expr); } \
372 catch (const except& ) { caught_ = true; } \
373 catch (...) { \
374 UnitTest::ReportFailure (__FILE__, __LINE__, \
375 "Unexpected exception in CHECK_THROW_EX"); \
376 } \
377 if (!caught_) { \
378 std::string str__{"Expected exception: \"" #except "\", not thrown"}; \
379 char message[UnitTest::MAX_MESSAGE_SIZE]; \
380 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
381 str__ += " - "; \
382 str__ += message; \
383 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
384 } \
385 } while(0)
386
394
395#ifdef CHECK_THROW_EQUAL
396#error Macro CHECK_THROW_EQUAL is already defined
397#endif
398#define CHECK_THROW_EQUAL(expression, value, except) \
399 do \
400 { \
401 bool caught_ = false; \
402 try { expression; } \
403 catch (const except& actual) { \
404 caught_ = true; \
405 std::string str__; \
406 if (!UnitTest::CheckEqual(value, actual, str__)) \
407 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
408 } \
409 catch (...) { \
410 UnitTest::ReportFailure (__FILE__, __LINE__, \
411 "Unexpected exception in CHECK_THROW_EQUAL"); \
412 } \
413 if (!caught_) \
414 UnitTest::ReportFailure (__FILE__, __LINE__, \
415 "Expected exception: \"" #except "\", not thrown"); \
416 } while(0)
417
426
427#ifdef CHECK_THROW_EQUAL_EX
428#error Macro CHECK_THROW_EQUAL_EX is already defined
429#endif
430#define CHECK_THROW_EQUAL_EX(expression, value, except, ...) \
431 do \
432 { \
433 bool caught_ = false; \
434 try { expression; } \
435 catch (const except& actual) { \
436 caught_ = true; \
437 std::string str__; \
438 if (!UnitTest::CheckEqual(value, actual, str__)) \
439 { \
440 char message[UnitTest::MAX_MESSAGE_SIZE]; \
441 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
442 str__ += " - "; \
443 str__ += message; \
444 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
445 } \
446 } \
447 catch (...) { \
448 UnitTest::ReportFailure (__FILE__, __LINE__, \
449 "Unexpected exception in CHECK_THROW_EQUAL"); \
450 } \
451 if (!caught_) \
452 { \
453 std::string str__{ "Expected exception: \"" #except "\", not thrown" }; \
454 char message[UnitTest::MAX_MESSAGE_SIZE]; \
455 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
456 str__ += " - "; \
457 str__ += message; \
458 UnitTest::ReportFailure (__FILE__, __LINE__, \
459 "Expected exception: \"" #except "\", not thrown"); \
460 } \
461 } while(0)
462
469#ifdef CHECK_FILE_EQUAL
470#error Macro CHECK_FILE_EQUAL is already defined
471#endif
472#define CHECK_FILE_EQUAL(expected, actual) \
473 do \
474 { \
475 try { \
476 std::string str__; \
477 if (!UnitTest::CheckFileEqual((expected), (actual), str__)) \
478 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
479 } \
480 catch (...) { \
481 UnitTest::ReportFailure (__FILE__, __LINE__, \
482 "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
483 } \
484 } while (0)
485
492#ifdef FAILURE
493#error Macro FAILURE is already defined
494#endif
495#define FAILURE(...) \
496 do \
497 { \
498 char message[UnitTest::MAX_MESSAGE_SIZE]; \
499 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
500 UnitTest::ReportFailure (__FILE__, __LINE__, message); \
501 } while (0)
502
504
505namespace UnitTest {
506
507#if _MSVC_LANG < 201703L
509extern double default_tolerance;
510#else
512inline double default_tolerance = 0;
513#endif
514
515//------------------ Check functions -----------------------------------------
516
523
524template <typename Value>
525bool Check (Value const value)
526{
527 return (bool)value;
528}
529
536template <typename Value>
537bool CheckNaN (Value const value)
538{
539 return std::isnan(value);
540}
541
551template <typename expected_T, typename actual_T>
552bool CheckEqual (const expected_T& expected, const actual_T& actual, std::string& msg)
553{
554 if (!(expected == actual))
555 {
556 std::stringstream stream;
557 stream << "Expected " << expected << " but was " << actual;
558 msg = stream.str ();
559 return false;
560 }
561 else
562 msg.clear ();
563 return true;
564}
565
566template <typename expected_T, typename actual_T>
567bool CheckEqual (const expected_T* expected, const actual_T* actual, std::string& msg)
568{
569 if (!(*expected == *actual))
570 {
571 std::stringstream stream;
572 stream << "Expected " << *expected << " but was " << *actual;
573 msg = stream.str ();
574 return false;
575 }
576 else
577 msg.clear ();
578 return true;
579}
580
581
593template <typename expected_T, typename actual_T>
594inline
595bool CheckEqual (const std::vector<expected_T>& expected, const std::vector<actual_T>& actual, std::string& msg)
596{
597 if (expected != actual)
598 {
599 std::stringstream stream;
600 stream << "Expected [ ";
601 for (auto& p : expected)
602 stream << p << " ";
603
604 stream << "] but was [ ";
605 for (auto& p : actual)
606 stream << p << " ";
607
608 stream << "]";
609 msg = stream.str ();
610 return false;
611 }
612 else
613 msg.clear ();
614 return true;
615}
616
628template <typename expected_T, typename actual_T, size_t N>
629inline
630bool CheckEqual (const std::array<expected_T,N>& expected, const std::array<actual_T,N>& actual, std::string& msg)
631{
632 if (expected != actual)
633 {
634 std::stringstream stream;
635 stream << "Expected [ ";
636 for (size_t i = 0; i < N; ++i)
637 stream << expected[i] << " ";
638
639 stream << "] but was [ ";
640 for (size_t i = 0; i < N; ++i)
641 stream << actual[i] << " ";
642
643 stream << "]";
644 msg = stream.str ();
645 return false;
646 }
647 else
648 msg.clear ();
649 return true;
650}
651
663template <typename expected_T, typename actual_T>
664inline
665bool CheckEqual (const std::list<expected_T>& expected, const std::list<actual_T>& actual, std::string& msg)
666{
667 if (expected != actual)
668 {
669 std::stringstream stream;
670 stream << "Expected ( ";
671 for (auto const& x : expected)
672 stream << x << " ";
673
674 stream << ") but was ( ";
675 for (auto const& x : actual)
676 stream << x << " ";
677
678 stream << ")";
679 msg = stream.str ();
680 return false;
681 }
682 else
683 msg.clear ();
684 return true;
685}
686
687
689#if __WCHAR_MAX__ > 0x10000
690inline std::string to_utf8 (const std::wstring& ws)
691{
692 std::string out;
693 auto in = ws.cbegin ();
694 while (in != ws.end ())
695 {
696 unsigned int c = (unsigned int)*in++;
697 if (c < 0x7f)
698 out.push_back ((char)c);
699 else if (c < 0x7ff)
700 {
701 out.push_back (0xC0 | (c >> 6));
702 out.push_back (0x80 | (c & 0x3f));
703 }
704 else if (c < 0xffff)
705 {
706 out.push_back (0xE0 | (c >> 12));
707 out.push_back (0x80 | ((c >> 6) & 0x3f));
708 out.push_back (0x80 | (c & 0x3f));
709 }
710 else
711 {
712 out.push_back (0xF0 | (c >> 18));
713 out.push_back (0x80 | ((c >> 12) & 0x3f));
714 out.push_back (0x80 | ((c >> 6) & 0x3f));
715 out.push_back (0x80 | (c & 0x3f));
716 }
717 }
718 return out;
719}
720#else
721inline std::string to_utf8 (const std::wstring& ws)
722{
723 std::string out;
724 auto in = ws.cbegin ();
725 while (in != ws.end ())
726 {
727 unsigned int c1 = (unsigned int)*in++;
728 if (c1 < 0xD800 || c1 > 0xe000)
729 {
730 if (c1 < 0x7f)
731 out.push_back ((char)c1);
732 else if (c1 < 0x7ff)
733 {
734 out.push_back (char(0xC0 | (c1 >> 6)));
735 out.push_back (char(0x80 | (c1 & 0x3f)));
736 }
737 else
738 {
739 out.push_back (char(0xE0 | (c1 >> 12)));
740 out.push_back (char(0x80 | ((c1 >> 6) & 0x3f)));
741 out.push_back (char(0x80 | (c1 & 0x3f)));
742 }
743 }
744 else if (in != ws.end ())
745 {
746 unsigned int c2 = (unsigned int)*in++;
747 if (c1 > 0xdbff || c2 < 0xdc00)
748 break; // invalid high/low surrogates order
749
750 c1 &= 0x3ff;
751 c2 &= 0x3ff;
752
753 unsigned int c = ((c1 << 10) | c2) + 0x10000;
754
755 out.push_back (char(0xF0 | (c >> 18)));
756 out.push_back (char(0x80 | ((c >> 12) & 0x3f)));
757 out.push_back (char(0x80 | ((c >> 6) & 0x3f)));
758 out.push_back (char(0x80 | (c & 0x3f)));
759 }
760 else
761 break; //malformed input; just bail out
762 }
763 return out;
764}
765#endif
776inline
777bool CheckEqual (const std::wstring expected, const std::wstring actual,
778 std::string& msg)
779{
780 if (expected != actual)
781 {
782 std::stringstream stream;
783 std::string u8exp = to_utf8 (expected);
784 std::string u8act = to_utf8 (actual);
785 stream << "Expected \'" << u8exp << "\' but was \'" << u8act << "\'";
786 msg = stream.str ();
787 return false;
788 }
789 else
790 msg.clear ();
791 return true;
792}
793
804inline
805bool CheckEqual (const wchar_t *expected, const wchar_t *actual,
806 std::string &msg)
807{
808 if (wcscmp (expected, actual))
809 {
810 std::stringstream stream;
811 std::string u8exp = to_utf8 (expected);
812 std::string u8act = to_utf8 (actual);
813 stream << "Expected \'" << u8exp << "\' but was \'" << u8act << "\'";
814 msg = stream.str ();
815 return false;
816 }
817 else
818 msg.clear ();
819 return true;
820}
821
822
823inline
824bool CheckEqual (wchar_t *expected, wchar_t *actual, std::string &msg)
825{
826 return CheckEqual (const_cast<const wchar_t *> (expected), const_cast<const wchar_t *> (actual), msg);
827}
828
829inline
830bool CheckEqual (const wchar_t *expected, wchar_t *actual, std::string &msg)
831{
832 return CheckEqual (expected, const_cast<const wchar_t *> (actual), msg);
833}
834
835inline
836bool CheckEqual (wchar_t *expected, const wchar_t *actual, std::string &msg)
837{
838 return CheckEqual (const_cast<const wchar_t *> (expected), actual, msg);
839}
840
842
847inline
848bool CheckEqual (const char* expected, const char* actual, std::string& msg)
849{
850 if (strcmp (expected, actual))
851 {
852 std::stringstream stream;
853 stream << "Expected \'" << expected << "\' but was \'" << actual << "\'";
854 msg = stream.str ();
855 return false;
856 }
857 return true;
858}
859
860inline
861bool CheckEqual (char* expected, char* actual, std::string& msg)
862{
863 return CheckEqual (const_cast<const char *>(expected), const_cast<const char*>(actual), msg);
864}
865
866inline
867bool CheckEqual (const char* expected, char* actual, std::string& msg)
868{
869 return CheckEqual (const_cast<const char *>(expected), const_cast<const char*>(actual), msg);
870}
871
872inline
873bool CheckEqual (char* expected, const char* actual, std::string& msg)
874{
875 return CheckEqual (const_cast<const char *>(expected), const_cast<const char*>(actual), msg);
876}
877
878
894template <typename expected_T, typename actual_T>
895bool isClose (const expected_T& expected, const actual_T& actual, double tolerance)
896{
897 if (tolerance == 0)
898 {
901 tolerance = UnitTest::default_tolerance;
902 }
903 return fabs ((double)(actual - expected)) <= tolerance;
904}
905
916template <typename expected_T, typename actual_T>
917bool CheckClose (const expected_T& expected, const actual_T& actual, double tolerance,
918 std::string& msg)
919{
920 if (!isClose(actual, expected, tolerance))
921 {
922 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
923 int prec = (int)(1 - log10 (fail_tol));
924 std::stringstream stream;
925 stream.precision (prec);
926 stream.setf (std::ios::fixed);
927 stream << "Expected " << expected << " +/- " << fail_tol << " but was " << actual;
928 msg = stream.str ();
929 return false;
930 }
931 else
932 msg.clear ();
933 return true;
934}
935
944template <typename expected_T, typename actual_T>
945bool Equal1D (const expected_T& expected, const actual_T& actual, size_t count)
946{
947 for (size_t i = 0; i < count; ++i)
948 if (expected[i] != actual[i])
949 return false;
950 return true;
951}
952
962template <typename expected_T, typename actual_T>
963bool CheckArrayEqual (const expected_T& expected, const actual_T& actual,
964 size_t count, std::string& msg)
965{
966 if (!Equal1D (expected, actual, count))
967 {
968 std::stringstream stream;
969 stream << "Expected [ ";
970 for (size_t i = 0; i < count; ++i)
971 stream << expected[i] << " ";
972
973 stream << "] but was [ ";
974 for (size_t i = 0; i < count; ++i)
975 stream << actual[i] << " ";
976
977 stream << "]";
978 msg = stream.str ();
979 return false;
980 }
981 return true;
982}
983
997template <typename expected_T, typename actual_T>
998bool isClose1D (const expected_T& expected, const actual_T& actual, size_t count, double tolerance)
999{
1000 for (size_t i = 0; i < count; ++i)
1001 {
1002 if (!isClose(expected[i], actual[i], tolerance))
1003 return false;
1004 }
1005 return true;
1006}
1007
1008
1021template <typename expected_T, typename actual_T>
1022bool CheckArrayClose (const expected_T& expected, const actual_T& actual, size_t count,
1023 double tolerance, std::string& msg)
1024{
1025 if (!isClose1D (expected, actual, count, tolerance))
1026 {
1027 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1028 int prec = (int)(1 - log10 (fail_tol));
1029
1030 std::stringstream stream;
1031 stream.precision (prec);
1032 stream.setf (std::ios::fixed);
1033 stream << "Expected [ ";
1034 for (size_t i = 0; i < count; ++i)
1035 stream << expected[i] << " ";
1036
1037 stream << "] +/- " << fail_tol << " but was [ ";
1038 for (size_t i = 0; i < count; ++i)
1039 stream << actual[i] << " ";
1040 stream << "]";
1041 msg = stream.str ();
1042 return false;
1043 }
1044 return true;
1045}
1046
1056template <>
1057inline
1058bool CheckEqual<void, void>(const void* expected, const void* actual, std::string& msg)
1059{
1060 if (!(expected == actual)) {
1061 std::stringstream stream;
1062 stream << "Expected " << expected << " but was " << actual;
1063 msg = stream.str();
1064 return false;
1065 } else
1066 msg.clear();
1067 return true;
1068}
1069
1080template <typename expected_T, typename actual_T>
1081bool CheckClose (const std::vector<expected_T>& expected, const std::vector<actual_T>& actual, double tolerance,
1082 std::string& msg)
1083{
1084 if (expected.size () != actual.size ()
1085 || !isClose1D (&expected[0], &actual[0], expected.size(), tolerance))
1086 {
1087 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1088 int prec = (int)(1 - log10 (fail_tol));
1089 std::stringstream stream;
1090 stream.precision (prec);
1091 stream.setf (std::ios::fixed);
1092 stream << "Expected [ ";
1093 for (auto& p : expected)
1094 stream << p << " ";
1095
1096 stream << "] +/- " << fail_tol << " but was [ ";
1097 for (auto& p : actual)
1098 stream << p << " ";
1099 stream << "]";
1100 msg = stream.str ();
1101 return false;
1102 }
1103 return true;
1104}
1105
1116template <typename expected_T, typename actual_T, size_t N>
1117bool CheckClose (const std::array<expected_T, N>& expected, const std::array<actual_T, N>& actual, double tolerance,
1118 std::string& msg)
1119{
1120 if (!isClose1D (&expected[0], &actual[0], N, tolerance))
1121 {
1122 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1123 int prec = (int)(1 - log10 (fail_tol));
1124 std::stringstream stream;
1125 stream.precision (prec);
1126 stream.setf (std::ios::fixed);
1127 stream << "Expected [ ";
1128 for (auto& p : expected)
1129 stream << p << " ";
1130
1131 stream << "] +/- " << fail_tol << " but was [ ";
1132 for (auto& p : actual)
1133 stream << p << " ";
1134 stream << "]";
1135 msg = stream.str ();
1136 return false;
1137 }
1138 return true;
1139}
1140
1150template <typename expected_T, typename actual_T>
1151bool Equal2D (const expected_T& expected, const actual_T& actual, size_t rows, size_t columns)
1152{
1153 for (size_t i = 0; i < rows; ++i)
1154 if (!Equal1D (expected[i], actual[i], columns))
1155 return false;
1156 return true;
1157}
1158
1169template <typename expected_T, typename actual_T>
1170bool CheckArray2DEqual (const expected_T& expected, const actual_T& actual,
1171 size_t rows, size_t columns, std::string& msg)
1172{
1173 if (!Equal2D (expected, actual, rows, columns))
1174 {
1175 std::stringstream stream;
1176 size_t i, j;
1177 stream << "Expected [\n";
1178 for (i = 0; i < rows; ++i)
1179 {
1180 stream << " [";
1181 for (j = 0; j < columns; ++j)
1182 stream << expected[i][j] << " ";
1183 stream << "]\n";
1184 }
1185
1186 stream << "] but was [\n";
1187 for (i = 0; i < rows; ++i)
1188 {
1189 stream << " [";
1190 for (j = 0; j < columns; ++j)
1191 stream << actual[i][j] << " ";
1192 stream << "]\n";
1193 }
1194 stream << "]";
1195 msg = stream.str ();
1196 return false;
1197 }
1198 return true;
1199}
1200
1211template <typename expected_T, typename actual_T>
1212bool isClose2D (const expected_T& expected, const actual_T& actual, size_t rows, size_t columns,
1213 double tolerance)
1214{
1215 for (size_t i = 0; i < rows; ++i)
1216 if (!isClose1D (expected[i], actual[i], columns, tolerance))
1217 return false;
1218 return true;
1219}
1220
1233template <typename expected_T, typename actual_T>
1234bool CheckArray2DClose (const expected_T& expected, const actual_T& actual,
1235 size_t rows, size_t columns, double tolerance, std::string& msg)
1236{
1237 if (!isClose2D (expected, actual, rows, columns, tolerance))
1238 {
1239 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1240 int prec = (int)(1 - log10 (fail_tol));
1241 std::stringstream stream;
1242 stream.precision (prec);
1243 stream.setf (std::ios::fixed);
1244 stream << "Expected [\n";
1245 size_t i, j;
1246 for (i = 0; i < rows; ++i)
1247 {
1248 stream << " [ ";
1249 for (j = 0; j < columns; ++j)
1250 stream << expected[i][j] << " ";
1251 stream << "]\n";
1252 }
1253
1254 stream << "] +/- " << fail_tol << " but was [\n";
1255 for (i = 0; i < rows; ++i)
1256 {
1257 stream << " [ ";
1258 for (j = 0; j < columns; ++j)
1259 stream << actual[i][j] << " ";
1260 stream << "]\n";
1261 }
1262 stream << "]";
1263 msg = stream.str ();
1264 return false;
1265 }
1266 msg.clear ();
1267 return true;
1268}
1269
1280inline
1281bool CheckFileEqual (const std::string& ref, const std::string& actual, std::string& message)
1282{
1283 struct stat st1, st2;
1284 std::ostringstream buf;
1285
1286 stat (ref.c_str(), &st1);
1287 stat (actual.c_str(), &st2);
1288 if (st1.st_size != st2.st_size)
1289 {
1290 buf << "Size is different (" << st1.st_size << " vs " << st2.st_size
1291 << ") while comparing " << ref << " and " << actual;
1292 message = buf.str();
1293 return false;
1294 }
1295
1296 FILE* f1, * f2;
1297 f1 = fopen (ref.c_str(), "r");
1298 f2 = fopen (actual.c_str(), "r");
1299 if (!f1 || !f2)
1300 {
1301 if (f1) fclose (f1);
1302 if (f2) fclose (f2);
1303 buf << "Failed to open files while comparing "
1304 << ref << " and " << actual;
1305 message = buf.str();
1306 return false; //something wrong with files
1307 }
1308
1309 size_t ln = 0;
1310 bool ok = true;
1311 char ln1[1024], ln2[1024];
1312 while (ok)
1313 {
1314 ln++;
1315 if (fgets (ln1, sizeof (ln1), f1)
1316 && fgets (ln2, sizeof (ln2), f2))
1317 ok = !strcmp (ln1, ln2);
1318 else
1319 break;
1320 }
1321 fclose (f1);
1322 fclose (f2);
1323 if (!ok)
1324 {
1325 char* p1, * p2;
1326 int off;
1327 for (off = 0, p1 = ln1, p2 = ln2;
1328 *p1 && *p2 && *p1 == *p2;
1329 p1++, p2++, off++)
1330 ;
1331 buf << "Difference at line " << ln << " position " << off
1332 << " while comparing " << ref << " and " << actual;
1333 message = buf.str();
1334 }
1335 else
1336 message.clear ();
1337 return ok;
1338}
1339
1340} //end namespace
1341
1347
1348#define EXPECT_TRUE(x) CHECK (x)
1349#define EXPECT_FALSE(x) CHECK (!(x))
1350#define EXPECT_EQ(A, B) CHECK_EQUAL (B, A)
1351#define EXPECT_NE(A, B) \
1352 do \
1353 { \
1354 try { \
1355 std::string str__; \
1356 if (UnitTest::CheckEqual ((A), (B), str__)) \
1357 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
1358 } \
1359 catch (...) { \
1360 UnitTest::ReportFailure (__FILE__, __LINE__, \
1361 "Unhandled exception in CHECK_EQUAL(" #A ", " #B ")"); \
1362 } \
1363 } while (0)
1364
1365#define EXPECT_GE(A, B) CHECK ((A) >= (B))
1366#define EXPECT_GT(A, B) CHECK ((A) > (B))
1367#define EXPECT_LE(A, B) CHECK ((A) <= (B))
1368#define EXPECT_LT(A, B) CHECK ((A) < (B))
1369
1370#define EXPECT_NEAR(A, B, tol) CHECK_CLOSE(B, A, tol)
1371#define EXPECT_THROW(expr, except) CHECK_THROW(expr, except)
1372
1373#define ASSERT_TRUE(expr) ABORT (!(expr))
1374#define ASSERT_FALSE(expr) ABORT (expr)
1375#define ASSERT_EQ(e1, e2) \
1376 do \
1377 { \
1378 std::string str__; \
1379 if (!UnitTest::CheckEqual((e1), (e2), str__)) \
1380 throw UnitTest::test_abort (__FILE__, __LINE__, str__.c_str()); \
1381 } while (0)
1382
1383#define ASSERT_NE(e1, e2) \
1384 do \
1385 { \
1386 std::string str__; \
1387 if (UnitTest::CheckEqual ((e1), (e2), str__)) \
1388 { \
1389 std::stringstream stream__; \
1390 stream__ << (e1) << " and " << (e2) << " should be different"; \
1391 throw UnitTest::test_abort (__FILE__, __LINE__, stream__.str ().c_str ());\
1392 } \
1393 } while (0)
1394
1395#define ASSERT_GE(e1, e2) ABORT ((e1) < (e2))
1396#define ASSERT_GT(e1, e2) ABORT ((e1) <= (e2))
1397#define ASSERT_LE(e1, e2) ABORT ((e1) > (e2))
1398#define ASSERT_LT(e1, e2) ABORT ((e1) >= (e2))
1400
bool CheckNaN(Value const value)
Definition checks.h:537
double default_tolerance
Default tolerance for CLOSE... macros.
std::string to_utf8(const std::wstring &ws)
Internal function for conversion from UTF-16 to UTF-8.
Definition checks.h:721
bool CheckArray2DClose(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns, double tolerance, std::string &msg)
Definition checks.h:1234
bool CheckClose(const expected_T &expected, const actual_T &actual, double tolerance, std::string &msg)
Definition checks.h:917
bool Check(Value const value)
Definition checks.h:525
bool isClose1D(const expected_T &expected, const actual_T &actual, size_t count, double tolerance)
Definition checks.h:998
bool CheckArrayClose(const expected_T &expected, const actual_T &actual, size_t count, double tolerance, std::string &msg)
Definition checks.h:1022
bool CheckArrayEqual(const expected_T &expected, const actual_T &actual, size_t count, std::string &msg)
Definition checks.h:963
bool CheckArray2DEqual(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns, std::string &msg)
Definition checks.h:1170
bool CheckEqual(const expected_T &expected, const actual_T &actual, std::string &msg)
Definition checks.h:552
bool Equal1D(const expected_T &expected, const actual_T &actual, size_t count)
Definition checks.h:945
bool isClose2D(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns, double tolerance)
Definition checks.h:1212
bool CheckFileEqual(const std::string &ref, const std::string &actual, std::string &message)
Definition checks.h:1281
bool Equal2D(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns)
Definition checks.h:1151
bool isClose(const expected_T &expected, const actual_T &actual, double tolerance)
Definition checks.h:895
Definition utpp.h:463