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