@@ -7,36 +7,85 @@ program test_string
77
88implicit none (type, external )
99
10+ character (* ), parameter :: fn= ' test_string.nc'
1011
11- type (netcdf_file) :: h
12+ call test_write(fn)
13+ print * , " OK: HDF5 string write"
1214
13- character (2 ) :: value
14- character (1024 ) :: val1k
15- character (* ), parameter :: path= ' test_string.nc'
15+ call test_read(fn)
16+ print * ,' OK: HDF5 string read'
17+
18+ call test_overwrite(fn)
19+ print * , " OK: string overwrite"
20+
21+ print * ,' PASSED: HDF5 string write/read'
22+
23+ contains
24+
25+
26+ subroutine test_write (fn )
1627
17- call h% open (path, action= ' w' )
28+ character (* ), intent (in ) :: fn
29+
30+ type (netcdf_file) :: h
31+
32+ call h% open (fn, action= ' w' )
1833
1934call h% write (' little' , ' 42' )
2035call h% write (' MySentence' , ' this is a little sentence.' )
2136
2237call h% close ()
2338
24- call h% open (path, action= ' r' )
39+ end subroutine test_write
40+
41+
42+ subroutine test_read (fn )
43+
44+ character (* ), intent (in ) :: fn
45+
46+ type (netcdf_file) :: h
47+ character (2 ) :: value
48+ character (1024 ) :: val1k
49+
50+ call h% open (fn, action= ' r' )
2551call h% read (' little' , value)
2652
53+ if (len_trim (value) /= 2 ) then
54+ write (stderr,' (a,i0,a)' ) " test_string: read length " , len_trim (value), " /= 2"
55+ error stop
56+ endif
2757if (value /= ' 42' ) error stop ' test_string: read/write verification failure. Value: ' // value
2858
29- print * ,' test_string_rw: reading too much data'
30- ! ! try reading too much data, then truncating to first C_NULL
59+ ! > longer character than data
3160call h% read (' little' , val1k)
3261
3362if (len_trim (val1k) /= 2 ) then
34- write (stderr, * ) ' expected len_trim 2, got len_trim = ' , len (val1k)
63+ write (stderr, ' (a,i0,/,a) ' ) ' expected character len_trim 2 but got len_trim() = ' , len_trim (val1k), val1k
3564 error stop
3665endif
3766
3867call h% close ()
3968
40- print * , ' OK: test_string'
69+ end subroutine test_read
70+
71+
72+ subroutine test_overwrite (fn )
73+
74+ character (* ), intent (in ) :: fn
75+
76+ type (netcdf_file) :: h
77+ character (2 ) :: v
78+
79+ call h% open (fn, action= ' rw' )
80+ call h% write (' little' , ' 73' )
81+ call h% close ()
82+
83+ call h% open (fn, action= ' r' )
84+ call h% read (' little' , v)
85+ call h% close ()
86+
87+ if (v /= ' 73' ) error stop ' test_string: overwrite string failure. Value: ' // v // " /= 73"
88+
89+ end subroutine test_overwrite
4190
4291end program
0 commit comments