summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gfortran.dg/elemental_optional_args_5.f03
blob: fbbdb6c10b85e074cd1a71e6b822047c86b61458 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
! { dg-do run }
!
! PR fortran/50981
! Test the handling of optional, polymorphic and non-polymorphic arguments
! to elemental procedures. 
!
! Original testcase by Tobias Burnus <burnus@net-b.de>

implicit none
type t
  integer :: a
end type t

type t2
  integer, allocatable :: a
  integer, allocatable :: a2(:)
  integer, pointer :: p => null()
  integer, pointer :: p2(:) => null()
end type t2

type(t), allocatable :: ta, taa(:)
type(t), pointer :: tp, tpa(:)
class(t), allocatable :: ca, caa(:)
class(t), pointer :: cp, cpa(:)

type(t2) :: x

integer :: s, v(2)

tp => null()
tpa => null()
cp => null()
cpa => null()

! =============== sub1 ==================
! SCALAR COMPONENTS: Non alloc/assoc

s = 3
v = [9, 33]

call sub1 (s, x%a, .false.)
call sub1 (v, x%a, .false.)
!print *, s, v
if (s /= 3) STOP 1
if (any (v /= [9, 33])) STOP 2

call sub1 (s, x%p, .false.)
call sub1 (v, x%p, .false.)
!print *, s, v
if (s /= 3) STOP 3
if (any (v /= [9, 33])) STOP 4


! SCALAR COMPONENTS: alloc/assoc

allocate (x%a, x%p)
x%a = 4
x%p = 5
call sub1 (s, x%a, .true.)
call sub1 (v, x%a, .true.)
!print *, s, v
if (s /= 4*2) STOP 5
if (any (v /= [4*2, 4*2])) STOP 6

call sub1 (s, x%p, .true.)
call sub1 (v, x%p, .true.)
!print *, s, v
if (s /= 5*2) STOP 7
if (any (v /= [5*2, 5*2])) STOP 8


! ARRAY COMPONENTS: Non alloc/assoc

v = [9, 33]

call sub1 (v, x%a2, .false.)
!print *, v
if (any (v /= [9, 33])) STOP 9

call sub1 (v, x%p2, .false.)
!print *, v
if (any (v /= [9, 33])) STOP 10


! ARRAY COMPONENTS: alloc/assoc

allocate (x%a2(2), x%p2(2))
x%a2(:) = [84, 82]
x%p2    = [35, 58]

call sub1 (v, x%a2, .true.)
!print *, v
if (any (v /= [84*2, 82*2])) STOP 11

call sub1 (v, x%p2, .true.)
!print *, v
if (any (v /= [35*2, 58*2])) STOP 12


! =============== sub_t ==================
! SCALAR DT: Non alloc/assoc

s = 3
v = [9, 33]

call sub_t (s, ta, .false.)
call sub_t (v, ta, .false.)
!print *, s, v
if (s /= 3) STOP 13
if (any (v /= [9, 33])) STOP 14

call sub_t (s, tp, .false.)
call sub_t (v, tp, .false.)
!print *, s, v
if (s /= 3) STOP 15
if (any (v /= [9, 33])) STOP 16

call sub_t (s, ca, .false.)
call sub_t (v, ca, .false.)
!print *, s, v
if (s /= 3) STOP 17
if (any (v /= [9, 33])) STOP 18

call sub_t (s, cp, .false.)
call sub_t (v, cp, .false.)
!print *, s, v
if (s /= 3) STOP 19
if (any (v /= [9, 33])) STOP 20

! SCALAR COMPONENTS: alloc/assoc

allocate (ta, tp, ca, cp)
ta%a = 4
tp%a = 5
ca%a = 6
cp%a = 7

call sub_t (s, ta, .true.)
call sub_t (v, ta, .true.)
!print *, s, v
if (s /= 4*2) STOP 21
if (any (v /= [4*2, 4*2])) STOP 22

call sub_t (s, tp, .true.)
call sub_t (v, tp, .true.)
!print *, s, v
if (s /= 5*2) STOP 23
if (any (v /= [5*2, 5*2])) STOP 24

call sub_t (s, ca, .true.)
call sub_t (v, ca, .true.)
!print *, s, v
if (s /= 6*2) STOP 25
if (any (v /= [6*2, 6*2])) STOP 26

call sub_t (s, cp, .true.)
call sub_t (v, cp, .true.)
!print *, s, v
if (s /= 7*2) STOP 27
if (any (v /= [7*2, 7*2])) STOP 28

! ARRAY COMPONENTS: Non alloc/assoc

v = [9, 33]

call sub_t (v, taa, .false.)
!print *, v
if (any (v /= [9, 33])) STOP 29

call sub_t (v, tpa, .false.)
!print *, v
if (any (v /= [9, 33])) STOP 30

call sub_t (v, caa, .false.)
!print *, v
if (any (v /= [9, 33])) STOP 31

call sub_t (v, cpa, .false.)
!print *, v
if (any (v /= [9, 33])) STOP 32

deallocate(ta, tp, ca, cp)


! ARRAY COMPONENTS: alloc/assoc

allocate (taa(2), tpa(2))
taa(1:2)%a = [44, 444]
tpa(1:2)%a = [55, 555]
allocate (caa(2), source=[t(66), t(666)])
allocate (cpa(2), source=[t(77), t(777)])

select type (caa)
type is (t)
  if (any (caa(:)%a /= [66, 666])) STOP 33
end select

select type (cpa)
type is (t)
  if (any (cpa(:)%a /= [77, 777])) STOP 34
end select

call sub_t (v, taa, .true.)
!print *, v
if (any (v /= [44*2, 444*2])) STOP 35

call sub_t (v, tpa, .true.)
!print *, v
if (any (v /= [55*2, 555*2])) STOP 36


call sub_t (v, caa, .true.)
!print *, v
if (any (v /= [66*2, 666*2])) STOP 37

call sub_t (v, cpa, .true.)
!print *, v
if (any (v /= [77*2, 777*2])) STOP 38

deallocate (taa, tpa, caa, cpa)


contains

  elemental subroutine sub1 (x, y, alloc)
    integer, intent(inout) :: x
    integer, intent(in), optional :: y
    logical, intent(in) :: alloc
    if (alloc .neqv. present (y)) &
      x = -99
    if (present(y)) &
      x = y*2
  end subroutine sub1

  elemental subroutine sub_t(x, y, alloc)
    integer, intent(inout) :: x
    type(t), intent(in), optional :: y
    logical, intent(in) :: alloc
    if (alloc .neqv. present (y)) &
      x = -99
    if (present(y)) &
      x = y%a*2
  end subroutine sub_t

end