Skip to content

Commit 1ef48ba

Browse files
committed
add no mpi cast test
1 parent 1ba3719 commit 1ef48ba

File tree

4 files changed

+125
-30
lines changed

4 files changed

+125
-30
lines changed

test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ cmake_path(SET string_file ${CMAKE_CURRENT_BINARY_DIR}/test_string_py.h5)
5656

5757
# --- non-MPI tests
5858

59-
set(nompi_tests array attributes)
59+
set(nompi_tests array attributes cast)
6060

6161
nompi_test("${nompi_tests}")
6262

@@ -80,7 +80,7 @@ endif()
8080

8181
# --- MPI tests
8282

83-
set(mpi_tests array_mpi attributes_mpi cast destructor exist fill groups layout shape
83+
set(mpi_tests array_mpi attributes_mpi cast_mpi destructor exist fill groups layout shape
8484
string string_read write
8585
)
8686

test/test_array.f90

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ subroutine test_basic_array(filename)
5252
r1 = i1
5353
r2 = i2
5454

55-
call h%open(filename, action='w', comp_lvl=1, mpi=.false.)
55+
call h%open(filename, action='w', comp_lvl=1)
5656

5757
call h%write('/int32-1d', i1)
5858
call h%write('/test/group2/int32-2d', i2)
@@ -62,7 +62,7 @@ subroutine test_basic_array(filename)
6262
call h%close()
6363

6464
!! read
65-
call h%open(filename, action='r', mpi=.false.)
65+
call h%open(filename, action='r')
6666

6767
!> int32
6868
call h%read('/int32-1d', i1t)
@@ -112,7 +112,7 @@ subroutine test_read_slice(filename)
112112
i2(i,:) = i2(1,:) * i
113113
enddo
114114

115-
call h%open(filename, action='r', mpi=.false.)
115+
call h%open(filename, action='r')
116116

117117
i1t = 0
118118
call h%read('/int32-1d', i1t(:2), istart=[2], iend=[3], stride=[1])
@@ -151,7 +151,7 @@ subroutine test_write_slice(filename)
151151

152152
dims = [3]
153153

154-
call h%open(filename, action='r+', debug=.true., mpi=.false.)
154+
call h%open(filename, action='r+', debug=.true.)
155155

156156
call h%create('/int32a-1d', dtype=H5T_NATIVE_INTEGER, dset_dims=dims)
157157
call h%write('/int32a-1d', [1,3], dset_dims=dims, istart=[1], iend=[2])
@@ -203,7 +203,7 @@ subroutine test_readwrite_array(filename, ng, nn, pn)
203203
flux = 1.0
204204
write(pnc,'(I2)') pn
205205

206-
call h%open(filename, action='rw', mpi=.false.)
206+
call h%open(filename, action='rw')
207207

208208
do i = 1,ng
209209
write(ic,'(I2)') i

test/test_cast.f90

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,19 @@ program test_cast
22
!! test HDF5 built-in casting
33

44
use h5fortran, 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-
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
107
use, intrinsic :: iso_fortran_env, only : real32, real64, int32, int64
118

129
implicit none (type, external)
1310

14-
external :: mpi_finalize
15-
1611
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-
2512

2613
call test_cast_write(fn)
27-
if(mpi_id == 0) print "(A)", "OK: cast write"
14+
print "(A)", "OK: cast write"
2815

2916
call test_cast_read(fn)
30-
if(mpi_id == 0) print "(A)", "OK: cast read"
31-
32-
33-
call mpi_finalize(ierr)
34-
if (ierr /= 0) error stop "mpi_finalize"
17+
print "(A)", "OK: cast read"
3518

3619

3720
contains
@@ -43,7 +26,7 @@ subroutine test_cast_write(fn)
4326

4427
type(hdf5_file) :: h
4528

46-
call h%open(fn, action='w', mpi=.true.)
29+
call h%open(fn, action='w')
4730

4831
!> test values
4932
call h%write('/scalar_int32', 42_int32)
@@ -70,7 +53,7 @@ subroutine test_cast_read(fn)
7053
integer(int32) :: i32
7154
integer(int64) :: i64, i1_64(2)
7255

73-
call h%open(fn, action='r', mpi=.true.)
56+
call h%open(fn, action='r')
7457

7558
!> %class method
7659
if (h%class("/scalar_int32") /= H5T_INTEGER_F) error stop "int32 not integer"
@@ -95,6 +78,7 @@ subroutine test_cast_read(fn)
9578
if(i64 /= 42) error stop 'scalar cast int32 => int64'
9679
call h%read('/scalar_int64', i32)
9780
if(i32 /= 42) error stop 'scalar cast int64 => int32'
81+
print *, 'PASSED: scalar cast on read'
9882

9983
!> 1D vector read casting -- real to int and int to real
10084
call h%read('/1d_real32', r1_64)
@@ -104,6 +88,8 @@ subroutine test_cast_read(fn)
10488

10589
call h%close()
10690

91+
print "(A)", "OK: cast"
92+
10793
end subroutine test_cast_read
10894

10995
end program

test/test_cast_mpi.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 h5fortran, 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+
26+
call test_cast_write(fn)
27+
if(mpi_id == 0) print "(A)", "OK: cast write"
28+
29+
call test_cast_read(fn)
30+
if(mpi_id == 0) print "(A)", "OK: cast read"
31+
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=.true.)
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

0 commit comments

Comments
 (0)