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 | |
---|
19 | namespace UnitTest { |
---|
20 | |
---|
21 | bool AreClose ( const itpp::vec &expected, const itpp::vec &actual, |
---|
22 | double tolerance ); |
---|
23 | |
---|
24 | bool AreClose ( const itpp::vec &expected, const itpp::vec &actual, |
---|
25 | const itpp::vec &tolerance ); |
---|
26 | |
---|
27 | bool AreClose ( const itpp::mat &expected, const itpp::mat &actual, |
---|
28 | double tolerance ); |
---|
29 | |
---|
30 | inline void CheckClose ( TestResults &results, const itpp::vec &expected, |
---|
31 | const itpp::vec &actual, double tolerance, |
---|
32 | TestDetails const &details ) { |
---|
33 | if ( !AreClose ( expected, actual, tolerance ) ) { |
---|
34 | MemoryOutStream stream; |
---|
35 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
36 | |
---|
37 | results.OnTestFailure ( details, stream.GetText() ); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | inline void CheckClose ( TestResults &results, const itpp::mat &expected, |
---|
42 | const itpp::mat &actual, double tolerance, |
---|
43 | TestDetails const &details ) { |
---|
44 | if ( !AreClose ( expected, actual, tolerance ) ) { |
---|
45 | MemoryOutStream stream; |
---|
46 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
47 | |
---|
48 | results.OnTestFailure ( details, stream.GetText() ); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | } |
---|
53 | |
---|
54 | /*! CHECK_EQUAL_EX and CHECK_CLOSE_EX macros should be used only in |
---|
55 | blocks having an instance of this class (which sets the globals for |
---|
56 | error reporting). */ |
---|
57 | class CurrentContext { |
---|
58 | private: |
---|
59 | static const char *config_name; |
---|
60 | static int index; |
---|
61 | |
---|
62 | public: |
---|
63 | // the pointer must stay valid for the lifetime of the object |
---|
64 | CurrentContext ( const char *name, int idx ); |
---|
65 | ~CurrentContext(); |
---|
66 | |
---|
67 | template<typename Expected, typename Actual> |
---|
68 | static void CheckEqualEx ( UnitTest::TestResults& results, |
---|
69 | Expected const& expected, |
---|
70 | Actual const& actual, |
---|
71 | UnitTest::TestDetails const& details ) { |
---|
72 | if ( ! ( expected == actual ) ) { |
---|
73 | UnitTest::MemoryOutStream stream; |
---|
74 | stream << "error at " << config_name << '[' << index << "]: expected " << expected << " but was " << actual; |
---|
75 | |
---|
76 | results.OnTestFailure ( details, stream.GetText() ); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | template<typename Expected, typename Actual, typename Tolerance> |
---|
81 | static void CheckCloseEx ( UnitTest::TestResults& results, |
---|
82 | Expected const& expected, |
---|
83 | Actual const& actual, |
---|
84 | Tolerance const& tolerance, |
---|
85 | UnitTest::TestDetails const& details ) { |
---|
86 | if ( !UnitTest::AreClose ( expected, actual, tolerance ) ) { |
---|
87 | UnitTest::MemoryOutStream stream; |
---|
88 | stream << "error at " << config_name << '[' << index << "]: expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
89 | |
---|
90 | results.OnTestFailure ( details, stream.GetText() ); |
---|
91 | } |
---|
92 | } |
---|
93 | }; |
---|
94 | |
---|
95 | #define CHECK_EQUAL_EX(expected, actual) \ |
---|
96 | do \ |
---|
97 | { \ |
---|
98 | try { \ |
---|
99 | CurrentContext::CheckEqualEx(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), 0, false)); \ |
---|
100 | } \ |
---|
101 | catch (...) { \ |
---|
102 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ |
---|
103 | "Unhandled exception in CHECK_EQUAL_EX(" #expected ", " #actual ")"); \ |
---|
104 | } \ |
---|
105 | } while (0) |
---|
106 | |
---|
107 | |
---|
108 | #define CHECK_CLOSE_EX(expected, actual, tolerance) \ |
---|
109 | do \ |
---|
110 | { \ |
---|
111 | try { \ |
---|
112 | CurrentContext::CheckCloseEx(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), 0, false)); \ |
---|
113 | } \ |
---|
114 | catch (...) { \ |
---|
115 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ |
---|
116 | "Unhandled exception in CHECK_CLOSE_EX(" #expected ", " #actual ")"); \ |
---|
117 | } \ |
---|
118 | } while (0) |
---|
119 | |
---|
120 | #endif |
---|