issue#11: cds: changed __CDS_ guard prefix to CDSLIB_ for all .h files
[libcds.git] / tests / cppunit / cppunit_mini.h
1 //$$CDS-header$$
2
3 /*
4  * Copyright (c) 2003, 2004
5  * Zdenek Nemec
6  *
7  * This material is provided "as is", with absolutely no warranty expressed
8  * or implied. Any use is at your own risk.
9  *
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.
15  *
16  */
17
18 /*
19     Partially changed and expanded by Maxim Khiszinsky (cds), 2009
20 */
21
22 /* $Id$ */
23
24 #ifndef CDS_CPPUNIT_MPFR_H_
25 #define CDS_CPPUNIT_MPFR_H_
26
27 #include <string.h>
28 #include <sstream>
29 #include <iostream>
30 #include <vector>
31 #include <string>
32 #include <map>
33 #include <assert.h>
34
35 #include <boost/lexical_cast.hpp>
36
37 namespace CppUnitMini
38 {
39   class Reporter {
40   public:
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 */) {}
45     virtual void end() {}
46     virtual void printSummary() {}
47   };
48
49   struct TestCfg
50   {
51       typedef std::map< std::string, std::string >  cfg_map;
52       cfg_map    m_Cfg ; // map param_name => value
53
54       template <typename T>
55       T get( const std::string& strParamName, T defVal ) const
56       {
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
60           try {
61               return boost::lexical_cast< T >( it->second );
62           }
63           catch ( boost::bad_lexical_cast& ex )
64           {
65               std::cerr << "bad_lexical_cast encountered while getting parameter "
66                   << strParamName << "=" << it->second
67                   << ": " << ex.what()
68                   << std::endl
69 ;
70           }
71           return defVal;
72       }
73
74       template <typename T>
75       T get( const char * pszParamName, T defVal ) const
76       {
77           return get( std::string( pszParamName ), defVal );
78       }
79
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
85       bool getBool( const char * pszParamName, bool bDefVal = false ) const
86       {
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
91           try {
92               return boost::lexical_cast< int >( it->second ) != 0;
93           }
94           catch ( boost::bad_lexical_cast& ex )
95           {
96               std::cerr << "bad_lexical_cast encountered while getting parameter "
97                   << strParamName << "=" << it->second
98                   << ": " << ex.what()
99                   << std::endl
100 ;
101           }
102           return bDefVal;
103       }
104
105   };
106
107   class Config {
108       std::map< std::string, TestCfg>  m_Cfg;
109
110   public:
111       Config() {}
112
113       void load( const char * fileName );
114
115       TestCfg& get( const std::string& strTestName )
116       {
117           return m_Cfg[ strTestName ];
118       }
119   };
120
121   class TestFixture {
122   public:
123     virtual ~TestFixture() {}
124
125     //! \brief Set up context before running a test.
126     virtual void setUp() {}
127
128     //! Clean up after the test run.
129     virtual void tearDown() {}
130   };
131
132   class TestCase : public TestFixture {
133   public:
134     TestCase() { registerTestCase(this); }
135
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);
140
141     static TestCase * current_test()
142     {
143         assert( m_pCurTestCase );
144         return m_pCurTestCase;
145     }
146
147     virtual void setUpParams( const TestCfg& /*cfg*/ ) {}
148     virtual void endTestCase() {}
149     virtual void myRun(const char * /*in_name*/, bool /*invert*/ = false) {}
150
151     virtual void error(const char *in_macroName, const char *in_macro, const char *in_file, int in_line) {
152       m_failed = true;
153       if (m_reporter) {
154         m_reporter->error(in_macroName, in_macro, in_file, in_line);
155       }
156     }
157
158     static void message(const char *msg) {
159       if (m_reporter) {
160         m_reporter->message(msg);
161       }
162     }
163
164     bool equalDoubles(double in_expected, double in_real, double in_maxErr) {
165       double diff = in_expected - in_real;
166       if (diff < 0.) {
167         diff = -diff;
168       }
169       return diff < in_maxErr;
170     }
171
172     virtual void progress(const char *in_className, const char *in_functionName, bool ignored, bool explicitTest) {
173       ++m_numTests;
174       if (m_reporter) {
175         m_reporter->progress(in_className, in_functionName, ignored, explicitTest);
176       }
177     }
178
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
182     void tearDown() {
183       print_gc_state();
184       if (m_failed)
185         ++m_numErrors;
186       m_reporter->end();
187     }
188
189     static void print_gc_state();
190
191     static std::vector<std::string> const &    getTestStrings();
192
193   protected:
194     static std::vector<std::string>  m_arrStrings ;   // array of test strings
195
196   public:
197       static bool m_bPrintGCState   ;   // print GC state after each test
198       static Config m_Cfg;
199       static std::string m_strTestDataDir;
200       static bool m_bExactMatch;
201
202   protected:
203     static int m_numErrors;
204     static int m_numTests;
205
206     static TestCase * m_pCurTestCase;
207
208   private:
209     static TestCase *m_root;
210     TestCase *m_next;
211     bool m_failed;
212
213     static Reporter *m_reporter;
214   };
215
216 }
217
218 #if !defined (CPPUNIT_MINI_HIDE_UNUSED_VARIABLE)
219 #  if defined (_MSC_VER)
220 #    define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v) (v);
221 #  else
222 #    define CPPUNIT_MINI_HIDE_UNUSED_VARIABLE(v)
223 #  endif
224 #endif
225
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 ));
232
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)
237
238 #define CPPUNIT_TEST_SUITE(X) CPPUNIT_TEST_SUITE_(X, #X)
239
240 #if defined CPPUNIT_MINI_USE_EXCEPTIONS
241 #  define CPPUNIT_TEST_BASE(X, Y) \
242   { \
243     bool do_progress; \
244     bool shouldRun = shouldRunThis(in_name, className, #X, invert, Y, do_progress); \
245     if (shouldRun || do_progress) { \
246       setUp(); \
247       progress(className, #X, ignoring || !shouldRun, !ignoring && Y); \
248       if (shouldRun && !ignoring) { \
249         try { \
250           X(); \
251         } \
252         catch(...) { \
253           Base::error("Test Failed: An exception was thrown.", #X, __FILE__, __LINE__); \
254         } \
255       } \
256       tearDown(); \
257     } \
258   }
259 #else
260 #  define CPPUNIT_TEST_BASE(X, Y) \
261   { \
262     bool do_progress; \
263     bool shouldRun = shouldRunThis(in_name, className, #X, invert, Y, do_progress); \
264     if (shouldRun || do_progress) { \
265       setUp(); \
266       progress(className, #X, ignoring || !shouldRun, !ignoring && Y); \
267       if (shouldRun && !ignoring) \
268         X(); \
269       tearDown(); \
270     } \
271   }
272 #endif
273
274 #define CPPUNIT_TEST(X) CPPUNIT_TEST_BASE(X, false)
275 #define CPPUNIT_EXPLICIT_TEST(X) CPPUNIT_TEST_BASE(X, true)
276
277 #define CPPUNIT_IGNORE \
278   ignoring = true
279
280 #define CPPUNIT_STOP_IGNORE \
281   ignoring = false
282
283 #define CPPUNIT_TEST_SUITE_END() endTestCase(); }
284 #define CPPUNIT_TEST_SUITE_END_PART() }
285
286 #define CPPUNIT_TEST_SUITE_REGISTRATION(X) static X local
287 #define CPPUNIT_TEST_SUITE_REGISTRATION_(X, NAME) static X NAME
288
289 #define CPPUNIT_CHECK(X) \
290   if (!(X)) { \
291     Base::error("CPPUNIT_CHECK", #X, __FILE__, __LINE__); \
292   }
293
294 #define CPPUNIT_CHECK_CURRENT(X) \
295   if (!(X)) { \
296     CppUnitMini::TestCase::current_test()->error("CPPUNIT_CHECK", #X, __FILE__, __LINE__); \
297   }
298
299 #define CPPUNIT_CHECK_EX(X, Y) \
300     if (!(X)) { \
301         std::stringstream st    ;   \
302         st << #X << ": " << Y   ;   \
303         Base::error("CPPUNIT_CHECK", st.str().c_str(), __FILE__, __LINE__); \
304     }
305
306 #define CPPUNIT_CHECK_CURRENT_EX(X, Y) \
307     if (!(X)) { \
308         std::stringstream st    ;   \
309         st << #X << ": " << Y   ;   \
310         CppUnitMini::TestCase::current_test()->error("CPPUNIT_CHECK", st.str().c_str(), __FILE__, __LINE__); \
311     }
312
313 #define CPPUNIT_ASSERT(X) \
314   if (!(X)) { \
315     Base::error("CPPUNIT_ASSERT", #X, __FILE__, __LINE__); \
316     return; \
317   }
318
319 #define CPPUNIT_ASSERT_CURRENT(X) \
320     if (!(X)) { \
321         CppUnitMini::TestCase::current_test()->error("CPPUNIT_ASSERT", #X, __FILE__, __LINE__); \
322         return; \
323     }
324
325
326 #define CPPUNIT_ASSERT_EX(A, X) \
327     if (!(A)) { \
328         std::stringstream st    ;   \
329         st << #A << ": " << X   ;   \
330         Base::error("CPPUNIT_ASSERT", st.str().c_str(), __FILE__, __LINE__); \
331         return; \
332     }
333
334 #define CPPUNIT_FAIL { \
335     Base::error("CPPUNIT_FAIL", "", __FILE__, __LINE__); \
336     return; \
337   }
338
339 #define CPPUNIT_ASSERT_EQUAL(X, Y) \
340   if ((X) != (Y)) { \
341     Base::error("CPPUNIT_ASSERT_EQUAL", #X","#Y, __FILE__, __LINE__); \
342     return; \
343   }
344
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__); \
348     return; \
349   }
350
351 // added by cds
352 #define CPPUNIT_MSG( X ) \
353     {   \
354         std::stringstream st    ;   \
355         st << X ;   \
356         if ( !st.str().empty() ) \
357             CppUnitMini::TestCase::message( st.str().c_str() );   \
358     }
359
360 #define CPPUNIT_MESSAGE(m) CppUnitMini::TestCase::message(m)
361
362 #define CPPUNIT_ASSERT_MSG( A, X ) \
363     if ( !(A) ){   \
364         std::stringstream st    ;   \
365         st << #A << ": " << X ;   \
366         error( "CPPUNIT_ASSERT_MSG", st.str().c_str(), __FILE__, __LINE__ )     ;   \
367     }
368
369 #endif // #ifndef CDS_CPPUNIT_MPFR_H_