CppUnit project page FAQ CppUnit home page

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages

Orthodox.h

Go to the documentation of this file.
00001 #ifndef CPPUNIT_EXTENSIONS_ORTHODOX_H
00002 #define CPPUNIT_EXTENSIONS_ORTHODOX_H
00003 
00004 #include <cppunit/TestCase.h>
00005 
00006 namespace CppUnit {
00007 
00008 /*
00009  * Orthodox performs a simple set of tests on an arbitary
00010  * class to make sure that it supports at least the
00011  * following operations:
00012  *
00013  *      default construction    - constructor
00014  *      equality/inequality     - operator== && operator!=
00015  *      assignment              - operator=
00016  *      negation                - operator!
00017  *      safe passage            - copy construction
00018  *
00019  * If operations for each of these are not declared
00020  * the template will not instantiate.  If it does 
00021  * instantiate, tests are performed to make sure
00022  * that the operations have correct semantics.
00023  *      
00024  * Adding an orthodox test to a suite is very 
00025  * easy: 
00026  * 
00027  * public: Test *suite ()  {
00028  *     TestSuite *suiteOfTests = new TestSuite;
00029  *     suiteOfTests->addTest (new ComplexNumberTest ("testAdd");
00030  *     suiteOfTests->addTest (new TestCaller<Orthodox<Complex> > ());
00031  *     return suiteOfTests;
00032  *  }
00033  *
00034  * Templated test cases be very useful when you are want to
00035  * make sure that a group of classes have the same form.
00036  *
00037  * see TestSuite
00038  */
00039 
00040 
00041 template <typename ClassUnderTest> class Orthodox : public TestCase
00042 {
00043 public:
00044                     Orthodox () : TestCase ("Orthodox") {}
00045 
00046 protected:
00047     ClassUnderTest  call (ClassUnderTest object);
00048     void            runTest ();
00049 
00050 
00051 };
00052 
00053 
00054 // Run an orthodoxy test
00055 template <typename ClassUnderTest> void Orthodox<ClassUnderTest>::runTest ()
00056 {
00057     // make sure we have a default constructor
00058     ClassUnderTest   a, b, c;
00059 
00060     // make sure we have an equality operator
00061     CPPUNIT_ASSERT (a == b);
00062 
00063     // check the inverse
00064     b.operator= (a.operator! ());
00065     CPPUNIT_ASSERT (a != b);
00066 
00067     // double inversion
00068     b = !!a;
00069     CPPUNIT_ASSERT (a == b);
00070 
00071     // invert again
00072     b = !a;
00073 
00074     // check calls
00075     c = a;
00076     CPPUNIT_ASSERT (c == call (a));
00077 
00078     c = b;
00079     CPPUNIT_ASSERT (c == call (b));
00080 
00081 }
00082 
00083 
00084 // Exercise a call
00085 template <typename ClassUnderTest> 
00086 ClassUnderTest Orthodox<ClassUnderTest>::call (ClassUnderTest object)
00087 {
00088     return object;
00089 }
00090 
00091 } // namespace CppUnit
00092 
00093 #endif

SourceForge Logo hosts this site. Send comments to:
CppUnit Developers