23 #ifndef __KDAB__UNITTEST__TEST_H__ 24 #define __KDAB__UNITTEST__TEST_H__ 26 #ifndef KDAB_NO_UNIT_TESTS 28 #include "kdchart_export.h" 39 #define assertNotNull( x ) _assertNotNull( ( x ), #x, __FILE__, __LINE__ ) 40 #define assertNull( x ) _assertNull( ( x ), #x, __FILE__, __LINE__ ) 41 #define assertTrue( x ) _assertTrue( (x), #x, __FILE__, __LINE__ ) 42 #define assertFalse( x ) _assertFalse( (x), #x, __FILE__, __LINE__ ) 43 #define assertEqual( x, y ) _assertEqual( (x), (y), #x, #y, __FILE__, __LINE__ ) 44 #define assertNotEqual( x, y ) _assertNotEqual( (x), (y), #x, #y, __FILE__, __LINE__ ) 46 #define assertNearEqual( x, y, z ) 47 #define assertEqualWithEpsilons( x, y, z ) _assertEqualWithEpsilons( (x), (y), (z), #x, #y, #z, __FILE__, __LINE__ ) 49 #define assertIsNaN( x ) _assertIsNaN( (x), #x, __FILE__, __LINE__ ) 50 #define assertIsNotNaN( x ) _assertIsNotNaN( (x), #x, __FILE__, __LINE__ ) 53 #define assertThrowsExceptionWithCode( x, E, code ) \ 57 fail( __FILE__, __LINE__ ) \ 58 << "\"" #x "\" didn't throw \"" #E "\"" << std::endl; \ 59 } catch ( E & ppq_ut_thrown ) { \ 61 ( void )ppq_ut_thrown; \ 64 fail( __FILE__, __LINE__ ) \ 65 << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \ 69 #define assertThrowsException( x, E ) assertThrowsExceptionWithCode( x, E, do{}while (0) ) 71 #define assertDoesNotThrowException( x, E ) \ 77 fail( __FILE__, __LINE__ ) \ 78 << "\"" #x "\" threw \"" #E "\", but shouldn't" << std::endl; \ 80 fail( __FILE__, __LINE__ ) \ 81 << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \ 87 const std::string mName;
88 unsigned int mFailed, mSucceeded;
93 const std::string &
name()
const {
return mName; }
94 unsigned int failed()
const {
return mFailed; }
95 unsigned int succeeded()
const {
return mSucceeded; }
97 virtual void run() = 0;
100 void _assertNotNull(
const void * x,
const char * expression,
const char * file,
unsigned int line );
101 void _assertNull(
const void * x,
const char * expression,
const char * file,
unsigned int line );
103 void _assertIsNaN( qreal v,
const char * expression,
const char * file,
unsigned int line );
104 void _assertIsNotNaN( qreal v,
const char * expression,
const char * file,
unsigned int line );
106 void _assertTrue(
bool x,
const char * expression,
const char * file,
unsigned int line );
107 void _assertFalse(
bool x,
const char * expression,
const char * file,
unsigned int line );
109 void _assertEqualWithEpsilons(
float x1,
float x2,
int prec,
const char * expr1,
const char * expr2,
const char * exprPrec,
const char * file,
unsigned int line );
110 void _assertEqualWithEpsilons(
double x1,
double x2,
int prec,
const char * expr1,
const char * expr2,
const char * exprPrec,
const char * file,
unsigned int line );
111 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 );
113 template <
typename T,
typename S>
114 void _assertEqual(
const T & x1,
const S & x2,
const char * expr1,
const char * expr2,
const char * file,
unsigned int line ) {
115 if ( x1 == x2 ) this->
success();
117 this->
fail( file, line ) <<
'"' << expr1 <<
"\" yielded " << x1 <<
"; expected: " << x2 <<
"(\"" << expr2 <<
"\")" << std::endl;
120 template <
typename T,
typename S>
121 void _assertNotEqual(
const T & x1,
const S & x2,
const char * expr1,
const char * expr2,
const char * file,
unsigned int line ) {
122 if ( x1 != x2 ) this->
success();
124 this->
fail( file, line ) <<
'"' << expr1 <<
"\" yielded " << x1 <<
"; expected something not equal to: " << x2 <<
"(\"" << expr2 <<
"\")" << std::endl;
129 std::ostream &
fail(
const char * file,
unsigned int line );
138 virtual Test * create()
const = 0;
149 template <
typename T_Test>
169 #define KDAB_EXPORT_UNITTEST( Class, Group ) \ 170 static const KDAB::UnitTest::GenericFactory< Class > __##Class##_unittest( Group ); \ 171 KDAB_EXPORT_STATIC_SYMBOLS( Class ) 173 #define KDAB_EXPORT_SCOPED_UNITTEST( Namespace, Class, Group ) \ 174 static const KDAB::UnitTest::GenericFactory< Namespace::Class > __##Class##_unittest( Group ); \ 175 KDAB_EXPORT_STATIC_SYMBOLS( Class ) 178 #define KDAB_IMPORT_UNITTEST( Class ) KDAB_IMPORT_STATIC_SYMBOLS( Class ) 182 #define KDAB_UNITTEST_SIMPLE( Class, Group ) \ 183 class Class##Test : public KDAB::UnitTest::Test { \ 185 Class##Test() : Test( #Class ) {} \ 188 KDAB_EXPORT_UNITTEST( Class##Test, Group ) \ 189 void Class##Test::run() 191 #define KDAB_SCOPED_UNITTEST_SIMPLE( Namespace, Class, Group ) \ 192 namespace Namespace { \ 193 class Class##Test : public KDAB::UnitTest::Test { \ 195 Class##Test() : Test( #Namespace "::" #Class ) {} \ 199 KDAB_EXPORT_SCOPED_UNITTEST( Namespace, Class##Test, Group ) \ 200 void Namespace::Class##Test::run() 202 #endif // KDAB_NO_UNIT_TESTS 204 #endif // __KDAB__UNITTEST__TEST_H__
void _assertNotEqual(const T &x1, const S &x2, const char *expr1, const char *expr2, const char *file, unsigned int line)
void _assertNull(const void *x, const char *expression, const char *file, unsigned int line)
static TestRegistry * instance()
unsigned int failed() const
void _assertNotNull(const void *x, const char *expression, const char *file, unsigned int line)
Test(const std::string &name)
void _assertFalse(bool x, const char *expression, const char *file, unsigned int line)
GenericFactory(const char *group=0)
std::ostream & fail(const char *file, unsigned int line)
const std::string & name() const
void _assertTrue(bool x, const char *expression, const char *file, unsigned int line)
void registerTestFactory(const TestFactory *tf, const char *group)
void _assertEqualWithEpsilons(float x1, float x2, int prec, const char *expr1, const char *expr2, const char *exprPrec, const char *file, unsigned int line)
void _assertEqual(const T &x1, const S &x2, const char *expr1, const char *expr2, const char *file, unsigned int line)
unsigned int succeeded() const
Test * create() const override