// DeletePolicy_t.cpp // Test pointer class DeletePolicy. #include "ptr/DeletePolicy.h" #include "ptr/TestPolicy.h" #include #include #include #ifndef DEFECT_NO_NAMESPACES using std::cout; using std::cerr; using std::endl; using std::string; #endif //********************************************************************** int main( ) { string component = "DeletePolicy"; string ok_prefix = component + " (I): "; string error_prefix = component + " test (E): "; cout << ok_prefix << "---------- Testing component " + component + ". ----------" << endl; // Make sure assert is enabled. bool assert_flag = false; assert ( ( assert_flag = true, assert_flag ) ); if ( ! assert_flag ) { cerr << "Assert is disabled" << endl; return 1; } //******************************************************************** cout << ok_prefix << "Construct intial policy." << endl; void* ptr = 0; DeletePolicy pol1(ptr); assert( pol1.is_valid() ); assert( pol1.has_last_management() ); //******************************************************************** cout << ok_prefix << "Copy policy." << endl; try { DeletePolicy pol2(ptr,pol1); assert( false ); } catch(const PtrCannotGetManagement&) { cout << "Caught PtrCannotGetManagement" << endl; } //******************************************************************** cout << ok_prefix << "Copy test policy." << endl; // test manages TestPolicy polt1(true); assert( polt1.has_last_management() ); DeletePolicy pol3(ptr,polt1); assert( pol3.is_valid() ); assert( ! polt1.has_last_management() ); assert( pol3.has_last_management() ); // test does not manage TestPolicy polt2(false); assert( ! polt2.has_last_management() ); try { DeletePolicy pol4(ptr,polt1); assert(false); } catch(const PtrDoesNotHaveManagement&) { cout << "Caught PtrDoesNotHaveManagement" << endl; } //******************************************************************** cout << ok_prefix << "Test delete." << endl; const int* pint = new int(123); pol3.delete_if_managing(pint); //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }