Show
Ignore:
Timestamp:
07/31/09 08:38:18 (15 years ago)
Author:
vbarta
Message:

custom test location for harness tests (extended UnitTest?++), configurable tolerance - all tests pass (most of the time)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/tests/mat_checks.h

    r428 r456  
    1919namespace UnitTest 
    2020{ 
    21   bool AreClose(const itpp::vec &expected, const itpp::vec &actual, 
    22                 double tolerance); 
    2321 
    24   bool AreClose(const itpp::mat &expected, const itpp::mat &actual, 
    25                 double tolerance); 
     22bool AreClose(const itpp::vec &expected, const itpp::vec &actual, 
     23              double tolerance); 
     24 
     25bool AreClose(const itpp::mat &expected, const itpp::mat &actual, 
     26              double tolerance); 
     27 
     28inline 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    } 
    2637} 
    2738 
     39inline 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). */ 
     55class CurrentContext 
     56{ 
     57private: 
     58    static const char *config_name; 
     59    static int index; 
     60 
     61public: 
     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 
    2893#endif