// SharedDeletePolicy_t.cpp #include "ptr/SharedDeletePolicy.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 = "SharedDeletePolicy"; 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; const int* pi = new int(6); SharedDeletePolicy pol1(pi); cout << pol1 << endl; assert( pol1.is_valid() ); assert( pol1.has_management() ); assert( pol1.has_last_management() ); const SharedPolicyTable* ptab1 = pol1.get_shared_policy_table(); assert( ptab1 != 0 ); //******************************************************************** cout << ok_prefix << "Copy policy." << endl; { SharedDeletePolicy pol2(pi,pol1); cout << pol1 << endl; cout << pol2 << endl; assert( pol1.is_valid() ); assert( pol1.has_management() ); assert( ! pol1.has_last_management() ); assert( pol2.is_valid() ); assert( pol2.has_management() ); assert( ! pol2.has_last_management() ); assert( pol2.get_shared_policy_table() == ptab1 ); } assert( pol1.has_last_management() ); //******************************************************************** cout << ok_prefix << "Copy from another type of policy." << endl; TestPolicy polt1(true); cout << polt1 << endl; assert( polt1.has_management() ); SharedDeletePolicy pol3(pi,polt1); cout << polt1 << endl; cout << pol3 << endl; assert( pol3.is_valid() ); assert( pol3.has_management() ); assert( pol3.has_last_management() ); assert( !polt1.has_management() ); //******************************************************************** cout << ok_prefix << "Copy policy which will not relinquish." << endl; TestPolicy polt2(false); cout << polt2 << endl; assert( ! polt2.has_management() ); try { SharedDeletePolicy pol4(pi,polt2); assert( false ); } catch( PtrCannotGetManagement& ) { } cout << polt2 << endl; //******************************************************************** cout << ok_prefix << "Test delete." << endl; pol3.delete_if_managing(pi); //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }