00001 #ifndef KDAB_NO_UNIT_TESTS 00002 00003 #include "test.h" 00004 00005 #ifdef TEMPORARILY_REMOVED 00006 #include <trinav/ascshared/libfakes/fakes.h> 00007 #endif 00008 00009 #include <cmath> 00010 #include <limits> 00011 00012 KDAB::UnitTest::Test::Test( const std::string & n ) 00013 : mName( n ), mFailed( 0 ), mSucceeded( 0 ) {} 00014 00015 void KDAB::UnitTest::Test::_assertNotNull( const void * x, const char * expression, const char * file, unsigned int line ) { 00016 if ( x ) success(); 00017 else fail( file, line ) << '"' << expression << "\" is NULL, expected non-NULL" << std::endl; 00018 } 00019 00020 void KDAB::UnitTest::Test::_assertNull( const void * x, const char * expression, const char * file, unsigned int line ) { 00021 if ( !x ) success(); 00022 else fail( file, line ) << '"' << expression << "\" is not NULL, expected NULL" << std::endl; 00023 } 00024 00025 #if 0 00026 void KDAB::UnitTest::Test::_assertIsNaN( double d, const char * expression, const char * file, unsigned int line ) { 00027 if ( std::isnan( d ) ) success(); 00028 else fail( file, line ) << '"' << expression << "\" yielded " << d << "; expected NaN" << std::endl; 00029 } 00030 00031 void KDAB::UnitTest::Test::_assertIsNotNaN( double d, const char * expression, const char * file, unsigned int line ) { 00032 if ( !std::isnan( d ) ) success(); 00033 else fail( file, line ) << '"' << expression << "\" yielded nan; expected something else" << std::endl; 00034 } 00035 #endif 00036 00037 void KDAB::UnitTest::Test::_assertTrue( bool x, const char * expression, const char * file, unsigned int line ) { 00038 if ( x ) success(); 00039 else fail( file, line ) << '"' << expression << "\" != TRUE" << std::endl; 00040 } 00041 00042 void KDAB::UnitTest::Test::_assertFalse( bool x, const char * expression, const char * file, unsigned int line ) { 00043 if ( !x ) success(); 00044 else fail( file, line ) << '"' << expression << "\" != FALSE" << std::endl; 00045 } 00046 00047 void KDAB::UnitTest::Test::_assertEqualWithEpsilons( float x1, float x2, int prec, const char * expr1, const char * expr2, const char * exprP, const char * file, unsigned int line ) { 00048 if ( qAbs( x1/x2 - 1.0f ) <= prec * std::numeric_limits<float>::epsilon() ) success(); 00049 else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected " 00050 << x2 << " (" << expr2 << ") by more than " 00051 << prec << " (" << exprP << ") epsilons." << std::endl; 00052 } 00053 00054 void KDAB::UnitTest::Test::_assertEqualWithEpsilons( double x1, double x2, int prec, const char * expr1, const char * expr2, const char * exprP, const char * file, unsigned int line ) { 00055 if ( qAbs( x1/x2 - 1.0 ) <= prec * std::numeric_limits<double>::epsilon() ) success(); 00056 else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected " 00057 << x2 << " (" << expr2 << ") by more than " 00058 << prec << " (" << exprP << ") epsilons." << std::endl; 00059 } 00060 00061 void KDAB::UnitTest::Test::_assertEqualWithEpsilons( long double x1, long double x2, int prec, const char * expr1, const char * expr2, const char * exprP, const char * file, unsigned int line ) { 00062 if ( qAbs( x1/x2 - 1.0l ) <= prec * std::numeric_limits<long double>::epsilon() ) success(); 00063 else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected " 00064 << x2 << " (" << expr2 << ") by more than " 00065 << prec << " (" << exprP << ") epsilons." << std::endl; 00066 } 00067 00068 std::ostream & KDAB::UnitTest::Test::fail( const char * file, unsigned int line ) { 00069 ++mFailed; 00070 return std::cerr << "FAIL: " << file << ':' << line << ": "; 00071 } 00072 00073 #endif // KDAB_NO_UNIT_TESTS