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 CDS_CPPUNIT_MPFR_H_
25 #define CDS_CPPUNIT_MPFR_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 );
189 static void print_gc_state();
191 static std::vector<std::string> const & getTestStrings();
194 static std::vector<std::string> m_arrStrings ; // array of test strings
197 static bool m_bPrintGCState ; // print GC state after each test
199 static std::string m_strTestDataDir;
200 static bool m_bExactMatch;
203 static int m_numErrors;
204 static int m_numTests;
206 static TestCase * m_pCurTestCase;
209 static TestCase *m_root;
213 static Reporter *m_reporter;
218 #if !defined (CPPUNIT_MINI_HIDE_UNUSED_VARIABLE)
219 # if defined (_MSC_VER)
220 # define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v) (v);
222 # define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v)
226 #define CPPUNIT_TEST_SUITE_(X, cfgBranchName) \
227 typedef CppUnitMini::TestCase Base; \
228 virtual void myRun(const char *in_name, bool invert = false) { \
229 const char *className = #X; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(className) \
230 bool ignoring = false; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(ignoring) \
231 setUpParams( m_Cfg.get( cfgBranchName ));
233 #define CPPUNIT_TEST_SUITE_PART(X, func) \
234 void X::func(const char *in_name, bool invert /*= false*/) { \
235 const char *className = #X; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(className) \
236 bool ignoring = false; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(ignoring)
238 #define CPPUNIT_TEST_SUITE(X) CPPUNIT_TEST_SUITE_(X, #X)
240 #if defined CPPUNIT_MINI_USE_EXCEPTIONS
241 # define CPPUNIT_TEST_BASE(X, Y) \
244 bool shouldRun = shouldRunThis(in_name, className, #X, invert, Y, do_progress); \
245 if (shouldRun || do_progress) { \
247 progress(className, #X, ignoring || !shouldRun, !ignoring && Y); \
248 if (shouldRun && !ignoring) { \
253 Base::error("Test Failed: An exception was thrown.", #X, __FILE__, __LINE__); \
260 # define CPPUNIT_TEST_BASE(X, Y) \
263 bool shouldRun = shouldRunThis(in_name, className, #X, invert, Y, do_progress); \
264 if (shouldRun || do_progress) { \
266 progress(className, #X, ignoring || !shouldRun, !ignoring && Y); \
267 if (shouldRun && !ignoring) \
274 #define CPPUNIT_TEST(X) CPPUNIT_TEST_BASE(X, false)
275 #define CPPUNIT_EXPLICIT_TEST(X) CPPUNIT_TEST_BASE(X, true)
277 #define CPPUNIT_IGNORE \
280 #define CPPUNIT_STOP_IGNORE \
283 #define CPPUNIT_TEST_SUITE_END() endTestCase(); }
284 #define CPPUNIT_TEST_SUITE_END_PART() }
286 #define CPPUNIT_TEST_SUITE_REGISTRATION(X) static X local
287 #define CPPUNIT_TEST_SUITE_REGISTRATION_(X, NAME) static X NAME
289 #define CPPUNIT_CHECK(X) \
291 Base::error("CPPUNIT_CHECK", #X, __FILE__, __LINE__); \
294 #define CPPUNIT_CHECK_CURRENT(X) \
296 CppUnitMini::TestCase::current_test()->error("CPPUNIT_CHECK", #X, __FILE__, __LINE__); \
299 #define CPPUNIT_CHECK_EX(X, Y) \
301 std::stringstream st ; \
302 st << #X << ": " << Y ; \
303 Base::error("CPPUNIT_CHECK", st.str().c_str(), __FILE__, __LINE__); \
306 #define CPPUNIT_CHECK_CURRENT_EX(X, Y) \
308 std::stringstream st ; \
309 st << #X << ": " << Y ; \
310 CppUnitMini::TestCase::current_test()->error("CPPUNIT_CHECK", st.str().c_str(), __FILE__, __LINE__); \
313 #define CPPUNIT_ASSERT(X) \
315 Base::error("CPPUNIT_ASSERT", #X, __FILE__, __LINE__); \
319 #define CPPUNIT_ASSERT_CURRENT(X) \
321 CppUnitMini::TestCase::current_test()->error("CPPUNIT_ASSERT", #X, __FILE__, __LINE__); \
326 #define CPPUNIT_ASSERT_EX(A, X) \
328 std::stringstream st ; \
329 st << #A << ": " << X ; \
330 Base::error("CPPUNIT_ASSERT", st.str().c_str(), __FILE__, __LINE__); \
334 #define CPPUNIT_FAIL { \
335 Base::error("CPPUNIT_FAIL", "", __FILE__, __LINE__); \
339 #define CPPUNIT_ASSERT_EQUAL(X, Y) \
341 Base::error("CPPUNIT_ASSERT_EQUAL", #X","#Y, __FILE__, __LINE__); \
345 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(X, Y, Z) \
346 if (!equalDoubles((X), (Y), (Z))) { \
347 Base::error("CPPUNIT_ASSERT_DOUBLES_EQUAL", #X","#Y","#Z, __FILE__, __LINE__); \
352 #define CPPUNIT_MSG( X ) \
354 std::stringstream st ; \
356 if ( !st.str().empty() ) \
357 CppUnitMini::TestCase::message( st.str().c_str() ); \
360 #define CPPUNIT_MESSAGE(m) CppUnitMini::TestCase::message(m)
362 #define CPPUNIT_ASSERT_MSG( A, X ) \
364 std::stringstream st ; \
365 st << #A << ": " << X ; \
366 error( "CPPUNIT_ASSERT_MSG", st.str().c_str(), __FILE__, __LINE__ ) ; \
369 #endif // #ifndef CDS_CPPUNIT_MPFR_H_