KD Chart 2
[rev.2.5]
|
00001 /**************************************************************************** 00002 ** Copyright (C) 2001-2012 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 <QtGlobal> 00032 00033 #include <string> 00034 #include <iostream> 00035 00036 namespace KDAB { 00037 namespace UnitTest { 00038 00039 #define assertNotNull( x ) _assertNotNull( ( x ), #x, __FILE__, __LINE__ ) 00040 #define assertNull( x ) _assertNull( ( x ), #x, __FILE__, __LINE__ ) 00041 #define assertTrue( x ) _assertTrue( (x), #x, __FILE__, __LINE__ ) 00042 #define assertFalse( x ) _assertFalse( (x), #x, __FILE__, __LINE__ ) 00043 #define assertEqual( x, y ) _assertEqual( (x), (y), #x, #y, __FILE__, __LINE__ ) 00044 #define assertNotEqual( x, y ) _assertNotEqual( (x), (y), #x, #y, __FILE__, __LINE__ ) 00045 // to be phased out: 00046 #define assertNearEqual( x, y, z ) 00047 #define assertEqualWithEpsilons( x, y, z ) _assertEqualWithEpsilons( (x), (y), (z), #x, #y, #z, __FILE__, __LINE__ ) 00048 #if 0 00049 #define assertIsNaN( x ) _assertIsNaN( (x), #x, __FILE__, __LINE__ ) 00050 #define assertIsNotNaN( x ) _assertIsNotNaN( (x), #x, __FILE__, __LINE__ ) 00051 #endif 00052 00053 #define assertThrowsExceptionWithCode( x, E, code ) \ 00054 do { \ 00055 try { \ 00056 x; \ 00057 fail( __FILE__, __LINE__ ) \ 00058 << "\"" #x "\" didn't throw \"" #E "\"" << std::endl; \ 00059 } catch ( E & ppq_ut_thrown ) { \ 00060 success(); \ 00061 ( void )ppq_ut_thrown; \ 00062 code; \ 00063 } catch ( ... ) { \ 00064 fail( __FILE__, __LINE__ ) \ 00065 << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \ 00066 } \ 00067 } while ( false ) 00068 00069 #define assertThrowsException( x, E ) assertThrowsExceptionWithCode( x, E, do{}while(0) ) 00070 00071 #define assertDoesNotThrowException( x, E ) \ 00072 do { \ 00073 try { \ 00074 x; \ 00075 success(); \ 00076 } catch ( E & ) { \ 00077 fail( __FILE__, __LINE__ ) \ 00078 << "\"" #x "\" threw \"" #E "\", but shouldn't" << std::endl; \ 00079 } catch ( ... ) { \ 00080 fail( __FILE__, __LINE__ ) \ 00081 << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \ 00082 } \ 00083 } while ( false ) 00084 00085 00086 class Test { 00087 const std::string mName; 00088 unsigned int mFailed, mSucceeded; 00089 public: 00090 Test( const std::string & name ); 00091 virtual ~Test() {} 00092 00093 const std::string & name() const { return mName; } 00094 unsigned int failed() const { return mFailed; } 00095 unsigned int succeeded() const { return mSucceeded; } 00096 00097 virtual void run() = 0; 00098 00099 protected: 00100 void _assertNotNull( const void * x, const char * expression, const char * file, unsigned int line ); 00101 void _assertNull( const void * x, const char * expression, const char * file, unsigned int line ); 00102 #if 0 00103 void _assertIsNaN( qreal v, const char * expression, const char * file, unsigned int line ); 00104 void _assertIsNotNaN( qreal v, const char * expression, const char * file, unsigned int line ); 00105 #endif 00106 void _assertTrue( bool x, const char * expression, const char * file, unsigned int line ); 00107 void _assertFalse( bool x, const char * expression, const char * file, unsigned int line ); 00108 00109 void _assertEqualWithEpsilons( float x1, float x2, int prec, const char * expr1, const char * expr2, const char * exprPrec, const char * file, unsigned int line ); 00110 void _assertEqualWithEpsilons( qreal x1, qreal x2, int prec, const char * expr1, const char * expr2, const char * exprPrec, const char * file, unsigned int line ); 00111 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 ); 00112 00113 template <typename T, typename S> 00114 void _assertEqual( const T & x1, const S & x2, const char * expr1, const char * expr2, const char * file, unsigned int line ) { 00115 if ( x1 == x2 ) this->success(); 00116 else { 00117 this->fail( file, line ) << '"' << expr1 << "\" yielded " << x1 << "; expected: " << x2 << "(\"" << expr2 << "\")" << std::endl; 00118 } 00119 } 00120 template <typename T, typename S> 00121 void _assertNotEqual( const T & x1, const S & x2, const char * expr1, const char * expr2, const char * file, unsigned int line ) { 00122 if ( x1 != x2 ) this->success(); 00123 else { 00124 this->fail( file, line ) << '"' << expr1 << "\" yielded " << x1 << "; expected something not equal to: " << x2 << "(\"" << expr2 << "\")" << std::endl; 00125 } 00126 } 00127 00128 protected: 00129 std::ostream & fail( const char * file, unsigned int line ); 00130 void success() { 00131 ++mSucceeded; 00132 } 00133 }; 00134 00135 class TestFactory { 00136 public: 00137 virtual ~TestFactory() {} 00138 virtual Test * create() const = 0; 00139 }; 00140 00141 } 00142 } 00143 00144 #include "testregistry.h" 00145 00146 namespace KDAB { 00147 namespace UnitTest { 00148 00149 template <typename T_Test> 00150 class GenericFactory : public TestFactory { 00151 public: 00152 GenericFactory( const char * group=0 ) { 00153 TestRegistry::instance()->registerTestFactory( this, group ); 00154 } 00155 Test * create() const { return new T_Test(); } 00156 }; 00157 00158 } 00159 } 00160 00161 #include "libutil.h" 00162 00163 // Use these macros to export your UnitTest class so that it gets executed by the test runner. 00164 // Use the second macro if your class is within a namespace. 00165 // Arguments : 00166 // - Namespace (unquoted) : the namespace in which the test class in contained 00167 // - Class (unquoted) : the test class, without namespace 00168 // - Group (quoted) : the name of the group this unit test belongs to 00169 #define KDAB_EXPORT_UNITTEST( Class, Group ) \ 00170 static const KDAB::UnitTest::GenericFactory< Class > __##Class##_unittest( Group ); \ 00171 KDAB_EXPORT_STATIC_SYMBOLS( Class ) 00172 00173 #define KDAB_EXPORT_SCOPED_UNITTEST( Namespace, Class, Group ) \ 00174 static const KDAB::UnitTest::GenericFactory< Namespace::Class > __##Class##_unittest( Group ); \ 00175 KDAB_EXPORT_STATIC_SYMBOLS( Class ) 00176 00177 // Use this macro to import the test explicitly (for windows static libs only) 00178 #define KDAB_IMPORT_UNITTEST( Class ) KDAB_IMPORT_STATIC_SYMBOLS( Class ) 00179 00180 // Convenience macros that create a simple test class for a single test and export it. 00181 // Usage : KDAB_UNITTEST_SIMPLE( MyClass, "mygroup" ) { doSomething(); assertEqual(...); } 00182 #define KDAB_UNITTEST_SIMPLE( Class, Group ) \ 00183 class Class##Test : public KDAB::UnitTest::Test { \ 00184 public: \ 00185 Class##Test() : Test( #Class ) {} \ 00186 void run(); \ 00187 }; \ 00188 KDAB_EXPORT_UNITTEST( Class##Test, Group ) \ 00189 void Class##Test::run() 00190 00191 #define KDAB_SCOPED_UNITTEST_SIMPLE( Namespace, Class, Group ) \ 00192 namespace Namespace { \ 00193 class Class##Test : public KDAB::UnitTest::Test { \ 00194 public: \ 00195 Class##Test() : Test( #Namespace "::" #Class ){} \ 00196 void run(); \ 00197 }; \ 00198 } \ 00199 KDAB_EXPORT_SCOPED_UNITTEST( Namespace, Class##Test, Group ) \ 00200 void Namespace::Class##Test::run() 00201 00202 #endif // KDAB_NO_UNIT_TESTS 00203 00204 #endif // __KDAB__UNITTEST__TEST_H__