root/library/tests/mat_checks.h @ 1276

Revision 1064, 4.9 kB (checked in by mido, 14 years ago)

astyle applied all over the library

  • Property svn:eol-style set to native
Line 
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
20namespace UnitTest {
21
22bool AreClose ( const itpp::vec &expected, const itpp::vec &actual,
23                double tolerance );
24
25bool AreClose ( const itpp::vec &expected, const itpp::vec &actual,
26                const itpp::vec &tolerance );
27
28bool AreClose ( const itpp::mat &expected, const itpp::mat &actual,
29                double tolerance );
30
31inline 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
42inline 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/*! Constructs the multiple of standard deviation used for sample
56  tests (currently 2). */
57inline itpp::vec make_close_tolerance ( const itpp::vec & variance, int nsamples ) {
58    // simplify overloading for Visual Studio
59    return 3 * ( sqrt ( variance ) / sqrt ( static_cast<double> ( nsamples ) ) );
60}
61
62/*! CHECK_*_EX macros should be used only in blocks having an instance
63  of this class (which sets the globals for error reporting). */
64class CurrentContext {
65public:
66    // how many times to repeat a failing test before reporting
67    // failure
68    static const int max_trial_count = 3;
69
70private:
71    static const char *config_name;
72    static int index;
73
74public:
75    // the pointer must stay valid for the lifetime of the object
76    CurrentContext ( const char *name, int idx );
77    ~CurrentContext();
78
79    /* Should be called only in blocks having an instance of
80       CurrentContext. The argument, when not default, should be
81       __LINE__ (and it is included in the returned string).
82    */
83    static std::string format_context ( int ln = -1 );
84
85    template<typename Expected, typename Actual>
86    static void CheckEqualEx ( UnitTest::TestResults& results,
87                               Expected const& expected,
88                               Actual const& actual,
89                               UnitTest::TestDetails const& details ) {
90        if ( ! ( expected == actual ) ) {
91            UnitTest::MemoryOutStream stream;
92            stream << format_context() << "expected " << expected << " but was " << actual;
93
94            results.OnTestFailure ( details, stream.GetText() );
95        }
96    }
97
98    template<typename Expected, typename Actual, typename Tolerance>
99    static void CheckCloseEx ( UnitTest::TestResults& results,
100                               Expected const& expected,
101                               Actual const& actual,
102                               Tolerance const& tolerance,
103                               UnitTest::TestDetails const& details ) {
104        if ( !UnitTest::AreClose ( expected, actual, tolerance ) ) {
105            UnitTest::MemoryOutStream stream;
106            stream << format_context() << "expected " << expected << " +/- " << tolerance << " but was " << actual;
107
108            results.OnTestFailure ( details, stream.GetText() );
109        }
110    }
111};
112
113#define CHECK_EQUAL_EX(expected, actual) \
114    do \
115    { \
116        try { \
117            CurrentContext::CheckEqualEx(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), 0, false)); \
118        } \
119        catch (...) { \
120            UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
121                    "Unhandled exception in CHECK_EQUAL_EX(" #expected ", " #actual ")"); \
122        } \
123    } while (0)
124
125
126#define CHECK_CLOSE_EX(expected, actual, tolerance) \
127    do \
128    { \
129        try { \
130            CurrentContext::CheckCloseEx(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), 0, false)); \
131        } \
132        catch (...) { \
133            UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
134                    "Unhandled exception in CHECK_CLOSE_EX(" #expected ", " #actual ")"); \
135        } \
136    } while (0)
137
138#endif
Note: See TracBrowser for help on using the browser.