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