2 * Copyright (c) 2003, 2004
5 * This material is provided "as is", with absolutely no warranty expressed
6 * or implied. Any use is at your own risk.
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.
18 #ifndef _CPPUNITMPFR_H_
19 #define _CPPUNITMPFR_H_
22 # define CPPUNIT_NS CppUnitMini
35 virtual ~Reporter() {}
36 virtual void error(const char * /*macroName*/, const char * /*in_macro*/, const char * /*in_file*/, int /*in_line*/) {}
37 virtual void message( const char * /*msg*/ ) {}
38 virtual void progress( const char * /*in_className*/, const char * /*in_testName*/, bool /*ignored*/, bool /* explicit */) {}
40 virtual void printSummary() {}
45 virtual ~TestFixture() {}
47 //! \brief Set up context before running a test.
48 virtual void setUp() {}
50 //! Clean up after the test run.
51 virtual void tearDown() {}
54 class TestCase : public TestFixture {
56 TestCase() { registerTestCase(this); }
58 void setUp() { m_failed = false; }
59 static int run(Reporter *in_reporter = 0, const char *in_testName = "", bool invert = false);
60 int numErrors() { return m_numErrors; }
61 static void registerTestCase(TestCase *in_testCase);
63 virtual void myRun(const char * /*in_name*/, bool /*invert*/ = false) {}
65 virtual void error(const char *in_macroName, const char *in_macro, const char *in_file, int in_line) {
68 m_reporter->error(in_macroName, in_macro, in_file, in_line);
72 static void message(const char *msg) {
74 m_reporter->message(msg);
78 bool equalDoubles(double in_expected, double in_real, double in_maxErr) {
79 double diff = in_expected - in_real;
83 return diff < in_maxErr;
86 virtual void progress(const char *in_className, const char *in_functionName, bool ignored, bool explicitTest) {
89 m_reporter->progress(in_className, in_functionName, ignored, explicitTest);
93 bool shouldRunThis(const char *in_desiredTest, const char *in_className, const char *in_functionName,
94 bool invert, bool explicit_test, bool &do_progress) {
95 if ((in_desiredTest) && (in_desiredTest[0] != '\0')) {
97 const char *ptr = strstr(in_desiredTest, "::");
99 bool match = (strncmp(in_desiredTest, in_className, strlen(in_className)) == 0) &&
100 (strncmp(ptr + 2, in_functionName, strlen(in_functionName)) == 0);
101 // Invert shall not make explicit test run:
102 return invert ? (match ? !match : !explicit_test)
105 bool match = (strcmp(in_desiredTest, in_className) == 0);
107 return !explicit_test && (match == !invert);
110 return !explicit_test;
120 static int m_numErrors;
121 static int m_numTests;
124 static TestCase *m_root;
128 static Reporter *m_reporter;
134 #if !defined (CPPUNIT_MINI_HIDE_UNUSED_VARIABLE)
135 # if defined (_MSC_VER)
136 # define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v) (v);
138 # define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v)
142 #define CPPUNIT_TEST_SUITE(X) \
143 typedef CPPUNIT_NS::TestCase Base; \
144 virtual void myRun(const char *in_name, bool invert = false) { \
145 const char *className = #X; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(className) \
146 bool ignoring = false; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(ignoring)
148 #if defined CPPUNIT_MINI_USE_EXCEPTIONS
149 # define CPPUNIT_TEST_BASE(X, Y) \
152 bool shouldRun = shouldRunThis(in_name, className, #X, invert, Y, do_progress); \
153 if (shouldRun || do_progress) { \
155 progress(className, #X, ignoring || !shouldRun, !ignoring && Y); \
156 if (shouldRun && !ignoring) { \
161 Base::error("Test Failed: An Exception was thrown.", #X, __FILE__, __LINE__); \
168 # define CPPUNIT_TEST_BASE(X, Y) \
171 bool shouldRun = shouldRunThis(in_name, className, #X, invert, Y, do_progress); \
172 if (shouldRun || do_progress) { \
174 progress(className, #X, ignoring || !shouldRun, !ignoring && Y); \
175 if (shouldRun && !ignoring) \
182 #define CPPUNIT_TEST(X) CPPUNIT_TEST_BASE(X, false)
183 #define CPPUNIT_EXPLICIT_TEST(X) CPPUNIT_TEST_BASE(X, true)
185 #define CPPUNIT_IGNORE \
188 #define CPPUNIT_STOP_IGNORE \
191 #define CPPUNIT_TEST_SUITE_END() }
193 #define CPPUNIT_TEST_SUITE_REGISTRATION(X) static X local
195 #define CPPUNIT_CHECK(X) \
197 Base::error("CPPUNIT_CHECK", #X, __FILE__, __LINE__); \
200 #define CPPUNIT_ASSERT(X) \
202 Base::error("CPPUNIT_ASSERT", #X, __FILE__, __LINE__); \
206 #define CPPUNIT_FAIL { \
207 Base::error("CPPUNIT_FAIL", "", __FILE__, __LINE__); \
211 #define CPPUNIT_ASSERT_EQUAL(X, Y) \
213 Base::error("CPPUNIT_ASSERT_EQUAL", #X","#Y, __FILE__, __LINE__); \
217 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(X, Y, Z) \
218 if (!equalDoubles((X), (Y), (Z))) { \
219 Base::error("CPPUNIT_ASSERT_DOUBLES_EQUAL", #X","#Y","#Z, __FILE__, __LINE__); \
223 #define CPPUNIT_MESSAGE(m) CPPUNIT_NS::TestCase::message(m)