00001
00002
00048 #ifndef ADH_test_h
00049 #define ADH_test_h
00050
00051 #include <string>
00052 #include <sstream>
00053
00054
00055
00057 namespace std {}
00058
00061 namespace ECCI { }
00062
00077 class TestFixture {
00078 public:
00079 virtual void setUp() { }
00080 virtual void tearDown() { }
00081
00082 };
00083
00091 class TestResult {
00092 private: TestResult();
00093 };
00094
00099 class Test {
00100 private: Test();
00101 public:
00103 virtual int countTestCases() = 0;
00105 virtual void run(TestResult& result) = 0;
00106 };
00107
00108 #include <typeinfo.h>
00109
00112 class TestCase : public TestFixture {
00113 protected:
00114 int m_pass;
00115 int m_error;
00116 const char * m_name;
00117
00118 std::string m_errorString;
00119 public:
00120 TestCase(const char * name=0);
00121 virtual ~TestCase() { }
00122 virtual bool run() = 0;
00123 bool Run() { return run(); }
00124 bool runTest() { return run(); }
00125 int nPass() const { return m_pass; }
00126 int nError() const { return m_error; }
00127 void recordSuccess() { ++m_pass; }
00128 virtual void reset() { m_pass = m_error = 0; m_errorString = ""; }
00129 public:
00130 int countTestCases() const { return nPass()+nError(); }
00131
00132 std::string getName() const { return ( m_name != 0 ? m_name : typeid(*this).name()); }
00133 void setName( const char * name=0 ) { m_name = name; }
00134
00135 const std::string& toString() const { return m_errorString; }
00136 const std::string& errorString() const { return toString(); }
00137 protected:
00138 void recordError( const std::string& lbl, const char* fname, int lineno);
00139 void testThis( bool cond, const std::string& lbl, const char* fname, long lineno);
00140 void testThis( bool cond, const char* lbl, const char* fname, long lineno) {
00141 testThis( cond, std::string(lbl), fname, lineno);
00142 }
00143 private:
00144 TestCase(const TestCase&);
00145 TestCase& operator=(const TestCase&);
00146 };
00147
00149 class TestSuite : public TestCase { };
00150
00151 inline TestCase::TestCase(const char * name) : m_pass(0), m_error(0), m_name(name), m_errorString() {
00152 m_errorString = "";
00153 }
00154
00165 inline void TestCase::testThis( bool cond, const std::string& lbl, const char* fname, long lineno) {
00166 if (cond) {
00167 recordSuccess();
00168 }
00169 else {
00170 recordError(lbl, fname, lineno);
00171 }
00172 }
00173
00181 inline void TestCase::recordError( const std::string& lbl, const char* fname, int lineno) {
00182
00183 std::basic_ostringstream<char> ost;
00184 ost << "=\\_error: " << lbl << " \n=/ (" << lineno << ") " << fname << "\n";
00185 m_errorString += ost.str();
00186 m_error++;
00187 }
00188
00190 #define TEST_ADH_test()
00191 #undef TEST_ADH_test
00192
00204 #define TEST_THIS(cond) testThis( cond, #cond, __FILE__, __LINE__ )
00205
00209 #define TEST_THIS_Msg(msg, cond) testThis( cond, msg, __FILE__, __LINE__ )
00210
00222 #define TEST_ERROR(str) recordError( str, __FILE__, __LINE__ )
00223
00242 #define TEST_SUCCESS() recordSuccess()
00243
00251 #define TEST_EQUAL(expected, actual) \
00252 testThis( (expected) == (actual), #expected " == " #actual, __FILE__, __LINE__ )
00253
00262 #define TEST_EQUAL_Msg(MSG, expected, actual) \
00263 testThis( (expected) == (actual), MSG, __FILE__, __LINE__ )
00264
00267 std::ostream & operator << (std::ostream& COUT, const TestCase& test) {
00268 COUT << "TestCase \"" << test.getName() << "\":\n"
00269 << "\tOK: " << test.nPass()
00270 << "\tERROR: " << test.nError() << "\n";
00271 return COUT;
00272 }
00273
00278 inline long Report( std::ostream& COUT, const TestCase &test ) {
00279 COUT << test;
00280 if (test.nError() != 0) {
00281 COUT << test.errorString();
00282 }
00283 return test.nError();
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 template <class T> bool check_ok( const T& );
00295 inline bool check_ok( const signed char & ) { return true; }
00296 inline bool check_ok( const unsigned char & ) { return true; }
00297 inline bool check_ok( const signed int & ) { return true; }
00298 inline bool check_ok( const unsigned int& ) { return true; }
00299 inline bool check_ok( const signed long & ) { return true; }
00300 inline bool check_ok( const unsigned long & ) { return true; }
00301 inline bool check_ok( const float & ) { return true; }
00302 inline bool check_ok( const double & ) { return true; }
00303 inline bool check_ok( const long double & ) { return true; }
00304
00307 template <class T>
00308 std::string toString( const T & val ) {
00309
00310 std::basic_ostringstream<char> temp;
00311 temp << val;
00312 return temp.str( );
00313 }
00314
00316 #define CPPUNIT_ADH_test()
00317 #undef CPPUNIT_ADH_test
00318
00321 #define CPPUNIT_ASSERT(condition) TEST_THIS(condition)
00322
00325 #define CPPUNIT_ASSERT_MESSAGE(message, condition) CPPUNIT_ASSERT(condition)
00326
00329 #define CPPUNIT_FAIL(message) TEST_ERROR(message)
00330
00333 #define CPPUNIT_ASSERT_EQUAL(expected, actual) TEST_EQUAL(expected, actual)
00334
00337 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual) \
00338 TEST_EQUAL_Msg(message, expected, actual)
00339
00342 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta) \
00343 assertEquals_Delta(expected, actual, delta)
00344
00347 #define CPPUNIT_ASSERT_THROW(expression, ExceptionType) \
00348 do { \
00349 bool cpputExceptionThrown_ = false; \
00350 try { \
00351 expression; \
00352 } catch ( const ExceptionType & ) { \
00353 cpputExceptionThrown_ = true; \
00354 } \
00355 \
00356 if ( cpputExceptionThrown_ ) { \
00357 break; \
00358 } \
00359 TEST_ERROR( \
00360 "Expected exception: " #ExceptionType ) \
00361 } while ( false )
00362
00365 #define CPPUNIT_ASSERT_NO_THROW(expression) \
00366 do { \
00367 try { \
00368 expression; \
00369 } catch ( ... ) { \
00370 TEST_ERROR("Unexpected exception caught"); \
00371 } \
00372 } while ( false )
00373
00376 #define CPPUNIT_ASSERT_ASSERTION_FAIL(assertion) \
00377 CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
00378
00381 #define CPPUNIT_ASSERT_ASSERTION_PASS(assertion) \
00382 CPPUNIT_ASSERT_NO_THROW( assertion )
00383
00384
00385
00386
00387 #define JUnit_ADH_test()
00388 #undef JUnit_ADH_test
00389
00393 #define assertEquals( EXPECTED, ACTUAL ) TEST_EQUAL(EXPECTED, ACTUAL)
00396 #define assertEquals_Msg( MSG, EXPECTED, ACTUAL ) TEST_EQUAL_Msg(MSG, EXPECTED, ACTUAL)
00397
00400 #define assertTrue( CONDITION ) testThis( CONDITION, #CONDITION, __FILE__, __LINE__ )
00403 #define assertTrue_Msg( MSG, CONDITION ) testThis( CONDITION, MSG, __FILE__, __LINE__ )
00404
00407 #define assertFalse( CONDITION ) testThis( !(CONDITION), "!(" #CONDITION ")", __FILE__, __LINE__ )
00410 #define assertFalse_Msg( MSG, CONDITION ) testThis( !(CONDITION), MSG, __FILE__, __LINE__ )
00411
00412 #include <math.h>
00415 #define assertEquals_Delta(EXPECTED, ACTUAL, DELTA ) \
00416 testThis( fabs( double(EXPECTED) - double(ACTUAL) ) < double(DELTA), \
00417 "|" #EXPECTED "-" #ACTUAL "| < " #DELTA, __FILE__, __LINE__ )
00418
00421 #define assertEquals_Delta_Msg( MSG, EXPECTED, ACTUAL, DELTA ) \
00422 testThis( fabs( double(EXPECTED) - double(ACTUAL) ) < double(DELTA), \
00423 MSG, __FILE__, __LINE__ )
00424
00427 #define assertNull(OBJECT) testThis( 0==&(OBJECT), "assertNull(" #OBJECT ")", __FILE__, __LINE__ )
00430 #define assertNotNull(OBJECT) testThis( 0!=&(OBJECT), "assertNotNull(" #OBJECT ")", __FILE__, __LINE__ )
00431
00434 #define assertSame(THIS, THAT) testThis( &(THIS)==&(THAT), "assertSame(" #THIS ", " #THAT ")", __FILE__, __LINE__ )
00437 #define assertNotSame(THIS, THAT) testThis( &(THIS)!=&(THAT), "assertNotSame(" #THIS ", " #THAT ")", __FILE__, __LINE__ )
00438
00441 #define fail( ) TEST_ERROR("ERROR")
00444 #define fail_Msg( MSG ) TEST_ERROR(MSG)
00445
00446 #endif // ADH_test_h
00447
00448