Skip to content

Commit 578ed7b

Browse files
committed
add test cast
1 parent cf15fe4 commit 578ed7b

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

src/interface.f90

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ module h5mpi
8484
private
8585
public :: mpi_h5comm, hdf5_file, mpi_tags, has_parallel_compression, is_hdf5, &
8686
check, hdf_rank_check, hdf_shape_check, mpi_collective, mpi_hyperslab, &
87-
hdf5version, HSIZE_T
87+
hdf5version, &
88+
HSIZE_T, &
89+
H5T_INTEGER_F, H5T_FLOAT_F, H5T_STRING_F, &
90+
H5T_NATIVE_REAL, H5T_NATIVE_DOUBLE, H5T_NATIVE_INTEGER, H5T_NATIVE_CHARACTER, H5T_STD_I64LE
8891

8992
interface !< write.f90
9093
module subroutine hdf_create(self, dname, dtype, mem_dims, dset_dims, filespace, memspace, dset_id, &

src/tests/unit/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ max_gcd(${MPI_MAX} ${MPIEXEC_MAX_NUMPROCS} Nmpi)
55

66
message(STATUS "Unit tests using ${Nmpi} processes")
77

8-
set(test_names deflate_write deflate_props deflate_read destructor layout shape)
8+
set(test_names cast deflate_write deflate_props deflate_read destructor layout shape)
99

1010
foreach(t IN LISTS test_names)
1111

src/tests/unit/test_cast.f90

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
program test_cast
2+
!! test HDF5 built-in casting
3+
4+
use h5mpi, only : hdf5_file, &
5+
H5T_INTEGER_F, H5T_FLOAT_F, H5T_STRING_F, &
6+
H5T_NATIVE_REAL, H5T_NATIVE_DOUBLE, H5T_NATIVE_INTEGER, H5T_NATIVE_CHARACTER, H5T_STD_I64LE
7+
8+
use mpi, only : mpi_init, MPI_COMM_WORLD, mpi_comm_rank
9+
10+
use, intrinsic :: iso_fortran_env, only : real32, real64, int32, int64
11+
12+
implicit none (type, external)
13+
14+
external :: mpi_finalize
15+
16+
character(*), parameter :: fn = 'test_cast.h5'
17+
integer :: ierr, mpi_id
18+
19+
call mpi_init(ierr)
20+
if (ierr /= 0) error stop "mpi_init"
21+
22+
call mpi_comm_rank(MPI_COMM_WORLD, mpi_id, ierr)
23+
if (ierr /= 0) error stop "mpi_comm_rank"
24+
25+
if(mpi_id == 0) then
26+
call test_cast_write(fn)
27+
print "(A)", "OK: cast write"
28+
endif
29+
30+
call test_cast_read(fn)
31+
if(mpi_id == 0) print "(A)", "OK: cast read"
32+
33+
call mpi_finalize(ierr)
34+
if (ierr /= 0) error stop "mpi_finalize"
35+
36+
37+
contains
38+
39+
40+
subroutine test_cast_write(fn)
41+
42+
character(*), intent(in) :: fn
43+
44+
type(hdf5_file) :: h
45+
46+
call h%open(fn, action='w', mpi=.false.)
47+
48+
!> test values
49+
call h%write('/scalar_int32', 42_int32)
50+
call h%write('/scalar_int64', 42_int64)
51+
call h%write('/scalar_real32', 42._real32)
52+
call h%write('/scalar_real64', 42._real64)
53+
call h%write('/1d_real32', [1._real32, 32._real32])
54+
call h%write('/1d_int32', [2_int32, 4_int32])
55+
call h%write('/char', "hello")
56+
57+
call h%close()
58+
59+
end subroutine test_cast_write
60+
61+
62+
subroutine test_cast_read(fn)
63+
64+
character(*), intent(in) :: fn
65+
66+
type(hdf5_file) :: h
67+
68+
real(real64) :: r64, r1_64(2)
69+
real(real32) :: r32
70+
integer(int32) :: i32
71+
integer(int64) :: i64, i1_64(2)
72+
73+
call h%open(fn, action='r', mpi=.true.)
74+
75+
!> %class method
76+
if (h%class("/scalar_int32") /= H5T_INTEGER_F) error stop "int32 not integer"
77+
if (h%class("/scalar_int64") /= H5T_INTEGER_F) error stop "int64 not integer"
78+
if (h%class("/scalar_real32") /= H5T_FLOAT_F) error stop "real32 not float"
79+
if (h%class("/scalar_real64") /= H5T_FLOAT_F) error stop "real64 not float"
80+
if (h%class("/char") /= H5T_STRING_F) error stop "char not string"
81+
82+
!> %dtype method
83+
if (h%dtype('/scalar_int32') /= H5T_NATIVE_INTEGER) error stop "int32 type"
84+
if (h%dtype("/scalar_int64") /= H5T_STD_I64LE) error stop "int64 type"
85+
if (h%dtype("/scalar_real32") /= H5T_NATIVE_REAL) error stop "real32 type"
86+
if (h%dtype("/scalar_real64") /= H5T_NATIVE_DOUBLE) error stop "real64 type"
87+
if (h%dtype("/char") /= H5T_NATIVE_CHARACTER) error stop "char type"
88+
89+
!> read casting -- real32 to real64 and int32 to int64
90+
call h%read('/scalar_real32', r64)
91+
if(r64 /= 42) error stop 'scalar cast real32 => real64'
92+
call h%read('/scalar_real64', r32)
93+
if(r32 /= 42) error stop 'scalar cast real64 => real32'
94+
call h%read('/scalar_int32', i64)
95+
if(i64 /= 42) error stop 'scalar cast int32 => int64'
96+
call h%read('/scalar_int64', i32)
97+
if(i32 /= 42) error stop 'scalar cast int64 => int32'
98+
99+
!> 1D vector read casting -- real to int and int to real
100+
call h%read('/1d_real32', r1_64)
101+
if (.not.all([1., 32.] == r1_64)) error stop '1D cast real32 => real64'
102+
call h%read('/1d_int32', i1_64)
103+
if (.not.all([2, 4] == i1_64)) error stop '1D cast int32 => int64'
104+
105+
call h%close()
106+
107+
end subroutine test_cast_read
108+
109+
end program

src/tests/unit/test_layout.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ program test_layout
55
use h5mpi, only : hdf5_file
66

77
use hdf5, only : H5D_COMPACT_F
8-
use mpi, only : mpi_init, MPI_COMM_WORLD, mpi_comm_rank, mpi_barrier
8+
use mpi, only : mpi_init, MPI_COMM_WORLD, mpi_comm_rank
99

1010
implicit none (type, external)
1111

0 commit comments

Comments
 (0)