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
487
488namespace UnitTest {
489
490#if _MSVC_LANG < 201703L
492extern double default_tolerance;
493#else
495inline double default_tolerance = 0;
496#endif
497
498//------------------ Check functions -----------------------------------------
499
506
507template <typename Value>
508bool Check (Value const value)
509{
510 return (bool)value;
511}
512
519template <typename Value>
520bool CheckNaN (Value const value)
521{
522 return std::isnan(value);
523}
524
534template <typename expected_T, typename actual_T>
535bool CheckEqual (const expected_T& expected, const actual_T& actual, std::string& msg)
536{
537 if (!(expected == actual))
538 {
539 std::stringstream stream;
540 stream << "Expected " << expected << " but was " << actual;
541 msg = stream.str ();
542 return false;
543 }
544 else
545 msg.clear ();
546 return true;
547}
548
549template <typename expected_T, typename actual_T>
550bool CheckEqual (const expected_T* expected, const actual_T* actual, std::string& msg)
551{
552 if (!(*expected == *actual))
553 {
554 std::stringstream stream;
555 stream << "Expected " << *expected << " but was " << *actual;
556 msg = stream.str ();
557 return false;
558 }
559 else
560 msg.clear ();
561 return true;
562}
563
564
576template <typename expected_T, typename actual_T>
577inline
578bool CheckEqual (const std::vector<expected_T>& expected, const std::vector<actual_T>& actual, std::string& msg)
579{
580 if (expected != actual)
581 {
582 std::stringstream stream;
583 stream << "Expected [ ";
584 for (auto& p : expected)
585 stream << p << " ";
586
587 stream << "] but was [ ";
588 for (auto& p : actual)
589 stream << p << " ";
590
591 stream << "]";
592 msg = stream.str ();
593 return false;
594 }
595 else
596 msg.clear ();
597 return true;
598}
599
611template <typename expected_T, typename actual_T, size_t N>
612inline
613bool CheckEqual (const std::array<expected_T,N>& expected, const std::array<actual_T,N>& actual, std::string& msg)
614{
615 if (expected != actual)
616 {
617 std::stringstream stream;
618 stream << "Expected [ ";
619 for (size_t i = 0; i < N; ++i)
620 stream << expected[i] << " ";
621
622 stream << "] but was [ ";
623 for (size_t i = 0; i < N; ++i)
624 stream << actual[i] << " ";
625
626 stream << "]";
627 msg = stream.str ();
628 return false;
629 }
630 else
631 msg.clear ();
632 return true;
633}
634
646template <typename expected_T, typename actual_T>
647inline
648bool CheckEqual (const std::list<expected_T>& expected, const std::list<actual_T>& actual, std::string& msg)
649{
650 if (expected != actual)
651 {
652 std::stringstream stream;
653 stream << "Expected ( ";
654 for (auto const& x : expected)
655 stream << x << " ";
656
657 stream << ") but was ( ";
658 for (auto const& x : actual)
659 stream << x << " ";
660
661 stream << ")";
662 msg = stream.str ();
663 return false;
664 }
665 else
666 msg.clear ();
667 return true;
668}
669
670
672#if __WCHAR_MAX__ > 0x10000
673inline std::string to_utf8 (const std::wstring& ws)
674{
675 std::string out;
676 auto in = ws.cbegin ();
677 while (in != ws.end ())
678 {
679 unsigned int c = (unsigned int)*in++;
680 if (c < 0x7f)
681 out.push_back ((char)c);
682 else if (c < 0x7ff)
683 {
684 out.push_back (0xC0 | (c >> 6));
685 out.push_back (0x80 | (c & 0x3f));
686 }
687 else if (c < 0xffff)
688 {
689 out.push_back (0xE0 | (c >> 12));
690 out.push_back (0x80 | ((c >> 6) & 0x3f));
691 out.push_back (0x80 | (c & 0x3f));
692 }
693 else
694 {
695 out.push_back (0xF0 | (c >> 18));
696 out.push_back (0x80 | ((c >> 12) & 0x3f));
697 out.push_back (0x80 | ((c >> 6) & 0x3f));
698 out.push_back (0x80 | (c & 0x3f));
699 }
700 }
701 return out;
702}
703#else
704inline std::string to_utf8 (const std::wstring& ws)
705{
706 std::string out;
707 auto in = ws.cbegin ();
708 while (in != ws.end ())
709 {
710 unsigned int c1 = (unsigned int)*in++;
711 if (c1 < 0xD800 || c1 > 0xe000)
712 {
713 if (c1 < 0x7f)
714 out.push_back ((char)c1);
715 else if (c1 < 0x7ff)
716 {
717 out.push_back (char(0xC0 | (c1 >> 6)));
718 out.push_back (char(0x80 | (c1 & 0x3f)));
719 }
720 else
721 {
722 out.push_back (char(0xE0 | (c1 >> 12)));
723 out.push_back (char(0x80 | ((c1 >> 6) & 0x3f)));
724 out.push_back (char(0x80 | (c1 & 0x3f)));
725 }
726 }
727 else if (in != ws.end ())
728 {
729 unsigned int c2 = (unsigned int)*in++;
730 if (c1 > 0xdbff || c2 < 0xdc00)
731 break; // invalid high/low surrogates order
732
733 c1 &= 0x3ff;
734 c2 &= 0x3ff;
735
736 unsigned int c = ((c1 << 10) | c2) + 0x10000;
737
738 out.push_back (char(0xF0 | (c >> 18)));
739 out.push_back (char(0x80 | ((c >> 12) & 0x3f)));
740 out.push_back (char(0x80 | ((c >> 6) & 0x3f)));
741 out.push_back (char(0x80 | (c & 0x3f)));
742 }
743 else
744 break; //malformed input; just bail out
745 }
746 return out;
747}
748#endif
759inline
760bool CheckEqual (const std::wstring expected, const std::wstring actual,
761 std::string& msg)
762{
763 if (expected != actual)
764 {
765 std::stringstream stream;
766 std::string u8exp = to_utf8 (expected);
767 std::string u8act = to_utf8 (actual);
768 stream << "Expected \'" << u8exp << "\' but was \'" << u8act << "\'";
769 msg = stream.str ();
770 return false;
771 }
772 else
773 msg.clear ();
774 return true;
775}
776
787inline
788bool CheckEqual (const wchar_t *expected, const wchar_t *actual,
789 std::string &msg)
790{
791 if (wcscmp (expected, actual))
792 {
793 std::stringstream stream;
794 std::string u8exp = to_utf8 (expected);
795 std::string u8act = to_utf8 (actual);
796 stream << "Expected \'" << u8exp << "\' but was \'" << u8act << "\'";
797 msg = stream.str ();
798 return false;
799 }
800 else
801 msg.clear ();
802 return true;
803}
804
805
806inline
807bool CheckEqual (wchar_t *expected, wchar_t *actual, std::string &msg)
808{
809 return CheckEqual (const_cast<const wchar_t *> (expected), const_cast<const wchar_t *> (actual), msg);
810}
811
812inline
813bool CheckEqual (const wchar_t *expected, wchar_t *actual, std::string &msg)
814{
815 return CheckEqual (expected, const_cast<const wchar_t *> (actual), msg);
816}
817
818inline
819bool CheckEqual (wchar_t *expected, const wchar_t *actual, std::string &msg)
820{
821 return CheckEqual (const_cast<const wchar_t *> (expected), actual, msg);
822}
823
825
830inline
831bool CheckEqual (const char* expected, const char* actual, std::string& msg)
832{
833 if (strcmp (expected, actual))
834 {
835 std::stringstream stream;
836 stream << "Expected \'" << expected << "\' but was \'" << actual << "\'";
837 msg = stream.str ();
838 return false;
839 }
840 return true;
841}
842
843inline
844bool CheckEqual (char* expected, char* actual, std::string& msg)
845{
846 return CheckEqual (const_cast<const char *>(expected), const_cast<const char*>(actual), msg);
847}
848
849inline
850bool CheckEqual (const char* expected, char* actual, std::string& msg)
851{
852 return CheckEqual (const_cast<const char *>(expected), const_cast<const char*>(actual), msg);
853}
854
855inline
856bool CheckEqual (char* expected, const char* actual, std::string& msg)
857{
858 return CheckEqual (const_cast<const char *>(expected), const_cast<const char*>(actual), msg);
859}
860
861
877template <typename expected_T, typename actual_T>
878bool isClose (const expected_T& expected, const actual_T& actual, double tolerance)
879{
880 if (tolerance == 0)
881 {
884 tolerance = UnitTest::default_tolerance;
885 }
886 return fabs ((double)(actual - expected)) <= tolerance;
887}
888
899template <typename expected_T, typename actual_T>
900bool CheckClose (const expected_T& expected, const actual_T& actual, double tolerance,
901 std::string& msg)
902{
903 if (!isClose(actual, expected, tolerance))
904 {
905 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
906 int prec = (int)(1 - log10 (fail_tol));
907 std::stringstream stream;
908 stream.precision (prec);
909 stream.setf (std::ios::fixed);
910 stream << "Expected " << expected << " +/- " << fail_tol << " but was " << actual;
911 msg = stream.str ();
912 return false;
913 }
914 else
915 msg.clear ();
916 return true;
917}
918
927template <typename expected_T, typename actual_T>
928bool Equal1D (const expected_T& expected, const actual_T& actual, size_t count)
929{
930 for (size_t i = 0; i < count; ++i)
931 if (expected[i] != actual[i])
932 return false;
933 return true;
934}
935
945template <typename expected_T, typename actual_T>
946bool CheckArrayEqual (const expected_T& expected, const actual_T& actual,
947 size_t count, std::string& msg)
948{
949 if (!Equal1D (expected, actual, count))
950 {
951 std::stringstream stream;
952 stream << "Expected [ ";
953 for (size_t i = 0; i < count; ++i)
954 stream << expected[i] << " ";
955
956 stream << "] but was [ ";
957 for (size_t i = 0; i < count; ++i)
958 stream << actual[i] << " ";
959
960 stream << "]";
961 msg = stream.str ();
962 return false;
963 }
964 return true;
965}
966
980template <typename expected_T, typename actual_T>
981bool isClose1D (const expected_T& expected, const actual_T& actual, size_t count, double tolerance)
982{
983 for (size_t i = 0; i < count; ++i)
984 {
985 if (!isClose(expected[i], actual[i], tolerance))
986 return false;
987 }
988 return true;
989}
990
991
1004template <typename expected_T, typename actual_T>
1005bool CheckArrayClose (const expected_T& expected, const actual_T& actual, size_t count,
1006 double tolerance, std::string& msg)
1007{
1008 if (!isClose1D (expected, actual, count, tolerance))
1009 {
1010 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1011 int prec = (int)(1 - log10 (fail_tol));
1012
1013 std::stringstream stream;
1014 stream.precision (prec);
1015 stream.setf (std::ios::fixed);
1016 stream << "Expected [ ";
1017 for (size_t i = 0; i < count; ++i)
1018 stream << expected[i] << " ";
1019
1020 stream << "] +/- " << fail_tol << " but was [ ";
1021 for (size_t i = 0; i < count; ++i)
1022 stream << actual[i] << " ";
1023 stream << "]";
1024 msg = stream.str ();
1025 return false;
1026 }
1027 return true;
1028}
1029
1039template <>
1040inline
1041bool CheckEqual<void, void>(const void* expected, const void* actual, std::string& msg)
1042{
1043 if (!(expected == actual)) {
1044 std::stringstream stream;
1045 stream << "Expected " << expected << " but was " << actual;
1046 msg = stream.str();
1047 return false;
1048 } else
1049 msg.clear();
1050 return true;
1051}
1052
1063template <typename expected_T, typename actual_T>
1064bool CheckClose (const std::vector<expected_T>& expected, const std::vector<actual_T>& actual, double tolerance,
1065 std::string& msg)
1066{
1067 if (expected.size () != actual.size ()
1068 || !isClose1D (&expected[0], &actual[0], expected.size(), tolerance))
1069 {
1070 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1071 int prec = (int)(1 - log10 (fail_tol));
1072 std::stringstream stream;
1073 stream.precision (prec);
1074 stream.setf (std::ios::fixed);
1075 stream << "Expected [ ";
1076 for (auto& p : expected)
1077 stream << p << " ";
1078
1079 stream << "] +/- " << fail_tol << " but was [ ";
1080 for (auto& p : actual)
1081 stream << p << " ";
1082 stream << "]";
1083 msg = stream.str ();
1084 return false;
1085 }
1086 return true;
1087}
1088
1099template <typename expected_T, typename actual_T, size_t N>
1100bool CheckClose (const std::array<expected_T, N>& expected, const std::array<actual_T, N>& actual, double tolerance,
1101 std::string& msg)
1102{
1103 if (!isClose1D (&expected[0], &actual[0], N, tolerance))
1104 {
1105 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1106 int prec = (int)(1 - log10 (fail_tol));
1107 std::stringstream stream;
1108 stream.precision (prec);
1109 stream.setf (std::ios::fixed);
1110 stream << "Expected [ ";
1111 for (auto& p : expected)
1112 stream << p << " ";
1113
1114 stream << "] +/- " << fail_tol << " but was [ ";
1115 for (auto& p : actual)
1116 stream << p << " ";
1117 stream << "]";
1118 msg = stream.str ();
1119 return false;
1120 }
1121 return true;
1122}
1123
1133template <typename expected_T, typename actual_T>
1134bool Equal2D (const expected_T& expected, const actual_T& actual, size_t rows, size_t columns)
1135{
1136 for (size_t i = 0; i < rows; ++i)
1137 if (!Equal1D (expected[i], actual[i], columns))
1138 return false;
1139 return true;
1140}
1141
1152template <typename expected_T, typename actual_T>
1153bool CheckArray2DEqual (const expected_T& expected, const actual_T& actual,
1154 size_t rows, size_t columns, std::string& msg)
1155{
1156 if (!Equal2D (expected, actual, rows, columns))
1157 {
1158 std::stringstream stream;
1159 size_t i, j;
1160 stream << "Expected [\n";
1161 for (i = 0; i < rows; ++i)
1162 {
1163 stream << " [";
1164 for (j = 0; j < columns; ++j)
1165 stream << expected[i][j] << " ";
1166 stream << "]\n";
1167 }
1168
1169 stream << "] but was [\n";
1170 for (i = 0; i < rows; ++i)
1171 {
1172 stream << " [";
1173 for (j = 0; j < columns; ++j)
1174 stream << actual[i][j] << " ";
1175 stream << "]\n";
1176 }
1177 stream << "]";
1178 msg = stream.str ();
1179 return false;
1180 }
1181 return true;
1182}
1183
1194template <typename expected_T, typename actual_T>
1195bool isClose2D (const expected_T& expected, const actual_T& actual, size_t rows, size_t columns,
1196 double tolerance)
1197{
1198 for (size_t i = 0; i < rows; ++i)
1199 if (!isClose1D (expected[i], actual[i], columns, tolerance))
1200 return false;
1201 return true;
1202}
1203
1216template <typename expected_T, typename actual_T>
1217bool CheckArray2DClose (const expected_T& expected, const actual_T& actual,
1218 size_t rows, size_t columns, double tolerance, std::string& msg)
1219{
1220 if (!isClose2D (expected, actual, rows, columns, tolerance))
1221 {
1222 auto fail_tol = tolerance ? tolerance : UnitTest::default_tolerance;
1223 int prec = (int)(1 - log10 (fail_tol));
1224 std::stringstream stream;
1225 stream.precision (prec);
1226 stream.setf (std::ios::fixed);
1227 stream << "Expected [\n";
1228 size_t i, j;
1229 for (i = 0; i < rows; ++i)
1230 {
1231 stream << " [ ";
1232 for (j = 0; j < columns; ++j)
1233 stream << expected[i][j] << " ";
1234 stream << "]\n";
1235 }
1236
1237 stream << "] +/- " << fail_tol << " but was [\n";
1238 for (i = 0; i < rows; ++i)
1239 {
1240 stream << " [ ";
1241 for (j = 0; j < columns; ++j)
1242 stream << actual[i][j] << " ";
1243 stream << "]\n";
1244 }
1245 stream << "]";
1246 msg = stream.str ();
1247 return false;
1248 }
1249 msg.clear ();
1250 return true;
1251}
1252
1263inline
1264bool CheckFileEqual (const std::string& ref, const std::string& actual, std::string& message)
1265{
1266 struct stat st1, st2;
1267 std::ostringstream buf;
1268
1269 stat (ref.c_str(), &st1);
1270 stat (actual.c_str(), &st2);
1271 if (st1.st_size != st2.st_size)
1272 {
1273 buf << "Size is different (" << st1.st_size << " vs " << st2.st_size
1274 << ") while comparing " << ref << " and " << actual;
1275 message = buf.str();
1276 return false;
1277 }
1278
1279 FILE* f1, * f2;
1280 f1 = fopen (ref.c_str(), "r");
1281 f2 = fopen (actual.c_str(), "r");
1282 if (!f1 || !f2)
1283 {
1284 if (f1) fclose (f1);
1285 if (f2) fclose (f2);
1286 buf << "Failed to open files while comparing "
1287 << ref << " and " << actual;
1288 message = buf.str();
1289 return false; //something wrong with files
1290 }
1291
1292 size_t ln = 0;
1293 bool ok = true;
1294 char ln1[1024], ln2[1024];
1295 while (ok)
1296 {
1297 ln++;
1298 if (fgets (ln1, sizeof (ln1), f1)
1299 && fgets (ln2, sizeof (ln2), f2))
1300 ok = !strcmp (ln1, ln2);
1301 else
1302 break;
1303 }
1304 fclose (f1);
1305 fclose (f2);
1306 if (!ok)
1307 {
1308 char* p1, * p2;
1309 int off;
1310 for (off = 0, p1 = ln1, p2 = ln2;
1311 *p1 && *p2 && *p1 == *p2;
1312 p1++, p2++, off++)
1313 ;
1314 buf << "Difference at line " << ln << " position " << off
1315 << " while comparing " << ref << " and " << actual;
1316 message = buf.str();
1317 }
1318 else
1319 message.clear ();
1320 return ok;
1321}
1322
1323} //end namespace
1324
1330
1331#define EXPECT_TRUE(x) CHECK (x)
1332#define EXPECT_FALSE(x) CHECK (!(x))
1333#define EXPECT_EQ(A, B) CHECK_EQUAL (B, A)
1334#define EXPECT_NE(A, B) \
1335 do \
1336 { \
1337 try { \
1338 std::string str__; \
1339 if (UnitTest::CheckEqual ((A), (B), str__)) \
1340 UnitTest::ReportFailure (__FILE__, __LINE__, str__); \
1341 } \
1342 catch (...) { \
1343 UnitTest::ReportFailure (__FILE__, __LINE__, \
1344 "Unhandled exception in CHECK_EQUAL(" #A ", " #B ")"); \
1345 } \
1346 } while (0)
1347
1348#define EXPECT_GE(A, B) CHECK ((A) >= (B))
1349#define EXPECT_GT(A, B) CHECK ((A) > (B))
1350#define EXPECT_LE(A, B) CHECK ((A) <= (B))
1351#define EXPECT_LT(A, B) CHECK ((A) < (B))
1352
1353#define EXPECT_NEAR(A, B, tol) CHECK_CLOSE(B, A, tol)
1354#define EXPECT_THROW(expr, except) CHECK_THROW(expr, except)
1355
1356#define ASSERT_TRUE(expr) ABORT (!(expr))
1357#define ASSERT_FALSE(expr) ABORT (expr)
1358#define ASSERT_EQ(e1, e2) \
1359 do \
1360 { \
1361 std::string str__; \
1362 if (!UnitTest::CheckEqual((e1), (e2), str__)) \
1363 throw UnitTest::test_abort (__FILE__, __LINE__, str__.c_str()); \
1364 } while (0)
1365
1366#define ASSERT_NE(e1, e2) \
1367 do \
1368 { \
1369 std::string str__; \
1370 if (UnitTest::CheckEqual ((e1), (e2), str__)) \
1371 { \
1372 std::stringstream stream__; \
1373 stream__ << (e1) << " and " << (e2) << " should be different"; \
1374 throw UnitTest::test_abort (__FILE__, __LINE__, stream__.str ().c_str ());\
1375 } \
1376 } while (0)
1377
1378#define ASSERT_GE(e1, e2) ABORT ((e1) < (e2))
1379#define ASSERT_GT(e1, e2) ABORT ((e1) <= (e2))
1380#define ASSERT_LE(e1, e2) ABORT ((e1) > (e2))
1381#define ASSERT_LT(e1, e2) ABORT ((e1) >= (e2))
1383
bool CheckNaN(Value const value)
Definition checks.h:520
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:704
bool CheckArray2DClose(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns, double tolerance, std::string &msg)
Definition checks.h:1217
bool CheckClose(const expected_T &expected, const actual_T &actual, double tolerance, std::string &msg)
Definition checks.h:900
bool Check(Value const value)
Definition checks.h:508
bool isClose1D(const expected_T &expected, const actual_T &actual, size_t count, double tolerance)
Definition checks.h:981
bool CheckArrayClose(const expected_T &expected, const actual_T &actual, size_t count, double tolerance, std::string &msg)
Definition checks.h:1005
bool CheckArrayEqual(const expected_T &expected, const actual_T &actual, size_t count, std::string &msg)
Definition checks.h:946
bool CheckArray2DEqual(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns, std::string &msg)
Definition checks.h:1153
bool CheckEqual(const expected_T &expected, const actual_T &actual, std::string &msg)
Definition checks.h:535
bool Equal1D(const expected_T &expected, const actual_T &actual, size_t count)
Definition checks.h:928
bool isClose2D(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns, double tolerance)
Definition checks.h:1195
bool CheckFileEqual(const std::string &ref, const std::string &actual, std::string &message)
Definition checks.h:1264
bool Equal2D(const expected_T &expected, const actual_T &actual, size_t rows, size_t columns)
Definition checks.h:1134
bool isClose(const expected_T &expected, const actual_T &actual, double tolerance)
Definition checks.h:878
Definition utpp.h:460