Orthodox.h 2.1 KB

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