Skip to content

Commit 87d96ca

Browse files
committed
add more scalar tests
1 parent 39d2f2e commit 87d96ca

File tree

1 file changed

+73
-41
lines changed

1 file changed

+73
-41
lines changed

test/test_scalar.f90

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,133 @@
1-
program scalar_test
1+
program test_scalar
22

33
use, intrinsic:: iso_fortran_env, only: int64, int32, real32, real64, stderr=>error_unit
4+
45
use nc4fortran, only : netcdf_file
6+
use netcdf, only : NF90_INT, NF90_INT64
57

68
implicit none (type, external)
79

10+
character(*), parameter :: fn = 'test_scalar.nc'
11+
12+
call test_scalar_write(fn)
13+
print *, 'OK: scalar and vector: write and rewrite'
14+
15+
call test_scalar_read(fN)
16+
print *, 'OK: scalar and vector: read'
17+
18+
contains
19+
20+
subroutine test_scalar_write(fn)
21+
22+
character(*), intent(in) :: fn
23+
824
type(netcdf_file) :: h
9-
real(real32), allocatable :: rr1(:)
10-
real(real32) :: rt, r1(4)
11-
integer(int32) :: it, i1(4)
12-
integer(int32), allocatable :: i1t(:)
13-
integer, allocatable :: dims(:)
25+
26+
real(real32) :: r1(4)
27+
integer(int32) :: i1(4)
28+
integer(int64) :: i1_64(4)
29+
1430
integer :: i
15-
character(*), parameter :: fn = 'test_scalar.nc'
1631

1732
do i = 1,size(i1)
1833
i1(i) = i
1934
enddo
2035

2136
r1 = i1
37+
i1_64 = i1
2238

2339
!> write
2440
call h%open(fn, action='w')
2541
!> scalar tests
2642
call h%write('scalar_int32', 42_int32)
27-
43+
call h%write('scalar_int64', 42_int64)
2844
call h%write('scalar_real32', -1._real32)
29-
call h%write('scalar_real32', 42._real32)
45+
call h%write('scalar_real64', -1._real64)
3046

3147
!> vector
32-
call h%write('vector_scalar_real', [37.])
3348
call h%write('1d_real', r1)
49+
call h%write('vector_scalar_real', [37.])
3450

51+
!> create then write
52+
call h%create('1d_int32', NF90_INT, dims=shape(i1))
3553
call h%write('1d_int32', i1)
3654

37-
!> create then write: not an nc4fortran feature yet
55+
call h%create('1d_int64', NF90_INT64, dims=shape(i1_64))
56+
call h%write('1d_int64', i1_64)
3857

39-
print *, 'PASSED: vector write'
4058
!> test rewrite
41-
call h%write('scalar_real32', 42.)
59+
call h%write('scalar_real32', 42._real32)
60+
call h%write('scalar_real64', 42._real64)
4261
call h%write('scalar_int32', 42_int32)
62+
call h%write('scalar_int64', 42_int64)
4363
call h%close()
4464

65+
end subroutine test_scalar_write
66+
67+
68+
subroutine test_scalar_read(fn)
69+
70+
character(*), intent(in) :: fn
71+
72+
type(netcdf_file) :: h
73+
74+
real(real32) :: rt, r1(4)
75+
integer(int32) :: i, it, i1(4)
76+
integer(int32), allocatable :: i1t(:)
77+
integer(int64) :: it_64, i1_64(4)
78+
integer(int64), allocatable :: i1t_64(:)
79+
real(real32), allocatable :: rr1_32(:)
80+
integer, allocatable :: dims(:)
81+
82+
!> test data
83+
do i = 1,size(i1)
84+
i1(i) = i
85+
enddo
86+
87+
r1 = i1
88+
i1_64 = i1
89+
4590
!> read
4691

4792
call h%open(fn, action='r')
4893

4994
call h%read('scalar_int32', it)
50-
call h%read('scalar_real32', rt)
95+
call h%read('scalar_int64', it_64)
5196

97+
call h%read('scalar_real32', rt)
5298
if (.not.(rt==it .and. it==42)) then
5399
write(stderr,*) it,'/=',rt
54100
error stop 'scalar real / int: not equal 42'
55101
endif
56-
print *, 'PASSED: scalar read/write'
57-
58-
!> read casting -- real to int and int to real
59-
call h%read('scalar_real32', it)
60-
if(it/=42) error stop 'scalar cast real => int'
61-
call h%read('scalar_int32', rt)
62-
if(rt/=42) error stop 'scalar cast int32 => real'
63-
print *, 'PASSED: scalar cast on read'
64102

65103
!> read vector length 1 as scalar
66104
call h%shape('vector_scalar_real', dims)
67-
if (any(dims /= [1])) then
68-
write(stderr,*) "expected dims 1, got dims:", dims, "rank:", rank(dims)
69-
error stop "vector_scalar: expected vector length 1"
70-
endif
105+
if (any(dims /= [1])) error stop "vector_scalar: expected vector length 1"
71106

72107
call h%read('vector_scalar_real', rt)
73-
if(rt/=37) error stop 'vector_scalar: 1d length 1 => scalar'
108+
if(rt /= 37) error stop 'vector_scalar: 1d length 1 => scalar'
74109

75-
!> 1D vector read write
110+
!> 1D vector read
76111
call h%shape('1d_real', dims)
77-
allocate(rr1(dims(1)))
78-
print *, "OK: 1d shape read real32"
79-
call h%read('1d_real', rr1)
80-
if (.not.all(r1 == rr1)) error stop 'real 1-D: read does not match write'
81-
print *, "OK: 1d read real32"
112+
allocate(rr1_32(dims(1)))
113+
114+
call h%read('1d_real', rr1_32)
115+
if (.not.all(r1 == rr1_32)) error stop 'real32 1-D: read does not match write'
82116

83117
call h%shape('1d_int32',dims)
84118
allocate(i1t(dims(1)))
85-
call h%read('1d_int32',i1t)
119+
call h%read('1d_int32', i1t)
86120
if (.not.all(i1==i1t)) error stop 'int32 1-D: read does not match write'
87121

88-
print *, 'PASSED: 1D read/write'
89-
90-
!> 1D vector read casting -- real to int and int to real
91-
call h%read('1d_real', i1t)
92-
if (.not.all(r1==i1t)) error stop '1Dcast real => int32'
93-
call h%read('1d_int32', rr1)
94-
if (.not.all(i1==rr1)) error stop '1D cast int32 => real'
122+
allocate(i1t_64(dims(1)))
123+
call h%read('1d_int64', i1t_64)
124+
if (.not.all(i1_64==i1t_64)) error stop 'int64 1-D: read does not match write'
95125

96126
!> check filename property
97127
if (.not. h%filename == fn) error stop h%filename // ' mismatch filename'
98128

99129
call h%close()
100130

131+
end subroutine test_scalar_read
132+
101133
end program

0 commit comments

Comments
 (0)