CppUnit project page FAQ

Orthodox.h
Go to the documentation of this file.
1#ifndef CPPUNIT_EXTENSIONS_ORTHODOX_H
2#define CPPUNIT_EXTENSIONS_ORTHODOX_H
3
4#include <cppunit/TestCase.h>
5
7
8
9/*
10 * Orthodox performs a simple set of tests on an arbitary
11 * class to make sure that it supports at least the
12 * following operations:
13 *
14 * default construction - constructor
15 * equality/inequality - operator== && operator!=
16 * assignment - operator=
17 * negation - operator!
18 * safe passage - copy construction
19 *
20 * If operations for each of these are not declared
21 * the template will not instantiate. If it does
22 * instantiate, tests are performed to make sure
23 * that the operations have correct semantics.
24 *
25 * Adding an orthodox test to a suite is very
26 * easy:
27 *
28 * public: Test *suite () {
29 * TestSuite *suiteOfTests = new TestSuite;
30 * suiteOfTests->addTest (new ComplexNumberTest ("testAdd");
31 * suiteOfTests->addTest (new TestCaller<Orthodox<Complex> > ());
32 * return suiteOfTests;
33 * }
34 *
35 * Templated test cases be very useful when you are want to
36 * make sure that a group of classes have the same form.
37 *
38 * see TestSuite
39 */
40
41
42template <class ClassUnderTest> class Orthodox : public TestCase
43{
44public:
45 Orthodox () : TestCase ("Orthodox") {}
46
47protected:
48 ClassUnderTest call (ClassUnderTest object);
49 void runTest ();
50
51
52};
53
54
55// Run an orthodoxy test
56template <class ClassUnderTest> void Orthodox<ClassUnderTest>::runTest ()
57{
58 // make sure we have a default constructor
59 ClassUnderTest a, b, c;
60
61 // make sure we have an equality operator
62 CPPUNIT_ASSERT (a == b);
63
64 // check the inverse
65 b.operator= (a.operator! ());
66 CPPUNIT_ASSERT (a != b);
67
68 // double inversion
69 b = !!a;
70 CPPUNIT_ASSERT (a == b);
71
72 // invert again
73 b = !a;
74
75 // check calls
76 c = a;
77 CPPUNIT_ASSERT (c == call (a));
78
79 c = b;
80 CPPUNIT_ASSERT (c == call (b));
81
82}
83
84
85// Exercise a call
86template <class ClassUnderTest>
87ClassUnderTest Orthodox<ClassUnderTest>::call (ClassUnderTest object)
88{
89 return object;
90}
91
92
94
95#endif
#define CPPUNIT_NS_END
Definition: Portability.h:106
#define CPPUNIT_NS_BEGIN
Definition: Portability.h:105
Definition: Orthodox.h:43
ClassUnderTest call(ClassUnderTest object)
Definition: Orthodox.h:87
Orthodox()
Definition: Orthodox.h:45
void runTest()
FIXME: this should probably be pure virtual.
Definition: Orthodox.h:56
A single test object.
Definition: TestCase.h:29
#define CPPUNIT_ASSERT(condition)
Assertions that a condition is true.
Definition: TestAssert.h:273

Send comments to:
CppUnit Developers