KD Chart 2  [rev.2.6]
test.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2019 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__UNITTEST__TEST_H__
24 #define __KDAB__UNITTEST__TEST_H__
25 
26 #ifndef KDAB_NO_UNIT_TESTS
27 
28 #include "kdchart_export.h"
29 #include "kdganttglobal.h"
30 
31 #include <QtGlobal>
32 
33 #include <string>
34 #include <iostream>
35 
36 namespace KDAB {
37 namespace UnitTest {
38 
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__ )
45 // to be phased out:
46 #define assertNearEqual( x, y, z )
47 #define assertEqualWithEpsilons( x, y, z ) _assertEqualWithEpsilons( (x), (y), (z), #x, #y, #z, __FILE__, __LINE__ )
48 #if 0
49 #define assertIsNaN( x ) _assertIsNaN( (x), #x, __FILE__, __LINE__ )
50 #define assertIsNotNaN( x ) _assertIsNotNaN( (x), #x, __FILE__, __LINE__ )
51 #endif
52 
53 #define assertThrowsExceptionWithCode( x, E, code ) \
54 do { \
55  try { \
56  x; \
57  fail( __FILE__, __LINE__ ) \
58  << "\"" #x "\" didn't throw \"" #E "\"" << std::endl; \
59  } catch ( E & ppq_ut_thrown ) { \
60  success(); \
61  ( void )ppq_ut_thrown; \
62  code; \
63  } catch ( ... ) { \
64  fail( __FILE__, __LINE__ ) \
65  << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \
66  } \
67 } while ( false )
68 
69 #define assertThrowsException( x, E ) assertThrowsExceptionWithCode( x, E, do{}while (0) )
70 
71 #define assertDoesNotThrowException( x, E ) \
72 do { \
73  try { \
74  x; \
75  success(); \
76  } catch ( E & ) { \
77  fail( __FILE__, __LINE__ ) \
78  << "\"" #x "\" threw \"" #E "\", but shouldn't" << std::endl; \
79  } catch ( ... ) { \
80  fail( __FILE__, __LINE__ ) \
81  << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \
82  } \
83 } while ( false )
84 
85 
86  class Test {
87  const std::string mName;
88  unsigned int mFailed, mSucceeded;
89  public:
90  Test( const std::string & name );
91  virtual ~Test() {}
92 
93  const std::string & name() const { return mName; }
94  unsigned int failed() const { return mFailed; }
95  unsigned int succeeded() const { return mSucceeded; }
96 
97  virtual void run() = 0;
98 
99  protected:
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 );
102 #if 0
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 );
105 #endif
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 );
108 
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 );
112 
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();
116  else {
117  this->fail( file, line ) << '"' << expr1 << "\" yielded " << x1 << "; expected: " << x2 << "(\"" << expr2 << "\")" << std::endl;
118  }
119  }
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();
123  else {
124  this->fail( file, line ) << '"' << expr1 << "\" yielded " << x1 << "; expected something not equal to: " << x2 << "(\"" << expr2 << "\")" << std::endl;
125  }
126  }
127 
128  protected:
129  std::ostream & fail( const char * file, unsigned int line );
130  void success() {
131  ++mSucceeded;
132  }
133  };
134 
135  class TestFactory {
136  public:
137  virtual ~TestFactory() {}
138  virtual Test * create() const = 0;
139  };
140 
141 }
142 }
143 
144 #include "testregistry.h"
145 
146 namespace KDAB {
147 namespace UnitTest {
148 
149  template <typename T_Test>
150  class GenericFactory : public TestFactory {
151  public:
152  GenericFactory( const char * group=0 ) {
154  }
155  Test * create() const { return new T_Test(); }
156  };
157 
158 }
159 }
160 
161 #include "libutil.h"
162 
163 // Use these macros to export your UnitTest class so that it gets executed by the test runner.
164 // Use the second macro if your class is within a namespace.
165 // Arguments :
166 // - Namespace (unquoted) : the namespace in which the test class in contained
167 // - Class (unquoted) : the test class, without namespace
168 // - Group (quoted) : the name of the group this unit test belongs to
169 #define KDAB_EXPORT_UNITTEST( Class, Group ) \
170  static const KDAB::UnitTest::GenericFactory< Class > __##Class##_unittest( Group ); \
171  KDAB_EXPORT_STATIC_SYMBOLS( Class )
172 
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 )
176 
177 // Use this macro to import the test explicitly (for windows static libs only)
178 #define KDAB_IMPORT_UNITTEST( Class ) KDAB_IMPORT_STATIC_SYMBOLS( Class )
179 
180 // Convenience macros that create a simple test class for a single test and export it.
181 // Usage : KDAB_UNITTEST_SIMPLE( MyClass, "mygroup" ) { doSomething(); assertEqual(...); }
182 #define KDAB_UNITTEST_SIMPLE( Class, Group ) \
183  class Class##Test : public KDAB::UnitTest::Test { \
184  public: \
185  Class##Test() : Test( #Class ) {} \
186  void run(); \
187  }; \
188  KDAB_EXPORT_UNITTEST( Class##Test, Group ) \
189  void Class##Test::run()
190 
191 #define KDAB_SCOPED_UNITTEST_SIMPLE( Namespace, Class, Group ) \
192  namespace Namespace { \
193  class Class##Test : public KDAB::UnitTest::Test { \
194  public: \
195  Class##Test() : Test( #Namespace "::" #Class ) {} \
196  void run(); \
197  }; \
198  } \
199  KDAB_EXPORT_SCOPED_UNITTEST( Namespace, Class##Test, Group ) \
200  void Namespace::Class##Test::run()
201 
202 #endif // KDAB_NO_UNIT_TESTS
203 
204 #endif // __KDAB__UNITTEST__TEST_H__
Definition: test.h:36
void _assertNotEqual(const T &x1, const S &x2, const char *expr1, const char *expr2, const char *file, unsigned int line)
Definition: test.h:121
virtual void run()=0
void _assertNull(const void *x, const char *expression, const char *file, unsigned int line)
Definition: test.cpp:42
static TestRegistry * instance()
unsigned int failed() const
Definition: test.h:94
void _assertNotNull(const void *x, const char *expression, const char *file, unsigned int line)
Definition: test.cpp:37
virtual ~Test()
Definition: test.h:91
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
GenericFactory(const char *group=0)
Definition: test.h:152
std::ostream & fail(const char *file, unsigned int line)
Definition: test.cpp:90
const std::string & name() const
Definition: test.h:93
void _assertTrue(bool x, const char *expression, const char *file, unsigned int line)
Definition: test.cpp:59
void registerTestFactory(const TestFactory *tf, const char *group)
virtual ~TestFactory()
Definition: test.h:137
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
void _assertEqual(const T &x1, const S &x2, const char *expr1, const char *expr2, const char *file, unsigned int line)
Definition: test.h:114
unsigned int succeeded() const
Definition: test.h:95
Test * create() const
Definition: test.h:155

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
https://www.kdab.com/
https://www.kdab.com/products/kd-chart/