test.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2011 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #ifndef __KDAB__UNITTEST__TEST_H__
00024 #define __KDAB__UNITTEST__TEST_H__
00025 
00026 #ifndef KDAB_NO_UNIT_TESTS
00027 
00028 #include "kdchart_export.h"
00029 #include "kdganttglobal.h"
00030 
00031 #include <string>
00032 #include <iostream>
00033 
00034 namespace KDAB {
00035 namespace UnitTest {
00036 
00037 #define assertNotNull( x ) _assertNotNull( ( x ), #x, __FILE__, __LINE__ )
00038 #define assertNull( x ) _assertNull( ( x ), #x, __FILE__, __LINE__ )
00039 #define assertTrue( x )  _assertTrue( (x), #x, __FILE__, __LINE__ )
00040 #define assertFalse( x ) _assertFalse( (x), #x, __FILE__, __LINE__ )
00041 #define assertEqual( x, y ) _assertEqual( (x), (y), #x, #y, __FILE__, __LINE__ )
00042 #define assertNotEqual( x, y ) _assertNotEqual( (x), (y), #x, #y, __FILE__, __LINE__ )
00043 // to be phased out:
00044 #define assertNearEqual( x, y, z )
00045 #define assertEqualWithEpsilons( x, y, z ) _assertEqualWithEpsilons( (x), (y), (z), #x, #y, #z, __FILE__, __LINE__ )
00046 #if 0
00047 #define assertIsNaN( x ) _assertIsNaN( (x), #x, __FILE__, __LINE__ )
00048 #define assertIsNotNaN( x ) _assertIsNotNaN( (x), #x, __FILE__, __LINE__ )
00049 #endif
00050 
00051 #define assertThrowsExceptionWithCode( x, E, code ) \
00052 do {                           \
00053   try {                        \
00054     x;                         \
00055     fail( __FILE__, __LINE__ ) \
00056       << "\"" #x "\" didn't throw \"" #E "\"" << std::endl; \
00057   } catch ( E & ppq_ut_thrown ) {                           \
00058     success();                 \
00059     ( void )ppq_ut_thrown;     \
00060     code;                      \
00061   } catch ( ... ) {            \
00062     fail( __FILE__, __LINE__ ) \
00063       << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \
00064   }                            \
00065 } while ( false )
00066 
00067 #define assertThrowsException( x, E ) assertThrowsExceptionWithCode( x, E, do{}while(0) )
00068 
00069 #define assertDoesNotThrowException( x, E ) \
00070 do {                           \
00071   try {                        \
00072     x;                         \
00073     success();                 \
00074   } catch ( E & ) {         \
00075     fail( __FILE__, __LINE__ ) \
00076       << "\"" #x "\" threw \"" #E "\", but shouldn't" << std::endl; \
00077   } catch ( ... ) {            \
00078     fail( __FILE__, __LINE__ ) \
00079       << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \
00080   }                            \
00081 } while ( false )
00082 
00083 
00084   class Test {
00085     const std::string mName;
00086     unsigned int mFailed, mSucceeded;
00087   public:
00088     Test( const std::string & name );
00089     virtual ~Test() {}
00090 
00091     const std::string & name() const { return mName; }
00092     unsigned int failed() const { return mFailed; }
00093     unsigned int succeeded() const { return mSucceeded; }
00094 
00095     virtual void run() = 0;
00096 
00097   protected:
00098     void _assertNotNull( const void * x, const char * expression, const char * file, unsigned int line );
00099     void _assertNull( const void * x, const char * expression, const char * file, unsigned int line );
00100 #if 0
00101     void _assertIsNaN( double v, const char * expression, const char * file, unsigned int line );
00102     void _assertIsNotNaN( double v, const char * expression, const char * file, unsigned int line );
00103 #endif
00104     void _assertTrue( bool x, const char * expression, const char * file, unsigned int line );
00105     void _assertFalse( bool x, const char * expression, const char * file, unsigned int line );
00106 
00107     void _assertEqualWithEpsilons( float x1, float x2, int prec, const char * expr1, const char * expr2, const char * exprPrec, const char * file, unsigned int line );
00108     void _assertEqualWithEpsilons( double x1, double x2, int prec, const char * expr1, const char * expr2, const char * exprPrec, const char * file, unsigned int line );
00109     void _assertEqualWithEpsilons( long double x1, long double x2, int prec, const char * expr1, const char * expr2, const char * exprPrec, const char * file, unsigned int line );
00110 
00111     template <typename T, typename S>
00112     void _assertEqual( const T & x1, const S & x2, const char * expr1, const char * expr2, const char * file, unsigned int line ) {
00113       if ( x1 == x2 ) this->success();
00114       else {
00115           this->fail( file, line ) << '"' << expr1 << "\" yielded " << x1 << "; expected: " << x2 << "(\"" << expr2 << "\")" << std::endl;
00116       }
00117     }
00118     template <typename T, typename S>
00119     void _assertNotEqual( const T & x1, const S & x2, const char * expr1, const char * expr2, const char * file, unsigned int line ) {
00120       if ( x1 != x2 ) this->success();
00121       else {
00122           this->fail( file, line ) << '"' << expr1 << "\" yielded " << x1 << "; expected something not equal to: " << x2 << "(\"" << expr2 << "\")" << std::endl;
00123       }
00124     }
00125 
00126   protected:
00127     std::ostream & fail( const char * file, unsigned int line );
00128     void success() {
00129       ++mSucceeded;
00130     }
00131   };
00132 
00133   class TestFactory {
00134   public:
00135     virtual ~TestFactory() {}
00136     virtual Test * create() const = 0;
00137   };
00138 
00139 }
00140 }
00141 
00142 #include "testregistry.h"
00143 
00144 namespace KDAB {
00145 namespace UnitTest {
00146 
00147   template <typename T_Test>
00148   class GenericFactory : public TestFactory {
00149   public:
00150     GenericFactory( const char * group=0 ) {
00151       TestRegistry::instance()->registerTestFactory( this, group );
00152     }
00153     Test * create() const { return new T_Test(); }
00154   };
00155 
00156 }
00157 }
00158 
00159 #include "libutil.h"
00160 
00161 // Use these macros to export your UnitTest class so that it gets executed by the test runner.
00162 // Use the second macro if your class is within a namespace.
00163 // Arguments :
00164 // - Namespace (unquoted) : the namespace in which the test class in contained
00165 // - Class (unquoted) : the test class, without namespace
00166 // - Group (quoted) : the name of the group this unit test belongs to
00167 #define KDAB_EXPORT_UNITTEST( Class, Group ) \
00168     static const KDAB::UnitTest::GenericFactory< Class > __##Class##_unittest( Group ); \
00169     KDAB_EXPORT_STATIC_SYMBOLS( Class )
00170 
00171 #define KDAB_EXPORT_SCOPED_UNITTEST( Namespace, Class, Group ) \
00172     static const KDAB::UnitTest::GenericFactory< Namespace::Class > __##Class##_unittest( Group ); \
00173     KDAB_EXPORT_STATIC_SYMBOLS( Class )
00174 
00175 // Use this macro to import the test explicitely (for windows static libs only)
00176 #define KDAB_IMPORT_UNITTEST( Class ) KDAB_IMPORT_STATIC_SYMBOLS( Class )
00177 
00178 // Convenience macros that create a simple test class for a single test and export it.
00179 // Usage : KDAB_UNITTEST_SIMPLE( MyClass, "mygroup" ) { doSomething(); assertEqual(...); }
00180 #define KDAB_UNITTEST_SIMPLE( Class, Group )                 \
00181     class Class##Test : public KDAB::UnitTest::Test {  \
00182     public:                                                 \
00183         Class##Test() : Test( #Class ) {}                   \
00184         void run();                                         \
00185     };                                                      \
00186     KDAB_EXPORT_UNITTEST( Class##Test, Group )               \
00187     void Class##Test::run()
00188 
00189 #define KDAB_SCOPED_UNITTEST_SIMPLE( Namespace, Class, Group )   \
00190     namespace Namespace {                                       \
00191         class Class##Test : public KDAB::UnitTest::Test {  \
00192         public:                                                 \
00193             Class##Test() : Test( #Namespace "::" #Class ){}    \
00194             void run();                                         \
00195         };                                                      \
00196     }                                                           \
00197     KDAB_EXPORT_SCOPED_UNITTEST( Namespace, Class##Test, Group ) \
00198     void Namespace::Class##Test::run()
00199 
00200 #endif // KDAB_NO_UNIT_TESTS
00201 
00202 #endif // __KDAB__UNITTEST__TEST_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/