1!****h* root/fortran/test/tH5I.f90 2! 3! NAME 4! tH5I.f90 5! 6! FUNCTION 7! Basic testing of Fortran H5I APIs. 8! 9! COPYRIGHT 10! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 11! Copyright by The HDF Group. * 12! Copyright by the Board of Trustees of the University of Illinois. * 13! All rights reserved. * 14! * 15! This file is part of HDF5. The full HDF5 copyright notice, including * 16! terms governing use, modification, and redistribution, is contained in * 17! the COPYING file, which can be found at the root of the source code * 18! distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. * 19! If you do not have access to either file, you may request a copy from * 20! help@hdfgroup.org. * 21! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 22! 23! CONTAINS SUBROUTINES 24! identifier_test 25! 26!***** 27MODULE TH5I 28 29CONTAINS 30 31 SUBROUTINE identifier_test(cleanup, total_error) 32 33! This subroutine tests following functionalities: h5iget_type_f 34 35 USE HDF5 ! This module contains all necessary modules 36 USE TH5_MISC 37 38 IMPLICIT NONE 39 LOGICAL, INTENT(IN) :: cleanup 40 INTEGER, INTENT(INOUT) :: total_error 41 42 CHARACTER(LEN=6), PARAMETER :: filename = "itestf" ! File name 43 CHARACTER(LEN=80) :: fix_filename 44 CHARACTER(LEN=10), PARAMETER :: dsetname = "/itestdset" ! Dataset name 45 CHARACTER(LEN=10), PARAMETER :: groupname = "itestgroup"! group name 46 CHARACTER(LEN=10), PARAMETER :: aname = "itestattr"! group name 47 48 49 50 INTEGER(HID_T) :: file_id ! File identifier 51 INTEGER(HID_T) :: new_file_id ! File identifier 52 INTEGER(HID_T) :: group_id ! group identifier 53 INTEGER(HID_T) :: dset_id ! Dataset identifier 54 INTEGER(HID_T) :: dspace_id ! Dataspace identifier 55 INTEGER(HID_T) :: attr_id ! Datatype attribute identifier 56 INTEGER(HID_T) :: aspace_id ! attribute data space identifier 57 INTEGER(HID_T) :: atype_id ! attribute data type identifier 58 59 60 INTEGER, DIMENSION(1) :: dset_data = 0 ! Data value 61 62 INTEGER(HSIZE_T), DIMENSION(1) :: dims = 1 ! Datasets dimensions 63 INTEGER(HSIZE_T), DIMENSION(1) :: adims = 1 ! Attribute dimensions 64 65 INTEGER, DIMENSION(1) :: attr_data = 12 66 INTEGER :: rank = 1 ! Datasets rank 67 INTEGER :: arank = 1 ! Attribute rank 68 69 INTEGER :: type !object identifier 70 INTEGER :: error ! Error flag 71 INTEGER(HSIZE_T), DIMENSION(2) :: data_dims 72 CHARACTER(LEN=80) name_buf 73 CHARACTER(LEN=280) name_buf1 74 INTEGER(SIZE_T) buf_size 75 INTEGER(SIZE_T) name_size 76 INTEGER :: ref_count ! Reference count for IDs 77 78 79 INTEGER(hid_t) :: dtype ! datatype id 80 LOGICAL :: tri_ret ! value 81 82 ! 83 ! Tests the function H5Iis_valid_f 84 ! 85 ! check that the ID is not valid 86 dtype = -1 87 CALL H5Iis_valid_f(dtype, tri_ret, error) 88 CALL check("H5Iis_valid_f", error, total_error) 89 CALL VerifyLogical("H5Iis_valid_f", tri_ret, .FALSE., total_error) 90 91 ! Create a datatype id 92 CALL H5Tcopy_f(H5T_NATIVE_INTEGER,dtype,error) 93 CALL check("H5Tcopy_f", error, total_error) 94 95 ! Check that the ID is valid 96 CALL H5Iis_valid_f(dtype, tri_ret, error) 97 CALL check("H5Iis_valid_f", error, total_error) 98 CALL VerifyLogical("H5Tequal_f", tri_ret, .TRUE., total_error) 99 100 CALL H5Tclose_f(dtype, error) 101 CALL check("H5Tclose_f", error, total_error) 102 103 ! 104 ! Create a new file using default properties. 105 ! 106 CALL h5_fixname_f(filename, fix_filename, H5P_DEFAULT_F, error) 107 if (error .ne. 0) then 108 write(*,*) "Cannot modify filename" 109 stop 110 endif 111 CALL h5fcreate_f(fix_filename, H5F_ACC_TRUNC_F, file_id, error) 112 CALL check("h5fcreate_f",error,total_error) 113 114 ! 115 ! Create a group named "/MyGroup" in the file. 116 ! 117 CALL h5gcreate_f(file_id, groupname, group_id, error) 118 CALL check("h5gcreate_f",error,total_error) 119 120 ! 121 !Create data space for the dataset. 122 ! 123 CALL h5screate_simple_f(rank, dims, dspace_id, error) 124 CALL check("h5screate_simple_f",error,total_error) 125 126 ! 127 ! create dataset in the file. 128 ! 129 CALL h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, dspace_id, & 130 dset_id, error) 131 CALL check("h5dcreate_f",error,total_error) 132 ! 133 ! Get dataset name from dataset identifier 134 ! 135 buf_size = 80 136 CALL h5iget_name_f(dset_id, name_buf, buf_size, name_size, error) 137 CALL check("h5iget_name_f",error,total_error) 138 if (name_size .ne. len(dsetname)) then 139 write(*,*) "h5iget_name returned wrong name size" 140 total_error = total_error + 1 141 else 142 if (name_buf(1:name_size) .ne. dsetname) then 143 write(*,*) "h5iget_name returned wrong name" 144 total_error = total_error + 1 145 endif 146 endif 147 148 ! 149 ! Get file identifier from dataset identifier and then get file name 150 ! 151 CALL h5iget_file_id_f(dset_id, new_file_id, error) 152 CALL check("h5iget_file_id_f",error,total_error) 153 name_size = 280 154 CALL h5fget_name_f(new_file_id, name_buf1, name_size, error) 155 CALL check("h5fget_name_f",error,total_error) 156 if (name_buf1(1:name_size) .ne. fix_filename(1:name_size)) then 157 write(*,*) "h5fget_name returned wrong file name" 158 total_error = total_error + 1 159 endif 160 161 ! 162 ! Write data_in to the dataset 163 ! 164 data_dims(1) = 1 165 CALL h5dwrite_f(dset_id, H5T_NATIVE_INTEGER, dset_data, data_dims, error) 166 CALL check("h5dwrite_f",error,total_error) 167 168 ! 169 ! Create scalar data space for dataset attribute. 170 ! 171 CALL h5screate_simple_f(arank, adims, aspace_id, error) 172 CALL check("h5screate_simple_f",error,total_error) 173 174 ! 175 ! Create datatype for the Integer attribute. 176 ! 177 CALL h5tcopy_f(H5T_NATIVE_INTEGER, atype_id, error) 178 CALL check("h5tcopy_f",error,total_error) 179 180 ! 181 ! Create dataset INTEGER attribute. 182 ! 183 CALL h5acreate_f(dset_id, aname, atype_id, aspace_id, attr_id, error) 184 CALL check("h5acreate_f",error,total_error) 185 186 ! 187 ! Write the Integer attribute data. 188 ! 189 CALL h5awrite_f(attr_id, atype_id, attr_data, data_dims, error) 190 CALL check("h5awrite_f",error,total_error) 191 192 ! 193 !Get the file identifier 194 ! 195 CALL h5iget_type_f(file_id, type, error) 196 CALL check("h5iget_type_f",error,total_error) 197 CALL verify("get file identifier wrong",type,H5I_FILE_F,total_error) 198 199 ! 200 !Get the group identifier 201 ! 202 CALL h5iget_type_f(group_id, type, error) 203 CALL check("h5iget_type_f",error,total_error) 204 CALL verify("get group identifier wrong",type,H5I_GROUP_F,total_error) 205 206 ! 207 !Get the datatype identifier 208 ! 209 CALL h5iget_type_f(atype_id, type, error) 210 CALL check("h5iget_type_f",error,total_error) 211 CALL verify("get datatype identifier wrong",type,H5I_DATATYPE_F,total_error) 212 213 ! 214 !Get the dataspace identifier 215 ! 216 CALL h5iget_type_f(aspace_id, type, error) 217 CALL check("h5iget_type_f",error,total_error) 218 CALL verify("get dataspace identifier wrong",type,H5I_DATASPACE_F,total_error) 219 220 ! 221 !Get the dataset identifier 222 ! 223 CALL h5iget_type_f(dset_id, type, error) 224 CALL check("h5iget_type_f",error,total_error) 225 CALL verify("get dataset identifier wrong",type,H5I_DATASET_F,total_error) 226 227 ! 228 !Get the attribute identifier 229 ! 230 CALL h5iget_type_f(attr_id, type, error) 231 CALL check("h5iget_type_f",error,total_error) 232 CALL verify("get attribute identifier wrong",type,H5I_ATTR_F,total_error) 233 234 ! 235 ! Close the attribute. 236 ! 237 CALL h5aclose_f(attr_id, error) 238 CALL check("h5aclose_f",error,total_error) 239 ! 240 ! Close the dataspace. 241 ! 242 CALL h5sclose_f(aspace_id, error) 243 CALL check("h5sclose_f",error,total_error) 244 CALL h5sclose_f(dspace_id, error) 245 CALL check("h5sclose_f",error,total_error) 246 ! 247 ! Close the dataype. 248 ! 249 CALL h5tclose_f(atype_id, error) 250 CALL check("h5tclose_f",error,total_error) 251 252 ! 253 ! Close the dataset. 254 ! 255 CALL h5dclose_f(dset_id, error) 256 CALL check("h5dclose_f",error,total_error) 257 258 ! 259 ! Close the group. 260 ! 261 CALL h5gclose_f(group_id, error) 262 CALL check("h5gclose_f",error,total_error) 263 264 ! 265 ! Close the file. 266 ! 267 CALL h5fclose_f(file_id, error) 268 CALL check("h5fclose_f",error,total_error) 269 CALL h5fclose_f(new_file_id, error) 270 CALL check("h5fclose_f",error,total_error) 271 272 ! 273 ! Basic Test of increment/decrement ID functions 274 ! 275 276 ! Create a file 277 CALL h5fcreate_f(fix_filename, H5F_ACC_TRUNC_F, file_id, error) 278 CALL check("h5fcreate_f",error,total_error) 279 280 ! Get the reference count for the file ID 281 CALL h5iget_ref_f(file_id, ref_count, error) 282 CALL check("h5iget_ref_f",error,total_error) 283 CALL verify("get file ref count wrong",ref_count,1,total_error) 284 285 ! Increment the reference count for the file ID 286 CALL h5iinc_ref_f(file_id, ref_count, error) 287 CALL check("h5iinc_ref_f",error,total_error) 288 CALL verify("get file ref count wrong",ref_count,2,total_error) 289 290 ! Close the file normally. 291 CALL h5fclose_f(file_id, error) 292 CALL check("h5fclose_f",error,total_error) 293 294 ! Get the reference count for the file ID 295 CALL h5iget_ref_f(file_id, ref_count, error) 296 CALL check("h5iget_ref_f",error,total_error) 297 CALL verify("get file ref count wrong",ref_count,1,total_error) 298 299 ! Close the file by decrementing the reference count 300 CALL h5idec_ref_f(file_id, ref_count, error) 301 CALL check("h5idec_ref_f",error,total_error) 302 CALL verify("get file ref count wrong",ref_count,0,total_error) 303 ! Try closing the file again (should fail) 304 CALL h5eset_auto_f(0, error) 305 CALL h5fclose_f(file_id, error) 306 CALL verify("file close should fail",error,-1,total_error) 307 ! Clear the error stack from the file close failure 308 CALL h5eset_auto_f(1, error) 309 CALL h5eclear_f(error) 310 311 if(cleanup) CALL h5_cleanup_f(filename, H5P_DEFAULT_F, error) 312 CALL check("h5_cleanup_f", error, total_error) 313 314 RETURN 315 END SUBROUTINE identifier_test 316 317END MODULE TH5I 318