function test_attdel ( ncfile )

if nargin < 1 
    ncfile = 'foo.nc'; 
end

create_test_file(ncfile);
test_delete_double(ncfile);
test_bad_ncid(ncfile);
test_bad_varid(ncfile);
test_does_not_exist(ncfile);

fprintf('ATTDEL succeeded.\n' );

%--------------------------------------------------------------------------
function create_test_file(ncfile)

[ncid, status] = oct_mexnc ( 'create', ncfile, nc_clobber_mode );
if status, error( oct_mexnc ( 'strerror', status ) ), end

[xdimid, status] = oct_mexnc ( 'def_dim', ncid, 'x', 20 );
if status, error( oct_mexnc ( 'strerror', status ) ), end

[varid, status] = oct_mexnc ( 'def_var', ncid, 'x', nc_double, 1, xdimid );
if status, error( oct_mexnc ( 'strerror', status ) ), end

input_data = 3.14159;
status = oct_mexnc ( 'put_att_double', ncid, varid, 'test_double', nc_double, 1, input_data );
if status, error( oct_mexnc ( 'strerror', status ) ), end

status = oct_mexnc('close',ncid);
if status, error( oct_mexnc ( 'strerror', status ) ), end


%--------------------------------------------------------------------------
function test_delete_double(ncfile)
[ncid, status] = oct_mexnc ( 'open', ncfile, nc_write_mode );
if status, error( oct_mexnc ( 'strerror', status ) ), end

status = oct_mexnc('redef',ncid);
if status, error( oct_mexnc ( 'strerror', status ) ), end

[varid,status] = oct_mexnc('inq_varid',ncid,'x');
if status, error( oct_mexnc ( 'strerror', status ) ), end

% Test 1:  oct_delete a double precision attribute of a variable
status = oct_mexnc ( 'ATTDEL', ncid, varid, 'test_double' );
if (status < 0), error( oct_mexnc ( 'strerror', status ) ), end

[attnum, status] = oct_mexnc ( 'inq_attid', ncid, varid, 'test_double' );
if ( status >= 0 )
	error  ( 'attribute was not deleted' );
end

status = oct_mexnc('close',ncid);
if status, error( oct_mexnc ( 'strerror', status ) ), end




%--------------------------------------------------------------------------
function test_bad_ncid(ncfile)
% Test 2:  try to oct_delete from a bad ncid

[ncid, status] = oct_mexnc ( 'open', ncfile, nc_write_mode );
if status, error( oct_mexnc ( 'strerror', status ) ), end

status = oct_mexnc('redef',ncid);
if status, error( oct_mexnc ( 'strerror', status ) ), end

[varid,status] = oct_mexnc('inq_varid',ncid,'x');
if status, error( oct_mexnc ( 'strerror', status ) ), end

status = oct_mexnc ( 'ATTDEL', -5, varid, 'test_double' );
if ( status ~= -1 )
	err_msg = sprintf ( '%s:  ATTDEL succeeded on a bad ncid.\n', mfilename );
	error ( err_msg );
end

status = oct_mexnc('close',ncid);
if status, error( oct_mexnc ( 'strerror', status ) ), end



%--------------------------------------------------------------------------
function test_bad_varid(ncfile)
% Test 3:  try to oct_delete from a bad varid

[ncid, status] = oct_mexnc ( 'open', ncfile, nc_write_mode );
if status, error( oct_mexnc ( 'strerror', status ) ), end

status = oct_mexnc('redef',ncid);
if status, error( oct_mexnc ( 'strerror', status ) ), end

[varid,status] = oct_mexnc('inq_varid',ncid,'x');
if status, error( oct_mexnc ( 'strerror', status ) ), end

status = oct_mexnc ( 'ATTDEL', ncid, -5, 'test_double' );
if ( status ~= -1 )
	error ( 'ATTDEL succeeded on a bad varid.' );
end


status = oct_mexnc('close',ncid);
if status, error( oct_mexnc ( 'strerror', status ) ), end



%--------------------------------------------------------------------------
function test_does_not_exist(ncfile)
% Test 4:  try to oct_delete a non-existant attribute

[ncid, status] = oct_mexnc ( 'open', ncfile, nc_write_mode );
if status, error( oct_mexnc ( 'strerror', status ) ), end

status = oct_mexnc('redef',ncid);
if status, error( oct_mexnc ( 'strerror', status ) ), end

[varid,status] = oct_mexnc('inq_varid',ncid,'x');
if status, error( oct_mexnc ( 'strerror', status ) ), end

status = oct_mexnc ( 'ATTDEL', ncid, varid, 'blah' );
if ( status ~= -1 )
	error ( 'ATTDEL succeeded on a bad attribute name.'  );
end

status = oct_mexnc('close',ncid);
if status, error( oct_mexnc ( 'strerror', status ) ), end



