mirror of
https://github.com/HDFGroup/hdf5.git
synced 2025-04-12 17:31:09 +08:00
[svn-r13144] Added a test case for VL type of compound type with VL string in the field.
This commit is contained in:
parent
8fa9daa174
commit
b5047cafdc
287
test/tvltypes.c
287
test/tvltypes.c
@ -40,6 +40,7 @@
|
||||
#define SPACE3_DIM1 128
|
||||
#define L1_INCM 16
|
||||
#define L2_INCM 8
|
||||
#define L3_INCM 2
|
||||
|
||||
void *test_vltypes_alloc_custom(size_t size, void *info);
|
||||
void test_vltypes_free_custom(void *mem, void *info);
|
||||
@ -1152,6 +1153,291 @@ test_vltypes_compound_vlen_vlen(void)
|
||||
CHECK(ret, FAIL, "H5Fclose");
|
||||
} /* end test_vltypes_compound_vlen_vlen() */
|
||||
|
||||
/****************************************************************
|
||||
**
|
||||
** test_vltypes_compound_vlstr(): Test VL datatype code.
|
||||
** Tests VL datatypes of compound datatypes with VL string.
|
||||
**
|
||||
****************************************************************/
|
||||
static void
|
||||
test_vltypes_compound_vlstr(void)
|
||||
{
|
||||
typedef enum {
|
||||
red,
|
||||
blue,
|
||||
green
|
||||
} e1;
|
||||
typedef struct {
|
||||
char *string;
|
||||
e1 color;
|
||||
} s2;
|
||||
typedef struct { /* Struct that the compound type are composed of */
|
||||
hvl_t v;
|
||||
} s1;
|
||||
s1 wdata[SPACE1_DIM1]; /* data to write */
|
||||
s1 wdata2[SPACE1_DIM1]; /* data to write */
|
||||
s1 rdata[SPACE1_DIM1]; /* data to read */
|
||||
s1 rdata2[SPACE1_DIM1]; /* data to read */
|
||||
char str[64] = "a\0";
|
||||
hid_t fid1; /* HDF5 File IDs */
|
||||
hid_t dataset, dset2; /* Dataset ID */
|
||||
hid_t sid1, sid2, filespace; /* Dataspace ID */
|
||||
hid_t tid1, tid2, tid3, tid4, tid5; /* Datatype IDs */
|
||||
hid_t cparms;
|
||||
hsize_t dims1[] = {SPACE1_DIM1};
|
||||
hsize_t chunk_dims[] = {SPACE1_DIM1/2};
|
||||
hsize_t maxdims[] = {H5S_UNLIMITED};
|
||||
hsize_t size[] = {SPACE1_DIM1};
|
||||
hsize_t offset[] = {0};
|
||||
unsigned i,j,k; /* counting variables */
|
||||
s2 *t1, *t2; /* Temporary pointer to VL information */
|
||||
int val;
|
||||
herr_t ret; /* Generic return value */
|
||||
|
||||
/* Output message about test being performed */
|
||||
MESSAGE(5, ("Testing VL Datatype of Compound Datatype with VL String Functionality\n"));
|
||||
|
||||
/* Allocate and initialize VL data to write */
|
||||
for(i=0; i<SPACE1_DIM1; i++) {
|
||||
wdata[i].v.p=(s2*)HDmalloc((i+L3_INCM)*sizeof(s2));
|
||||
wdata[i].v.len=i+L3_INCM;
|
||||
for(t1=(wdata[i].v).p, j=0; j<(i+L3_INCM); j++, t1++) {
|
||||
strcat(str, "m");
|
||||
t1->string = (char*)HDmalloc(strlen(str)*sizeof(char)+1);
|
||||
strcpy(t1->string, str);
|
||||
t1->color = red;
|
||||
}
|
||||
} /* end for */
|
||||
|
||||
/* Create file */
|
||||
fid1 = H5Fcreate(FILENAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
||||
CHECK(fid1, FAIL, "H5Fcreate");
|
||||
|
||||
/* Create dataspace for datasets */
|
||||
sid1 = H5Screate_simple(SPACE1_RANK, dims1, maxdims);
|
||||
CHECK(sid1, FAIL, "H5Screate_simple");
|
||||
|
||||
/* Create a VL string type*/
|
||||
tid4 = H5Tcopy (H5T_C_S1);
|
||||
CHECK(tid4, FAIL, "H5Tcopy");
|
||||
ret = H5Tset_size (tid4,H5T_VARIABLE);
|
||||
CHECK(ret, FAIL, "H5Tset_size");
|
||||
|
||||
/* Create an enum type */
|
||||
tid3 = H5Tenum_create(H5T_STD_I32BE);
|
||||
val = 0;
|
||||
ret = H5Tenum_insert(tid3, "RED", &val);
|
||||
CHECK(ret, FAIL, "H5Tenum_insert");
|
||||
val = 1;
|
||||
ret = H5Tenum_insert(tid3, "BLUE", &val);
|
||||
CHECK(ret, FAIL, "H5Tenum_insert");
|
||||
val = 2;
|
||||
ret = H5Tenum_insert(tid3, "GREEN", &val);
|
||||
CHECK(ret, FAIL, "H5Tenum_insert");
|
||||
|
||||
/* Create the first layer compound type */
|
||||
tid5 = H5Tcreate(H5T_COMPOUND, sizeof(s2));
|
||||
CHECK(tid5, FAIL, "H5Tcreate");
|
||||
/* Insert fields */
|
||||
ret=H5Tinsert(tid5, "string", HOFFSET(s2, string), tid4);
|
||||
CHECK(ret, FAIL, "H5Tinsert");
|
||||
/* Insert fields */
|
||||
ret=H5Tinsert(tid5, "enumerate", HOFFSET(s2, color), tid3);
|
||||
CHECK(ret, FAIL, "H5Tinsert");
|
||||
|
||||
|
||||
/* Create a VL datatype of first layer compound type */
|
||||
tid1 = H5Tvlen_create (tid5);
|
||||
CHECK(tid1, FAIL, "H5Tvlen_create");
|
||||
|
||||
/* Create the base compound type */
|
||||
tid2 = H5Tcreate(H5T_COMPOUND, sizeof(s1));
|
||||
CHECK(tid2, FAIL, "H5Tcreate");
|
||||
|
||||
/* Insert fields */
|
||||
ret=H5Tinsert(tid2, "v", HOFFSET(s1, v), tid1);
|
||||
CHECK(ret, FAIL, "H5Tinsert");
|
||||
|
||||
/* Modify dataset creation properties, i.e. enable chunking */
|
||||
cparms = H5Pcreate (H5P_DATASET_CREATE);
|
||||
ret = H5Pset_chunk ( cparms, SPACE1_RANK, chunk_dims);
|
||||
CHECK(ret, FAIL, "H5Pset_chunk");
|
||||
|
||||
/* Create a dataset */
|
||||
dataset=H5Dcreate(fid1,"Dataset1",tid2,sid1,cparms);
|
||||
CHECK(dataset, FAIL, "H5Dcreate");
|
||||
|
||||
/* Extend the dataset. This call assures that dataset is 4.*/
|
||||
ret = H5Dextend (dataset, size);
|
||||
CHECK(ret, FAIL, "H5Dextend");
|
||||
|
||||
/* Select a hyperslab */
|
||||
filespace = H5Dget_space (dataset);
|
||||
ret = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL,
|
||||
dims1, NULL);
|
||||
CHECK(ret, FAIL, "H5Sselect_hyperslab");
|
||||
|
||||
/* Write dataset to disk */
|
||||
ret=H5Dwrite(dataset,tid2,sid1,filespace,H5P_DEFAULT,wdata);
|
||||
CHECK(ret, FAIL, "H5Dwrite");
|
||||
|
||||
ret=H5Fflush(fid1,H5F_SCOPE_GLOBAL);
|
||||
CHECK(ret, FAIL, "H5Fflush");
|
||||
|
||||
|
||||
dset2=H5Dopen(fid1,"Dataset1");
|
||||
CHECK(dset2, FAIL, "H5Dopen");
|
||||
|
||||
/* Read dataset from disk */
|
||||
ret=H5Dread(dset2,tid2,H5S_ALL,H5S_ALL,H5P_DEFAULT,rdata);
|
||||
CHECK(ret, FAIL, "H5Dread");
|
||||
|
||||
/* Compare data read in */
|
||||
for(i=0; i<SPACE1_DIM1; i++) {
|
||||
if(wdata[i].v.len!=rdata[i].v.len) {
|
||||
TestErrPrintf("%d: VL data length don't match!, wdata[%d].v.len=%d, rdata[%d].v.len=%d\n",__LINE__,(int)i,(int)wdata[i].v.len,(int)i,(int)rdata[i].v.len);
|
||||
continue;
|
||||
} /* end if */
|
||||
|
||||
for(t1=wdata[i].v.p, t2=rdata[i].v.p, j=0; j<rdata[i].v.len; j++, t1++, t2++) {
|
||||
if( strcmp(t1->string, t2->string) ) {
|
||||
TestErrPrintf("VL data values don't match!, t1->string=%s, t2->string=%s\n",t1->string, t2->string);
|
||||
continue;
|
||||
} /* end if */
|
||||
if(t1->color != t2->color) {
|
||||
TestErrPrintf("VL data values don't match!, t1->color=%d, t2->color=%d\n",t1->color, t2->color);
|
||||
continue;
|
||||
} /* end if */
|
||||
} /* end for */
|
||||
} /* end for */
|
||||
|
||||
/* Reclaim the VL data */
|
||||
ret=H5Dvlen_reclaim(tid2,sid1,H5P_DEFAULT,rdata);
|
||||
CHECK(ret, FAIL, "H5Dvlen_reclaim");
|
||||
|
||||
/* Reclaim the write VL data */
|
||||
ret=H5Dvlen_reclaim(tid2,sid1,H5P_DEFAULT,wdata);
|
||||
CHECK(ret, FAIL, "H5Dvlen_reclaim");
|
||||
|
||||
#ifdef TMP
|
||||
for(i=0; i<SPACE1_DIM1; i++) {
|
||||
wdata[i].v.p=(s2*)HDmalloc((i+L3_INCM)*sizeof(s2));
|
||||
wdata[i].v.len=i+L3_INCM;
|
||||
for(t1=(wdata[i].v).p, j=0; j<(i+L3_INCM); j++, t1++) {
|
||||
strcat(str, "n");
|
||||
t1->string = (char*)HDmalloc(strlen(str)*sizeof(char)+1);
|
||||
strcpy(t1->string, str);
|
||||
t1->color = blue;
|
||||
}
|
||||
} /* end for */
|
||||
#else
|
||||
/* Use this part for new data */
|
||||
strcpy(str, "b\0");
|
||||
for(i=0; i<SPACE1_DIM1; i++) {
|
||||
wdata2[i].v.p=(s2*)HDmalloc(sizeof(s2));
|
||||
wdata2[i].v.len=1;
|
||||
for(t1=(s2*)(wdata2[i].v).p, j=0; j<1; j++, t1++) {
|
||||
strcat(str, "p");
|
||||
t1->string = (char*)HDmalloc(strlen(str)*sizeof(char)+1);
|
||||
strcpy(t1->string, str);
|
||||
t1->color = green;
|
||||
}
|
||||
} /* end for */
|
||||
#endif /*TMP*/
|
||||
|
||||
/* Select a hyperslab */
|
||||
filespace = H5Dget_space (dset2);
|
||||
ret = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL,
|
||||
dims1, NULL);
|
||||
CHECK(ret, FAIL, "H5Sselect_hyperslab");
|
||||
|
||||
/* Create dataspace for datasets */
|
||||
sid2 = H5Screate_simple(SPACE1_RANK, dims1, NULL);
|
||||
CHECK(sid1, FAIL, "H5Screate_simple");
|
||||
|
||||
/* Write dataset to disk */
|
||||
ret=H5Dwrite(dset2,tid2,sid2,filespace,H5P_DEFAULT, &wdata2);
|
||||
CHECK(ret, FAIL, "H5Dwrite");
|
||||
|
||||
/* Read dataset from disk */
|
||||
ret=H5Dread(dset2,tid2,H5S_ALL,H5S_ALL,H5P_DEFAULT,rdata2);
|
||||
CHECK(ret, FAIL, "H5Dread");
|
||||
|
||||
/* Compare data read in */
|
||||
for(i=0; i<SPACE1_DIM1; i++) {
|
||||
if(wdata2[i].v.len!=rdata2[i].v.len) {
|
||||
TestErrPrintf("%d: VL data length don't match!, wdata2[%d].v.len=%d, rdata2[%d].v.len=%d\n",__LINE__,(int)i,(int)wdata2[i].v.len,(int)i,(int)rdata2[i].v.len);
|
||||
continue;
|
||||
} /* end if */
|
||||
|
||||
for(t1=wdata2[i].v.p, t2=rdata2[i].v.p, j=0; j<rdata2[i].v.len; j++, t1++, t2++) {
|
||||
if( strcmp(t1->string, t2->string) ) {
|
||||
TestErrPrintf("VL data values don't match!, t1->string=%s, t2->string=%s\n",t1->string, t2->string);
|
||||
continue;
|
||||
} /* end if */
|
||||
if(t1->color != t2->color) {
|
||||
TestErrPrintf("VL data values don't match!, t1->color=%d, t2->color=%d\n",t1->color, t2->color);
|
||||
continue;
|
||||
} /* end if */
|
||||
} /* end for */
|
||||
} /* end for */
|
||||
|
||||
/* Reclaim the write VL data */
|
||||
ret=H5Dvlen_reclaim(tid2,sid1,H5P_DEFAULT,wdata2);
|
||||
CHECK(ret, FAIL, "H5Dvlen_reclaim");
|
||||
|
||||
/* Reclaim the VL data */
|
||||
ret=H5Dvlen_reclaim(tid2,sid1,H5P_DEFAULT,rdata2);
|
||||
CHECK(ret, FAIL, "H5Dvlen_reclaim");
|
||||
|
||||
/* Close Dataset */
|
||||
ret = H5Dclose(dataset);
|
||||
CHECK(ret, FAIL, "H5Dclose");
|
||||
|
||||
ret = H5Dclose(dset2);
|
||||
CHECK(ret, FAIL, "H5Dclose");
|
||||
|
||||
/* Close disk dataspace */
|
||||
ret = H5Sclose(sid1);
|
||||
CHECK(ret, FAIL, "H5Sclose");
|
||||
|
||||
/* Close disk dataspace */
|
||||
ret = H5Sclose(sid2);
|
||||
CHECK(ret, FAIL, "H5Sclose");
|
||||
|
||||
/* Close disk dataspace */
|
||||
ret = H5Sclose(filespace);
|
||||
CHECK(ret, FAIL, "H5Sclose");
|
||||
|
||||
/* Close datatype */
|
||||
ret = H5Tclose(tid4);
|
||||
CHECK(ret, FAIL, "H5Tclose");
|
||||
|
||||
/* Close datatype */
|
||||
ret = H5Tclose(tid5);
|
||||
CHECK(ret, FAIL, "H5Tclose");
|
||||
|
||||
/* Close datatype */
|
||||
ret = H5Tclose(tid3);
|
||||
CHECK(ret, FAIL, "H5Tclose");
|
||||
|
||||
/* Close datatype */
|
||||
ret = H5Tclose(tid2);
|
||||
CHECK(ret, FAIL, "H5Tclose");
|
||||
|
||||
/* Close datatype */
|
||||
ret = H5Tclose(tid1);
|
||||
CHECK(ret, FAIL, "H5Tclose");
|
||||
|
||||
/* Close Property list */
|
||||
ret = H5Pclose(cparms);
|
||||
CHECK(ret, FAIL, "H5Pclose");
|
||||
|
||||
/* Close file */
|
||||
ret = H5Fclose(fid1);
|
||||
CHECK(ret, FAIL, "H5Fclose");
|
||||
} /* end test_vltypes_compound_vlstr() */
|
||||
|
||||
/****************************************************************
|
||||
**
|
||||
** test_vltypes_compound_vlen_atomic(): Test basic VL datatype code.
|
||||
@ -2113,6 +2399,7 @@ test_vltypes(void)
|
||||
rewrite_longer_vltypes_vlen_vlen_atomic(); /*overwrite with VL data of longer sequence*/
|
||||
rewrite_shorter_vltypes_vlen_vlen_atomic(); /*overwrite with VL data of shorted sequence*/
|
||||
test_vltypes_compound_vlen_vlen(); /* Test compound datatypes with VL atomic components */
|
||||
test_vltypes_compound_vlstr(); /* Test VL of compound datatype with VL string components */
|
||||
} /* test_vltypes() */
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user