test.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2010 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 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_NO_UNIT_TESTS
00024 
00025 #include "test.h"
00026 
00027 #ifdef TEMPORARILY_REMOVED
00028 #include <trinav/ascshared/libfakes/fakes.h>
00029 #endif
00030 
00031 #include <cmath>
00032 #include <limits>
00033 
00034 KDAB::UnitTest::Test::Test( const std::string & n )
00035     : mName( n ), mFailed( 0 ), mSucceeded( 0 ) {}
00036 
00037 void KDAB::UnitTest::Test::_assertNotNull( const void * x, const char * expression, const char * file, unsigned int line ) {
00038     if ( x ) success();
00039     else fail( file, line ) << '"' << expression << "\" is NULL, expected non-NULL" << std::endl;
00040 }
00041 
00042 void KDAB::UnitTest::Test::_assertNull( const void * x, const char * expression, const char * file, unsigned int line ) {
00043     if ( !x ) success();
00044     else fail( file, line ) << '"' << expression << "\" is not NULL, expected NULL" << std::endl;
00045 }
00046 
00047 #if 0
00048 void KDAB::UnitTest::Test::_assertIsNaN( double d, const char * expression, const char * file, unsigned int line ) {
00049     if ( std::isnan( d ) ) success();
00050     else fail( file, line ) << '"' << expression << "\" yielded " << d << "; expected NaN" << std::endl;
00051 }
00052 
00053 void KDAB::UnitTest::Test::_assertIsNotNaN( double d, const char * expression, const char * file, unsigned int line ) {
00054     if ( !std::isnan( d ) ) success();
00055     else fail( file, line ) << '"' << expression << "\" yielded nan; expected something else" << std::endl;
00056 }
00057 #endif
00058 
00059 void KDAB::UnitTest::Test::_assertTrue( bool x, const char * expression, const char * file, unsigned int line ) {
00060     if ( x ) success();
00061     else fail( file, line ) << '"' << expression << "\" != TRUE" << std::endl;
00062 }
00063 
00064 void KDAB::UnitTest::Test::_assertFalse( bool x, const char * expression, const char * file, unsigned int line ) {
00065     if ( !x ) success();
00066     else fail( file, line ) << '"' << expression << "\" != FALSE" << std::endl;
00067 }
00068 
00069 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 ) {
00070     if ( qAbs( x1/x2 - 1.0f ) <= prec * std::numeric_limits<float>::epsilon() ) success();
00071     else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected "
00072                             << x2 << " (" << expr2 << ") by more than "
00073                             << prec << " (" << exprP << ") epsilons." << std::endl;
00074 }
00075 
00076 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 ) {
00077     if ( qAbs( x1/x2 - 1.0 ) <= prec * std::numeric_limits<double>::epsilon() ) success();
00078     else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected "
00079                             << x2 << " (" << expr2 << ") by more than "
00080                             << prec << " (" << exprP << ") epsilons." << std::endl;
00081 }
00082 
00083 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 ) {
00084     if ( qAbs( x1/x2 - 1.0l ) <= prec * std::numeric_limits<long double>::epsilon() ) success();
00085     else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected "
00086                             << x2 << " (" << expr2 << ") by more than "
00087                             << prec << " (" << exprP << ") epsilons." << std::endl;
00088 }
00089 
00090 std::ostream & KDAB::UnitTest::Test::fail( const char * file, unsigned int line ) {
00091     ++mFailed;
00092     return std::cerr << "FAIL: " << file << ':' << line << ": ";
00093 }
00094 
00095 #endif // KDAB_NO_UNIT_TESTS