4 * Copyright (c) 2003, 2004
7 * This material is provided "as is", with absolutely no warranty expressed
8 * or implied. Any use is at your own risk.
10 * Permission to use or copy this software for any purpose is hereby granted
11 * without fee, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
19 Partially changed and expanded by Maxim Khiszinsky (cds), 2009
24 #ifndef _CPPUNITMPFR_H_
25 #define _CPPUNITMPFR_H_
35 #include <boost/lexical_cast.hpp>
41 virtual ~Reporter() {}
42 virtual void error(const char * /*macroName*/, const char * /*in_macro*/, const char * /*in_file*/, int /*in_line*/) {}
43 virtual void message( const char * /*msg*/ ) {}
44 virtual void progress( const char * /*in_className*/, const char * /*in_testName*/, bool /*ignored*/, bool /* explicit */) {}
46 virtual void printSummary() {}
51 typedef std::map< std::string, std::string > cfg_map;
52 cfg_map m_Cfg ; // map param_name => value
55 T get( const std::string& strParamName, T defVal ) const
57 cfg_map::const_iterator it = m_Cfg.find( strParamName );
58 if ( it == m_Cfg.end() )
59 return defVal ; // param not found -> returns default value
61 return boost::lexical_cast< T >( it->second );
63 catch ( boost::bad_lexical_cast& ex )
65 std::cerr << "bad_lexical_cast encountered while getting parameter "
66 << strParamName << "=" << it->second
75 T get( const char * pszParamName, T defVal ) const
77 return get( std::string( pszParamName ), defVal );
80 int getInt( const char * pszParamName, int nDefVal = 0 ) const { return get( pszParamName, nDefVal ) ; }
81 unsigned int getUInt( const char * pszParamName, unsigned int nDefVal = 0 ) const { return get( pszParamName, nDefVal ) ; }
82 long getLong( const char * pszParamName, long nDefVal = 0 ) const { return get( pszParamName, nDefVal ) ; }
83 unsigned long getULong( const char * pszParamName, unsigned long nDefVal = 0 ) const { return get( pszParamName, nDefVal ) ; }
85 bool getBool( const char * pszParamName, bool bDefVal = false ) const
87 std::string strParamName( pszParamName );
88 cfg_map::const_iterator it = m_Cfg.find( strParamName );
89 if ( it == m_Cfg.end() )
90 return bDefVal ; // param not found -> returns default value
92 return boost::lexical_cast< int >( it->second ) != 0;
94 catch ( boost::bad_lexical_cast& ex )
96 std::cerr << "bad_lexical_cast encountered while getting parameter "
97 << strParamName << "=" << it->second
108 std::map< std::string, TestCfg> m_Cfg;
113 void load( const char * fileName );
115 TestCfg& get( const std::string& strTestName )
117 return m_Cfg[ strTestName ];
123 virtual ~TestFixture() {}
125 //! \brief Set up context before running a test.
126 virtual void setUp() {}
128 //! Clean up after the test run.
129 virtual void tearDown() {}
132 class TestCase : public TestFixture {
134 TestCase() { registerTestCase(this); }
136 void setUp() { m_failed = false; }
137 static int run(Reporter *in_reporter = 0, const char *in_testName = "", bool invert = false);
138 int numErrors() { return m_numErrors; }
139 static void registerTestCase(TestCase *in_testCase);
141 static TestCase * current_test()
143 assert( m_pCurTestCase );
144 return m_pCurTestCase;
147 virtual void setUpParams( const TestCfg& cfg ) {}
148 virtual void endTestCase() {}
149 virtual void myRun(const char * /*in_name*/, bool /*invert*/ = false) {}
151 virtual void error(const char *in_macroName, const char *in_macro, const char *in_file, int in_line) {
154 m_reporter->error(in_macroName, in_macro, in_file, in_line);
158 static void message(const char *msg) {
160 m_reporter->message(msg);
164 bool equalDoubles(double in_expected, double in_real, double in_maxErr) {
165 double diff = in_expected - in_real;
169 return diff < in_maxErr;
172 virtual void progress(const char *in_className, const char *in_functionName, bool ignored, bool explicitTest) {
175 m_reporter->progress(in_className, in_functionName, ignored, explicitTest);
179 bool shouldRunThis(const char *in_desiredTest, const char *in_className, const char *in_functionName,
180 bool invert, bool explicit_test, bool &do_progress) {
181 if ((in_desiredTest) && (in_desiredTest[0] != '\0')) {
183 const char *ptr = strstr(in_desiredTest, "::");
185 bool match = (strncmp(in_desiredTest, in_className, strlen(in_className)) == 0 && in_desiredTest[strlen(in_className)] == ':' )
186 && (strncmp(ptr + 2, in_functionName, strlen(ptr + 2)) == 0);
187 // Invert shall not make explicit test run:
188 return invert ? (match ? !match : !explicit_test)
191 bool match = (strcmp(in_desiredTest, in_className) == 0);
193 return !explicit_test && (match == !invert);
196 return !explicit_test;
206 static void print_gc_state();
208 static std::vector<std::string> const & getTestStrings();
211 static std::vector<std::string> m_arrStrings ; // array of test strings
214 static bool m_bPrintGCState ; // print GC state after each test
216 static std::string m_strTestDataDir;
219 static int m_numErrors;
220 static int m_numTests;
222 static TestCase * m_pCurTestCase;
225 static TestCase *m_root;
229 static Reporter *m_reporter;
234 #if !defined (CPPUNIT_MINI_HIDE_UNUSED_VARIABLE)
235 # if defined (_MSC_VER)
236 # define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v) (v);
238 # define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v)
242 #define CPPUNIT_TEST_SUITE_(X, cfgBranchName) \
243 typedef CppUnitMini::TestCase Base; \
244 virtual void myRun(const char *in_name, bool invert = false) { \
245 const char *className = #X; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(className) \
246 bool ignoring = false; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(ignoring) \
247 setUpParams( m_Cfg.get( cfgBranchName ));
249 #define CPPUNIT_TEST_SUITE_PART(X, func) \
250 void X::func(const char *in_name, bool invert /*= false*/) { \
251 const char *className = #X; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(className) \
252 bool ignoring = false; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(ignoring)
254 #define CPPUNIT_TEST_SUITE(X) CPPUNIT_TEST_SUITE_(X, #X)
256 #if defined CPPUNIT_MINI_USE_EXCEPTIONS
257 # define CPPUNIT_TEST_BASE(X, Y) \
260 bool shouldRun = shouldRunThis(in_name, className, #X, invert, Y, do_progress); \
261 if (shouldRun || do_progress) { \
263 progress(className, #X, ignoring || !shouldRun, !ignoring && Y); \
264 if (shouldRun && !ignoring) { \
269 Base::error("Test Failed: An Exception was thrown.", #X, __FILE__, __LINE__); \
276 # define CPPUNIT_TEST_BASE(X, Y) \
279 bool shouldRun = shouldRunThis(in_name, className, #X, invert, Y, do_progress); \
280 if (shouldRun || do_progress) { \
282 progress(className, #X, ignoring || !shouldRun, !ignoring && Y); \
283 if (shouldRun && !ignoring) \
290 #define CPPUNIT_TEST(X) CPPUNIT_TEST_BASE(X, false)
291 #define CPPUNIT_EXPLICIT_TEST(X) CPPUNIT_TEST_BASE(X, true)
293 #define CPPUNIT_IGNORE \
296 #define CPPUNIT_STOP_IGNORE \
299 #define CPPUNIT_TEST_SUITE_END() endTestCase(); }
300 #define CPPUNIT_TEST_SUITE_END_PART() }
302 #define CPPUNIT_TEST_SUITE_REGISTRATION(X) static X local
303 #define CPPUNIT_TEST_SUITE_REGISTRATION_(X, NAME) static X NAME
305 #define CPPUNIT_CHECK(X) \
307 Base::error("CPPUNIT_CHECK", #X, __FILE__, __LINE__); \
310 #define CPPUNIT_CHECK_CURRENT(X) \
312 CppUnitMini::TestCase::current_test()->error("CPPUNIT_CHECK", #X, __FILE__, __LINE__); \
315 #define CPPUNIT_CHECK_EX(X, Y) \
317 std::stringstream st ; \
318 st << #X << ": " << Y ; \
319 Base::error("CPPUNIT_CHECK", st.str().c_str(), __FILE__, __LINE__); \
322 #define CPPUNIT_CHECK_CURRENT_EX(X, Y) \
324 std::stringstream st ; \
325 st << #X << ": " << Y ; \
326 CppUnitMini::TestCase::current_test()->error("CPPUNIT_CHECK", st.str().c_str(), __FILE__, __LINE__); \
329 #define CPPUNIT_ASSERT(X) \
331 Base::error("CPPUNIT_ASSERT", #X, __FILE__, __LINE__); \
335 #define CPPUNIT_ASSERT_CURRENT(X) \
337 CppUnitMini::TestCase::current_test()->error("CPPUNIT_ASSERT", #X, __FILE__, __LINE__); \
342 #define CPPUNIT_ASSERT_EX(A, X) \
344 std::stringstream st ; \
345 st << #A << ": " << X ; \
346 Base::error("CPPUNIT_ASSERT", st.str().c_str(), __FILE__, __LINE__); \
350 #define CPPUNIT_FAIL { \
351 Base::error("CPPUNIT_FAIL", "", __FILE__, __LINE__); \
355 #define CPPUNIT_ASSERT_EQUAL(X, Y) \
357 Base::error("CPPUNIT_ASSERT_EQUAL", #X","#Y, __FILE__, __LINE__); \
361 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(X, Y, Z) \
362 if (!equalDoubles((X), (Y), (Z))) { \
363 Base::error("CPPUNIT_ASSERT_DOUBLES_EQUAL", #X","#Y","#Z, __FILE__, __LINE__); \
368 #define CPPUNIT_MSG( X ) \
370 std::stringstream st ; \
372 if ( !st.str().empty() ) \
373 CppUnitMini::TestCase::message( st.str().c_str() ); \
376 #define CPPUNIT_MESSAGE(m) CppUnitMini::TestCase::message(m)
378 #define CPPUNIT_ASSERT_MSG( A, X ) \
380 std::stringstream st ; \
381 st << #A << ": " << X ; \
382 error( "CPPUNIT_ASSERT_MSG", st.str().c_str(), __FILE__, __LINE__ ) ; \