#include #include #include "IHit.hxx" #include "IMCHit.hxx" #include "IComboHit.hxx" #include "IHandle.hxx" #include "IHandleHack.hxx" namespace tut { COMET::IHit* CanDelete(void) { COMET::IHit* p = new COMET::IMCHit(); p->SetBit(kCanDelete,true); return p; } COMET::IHit* CannotDelete(void) { COMET::IHit* p = new COMET::IMCHit(); p->SetBit(kCanDelete,false); return p; } struct baseTHandle { baseTHandle() { // Run before each test. } ~baseTHandle() { // Run after each test. } }; // Declare the test typedef test_group::object testTHandle; test_group groupTHandle("IHandle"); // Test the default constructor and destructor. Make sure the default // values are all null. template<> template<> void testTHandle::test<1> () { { COMET::IHandle handle; ensure("Handle value is null", GetPointer(handle) == NULL); ensure("Handle is false", !handle); ensure("Handle is null", handle == NULL); } ensure("Handle Registry is clean", COMET::CleanHandleRegistry()); } /// Test the constructor and destructor. template<> template<> void testTHandle::test<2> () { { COMET::IHit* hit = CanDelete(); COMET::IHandle handle(hit); ensure("Handle is valid", handle); ensure_equals("Handle value is matchs pointer", GetPointer(handle), hit); } ensure("Handle Registry is clean", COMET::CleanHandleRegistry()); } // Test the copy constructor. template <> template <> void testTHandle::test<3> () { { COMET::IHit* hit = CanDelete(); COMET::IHandle a(hit); ensure("Copy from pointer", a); ensure_equals("Pointers equal", GetPointer(a), hit); COMET::IHandle b(a); ensure("Copy from handle", b); ensure("Handle copied pointer", GetPointer(b) == hit); ensure("Handles equal", a == b); COMET::IHandle c(a); ensure("Copy from handle with down cast", b); ensure("Handle cast copied pointer", GetPointer(b) == hit); ensure("Handles equal after cast", a == b); COMET::IHandle combo(a); ensure("Copy from COMET::IMCHit to COMET::IComboHit should produce null",!combo); } ensure("Handle Registry is clean", COMET::CleanHandleRegistry()); } // Test the assigments template <> template <> void testTHandle::test<4> () { COMET::IHit* hit = CanDelete(); COMET::IHandle a(hit); ensure("Copy from pointer", a); ensure("Pointers equal", GetPointer(a) == hit); COMET::IHandle b = a; ensure("IHandle b = a", a == b); COMET::IHandle c; c = a; ensure("Assign handle", c == a); COMET::IHandle d = c; ensure("Assign with cast", d == a); COMET::IHandle e; e = c; ensure("Assignment with down cast", e == d); COMET::IHandle f; f = e; ensure("Assignment with up cast", f == a); COMET::IHandle g = a; ensure("Assignment with down cast to incompatible base",!g); COMET::IHandle h; h = a; ensure("Assign handle with down cast to incompatible base",!h); } namespace { void ReferenceTest(const COMET::IHit& hit, std::string expectedType) { ensure_equals("Reference test hit type mismatch", hit.ClassName(), expectedType); } } // Test call by reference template <> template <> void testTHandle::test<5> () { COMET::IHit* hit = CanDelete(); COMET::IHandle a(hit); COMET::IHandle b(a); ReferenceTest(*a,"COMET::IMCHit"); ReferenceTest(*b,"COMET::IMCHit"); } namespace { void ValueTest(const COMET::IHandle h, std::string expectedType) { ensure_equals("Value test hit type mismatch", h->ClassName(), expectedType); } } // Test call by reference template <> template <> void testTHandle::test<6> () { COMET::IHit* hit = CanDelete(); COMET::IHandle a(hit); COMET::IHandle b(a); ValueTest(a,"COMET::IMCHit"); ValueTest(b,"COMET::IMCHit"); } namespace { COMET::IHandle ReturnTest() { COMET::IHit* hit = CanDelete(); COMET::IHandle a(hit); ensure_equals("Returned Pointer is OK", GetPointer(a), hit); return a; } } // Test function return value. template <> template <> void testTHandle::test<7> () { { COMET::IHandle a(ReturnTest()); COMET::IHandle b; b = ReturnTest(); ensure("Returned handle was copied",a); ensure("Returned handle was assigned",b); ensure_equals("Returned class name", a->ClassName(), std::string("COMET::IMCHit")); ensure_equals("Returned class name", b->ClassName(), std::string("COMET::IMCHit")); } ensure("Handle Registry is clean", COMET::CleanHandleRegistry()); } // Test the less than operator. template <> template <> void testTHandle::test<8> () { { COMET::IHit* aPtr = CanDelete(); COMET::IHit* bPtr = CanDelete(); COMET::IHandle a(aPtr); COMET::IHandle b(bPtr); ensure("First pointer is valid",aPtr); ensure("First Handle is valid",a); ensure("Second pointer is valid",bPtr); ensure("Second Handle is valid",b); ensure("Pointers are not equal",aPtr != bPtr); ensure("Handles are not equal", !(a == b)); if (aPtr < bPtr) { ensure("aPtr < bPtr && a < b",aPtr < bPtr && a < b); ensure("aPtr < bPtr && !(b < a)",aPtr < bPtr && !(b < a)); } else { ensure("bPtr < aPtr && b < a",bPtr < aPtr && b < a); ensure("bPtr < aPtr && !(a < b)",bPtr < aPtr && !(a < b)); } } ensure("Handle Registry is clean", COMET::CleanHandleRegistry()); } };