hdf5/test/set_extent.c
2008-10-07 13:10:49 -05:00

721 lines
23 KiB
C

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Programmer: Pedro Vicente <pvn@ncsa.uiuc.edu>
* April 12, 2002
*
* Purpose: Tests the H5Dset_extent call
*/
#include "hdf5.h"
#include "h5test.h"
/*-------------------------------------------------------------------------
*
* Tests the function H5Dset_extent. In the current version of the library
* the dataset MUST be chunked.
*
*-------------------------------------------------------------------------
*/
#define RANK 2
#define ISTORE_IK 64
int main( void )
{
hid_t fid;
hid_t did=(-1);
hid_t sid=(-1);
hid_t dcpl=(-1);
hid_t fcpl;
hsize_t dims[RANK] = { 90, 90 };
hsize_t dims_new[RANK] = { 70, 70 };
hsize_t dims_chunk[RANK] = { 20, 20 };
hsize_t dims_out[RANK];
hsize_t dims3[RANK];
hsize_t maxdims[RANK] = { H5S_UNLIMITED, H5S_UNLIMITED };
int data[ 90 ][ 90 ];
int buf1[ 70 ][ 70 ];
int buf2[ 90 ][ 90 ];
int i, j, n = 0;
int fillvalue = 1; /* Fill value for the dataset */
#ifdef H5_HAVE_FILTER_DEFLATE
hbool_t do_compress; /* Iterator for looping over compress/no compress */
#endif /* H5_HAVE_FILTER_DEFLATE */
for( i = 0; i < 90; i++ )
for( j = 0; j < 90; j++ )
data[i][j] = n++;
/*-------------------------------------------------------------------------
* Test H5Dset_extent with chunks on the raw data cache
*-------------------------------------------------------------------------
*/
#ifdef H5_HAVE_FILTER_DEFLATE
for (do_compress = FALSE; do_compress <= TRUE; do_compress++)
{
if(do_compress)
puts("Testing WITH compression on chunks.");
else
puts("Testing with NO compression on chunks.");
#else /* H5_HAVE_FILTER_DEFLATE */
puts("** deflate filter nor available - Skipping tests for compression on chunks. **");
#endif /* H5_HAVE_FILTER_DEFLATE */
TESTING("extend dataset create with fill value");
/* Create a new file using default properties. */
if((fid = H5Fcreate("set_extent_create.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Create the data space with unlimited dimensions. */
if((sid = H5Screate_simple(RANK, dims, maxdims)) < 0)
TEST_ERROR;
/* Modify dataset creation properties, i.e. enable chunking. */
if((dcpl = H5Pcreate (H5P_DATASET_CREATE)) < 0)
TEST_ERROR;
if(H5Pset_chunk(dcpl, RANK, dims_chunk) < 0)
TEST_ERROR;
if(H5Pset_fill_value(dcpl, H5T_NATIVE_INT, &fillvalue) < 0)
TEST_ERROR;
#ifdef H5_HAVE_FILTER_DEFLATE
if(do_compress)
{
if(H5Pset_deflate(dcpl, 9) < 0)
FAIL_STACK_ERROR
}
#endif /* H5_HAVE_FILTER_DEFLATE */
/*-------------------------------------------------------------------------
* Create and write one dataset
*-------------------------------------------------------------------------
*/
/* Create a new dataset */
if((did = H5Dcreate2(fid , "Dataset1", H5T_NATIVE_INT, sid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Write the data. */
if(H5Dwrite(did , H5T_NATIVE_INT, sid, H5S_ALL, H5P_DEFAULT, data) < 0)
TEST_ERROR;
/*-------------------------------------------------------------------------
* Set new dimensions for the array; shrink it
*-------------------------------------------------------------------------
*/
/* Set new dimensions for the array. */
if(H5Dset_extent(did , dims_new) < 0)
TEST_ERROR;
/* Get the space. */
if((sid = H5Dget_space(did)) < 0)
TEST_ERROR;
/* Get dimensions. */
if(H5Sget_simple_extent_dims(sid, dims_out, NULL) < 0)
TEST_ERROR;
if(dims_out[0] != dims_new[0])
TEST_ERROR;
if(dims_out[1] != dims_new[1])
TEST_ERROR;
/*-------------------------------------------------------------------------
* Read
*-------------------------------------------------------------------------
*/
/* Read the new dataset. */
if (H5Dread( did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1 ) < 0)
TEST_ERROR;
/* Compare the read array with the original array */
for( i = 0; i < (int)dims_out[0]; i++ )
{
for( j = 0; j < (int)dims_out[1]; j++ )
{
if ( buf1[i][j] != data[i][j] )
{
printf("buf1[%d][%d] = %d\n", i, j, buf1[i][j]);
printf("data[%d][%d] = %d\n", i, j, data[i][j]);
TEST_ERROR;
}
}
}
/*-------------------------------------------------------------------------
* Set new dimensions for the array; expand it back to original size
*-------------------------------------------------------------------------
*/
/* Set new dimensions for the array. */
if(H5Dset_extent(did, dims) < 0)
TEST_ERROR;
/* Get the space. */
if((sid = H5Dget_space(did)) < 0)
TEST_ERROR;
/* Get dimensions. */
if(H5Sget_simple_extent_dims(sid, dims_out, NULL) < 0)
TEST_ERROR;
if(dims_out[0] != dims[0])
TEST_ERROR;
if(dims_out[1] != dims[1])
TEST_ERROR;
/*-------------------------------------------------------------------------
* Read
*-------------------------------------------------------------------------
*/
/* Read the new dataset. */
if(H5Dread(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2) < 0)
TEST_ERROR;
/* Compare the read array with the original array */
for(i = 0; i < (int)dims_out[0]; i++ )
{
for(j = 0; j < (int)dims_out[1]; j++ )
{
if(i >= 70 || j >= 70)
{
if(buf2[i][j] != fillvalue) {
printf("buf1[%d][%d] = %d\n", i, j, buf1[i][j]);
printf("fillvalue = %d\n", fillvalue);
TEST_ERROR;
} /* end if */
} /* end if */
else
{
if(buf2[i][j] != data[i][j])
TEST_ERROR;
}
}
}
/*-------------------------------------------------------------------------
* Close/release resources
*-------------------------------------------------------------------------
*/
if(H5Dclose(did) < 0)
TEST_ERROR
if(H5Sclose(sid) < 0)
TEST_ERROR
if(H5Pclose(dcpl) < 0)
TEST_ERROR
PASSED();
TESTING("extend dataset create without fill value");
/* Create the data space with unlimited dimensions. */
if((sid = H5Screate_simple(RANK, dims, maxdims)) < 0)
TEST_ERROR;
/* Modify dataset creation properties, i.e. enable chunking. */
if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0)
TEST_ERROR;
if(H5Pset_chunk(dcpl, RANK, dims_chunk) < 0)
TEST_ERROR;
if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0)
TEST_ERROR;
#ifdef H5_HAVE_FILTER_DEFLATE
if(do_compress)
if(H5Pset_deflate(dcpl, 9) < 0)
FAIL_STACK_ERROR
#endif /* H5_HAVE_FILTER_DEFLATE */
/*-------------------------------------------------------------------------
* Create and write one dataset
*-------------------------------------------------------------------------
*/
/* Create a new dataset */
if((did = H5Dcreate2(fid , "Dataset2", H5T_NATIVE_INT, sid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Write the data. */
if(H5Dwrite(did , H5T_NATIVE_INT, sid, H5S_ALL, H5P_DEFAULT, data) < 0)
TEST_ERROR;
/*-------------------------------------------------------------------------
* Set new dimensions for the array; shrink it
*-------------------------------------------------------------------------
*/
/* Set new dimensions for the array. */
if(H5Dset_extent(did , dims_new) < 0)
TEST_ERROR;
/* Get the space. */
if((sid = H5Dget_space(did)) < 0)
TEST_ERROR;
/* Get dimensions. */
if(H5Sget_simple_extent_dims(sid, dims_out, NULL) < 0)
TEST_ERROR;
if(dims_out[0] != dims_new[0])
TEST_ERROR;
/*-------------------------------------------------------------------------
* Read
*-------------------------------------------------------------------------
*/
/* Read the new dataset. */
if (H5Dread( did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1 ) < 0)
TEST_ERROR;
/* Compare the read array with the original array */
for( i = 0; i < (int)dims_out[0]; i++ )
{
for( j = 0; j < (int)dims_out[1]; j++ )
{
if ( buf1[i][j] != data[i][j] )
TEST_ERROR;
}
}
/*-------------------------------------------------------------------------
* Set new dimensions for the array; expand it again
*-------------------------------------------------------------------------
*/
/* Set new dimensions for the array. */
if (H5Dset_extent( did , dims ) < 0)
TEST_ERROR;
/* Get the space. */
if ((sid = H5Dget_space( did )) < 0)
TEST_ERROR;
/* Get dimensions. */
if (H5Sget_simple_extent_dims( sid, dims_out, NULL ) < 0)
TEST_ERROR;
if ( dims_out[0] != dims[0] )
TEST_ERROR;
/*-------------------------------------------------------------------------
* Read
*-------------------------------------------------------------------------
*/
/* Read the new dataset. */
if (H5Dread( did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2 ) < 0)
TEST_ERROR;
/* Compare the read array with the original array */
for( i = 0; i < (int)dims_out[0]; i++ )
{
for( j = 0; j < (int)dims_out[1]; j++ )
{
if ( i >= 70 || j >= 70 ) {
if ( buf2[i][j] != 0 )
TEST_ERROR;
}
else {
if ( buf2[i][j] != data[i][j] )
TEST_ERROR;
}
}
}
/*-------------------------------------------------------------------------
* Close/release resources
*-------------------------------------------------------------------------
*/
if (H5Dclose(did) < 0)
TEST_ERROR
if (H5Sclose(sid) < 0)
TEST_ERROR
if (H5Pclose(dcpl) < 0)
TEST_ERROR
if (H5Fclose( fid ) < 0)
TEST_ERROR
PASSED();
/*-------------------------------------------------------------------------
* Test H5Dset_extent with chunks written to file
*-------------------------------------------------------------------------
*/
/* Create a file creation property list */
if((fcpl = H5Pcreate(H5P_FILE_CREATE)) < 0)
TEST_ERROR;
/* Set non-default indexed storage B-tree internal 'K' value */
if(H5Pset_istore_k(fcpl,ISTORE_IK) < 0)
TEST_ERROR;
/* Create a new file using properties. */
if((fid = H5Fcreate("set_extent_read.h5", H5F_ACC_TRUNC, fcpl, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Close property list */
if(H5Pclose(fcpl) < 0)
TEST_ERROR;
TESTING("extend dataset read with fill value");
/* Create the data space with unlimited dimensions. */
if((sid = H5Screate_simple(RANK, dims, maxdims)) < 0)
TEST_ERROR;
/* Modify dataset creation properties, i.e. enable chunking. */
if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0)
TEST_ERROR;
if(H5Pset_chunk(dcpl, RANK, dims_chunk) < 0)
TEST_ERROR;
if(H5Pset_fill_value(dcpl, H5T_NATIVE_INT, &fillvalue) < 0)
TEST_ERROR;
#ifdef H5_HAVE_FILTER_DEFLATE
if(do_compress)
if(H5Pset_deflate(dcpl, 9) < 0)
FAIL_STACK_ERROR
#endif /* H5_HAVE_FILTER_DEFLATE */
/* Create a new dataset within the file using cparms creation properties. */
if((did = H5Dcreate2(fid , "Dataset1", H5T_NATIVE_INT, sid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Write the data. */
if(H5Dwrite(did , H5T_NATIVE_INT, sid, H5S_ALL, H5P_DEFAULT, data) < 0)
TEST_ERROR;
/* Close/release resources. */
if(H5Dclose(did) < 0) FAIL_STACK_ERROR
if(H5Sclose(sid) < 0) FAIL_STACK_ERROR
if(H5Pclose(dcpl) < 0) FAIL_STACK_ERROR
if(H5Fclose(fid) < 0) FAIL_STACK_ERROR
/* Open the file */
if((fid = H5Fopen("set_extent_read.h5", H5F_ACC_RDWR, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Open the dataset */
if((did = H5Dopen2(fid , "Dataset1", H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Set new dimensions for the array. */
if(H5Dset_extent(did, dims_new) < 0)
TEST_ERROR;
/* Get the space. */
if((sid = H5Dget_space(did)) < 0)
TEST_ERROR;
/* Get dimensions. */
if(H5Sget_simple_extent_dims(sid, dims_out, NULL) < 0)
TEST_ERROR;
if(dims_out[0] != dims_new[0])
TEST_ERROR;
/* Read the new dataset. */
if(H5Dread(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1) < 0)
TEST_ERROR;
/* Compare the read array with the original array */
for(i = 0; i < (int)dims_out[0]; i++)
{
for(j = 0; j < (int)dims_out[1]; j++)
{
if(buf1[i][j] != data[i][j])
TEST_ERROR;
}
}
/*-------------------------------------------------------------------------
* Set new dimensions for the array; expand it again
*-------------------------------------------------------------------------
*/
/* Set new dimensions for the array. */
if (H5Dset_extent( did , dims ) < 0)
TEST_ERROR;
/* Get the space. */
if ((sid = H5Dget_space( did )) < 0)
TEST_ERROR;
/* Get dimensions. */
if (H5Sget_simple_extent_dims( sid, dims_out, NULL ) < 0)
TEST_ERROR;
if ( dims_out[0] != dims[0] )
TEST_ERROR;
/* Read the new dataset. */
if (H5Dread( did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2 ) < 0)
TEST_ERROR;
/* Compare the read array with the original array */
for( i = 0; i < (int)dims_out[0]; i++ )
{
for( j = 0; j < (int)dims_out[1]; j++ )
{
if ( i >= 70 || j >= 70 )
{
if ( buf2[i][j] != fillvalue )
TEST_ERROR;
}
else
{
if ( buf2[i][j] != data[i][j] )
TEST_ERROR;
}
}
}
/*-------------------------------------------------------------------------
* Close/release resources
*-------------------------------------------------------------------------
*/
if (H5Dclose(did) < 0)
TEST_ERROR
if (H5Sclose(sid) < 0)
TEST_ERROR
PASSED();
TESTING("extend dataset read without fill value");
/* Create the data space with unlimited dimensions. */
if((sid = H5Screate_simple(RANK, dims, maxdims)) < 0)
TEST_ERROR;
/* Modify dataset creation properties, i.e. enable chunking. */
if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0)
TEST_ERROR;
if(H5Pset_chunk(dcpl, RANK, dims_chunk) < 0)
TEST_ERROR;
if(H5Pset_fill_time(dcpl, H5D_FILL_TIME_ALLOC) < 0)
TEST_ERROR;
#ifdef H5_HAVE_FILTER_DEFLATE
if(do_compress)
if(H5Pset_deflate(dcpl, 9) < 0)
FAIL_STACK_ERROR
#endif /* H5_HAVE_FILTER_DEFLATE */
/* Create a new dataset within the file using cparms creation properties. */
if((did = H5Dcreate2(fid , "Dataset2", H5T_NATIVE_INT, sid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Write the data. */
if(H5Dwrite(did , H5T_NATIVE_INT, sid, H5S_ALL, H5P_DEFAULT, data) < 0)
TEST_ERROR;
/* Close/release resources. */
if(H5Dclose(did) < 0) FAIL_STACK_ERROR
if(H5Sclose(sid) < 0) FAIL_STACK_ERROR
if(H5Pclose(dcpl) < 0) FAIL_STACK_ERROR
if(H5Fclose(fid) < 0) FAIL_STACK_ERROR
/* Open the file */
if((fid = H5Fopen("set_extent_read.h5", H5F_ACC_RDWR, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Open the dataset */
if((did = H5Dopen2(fid , "Dataset2", H5P_DEFAULT)) < 0)
TEST_ERROR;
/* Set new dimensions for the array. */
if(H5Dset_extent(did, dims_new) < 0)
TEST_ERROR;
/* Get the space. */
if((sid = H5Dget_space(did)) < 0)
TEST_ERROR;
/* Get dimensions. */
if(H5Sget_simple_extent_dims(sid, dims_out, NULL) < 0)
TEST_ERROR;
if(dims_out[0] != dims_new[0])
TEST_ERROR;
/* Read the new dataset. */
if(H5Dread(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1) < 0)
TEST_ERROR;
/* Compare the read array with the original array */
for(i = 0; i < (int)dims_out[0]; i++)
{
for(j = 0; j < (int)dims_out[1]; j++)
{
if(buf1[i][j] != data[i][j])
TEST_ERROR;
}
}
/*-------------------------------------------------------------------------
* Set new dimensions for the array; expand it again
*-------------------------------------------------------------------------
*/
/* Set new dimensions for the array. */
if (H5Dset_extent( did , dims ) < 0)
TEST_ERROR;
/* Get the space. */
if ((sid = H5Dget_space( did )) < 0)
TEST_ERROR;
/* Get dimensions. */
if (H5Sget_simple_extent_dims( sid, dims_out, NULL ) < 0)
TEST_ERROR;
if ( dims_out[0] != dims[0] )
TEST_ERROR;
/* Read the new dataset. */
if (H5Dread( did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2 ) < 0)
TEST_ERROR;
/* Compare the read array with the original array */
for( i = 0; i < (int)dims_out[0]; i++ )
{
for( j = 0; j < (int)dims_out[1]; j++ )
{
if ( i >= 70 || j >= 70 )
{
if ( buf2[i][j] != 0 )
TEST_ERROR;
}
else
{
if ( buf2[i][j] != data[i][j] )
TEST_ERROR;
}
}
}
/*-------------------------------------------------------------------------
* close
*-------------------------------------------------------------------------
*/
if (H5Dclose(did) < 0)
TEST_ERROR
if (H5Sclose(sid) < 0)
TEST_ERROR
if (H5Fclose( fid ) < 0)
TEST_ERROR
PASSED();
/*-------------------------------------------------------------------------
* test a dataset with non initialized chunks
*-------------------------------------------------------------------------
*/
TESTING("non initialized chunks");
dims3[ 0 ] = 90;
dims3[ 1 ] = 90;
if ((fid = H5Fopen("set_extent_create.h5", H5F_ACC_RDWR, H5P_DEFAULT)) < 0)
TEST_ERROR;
if ((sid = H5Screate_simple(RANK, dims3, maxdims)) < 0)
TEST_ERROR;
if ((dcpl = H5Pcreate (H5P_DATASET_CREATE)) < 0)
TEST_ERROR;
if (H5Pset_chunk(dcpl, RANK, dims_chunk) < 0)
TEST_ERROR;
#ifdef H5_HAVE_FILTER_DEFLATE
if(do_compress)
{
if(H5Pset_deflate(dcpl, 9) < 0)
FAIL_STACK_ERROR
}
#endif /* H5_HAVE_FILTER_DEFLATE */
/* create a new dataset */
if((did = H5Dcreate2(fid , "Dataset3", H5T_NATIVE_INT, sid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
TEST_ERROR;
/* set new dimensions for the array */
dims3[ 0 ] = 0;
dims3[ 1 ] = 0;
if (H5Dset_extent( did , dims3 ) < 0)
TEST_ERROR;
if (H5Dclose(did) < 0)
TEST_ERROR
if (H5Sclose(sid) < 0)
TEST_ERROR
if (H5Pclose(dcpl) < 0)
TEST_ERROR
if (H5Fclose( fid ) < 0)
TEST_ERROR
PASSED();
#ifdef H5_HAVE_FILTER_DEFLATE
} /* end for */
#endif /* H5_HAVE_FILTER_DEFLATE */
puts("All set_extent tests passed.");
return 0;
error:
H5Dclose( did );
H5Sclose( sid );
H5Pclose( dcpl );
H5Fclose( fid );
H5_FAILED();
return 1;
}