root/library/tests/mat_checks.h @ 727

Revision 727, 4.7 kB (checked in by smidl, 15 years ago)

Logger change. Loggers can now store settings.

Unit Tests and fixes.

  • Property svn:eol-style set to native
RevLine 
[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]20namespace UnitTest {
[428]21
[477]22bool AreClose ( const itpp::vec &expected, const itpp::vec &actual,
23                double tolerance );
[456]24
[477]25bool AreClose ( const itpp::vec &expected, const itpp::vec &actual,
26                const itpp::vec &tolerance );
[465]27
[477]28bool AreClose ( const itpp::mat &expected, const itpp::mat &actual,
29                double tolerance );
[456]30
[477]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;
[456]37
[477]38                results.OnTestFailure ( details, stream.GetText() );
39        }
[426]40}
41
[477]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;
[456]48
[477]49                results.OnTestFailure ( details, stream.GetText() );
50        }
[456]51}
52
53}
54
[484]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
[727]59        return 3 * ( sqrt ( variance ) / sqrt ( static_cast<double> ( nsamples ) ) );
[484]60}
61
[481]62/*! CHECK_*_EX macros should be used only in blocks having an instance
63  of this class (which sets the globals for error reporting). */
[477]64class CurrentContext {
[481]65public:
66        // how many times to repeat a failing test before reporting
67        // failure
68        static const int max_trial_count = 3;
69
[456]70private:
[477]71        static const char *config_name;
72        static int index;
[456]73
74public:
[477]75        // the pointer must stay valid for the lifetime of the object
76        CurrentContext ( const char *name, int idx );
77        ~CurrentContext();
[456]78
[481]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        */
[722]83        static std::string format_context ( int ln = -1 );
[481]84
[477]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;
[481]92                        stream << format_context() << "expected " << expected << " but was " << actual;
[465]93
[477]94                        results.OnTestFailure ( details, stream.GetText() );
95                }
[465]96        }
97
[477]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;
[481]106                        stream << format_context() << "expected " << expected << " +/- " << tolerance << " but was " << actual;
[456]107
[477]108                        results.OnTestFailure ( details, stream.GetText() );
109                }
[456]110        }
111};
112
[465]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
[456]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__), \
[465]134                    "Unhandled exception in CHECK_CLOSE_EX(" #expected ", " #actual ")"); \
[456]135        } \
136    } while (0)
137
[426]138#endif
Note: See TracBrowser for help on using the browser.