File indexing completed on 2024-12-15 04:02:45

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KGantt library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef KDAB_NO_UNIT_TESTS
0010 
0011 #include "test.h"
0012 
0013 #ifdef TEMPORARILY_REMOVED
0014 #include <trinav/ascshared/libfakes/fakes.h>
0015 #endif
0016 
0017 #include <cmath>
0018 #include <limits>
0019 
0020 KDAB::UnitTest::Test::Test( const std::string & n )
0021     : mName( n ), mFailed( 0 ), mSucceeded( 0 ) {}
0022 
0023 void KDAB::UnitTest::Test::_assertNotNull( const void * x, const char * expression, const char * file, unsigned int line ) {
0024     if ( x ) success();
0025     else fail( file, line ) << '"' << expression << "\" is NULL, expected non-NULL" << std::endl;
0026 }
0027 
0028 void KDAB::UnitTest::Test::_assertNull( const void * x, const char * expression, const char * file, unsigned int line ) {
0029     if ( !x ) success();
0030     else fail( file, line ) << '"' << expression << "\" is not NULL, expected NULL" << std::endl;
0031 }
0032 
0033 #if 0
0034 void KDAB::UnitTest::Test::_assertIsNaN( qreal d, const char * expression, const char * file, unsigned int line ) {
0035     if ( std::isnan( d ) ) success();
0036     else fail( file, line ) << '"' << expression << "\" yielded " << d << "; expected NaN" << std::endl;
0037 }
0038 
0039 void KDAB::UnitTest::Test::_assertIsNotNaN( qreal d, const char * expression, const char * file, unsigned int line ) {
0040     if ( !std::isnan( d ) ) success();
0041     else fail( file, line ) << '"' << expression << "\" yielded nan; expected something else" << std::endl;
0042 }
0043 #endif
0044 
0045 void KDAB::UnitTest::Test::_assertTrue( bool x, const char * expression, const char * file, unsigned int line ) {
0046     if ( x ) success();
0047     else fail( file, line ) << '"' << expression << "\" != TRUE" << std::endl;
0048 }
0049 
0050 void KDAB::UnitTest::Test::_assertFalse( bool x, const char * expression, const char * file, unsigned int line ) {
0051     if ( !x ) success();
0052     else fail( file, line ) << '"' << expression << "\" != FALSE" << std::endl;
0053 }
0054 
0055 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 ) {
0056     if ( qAbs( x1/x2 - 1.0f ) <= prec * std::numeric_limits<float>::epsilon() ) success();
0057     else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected "
0058                             << x2 << " (" << expr2 << ") by more than "
0059                             << prec << " (" << exprP << ") epsilons." << std::endl;
0060 }
0061 
0062 void KDAB::UnitTest::Test::_assertEqualWithEpsilons( qreal x1, qreal x2, int prec, const char * expr1, const char * expr2, const char * exprP, const char * file, unsigned int line ) {
0063     if ( qAbs( x1/x2 - 1.0 ) <= prec * std::numeric_limits<qreal>::epsilon() ) success();
0064     else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected "
0065                             << x2 << " (" << expr2 << ") by more than "
0066                             << prec << " (" << exprP << ") epsilons." << std::endl;
0067 }
0068 
0069 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 ) {
0070     if ( qAbs( x1/x2 - 1.0l ) <= prec * std::numeric_limits<long double>::epsilon() ) success();
0071     else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected "
0072                             << x2 << " (" << expr2 << ") by more than "
0073                             << prec << " (" << exprP << ") epsilons." << std::endl;
0074 }
0075 
0076 std::ostream & KDAB::UnitTest::Test::fail( const char * file, unsigned int line ) {
0077     ++mFailed;
0078     return std::cerr << "FAIL: " << file << ':' << line << ": ";
0079 }
0080 
0081 #endif // KDAB_NO_UNIT_TESTS