3 ! Functional test of User Defined Derived Type IO.
5 ! This tests recursive calls where a derived type has a member that is
11 CHARACTER (LEN
=20) :: name
13 type(person
), pointer :: next
=> NULL()
17 GENERIC
:: WRITE(FORMATTED
) => pwf
18 GENERIC
:: READ(FORMATTED
) => prf
21 RECURSIVE SUBROUTINE pwf (dtv
,unit
,iotype
,vlist
,iostat
,iomsg
)
22 CLASS(person
), INTENT(IN
) :: dtv
23 INTEGER, INTENT(IN
) :: unit
24 CHARACTER (LEN
=*), INTENT(IN
) :: iotype
25 INTEGER, INTENT(IN
) :: vlist(:)
26 INTEGER, INTENT(OUT
) :: iostat
27 CHARACTER (LEN
=*), INTENT(INOUT
) :: iomsg
28 CHARACTER (LEN
=30) :: udfmt
34 if (iotype
.eq
."DT") then
35 if (size(vlist
).ne
.0) print *, 36
36 if (associated(dtv
%next
)) then
37 WRITE(unit
, FMT
= '(a20,i2, DT)', IOSTAT
=iostat
, advance
='no') dtv
%name
, dtv
%age
, dtv
%next
39 WRITE(unit
, FMT
= '(a20,i2)', IOSTAT
=iostat
, advance
='no') dtv
%name
, dtv
%age
41 if (iostat
.ne
.0) iomsg
= "Fail PWF DT"
43 if (iotype
.eq
."DTzeroth") then
44 if (size(vlist
).ne
.0) print *, 40
45 WRITE(unit
, FMT
= '(g0,g0)', advance
='no') dtv
%name
, dtv
%age
46 if (iostat
.ne
.0) iomsg
= "Fail PWF DTzeroth"
48 if (iotype
.eq
."DTtwo") then
49 if (size(vlist
).ne
.2) STOP 1
50 WRITE(udfmt
,'(A,A,I1,A,I1,A)') '(', 'A', vlist(1),',I', vlist(2), ')'
51 WRITE(unit
, FMT
='(A8,I2)') dtv
%name
, dtv
%age
52 if (iostat
.ne
.0) iomsg
= "Fail PWF DTtwo"
54 if (iotype
.eq
."DTthree") then
55 WRITE(udfmt
,'(2A,I2,A,I1,A,I2,A)',iostat
=myios
) '(', 'A', vlist(1),',I', vlist(2), ',F', vlist(3), '.2)'
56 WRITE(unit
, FMT
=udfmt
, IOSTAT
=iostat
, advance
='no') trim(dtv
%name
), dtv
%age
, 3.14
57 if (iostat
.ne
.0) iomsg
= "Fail PWF DTthree"
59 if (iotype
.eq
."LISTDIRECTED") then
60 if (size(vlist
).ne
.0) print *, 55
61 if (associated(dtv
%next
)) then
62 WRITE(unit
, FMT
= *) dtv
%name
, dtv
%age
, dtv
%next
64 WRITE(unit
, FMT
= *) dtv
%name
, dtv
%age
66 if (iostat
.ne
.0) iomsg
= "Fail PWF LISTDIRECTED"
68 if (iotype
.eq
."NAMELIST") then
69 if (size(vlist
).ne
.0) print *, 59
72 if (associated (dtv
%next
) .and
. (iotype
.eq
."LISTDIRECTED")) write(unit
, fmt
= *) dtv
%next
75 RECURSIVE SUBROUTINE prf (dtv
,unit
,iotype
,vlist
,iostat
,iomsg
)
76 CLASS(person
), INTENT(INOUT
) :: dtv
77 INTEGER, INTENT(IN
) :: unit
78 CHARACTER (LEN
=*), INTENT(IN
) :: iotype
79 INTEGER, INTENT(IN
) :: vlist(:)
80 INTEGER, INTENT(OUT
) :: iostat
81 CHARACTER (LEN
=*), INTENT(INOUT
) :: iomsg
82 CHARACTER (LEN
=30) :: udfmt
88 if (iotype
.eq
."DT") then
89 if (size(vlist
).ne
.0) print *, 36
90 if (associated(dtv
%next
)) then
91 READ(unit
, FMT
= '(a20,i2, DT)', IOSTAT
=iostat
, advance
='no') dtv
%name
, dtv
%age
, dtv
%next
93 READ(unit
, FMT
= '(a20,i2)', IOSTAT
=iostat
, advance
='no') dtv
%name
, dtv
%age
95 if (iostat
.ne
.0) iomsg
= "Fail PWF DT"
97 if (iotype
.eq
."DTzeroth") then
98 if (size(vlist
).ne
.0) print *, 40
99 READ(unit
, FMT
= '(a,I2)', advance
='no') dtv
%name
, dtv
%age
100 if (iostat
.ne
.0) iomsg
= "Fail PWF DTzeroth"
102 if (iotype
.eq
."DTtwo") then
103 if (size(vlist
).ne
.2) STOP 1
104 WRITE(udfmt
,'(A,A,I1,A,I1,A)') '(', 'A', vlist(1),',I', vlist(2), ')'
105 READ(unit
, FMT
='(A8,I2)') dtv
%name
, dtv
%age
106 if (iostat
.ne
.0) iomsg
= "Fail PWF DTtwo"
108 if (iotype
.eq
."DTthree") then
109 WRITE(udfmt
,'(2A,I2,A,I1,A,I2,A)',iostat
=myios
) '(', 'A', vlist(1),',I', vlist(2), ',F', vlist(3), '.2)'
110 READ(unit
, FMT
=udfmt
, IOSTAT
=iostat
, advance
='no') dtv
%name
, dtv
%age
, areal
111 if (iostat
.ne
.0) iomsg
= "Fail PWF DTthree"
113 if (iotype
.eq
."LISTDIRECTED") then
114 if (size(vlist
).ne
.0) print *, 55
115 READ(unit
, FMT
= *) dtv
%name
, dtv
%age
116 if (iostat
.ne
.0) iomsg
= "Fail PWF LISTDIRECTED"
118 if (iotype
.eq
."NAMELIST") then
119 if (size(vlist
).ne
.0) print *, 59
122 !READ (UNIT = UNIT, FMT = *) dtv%name, dtv%age
129 TYPE (person
) :: chairman
130 TYPE (person
), target
:: member
131 character(80) :: astring
134 chairman
%name
="Charlie"
139 ! At this point, next is NULL as defined up in the type block.
140 open(10, status
= "scratch")
141 write (10, *, iostat
=myiostat
, iomsg
=astring
) member
, chairman
144 chairman
%name
="bogus1"
148 read (10, *, iostat
=myiostat
, iomsg
=astring
) member
, chairman
149 if (astring
.ne
."SUCCESS") print *, astring
150 if (member
%name
.ne
."George") STOP 1
151 if (chairman
%name
.ne
."Charlie") STOP 1
152 if (member
%age
.ne
.42) STOP 1
153 if (chairman
%age
.ne
.62) STOP 1
154 close(10, status
='delete')
155 ! Now we set next to point to member. This changes the code path
156 ! in the pwf and prf procedures.
157 chairman
%next
=> member
158 open(10, status
= "scratch")
159 write (10,"(DT)") chairman
161 chairman
%name
="bogus1"
165 read (10,"(DT)", iomsg
=astring
) chairman
166 !print *, trim(astring)
167 if (member
%name
.ne
."George") STOP 1
168 if (chairman
%name
.ne
."Charlie") STOP 1
169 if (member
%age
.ne
.42) STOP 1
170 if (chairman
%age
.ne
.62) STOP 1