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 ) ; }
84 size_t getSizeT( const char * pszParamName, size_t nDefVal = 0 ) const
86 return static_cast<size_t>( getULong( pszParamName, static_cast<unsigned long>(nDefVal)));
89 bool getBool( const char * pszParamName, bool bDefVal = false ) const
91 std::string strParamName( pszParamName );
92 cfg_map::const_iterator it = m_Cfg.find( strParamName );
93 if ( it == m_Cfg.end() )
94 return bDefVal ; // param not found -> returns default value
96 return boost::lexical_cast< int >( it->second ) != 0;
98 catch ( boost::bad_lexical_cast& ex )
100 std::cerr << "bad_lexical_cast encountered while getting parameter "
101 << strParamName << "=" << it->second
111 std::map< std::string, TestCfg> m_Cfg;
116 void load( const char * fileName );
118 TestCfg& get( const std::string& strTestName )
120 return m_Cfg[ strTestName ];
126 virtual ~TestFixture() {}
128 //! \brief Set up context before running a test.
129 virtual void setUp() {}
131 //! Clean up after the test run.
132 virtual void tearDown() {}
135 class TestCase : public TestFixture {
137 TestCase() { registerTestCase(this); }
139 void setUp() { m_failed = false; }
140 static int run(Reporter *in_reporter = 0, const char *in_testName = "", bool invert = false);
141 int numErrors() { return m_numErrors; }
142 static void registerTestCase(TestCase *in_testCase);
144 static TestCase * current_test()
146 assert( m_pCurTestCase );
147 return m_pCurTestCase;
150 virtual void setUpParams( const TestCfg& /*cfg*/ ) {}
151 virtual void endTestCase() {}
152 virtual void myRun(const char * /*in_name*/, bool /*invert*/ = false) {}
154 virtual void error(const char *in_macroName, const char *in_macro, const char *in_file, int in_line) {
157 m_reporter->error(in_macroName, in_macro, in_file, in_line);
161 static void message(const char *msg) {
163 m_reporter->message(msg);
167 bool equalDoubles(double in_expected, double in_real, double in_maxErr) {
168 double diff = in_expected - in_real;
172 return diff < in_maxErr;
175 virtual void progress(const char *in_className, const char *in_functionName, bool ignored, bool explicitTest) {
178 m_reporter->progress(in_className, in_functionName, ignored, explicitTest);
182 bool shouldRunThis( const char *in_desiredTest, const char *in_className, const char *in_functionName,
183 bool invert, bool explicit_test, bool &do_progress );
192 static void print_gc_state();
194 static std::vector<std::string> const & getTestStrings();
197 static std::vector<std::string> m_arrStrings ; // array of test strings
200 static bool m_bPrintGCState ; // print GC state after each test
202 static std::string m_strTestDataDir;
203 static bool m_bExactMatch;
206 static int m_numErrors;
207 static int m_numTests;
209 static TestCase * m_pCurTestCase;
212 static TestCase *m_root;
216 static Reporter *m_reporter;
221 #if !defined (CPPUNIT_MINI_HIDE_UNUSED_VARIABLE)
222 # if defined (_MSC_VER)
223 # define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v) (v);
225 # define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v)
229 #define CPPUNIT_TEST_SUITE_(X, cfgBranchName) \
230 typedef CppUnitMini::TestCase Base; \
231 virtual void myRun(const char *in_name, bool invert = false) { \
232 const char *className = #X; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(className) \
233 bool ignoring = false; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(ignoring) \
234 setUpParams( m_Cfg.get( cfgBranchName ));
236 #define CPPUNIT_TEST_SUITE_PART(X, func) \
237 void X::func(const char *in_name, bool invert /*= false*/) { \
238 const char *className = #X; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(className) \
239 bool ignoring = false; CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(ignoring)
241 #define CPPUNIT_TEST_SUITE(X) CPPUNIT_TEST_SUITE_(X, #X)
243 #if defined CPPUNIT_MINI_USE_EXCEPTIONS
244 # define CPPUNIT_TEST_BASE(X, Y) \
247 bool shouldRun = shouldRunThis(in_name, className, #X, invert, Y, do_progress); \
248 if (shouldRun || do_progress) { \
250 progress(className, #X, ignoring || !shouldRun, !ignoring && Y); \
251 if (shouldRun && !ignoring) { \
256 Base::error("Test Failed: An exception was thrown.", #X, __FILE__, __LINE__); \
263 # define CPPUNIT_TEST_BASE(X, Y) \
266 bool shouldRun = shouldRunThis(in_name, className, #X, invert, Y, do_progress); \
267 if (shouldRun || do_progress) { \
269 progress(className, #X, ignoring || !shouldRun, !ignoring && Y); \
270 if (shouldRun && !ignoring) \
277 #define CPPUNIT_TEST(X) CPPUNIT_TEST_BASE(X, false)
278 #define CPPUNIT_EXPLICIT_TEST(X) CPPUNIT_TEST_BASE(X, true)
280 #define CDSUNIT_DECLARE_TEST(X) void X();
282 #define CPPUNIT_IGNORE \
285 #define CPPUNIT_STOP_IGNORE \
288 #define CPPUNIT_TEST_SUITE_END() endTestCase(); }
289 #define CPPUNIT_TEST_SUITE_END_PART() }
291 #define CPPUNIT_TEST_SUITE_REGISTRATION(X) static X local
292 #define CPPUNIT_TEST_SUITE_REGISTRATION_(X, NAME) static X NAME
294 #define CPPUNIT_CHECK(X) \
296 Base::error("CPPUNIT_CHECK", #X, __FILE__, __LINE__); \
299 #define CPPUNIT_CHECK_CURRENT(X) \
301 CppUnitMini::TestCase::current_test()->error("CPPUNIT_CHECK", #X, __FILE__, __LINE__); \
304 #define CPPUNIT_CHECK_EX(X, Y) \
306 std::stringstream st ; \
307 st << #X << ": " << Y ; \
308 Base::error("CPPUNIT_CHECK", st.str().c_str(), __FILE__, __LINE__); \
311 #define CPPUNIT_CHECK_CURRENT_EX(X, Y) \
313 std::stringstream st ; \
314 st << #X << ": " << Y ; \
315 CppUnitMini::TestCase::current_test()->error("CPPUNIT_CHECK", st.str().c_str(), __FILE__, __LINE__); \
318 #define CPPUNIT_ASSERT(X) \
320 Base::error("CPPUNIT_ASSERT", #X, __FILE__, __LINE__); \
324 #define CPPUNIT_ASSERT_CURRENT(X) \
326 CppUnitMini::TestCase::current_test()->error("CPPUNIT_ASSERT", #X, __FILE__, __LINE__); \
331 #define CPPUNIT_ASSERT_EX(A, X) \
333 std::stringstream st ; \
334 st << #A << ": " << X ; \
335 Base::error("CPPUNIT_ASSERT", st.str().c_str(), __FILE__, __LINE__); \
339 #define CPPUNIT_FAIL { \
340 Base::error("CPPUNIT_FAIL", "", __FILE__, __LINE__); \
344 #define CPPUNIT_ASSERT_EQUAL(X, Y) \
346 Base::error("CPPUNIT_ASSERT_EQUAL", #X","#Y, __FILE__, __LINE__); \
350 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(X, Y, Z) \
351 if (!equalDoubles((X), (Y), (Z))) { \
352 Base::error("CPPUNIT_ASSERT_DOUBLES_EQUAL", #X","#Y","#Z, __FILE__, __LINE__); \
357 #define CPPUNIT_MSG( X ) \
359 std::stringstream st ; \
361 if ( !st.str().empty() ) \
362 CppUnitMini::TestCase::message( st.str().c_str() ); \
365 #define CPPUNIT_MESSAGE(m) CppUnitMini::TestCase::message(m)
367 #define CPPUNIT_ASSERT_MSG( A, X ) \
369 std::stringstream st ; \
370 st << #A << ": " << X ; \
371 error( "CPPUNIT_ASSERT_MSG", st.str().c_str(), __FILE__, __LINE__ ) ; \
374 #endif // #ifndef CDS_CPPUNIT_MPFR_H_