// SharedNullPolicy_t.cpp #include "ptr/SharedNullPolicy.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 = "SharedNullPolicy"; 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; int* pi = new int(6); const int* pci = pi; SharedNullPolicy pol1(pci); 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 ); assert( ptab1->get_reference_count() == 1 ); assert( ptab1->get_managing_reference_count() == 0 ); //******************************************************************** cout << ok_prefix << "Copy policy." << endl; { SharedNullPolicy pol2(pi,pol1); cout << pol1 << endl; cout << pol2 << endl; assert( pol1.is_valid() ); assert( pol2.is_valid() ); assert( pol2.get_shared_policy_table() == ptab1 ); assert( ptab1->get_reference_count() == 2 ); } assert( ptab1->get_reference_count() == 1 ); //******************************************************************** cout << ok_prefix << "Copy from another type of policy." << endl; TestPolicy polt1(true); cout << polt1 << endl; assert( polt1.has_management() ); SharedNullPolicy pol3(pi,polt1); cout << polt1 << endl; cout << pol3 << endl; assert( pol3.is_valid() ); //******************************************************************** cout << ok_prefix << "Test delete." << endl; pol3.delete_if_managing(pi); delete pi; //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }