/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html. If you do not have * * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /*********************************************************** * * Test program: tattr * * Test the attribute functionality * *************************************************************/ #include "testhdf5.h" #include "h5test.h" #include "hdf5.h" #define FILENAME "tattr.h5" #define ATTR_NAME_LEN 16 #define ATTR_MAX_DIMS 7 #define ATTR_TMP_NAME "temp_name" /* 3-D dataset with fixed dimensions */ #define SPACE1_NAME "Space1" #define SPACE1_RANK 3 #define SPACE1_DIM1 3 #define SPACE1_DIM2 15 #define SPACE1_DIM3 13 /* Dataset Information */ #define DSET1_NAME "Dataset1" #define DSET2_NAME "Dataset2" /* Group Information */ #define GROUP1_NAME "/Group1" /* Named Datatype Information */ #define TYPE1_NAME "/Type" /* Attribute Rank & Dimensions */ #define ATTR1_NAME "Attr1" #define ATTR1_RANK 1 #define ATTR1_DIM1 3 int attr_data1[ATTR1_DIM1]={512,-234,98123}; /* Test data for 1st attribute */ /* rank & dimensions for another attribute */ #define ATTR1A_NAME "Attr1_a" int attr_data1a[ATTR1_DIM1]={256,11945,-22107}; #define ATTR2_NAME "Attr2" #define ATTR2_RANK 2 #define ATTR2_DIM1 2 #define ATTR2_DIM2 2 int attr_data2[ATTR2_DIM1][ATTR2_DIM2]={{7614,-416},{197814,-3}}; /* Test data for 2nd attribute */ #define ATTR3_NAME "Attr3" #define ATTR3_RANK 3 #define ATTR3_DIM1 2 #define ATTR3_DIM2 2 #define ATTR3_DIM3 2 double attr_data3[ATTR3_DIM1][ATTR3_DIM2][ATTR3_DIM3]={{{2.3,-26.1},{0.123,-10.0}},{{981724.2,-0.91827},{2.0,23.0}}}; /* Test data for 3rd attribute */ #define ATTR4_NAME "Attr4" #define ATTR4_RANK 2 #define ATTR4_DIM1 2 #define ATTR4_DIM2 2 #define ATTR4_FIELDNAME1 "i" #define ATTR4_FIELDNAME2 "d" #define ATTR4_FIELDNAME3 "c" size_t attr4_field1_off=0; size_t attr4_field2_off=0; size_t attr4_field3_off=0; struct attr4_struct { int i; double d; char c; } attr_data4[ATTR4_DIM1][ATTR4_DIM2]={{{3,-26.1,'d'},{-100000, 0.123,'3'}}, {{-23,981724.2,'Q'},{0,2.0,'\n'}}}; /* Test data for 4th attribute */ #define ATTR5_NAME "Attr5" #define ATTR5_RANK 0 float attr_data5=(float)-5.123; /* Test data for 5th attribute */ herr_t attr_op1(hid_t loc_id, const char *name, void *op_data); /**************************************************************** ** ** test_attr_basic_write(): Test basic H5A (attribute) code. ** Tests integer attributes on both datasets and groups ** ****************************************************************/ static void test_attr_basic_write(hid_t fapl) { hid_t fid1; /* HDF5 File IDs */ hid_t dataset; /* Dataset ID */ hid_t group; /* Group ID */ hid_t sid1,sid2; /* Dataspace ID */ hid_t attr, attr2; /* Attribute ID */ hsize_t attr_size; /* storage size for attribute */ ssize_t attr_name_size; /* size of attribute name */ char *attr_name=NULL; /* name of attribute */ hsize_t dims1[] = {SPACE1_DIM1, SPACE1_DIM2, SPACE1_DIM3}; hsize_t dims2[] = {ATTR1_DIM1}; hsize_t dims3[] = {ATTR2_DIM1,ATTR2_DIM2}; int read_data1[ATTR1_DIM1]={0}; /* Buffer for reading 1st attribute */ int i; herr_t ret; /* Generic return value */ /* Output message about test being performed */ MESSAGE(5, ("Testing Basic Scalar Attribute Writing Functions\n")); /* Create file */ fid1 = H5Fcreate(FILENAME, H5F_ACC_TRUNC, H5P_DEFAULT, fapl); CHECK(fid1, FAIL, "H5Fcreate"); /* Create dataspace for dataset */ sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL); CHECK(sid1, FAIL, "H5Screate_simple"); /* Create a dataset */ dataset=H5Dcreate(fid1,DSET1_NAME,H5T_NATIVE_UCHAR,sid1,H5P_DEFAULT); CHECK(dataset, FAIL, "H5Dcreate"); /* Create dataspace for attribute */ sid2 = H5Screate_simple(ATTR1_RANK, dims2, NULL); CHECK(sid2, FAIL, "H5Screate_simple"); /* Try to create an attribute on the file (should fail) */ ret=H5Acreate(fid1,ATTR1_NAME,H5T_NATIVE_INT,sid2,H5P_DEFAULT); VERIFY(ret, FAIL, "H5Acreate"); /* Create an attribute for the dataset */ attr=H5Acreate(dataset,ATTR1_NAME,H5T_NATIVE_INT,sid2,H5P_DEFAULT); CHECK(attr, FAIL, "H5Acreate"); /* Try to create the same attribute again (should fail) */ ret=H5Acreate(dataset,ATTR1_NAME,H5T_NATIVE_INT,sid2,H5P_DEFAULT); VERIFY(ret, FAIL, "H5Acreate"); /* Write attribute information */ ret=H5Awrite(attr,H5T_NATIVE_INT,attr_data1); CHECK(ret, FAIL, "H5Awrite"); /* Create an another attribute for the dataset */ attr2=H5Acreate(dataset,ATTR1A_NAME,H5T_NATIVE_INT,sid2,H5P_DEFAULT); CHECK(attr, FAIL, "H5Acreate"); /* Write attribute information */ ret=H5Awrite(attr2,H5T_NATIVE_INT,attr_data1a); CHECK(ret, FAIL, "H5Awrite"); /* Check storage size for attribute */ attr_size=H5Aget_storage_size(attr); VERIFY(attr_size, (ATTR1_DIM1*sizeof(int)), "H5A_get_storage_size"); /* Read attribute information immediately, without closing attribute */ ret=H5Aread(attr,H5T_NATIVE_INT,read_data1); CHECK(ret, FAIL, "H5Aread"); /* Verify values read in */ for(i=0; i0) attr_name = (char*)HDcalloc((size_t)(attr_name_size+1), sizeof(char)); ret=(herr_t)H5Aget_name(attr, (size_t)(attr_name_size+1), attr_name); CHECK(ret, FAIL, "H5Aget_name"); ret=HDstrcmp(attr_name, ATTR_TMP_NAME); VERIFY(ret, 0, "HDstrcmp"); if(attr_name) HDfree(attr_name); /* Read attribute information immediately, without closing attribute */ ret=H5Aread(attr,H5T_NATIVE_INT,read_data1); CHECK(ret, FAIL, "H5Aread"); /* Verify values read in */ for(i=0; i0) attr_name = (char*)HDcalloc((size_t)(attr_name_size+1), sizeof(char)); ret=(herr_t)H5Aget_name(attr2, (size_t)(attr_name_size+1), attr_name); CHECK(ret, FAIL, "H5Aget_name"); ret=HDstrcmp(attr_name, ATTR1A_NAME); VERIFY(ret, 0, "HDstrcmp"); if(attr_name) HDfree(attr_name); /* Read attribute information immediately, without closing attribute */ ret=H5Aread(attr2,H5T_NATIVE_INT,read_data1); CHECK(ret, FAIL, "H5Aread"); /* Verify values read in */ for(i=0; i