CppUnit project page FAQ CppUnit home page

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

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