|
| 1 | +program test_string |
| 2 | + |
| 3 | +use, intrinsic:: iso_fortran_env, only: stderr=>error_unit |
| 4 | + |
| 5 | +use mpi, only : mpi_init, MPI_COMM_WORLD, mpi_comm_rank |
| 6 | + |
| 7 | +use h5mpi, only : hdf5_file |
| 8 | + |
| 9 | +implicit none (type, external) |
| 10 | + |
| 11 | +external :: mpi_finalize |
| 12 | + |
| 13 | +character(*), parameter :: fn='test_string.h5' |
| 14 | + |
| 15 | +integer :: ierr, mpi_id |
| 16 | + |
| 17 | +call mpi_init(ierr) |
| 18 | +if (ierr /= 0) error stop "mpi_init" |
| 19 | + |
| 20 | +call mpi_comm_rank(MPI_COMM_WORLD, mpi_id, ierr) |
| 21 | +if (ierr /= 0) error stop "mpi_comm_rank" |
| 22 | + |
| 23 | +if(mpi_id == 0) then |
| 24 | + call test_write(fn) |
| 25 | + print *, "OK: HDF5 string write" |
| 26 | +endif |
| 27 | + |
| 28 | +call test_read(fn) |
| 29 | +if(mpi_id == 0) print *,'OK: HDF5 string read' |
| 30 | + |
| 31 | +call mpi_finalize(ierr) |
| 32 | +if (ierr /= 0) error stop "mpi_finalize" |
| 33 | + |
| 34 | +contains |
| 35 | + |
| 36 | + |
| 37 | +subroutine test_write(fn) |
| 38 | + |
| 39 | +character(*), intent(in) :: fn |
| 40 | + |
| 41 | +type(hdf5_file) :: h |
| 42 | + |
| 43 | +call h%open(fn, action='w', mpi=.false.) |
| 44 | + |
| 45 | +call h%write('/little', '42') |
| 46 | +call h%write('/MySentence', 'this is a little sentence.') |
| 47 | + |
| 48 | +call h%close() |
| 49 | + |
| 50 | +end subroutine test_write |
| 51 | + |
| 52 | + |
| 53 | +subroutine test_read(fn) |
| 54 | + |
| 55 | +character(*), intent(in) :: fn |
| 56 | + |
| 57 | +type(hdf5_file) :: h |
| 58 | +character(2) :: value |
| 59 | +character(1024) :: val1k |
| 60 | + |
| 61 | +call h%open(fn, action='r', mpi=.true.) |
| 62 | +call h%read('/little', value) |
| 63 | + |
| 64 | +if (value /= '42') error stop 'test_string: read/write verification failure. Value: '// value |
| 65 | + |
| 66 | +!> longer character than data |
| 67 | +call h%read('/little', val1k) |
| 68 | + |
| 69 | +if (len_trim(val1k) /= 2) then |
| 70 | + write(stderr, *) 'expected character len_trim 2 but got len_trim() = ', len_trim(val1k) |
| 71 | + error stop |
| 72 | +endif |
| 73 | + |
| 74 | +call h%close() |
| 75 | + |
| 76 | +end subroutine test_read |
| 77 | + |
| 78 | +end program |
0 commit comments