// NullPolicy_t.cpp // Test pointer class NullPolicy. #include "ptr/NullPolicy.h" #include "ptr/TestPolicy.h" #include #include #include #include #ifndef DEFECT_NO_NAMESPACES using std::cout; using std::cerr; using std::endl; using std::string; #endif //********************************************************************** int main( ) { string component = "NullPolicy"; 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; NullPolicy pol1(ptr); assert( pol1.is_valid() ); assert( ! pol1.has_last_management() ); //******************************************************************** cout << ok_prefix << "Copy policy." << endl; NullPolicy pol2(ptr,pol1); assert( pol2.is_valid() ); assert( ! pol2.has_last_management() ); //******************************************************************** cout << ok_prefix << "Copy test policy." << endl; // test manages TestPolicy polt1(true); assert( polt1.has_last_management() ); NullPolicy 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() ); NullPolicy pol4(ptr,polt1); assert( pol4.is_valid() ); assert( ! polt2.has_last_management() ); assert( ! pol4.has_last_management() ); //******************************************************************** cout << ok_prefix << "Test delete." << endl; const int* pint = new int(123); pol3.delete_if_managing(pint); #ifndef DEFECT_NO_DELETE_CONST delete pint; #else delete (int*) pint; #endif //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }