[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" |
---|
[481] | 18 | #include <string> |
---|
[426] | 19 | |
---|
[477] | 20 | namespace UnitTest { |
---|
[428] | 21 | |
---|
[477] | 22 | bool AreClose ( const itpp::vec &expected, const itpp::vec &actual, |
---|
| 23 | double tolerance ); |
---|
[456] | 24 | |
---|
[477] | 25 | bool AreClose ( const itpp::vec &expected, const itpp::vec &actual, |
---|
| 26 | const itpp::vec &tolerance ); |
---|
[465] | 27 | |
---|
[477] | 28 | bool AreClose ( const itpp::mat &expected, const itpp::mat &actual, |
---|
| 29 | double tolerance ); |
---|
[456] | 30 | |
---|
[477] | 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; |
---|
[456] | 37 | |
---|
[477] | 38 | results.OnTestFailure ( details, stream.GetText() ); |
---|
| 39 | } |
---|
[426] | 40 | } |
---|
| 41 | |
---|
[477] | 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; |
---|
[456] | 48 | |
---|
[477] | 49 | results.OnTestFailure ( details, stream.GetText() ); |
---|
| 50 | } |
---|
[456] | 51 | } |
---|
| 52 | |
---|
| 53 | } |
---|
| 54 | |
---|
[481] | 55 | /*! CHECK_*_EX macros should be used only in blocks having an instance |
---|
| 56 | of this class (which sets the globals for error reporting). */ |
---|
[477] | 57 | class CurrentContext { |
---|
[481] | 58 | public: |
---|
| 59 | // how many times to repeat a failing test before reporting |
---|
| 60 | // failure |
---|
| 61 | static const int max_trial_count = 3; |
---|
| 62 | |
---|
[456] | 63 | private: |
---|
[477] | 64 | static const char *config_name; |
---|
| 65 | static int index; |
---|
[456] | 66 | |
---|
| 67 | public: |
---|
[477] | 68 | // the pointer must stay valid for the lifetime of the object |
---|
| 69 | CurrentContext ( const char *name, int idx ); |
---|
| 70 | ~CurrentContext(); |
---|
[456] | 71 | |
---|
[481] | 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 | |
---|
[477] | 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; |
---|
[481] | 85 | stream << format_context() << "expected " << expected << " but was " << actual; |
---|
[465] | 86 | |
---|
[477] | 87 | results.OnTestFailure ( details, stream.GetText() ); |
---|
| 88 | } |
---|
[465] | 89 | } |
---|
| 90 | |
---|
[477] | 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; |
---|
[481] | 99 | stream << format_context() << "expected " << expected << " +/- " << tolerance << " but was " << actual; |
---|
[456] | 100 | |
---|
[477] | 101 | results.OnTestFailure ( details, stream.GetText() ); |
---|
| 102 | } |
---|
[456] | 103 | } |
---|
| 104 | }; |
---|
| 105 | |
---|
[465] | 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 | |
---|
[456] | 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__), \ |
---|
[465] | 127 | "Unhandled exception in CHECK_CLOSE_EX(" #expected ", " #actual ")"); \ |
---|
[456] | 128 | } \ |
---|
| 129 | } while (0) |
---|
| 130 | |
---|
[426] | 131 | #endif |
---|