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