|
| 1 | +program test_attributes |
| 2 | + |
| 3 | +use, intrinsic:: iso_fortran_env, only: int32, real32, real64, stderr=>error_unit |
| 4 | + |
| 5 | +use h5fortran, only: hdf5_file |
| 6 | +use mpi, only : mpi_init, MPI_COMM_WORLD, mpi_comm_rank |
| 7 | + |
| 8 | +implicit none (type, external) |
| 9 | + |
| 10 | +external :: mpi_finalize |
| 11 | + |
| 12 | +character(*), parameter :: filename = 'test_attr.h5' |
| 13 | + |
| 14 | +integer :: ierr, mpi_id |
| 15 | + |
| 16 | +call mpi_init(ierr) |
| 17 | +if (ierr /= 0) error stop "mpi_init" |
| 18 | + |
| 19 | +call mpi_comm_rank(MPI_COMM_WORLD, mpi_id, ierr) |
| 20 | +if (ierr /= 0) error stop "mpi_comm_rank" |
| 21 | + |
| 22 | + |
| 23 | +call test_write_attributes(filename) |
| 24 | +if(mpi_id == 0) print *,'PASSED: HDF5 write attributes' |
| 25 | + |
| 26 | +call test_read_attributes(filename) |
| 27 | +if(mpi_id == 0) print *, 'PASSED: HDF5 read attributes' |
| 28 | + |
| 29 | +call mpi_finalize(ierr) |
| 30 | +if (ierr /= 0) error stop "mpi_finalize" |
| 31 | + |
| 32 | + |
| 33 | +contains |
| 34 | + |
| 35 | +subroutine test_write_attributes(path) |
| 36 | + |
| 37 | +type(hdf5_file) :: h |
| 38 | +character(*), intent(in) :: path |
| 39 | + |
| 40 | +integer :: i2(1,1), i3(1,1,1), i4(1,1,1,1), i5(1,1,1,1,1), i6(1,1,1,1,1,1), i7(1,1,1,1,1,1,1) |
| 41 | + |
| 42 | +call h%open(path, action='w', mpi=.true.) |
| 43 | + |
| 44 | +call h%write('/x', 1) |
| 45 | + |
| 46 | +call h%writeattr('/x', 'int32-scalar', 42) |
| 47 | +call h%writeattr('/x', 'char','this is just a little number') |
| 48 | +call h%writeattr('/x', 'hello', 'hi') |
| 49 | +call h%writeattr('/x', 'real32_1d', [real(real32) :: 42, 84]) |
| 50 | +call h%writeattr('/x', 'real64_1d0', [42._real64]) |
| 51 | + |
| 52 | +call h%writeattr('/x', 'i2', i2) |
| 53 | +call h%writeattr('/x', 'i3', i3) |
| 54 | +call h%writeattr('/x', 'i4', i4) |
| 55 | +call h%writeattr('/x', 'i5', i5) |
| 56 | +call h%writeattr('/x', 'i6', i6) |
| 57 | +call h%writeattr('/x', 'i7', i7) |
| 58 | + |
| 59 | +call h%close() |
| 60 | + |
| 61 | +call h%open(path, action='a', mpi=.true.) |
| 62 | +call h%writeattr('/x', 'int32-scalar', 142) |
| 63 | +call h%writeattr('/x', 'real32_1d', [real(real32) :: 142, 84]) |
| 64 | +call h%writeattr('/x', 'char', 'overwrite attrs') |
| 65 | +call h%delete_attr('/x', 'hello') |
| 66 | +call h%close() |
| 67 | + |
| 68 | +end subroutine test_write_attributes |
| 69 | + |
| 70 | + |
| 71 | +subroutine test_read_attributes(path) |
| 72 | + |
| 73 | +type(hdf5_file) :: h |
| 74 | +character(*), intent(in) :: path |
| 75 | +character(1024) :: attr_str |
| 76 | +integer :: int32_0 |
| 77 | +real(real32) :: attr32(2) |
| 78 | +real(real64) :: attr64 |
| 79 | + |
| 80 | +integer :: x |
| 81 | + |
| 82 | +integer :: i2(1,1), i3(1,1,1), i4(1,1,1,1), i5(1,1,1,1,1), i6(1,1,1,1,1,1), i7(1,1,1,1,1,1,1) |
| 83 | + |
| 84 | +call h%open(path, action='r', mpi=.true.) |
| 85 | + |
| 86 | +call h%read('/x', x) |
| 87 | +if (x/=1) error stop 'readattr: unexpected value' |
| 88 | + |
| 89 | +call h%readattr('/x', 'char', attr_str) |
| 90 | +if (attr_str /= 'overwrite attrs') error stop 'overwrite attrs failed: ' // attr_str |
| 91 | + |
| 92 | +call h%readattr('/x', 'int32-scalar', int32_0) |
| 93 | +if (int32_0 /= 142) error stop 'readattr: int32-scalar' |
| 94 | + |
| 95 | +call h%readattr('/x', 'real32_1d', attr32) |
| 96 | +if (any(attr32 /= [real(real32) :: 142, 84])) error stop 'readattr: real32' |
| 97 | + |
| 98 | +call h%readattr('/x', 'real64_1d0', attr64) |
| 99 | +if (attr64 /= 42._real64) error stop 'readattr: real64' |
| 100 | + |
| 101 | +if (h%exist_attr('/x', 'hello')) error stop "delete attr failed" |
| 102 | + |
| 103 | +call h%readattr('/x', 'i2', i2) |
| 104 | +call h%readattr('/x', 'i3', i3) |
| 105 | +call h%readattr('/x', 'i4', i4) |
| 106 | +call h%readattr('/x', 'i5', i5) |
| 107 | +call h%readattr('/x', 'i6', i6) |
| 108 | +call h%readattr('/x', 'i7', i7) |
| 109 | + |
| 110 | +call h%close() |
| 111 | + |
| 112 | +end subroutine test_read_attributes |
| 113 | + |
| 114 | +end program |
0 commit comments