1 | /*! |
---|
2 | * \file |
---|
3 | * \brief UnitTest++ checks specialized for IT++ matrices. |
---|
4 | * \author Vaclav Barta. |
---|
5 | * |
---|
6 | * ----------------------------------- |
---|
7 | * BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
8 | * |
---|
9 | * Using IT++ for numerical operations |
---|
10 | * ----------------------------------- |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef MAT_CHECKS_H |
---|
14 | #define MAT_CHECKS_H |
---|
15 | |
---|
16 | #include "../bdm/itpp_ext.h" |
---|
17 | #include "UnitTest++.h" |
---|
18 | #include <string> |
---|
19 | |
---|
20 | namespace UnitTest { |
---|
21 | |
---|
22 | bool AreClose ( const itpp::vec &expected, const itpp::vec &actual, |
---|
23 | double tolerance ); |
---|
24 | |
---|
25 | bool AreClose ( const itpp::vec &expected, const itpp::vec &actual, |
---|
26 | const itpp::vec &tolerance ); |
---|
27 | |
---|
28 | bool AreClose ( const itpp::mat &expected, const itpp::mat &actual, |
---|
29 | double tolerance ); |
---|
30 | |
---|
31 | inline void CheckClose ( TestResults &results, const itpp::vec &expected, |
---|
32 | const itpp::vec &actual, double tolerance, |
---|
33 | TestDetails const &details ) { |
---|
34 | if ( !AreClose ( expected, actual, tolerance ) ) { |
---|
35 | MemoryOutStream stream; |
---|
36 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
37 | |
---|
38 | results.OnTestFailure ( details, stream.GetText() ); |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | inline void CheckClose ( TestResults &results, const itpp::mat &expected, |
---|
43 | const itpp::mat &actual, double tolerance, |
---|
44 | TestDetails const &details ) { |
---|
45 | if ( !AreClose ( expected, actual, tolerance ) ) { |
---|
46 | MemoryOutStream stream; |
---|
47 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
48 | |
---|
49 | results.OnTestFailure ( details, stream.GetText() ); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | /*! CHECK_*_EX macros should be used only in blocks having an instance |
---|
56 | of this class (which sets the globals for error reporting). */ |
---|
57 | class CurrentContext { |
---|
58 | public: |
---|
59 | // how many times to repeat a failing test before reporting |
---|
60 | // failure |
---|
61 | static const int max_trial_count = 3; |
---|
62 | |
---|
63 | private: |
---|
64 | static const char *config_name; |
---|
65 | static int index; |
---|
66 | |
---|
67 | public: |
---|
68 | // the pointer must stay valid for the lifetime of the object |
---|
69 | CurrentContext ( const char *name, int idx ); |
---|
70 | ~CurrentContext(); |
---|
71 | |
---|
72 | /* Should be called only in blocks having an instance of |
---|
73 | CurrentContext. The argument, when not default, should be |
---|
74 | __LINE__ (and it is included in the returned string). |
---|
75 | */ |
---|
76 | static std::string format_context( int ln = -1 ); |
---|
77 | |
---|
78 | template<typename Expected, typename Actual> |
---|
79 | static void CheckEqualEx ( UnitTest::TestResults& results, |
---|
80 | Expected const& expected, |
---|
81 | Actual const& actual, |
---|
82 | UnitTest::TestDetails const& details ) { |
---|
83 | if ( ! ( expected == actual ) ) { |
---|
84 | UnitTest::MemoryOutStream stream; |
---|
85 | stream << format_context() << "expected " << expected << " but was " << actual; |
---|
86 | |
---|
87 | results.OnTestFailure ( details, stream.GetText() ); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | template<typename Expected, typename Actual, typename Tolerance> |
---|
92 | static void CheckCloseEx ( UnitTest::TestResults& results, |
---|
93 | Expected const& expected, |
---|
94 | Actual const& actual, |
---|
95 | Tolerance const& tolerance, |
---|
96 | UnitTest::TestDetails const& details ) { |
---|
97 | if ( !UnitTest::AreClose ( expected, actual, tolerance ) ) { |
---|
98 | UnitTest::MemoryOutStream stream; |
---|
99 | stream << format_context() << "expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
100 | |
---|
101 | results.OnTestFailure ( details, stream.GetText() ); |
---|
102 | } |
---|
103 | } |
---|
104 | }; |
---|
105 | |
---|
106 | #define CHECK_EQUAL_EX(expected, actual) \ |
---|
107 | do \ |
---|
108 | { \ |
---|
109 | try { \ |
---|
110 | CurrentContext::CheckEqualEx(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), 0, false)); \ |
---|
111 | } \ |
---|
112 | catch (...) { \ |
---|
113 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ |
---|
114 | "Unhandled exception in CHECK_EQUAL_EX(" #expected ", " #actual ")"); \ |
---|
115 | } \ |
---|
116 | } while (0) |
---|
117 | |
---|
118 | |
---|
119 | #define CHECK_CLOSE_EX(expected, actual, tolerance) \ |
---|
120 | do \ |
---|
121 | { \ |
---|
122 | try { \ |
---|
123 | CurrentContext::CheckCloseEx(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), 0, false)); \ |
---|
124 | } \ |
---|
125 | catch (...) { \ |
---|
126 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ |
---|
127 | "Unhandled exception in CHECK_CLOSE_EX(" #expected ", " #actual ")"); \ |
---|
128 | } \ |
---|
129 | } while (0) |
---|
130 | |
---|
131 | #endif |
---|