os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/cppunit/cppunit_mini.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2  * Copyright (c) 2003, 2004
     3  * Zdenek Nemec
     4  *
     5  * This material is provided "as is", with absolutely no warranty expressed
     6  * or implied. Any use is at your own risk.
     7  *
     8  * Permission to use or copy this software for any purpose is hereby granted
     9  * without fee, provided the above notices are retained on all copies.
    10  * Permission to modify the code and to distribute modified code is granted,
    11  * provided the above notices are retained, and a notice that the code was
    12  * modified is included with the above copyright notice.
    13  *
    14  */
    15 
    16 /* $Id: cppunit_mini.h 2490 2006-06-27 19:49:26Z dums $ */
    17 
    18 #ifndef _CPPUNITMPFR_H_
    19 #define _CPPUNITMPFR_H_
    20 
    21 #define CPPUNIT_NS CppUnitMini
    22 
    23 #include <string.h>
    24 
    25 namespace CPPUNIT_NS
    26 {
    27   class Reporter {
    28   public:
    29     virtual ~Reporter() {}
    30     virtual void error(const char * /*macroName*/, const char * /*in_macro*/, const char * /*in_file*/, int /*in_line*/) {}
    31     virtual void failure(const char * /*macroName*/, const char * /*in_macro*/, const char * /*in_file*/, int /*in_line*/) {}
    32     virtual void message( const char * /*msg*/ ) {}
    33     virtual void progress( const char * /*in_className*/, const char * /*in_testName*/, bool /*ignored*/) {}
    34     virtual void end() {}
    35     virtual void printSummary() {}
    36     void setFirstStep(const bool in_firstStep) {m_firstStep = in_firstStep;}
    37     const bool firstStep() { return m_firstStep; }
    38   private:
    39     bool m_firstStep;
    40   };
    41 
    42   class TestFixture {
    43   public:
    44     virtual ~TestFixture() {}
    45 
    46     //! \brief Set up context before running a test.
    47     virtual void setUp() {}
    48 
    49     //! Clean up after the test run.
    50     virtual void tearDown() {}
    51   };
    52 
    53   class TestCase : public TestFixture {
    54   public:
    55     TestCase() { m_failed = false; registerTestCase(this); }
    56 
    57     static int run(Reporter *in_reporter = 0, const char *in_testName = "", bool invert = false);
    58     int numErrors() { return m_numErrors; }
    59     static void registerTestCase(TestCase *in_testCase);
    60 
    61     virtual void myRun(const char * /*in_name*/, bool /*invert*/ = false) {}
    62 
    63     virtual void failure(const char *in_macroName, const char *in_macro, const char *in_file, int in_line) {
    64       m_failed = true;
    65       if (m_reporter) {
    66         m_reporter->failure(in_macroName, in_macro, in_file, in_line);
    67       }
    68     }
    69 
    70     virtual void error(const char *in_macroName, const char *in_macro, const char *in_file, int in_line) {
    71       ++m_numErrors;
    72       if (m_reporter) {
    73         m_reporter->error(in_macroName, in_macro, in_file, in_line);
    74       }
    75     }
    76 
    77     static void message(const char *msg) {
    78       if (m_reporter) {
    79         m_reporter->message(msg);
    80       }
    81     }
    82 
    83     bool equalDoubles(double in_expected, double in_real, double in_maxErr) {
    84       double diff = in_expected - in_real;
    85       if (diff < 0.) {
    86         diff = -diff;
    87       }
    88       return diff < in_maxErr;
    89     }
    90 
    91     virtual void progress(const char *in_className, const char *in_functionName, bool ignored) {
    92       ++m_numTests;
    93       if (m_reporter) {
    94         m_reporter->progress(in_className, in_functionName, ignored);
    95       }
    96     }
    97 
    98     bool shouldRunThis(const char *in_desiredTest, const char *in_className, const char *in_functionName, bool invert = false) {
    99       if ((in_desiredTest) && (in_desiredTest[0] != '\0')) {
   100         const char *ptr = strstr(in_desiredTest, "::");
   101         if (ptr) {
   102           bool decision = (strncmp(in_desiredTest, in_className, strlen(in_className)) == 0) &&
   103                           (strncmp(ptr + 2, in_functionName, strlen(in_functionName)) == 0);
   104           return invert ? !decision : decision;
   105         }
   106         bool decision = strcmp(in_desiredTest, in_className) == 0;
   107         return invert ? !decision : decision;
   108       }
   109       return true;
   110     }
   111 
   112     void tearDown() {
   113       if (m_failed)
   114         ++m_numErrors;
   115       m_reporter->end();
   116     }
   117 
   118   protected:
   119     static int m_numErrors;
   120     static int m_numTests;
   121 
   122   private:
   123     static TestCase *m_root;
   124     TestCase *m_next;
   125     bool m_failed;
   126 
   127     static Reporter *m_reporter;
   128   };
   129 }
   130 
   131 #if !defined (CPPUNIT_MINI_HIDE_UNUSED_VARIABLE)
   132 #  if defined (_MSC_VER)
   133 #    define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v) (v);
   134 #  else
   135 #    define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v)
   136 #  endif
   137 #endif
   138 
   139 #define CPPUNIT_TEST_SUITE(X) \
   140   typedef CPPUNIT_NS::TestCase Base; \
   141   virtual void myRun(const char *in_name, bool invert = false) { \
   142     char *className = #X; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(className) \
   143     bool ignoring = false; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(ignoring)
   144 
   145 #if defined CPPUNIT_MINI_USE_EXCEPTIONS
   146 #  define CPPUNIT_TEST(X) \
   147   if (shouldRunThis(in_name, className, #X, invert)) { \
   148     setUp(); \
   149     progress(className, #X, ignoring); \
   150     if (!ignoring) { \
   151       try { \
   152         X(); \
   153       } \
   154       catch(...) { \
   155         Base::error("Test Failed: An Exception was thrown.", #X, __FILE__, __LINE__); \
   156       } \
   157     } \
   158     tearDown(); \
   159   }
   160 #else
   161 #  define CPPUNIT_TEST(X) \
   162   if (shouldRunThis(in_name, className, #X, invert)) { \
   163     setUp(); \
   164     progress(className, #X, ignoring); \
   165     if (!ignoring) \
   166       X(); \
   167     tearDown(); \
   168   }
   169 #endif
   170 
   171 #  define CPPUNIT_IGNORE \
   172   ignoring = true
   173 
   174 #  define CPPUNIT_STOP_IGNORE \
   175   ignoring = false
   176 
   177 #define CPPUNIT_TEST_EXCEPTION(X, Y) \
   178   if (shouldRunThis(in_name, className, #X, invert)) { \
   179     progress(className, #X, ignoring); \
   180   }
   181 
   182 #define CPPUNIT_TEST_SUITE_END() }
   183 
   184 #define CPPUNIT_TEST_SUITE_REGISTRATION(X) static X local
   185 
   186 #define CPPUNIT_CHECK(X) \
   187   if (!(X)) { \
   188     Base::failure("CPPUNIT_CHECK", #X, __FILE__, __LINE__); \
   189   }
   190 
   191 #define CPPUNIT_ASSERT(X) \
   192   if (!(X)) { \
   193     Base::error("CPPUNIT_ASSERT", #X, __FILE__, __LINE__); \
   194     return; \
   195   }
   196 
   197 #define CPPUNIT_ASSERT_EQUAL(X, Y) \
   198   if ((X) != (Y)) { \
   199     Base::error("CPPUNIT_ASSERT_EQUAL", #X","#Y, __FILE__, __LINE__); \
   200     return; \
   201   }
   202 
   203 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(X, Y, Z) \
   204   if (!equalDoubles((X), (Y), (Z))) { \
   205     Base::error("CPPUNIT_ASSERT_DOUBLES_EQUAL", #X","#Y","#Z, __FILE__, __LINE__); \
   206     return; \
   207   }
   208 
   209 #define CPPUNIT_MESSAGE(m) CPPUNIT_NS::TestCase::message(m)
   210 
   211 #endif