KD Chart 2  [rev.2.7]
test.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #ifndef KDAB_NO_UNIT_TESTS
24 
25 #include "test.h"
26 
27 #ifdef TEMPORARILY_REMOVED
28 #include <trinav/ascshared/libfakes/fakes.h>
29 #endif
30 
31 #include <cmath>
32 #include <limits>
33 
34 KDAB::UnitTest::Test::Test( const std::string & n )
35  : mName( n ), mFailed( 0 ), mSucceeded( 0 ) {}
36 
37 void KDAB::UnitTest::Test::_assertNotNull( const void * x, const char * expression, const char * file, unsigned int line ) {
38  if ( x ) success();
39  else fail( file, line ) << '"' << expression << "\" is NULL, expected non-NULL" << std::endl;
40 }
41 
42 void KDAB::UnitTest::Test::_assertNull( const void * x, const char * expression, const char * file, unsigned int line ) {
43  if ( !x ) success();
44  else fail( file, line ) << '"' << expression << "\" is not NULL, expected NULL" << std::endl;
45 }
46 
47 #if 0
48 void KDAB::UnitTest::Test::_assertIsNaN( qreal d, const char * expression, const char * file, unsigned int line ) {
49  if ( std::isnan( d ) ) success();
50  else fail( file, line ) << '"' << expression << "\" yielded " << d << "; expected NaN" << std::endl;
51 }
52 
53 void KDAB::UnitTest::Test::_assertIsNotNaN( qreal d, const char * expression, const char * file, unsigned int line ) {
54  if ( !std::isnan( d ) ) success();
55  else fail( file, line ) << '"' << expression << "\" yielded nan; expected something else" << std::endl;
56 }
57 #endif
58 
59 void KDAB::UnitTest::Test::_assertTrue( bool x, const char * expression, const char * file, unsigned int line ) {
60  if ( x ) success();
61  else fail( file, line ) << '"' << expression << "\" != TRUE" << std::endl;
62 }
63 
64 void KDAB::UnitTest::Test::_assertFalse( bool x, const char * expression, const char * file, unsigned int line ) {
65  if ( !x ) success();
66  else fail( file, line ) << '"' << expression << "\" != FALSE" << std::endl;
67 }
68 
69 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 ) {
70  if ( qAbs( x1/x2 - 1.0f ) <= prec * std::numeric_limits<float>::epsilon() ) success();
71  else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected "
72  << x2 << " (" << expr2 << ") by more than "
73  << prec << " (" << exprP << ") epsilons." << std::endl;
74 }
75 
76 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 ) {
77  if ( qAbs( x1/x2 - 1.0 ) <= prec * std::numeric_limits<double>::epsilon() ) success();
78  else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected "
79  << x2 << " (" << expr2 << ") by more than "
80  << prec << " (" << exprP << ") epsilons." << std::endl;
81 }
82 
83 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 ) {
84  if ( qAbs( x1/x2 - 1.0l ) <= prec * std::numeric_limits<long double>::epsilon() ) success();
85  else fail( file, line ) << x1 << " (" << expr1 << ") deviates from expected "
86  << x2 << " (" << expr2 << ") by more than "
87  << prec << " (" << exprP << ") epsilons." << std::endl;
88 }
89 
90 std::ostream & KDAB::UnitTest::Test::fail( const char * file, unsigned int line ) {
91  ++mFailed;
92  return std::cerr << "FAIL: " << file << ':' << line << ": ";
93 }
94 
95 #endif // KDAB_NO_UNIT_TESTS
void _assertNull(const void *x, const char *expression, const char *file, unsigned int line)
Definition: test.cpp:42
void _assertNotNull(const void *x, const char *expression, const char *file, unsigned int line)
Definition: test.cpp:37
Test(const std::string &name)
Definition: test.cpp:34
void _assertFalse(bool x, const char *expression, const char *file, unsigned int line)
Definition: test.cpp:64
std::ostream & fail(const char *file, unsigned int line)
Definition: test.cpp:90
void _assertTrue(bool x, const char *expression, const char *file, unsigned int line)
Definition: test.cpp:59
void _assertEqualWithEpsilons(float x1, float x2, int prec, const char *expr1, const char *expr2, const char *exprPrec, const char *file, unsigned int line)
Definition: test.cpp:69
void success()
Definition: test.h:130

Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/

https://www.kdab.com/development-resources/qt-tools/kd-chart/