Debian Codegraph @

gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.f90

raw
1 ! { dg-do run { target c99_runtime } }
2 ! { dg-additional-sources ISO_Fortran_binding_1.c }
3 !
4 ! Test F2008 18.5: ISO_Fortran_binding.h functions.
5 !
6 USE, INTRINSIC :: ISO_C_BINDING
7
8 TYPE, BIND(C) :: T
9 REAL(C_DOUBLE) :: X
10 complex(C_DOUBLE_COMPLEX) :: Y
11 END TYPE
12
13 type :: mytype
14 integer :: i
15 integer :: j
16 end type
17
18 INTERFACE
19 FUNCTION elemental_mult(a, b, c) BIND(C, NAME="elemental_mult_c") RESULT(err)
20 USE, INTRINSIC :: ISO_C_BINDING
21 INTEGER(C_INT) :: err
22 type(*), DIMENSION(..) :: a, b, c
23 END FUNCTION elemental_mult
24
25 FUNCTION c_deallocate(a) BIND(C, NAME="deallocate_c") RESULT(err)
26 USE, INTRINSIC :: ISO_C_BINDING
27 INTEGER(C_INT) :: err
28 INTEGER(C_INT), DIMENSION(..), allocatable :: a
29 END FUNCTION c_deallocate
30
31 FUNCTION c_allocate(a, lower, upper) BIND(C, NAME="allocate_c") RESULT(err)
32 USE, INTRINSIC :: ISO_C_BINDING
33 INTEGER(C_INT) :: err
34 INTEGER(C_INT), DIMENSION(..), allocatable :: a
35 integer(C_INTPTR_T), DIMENSION(15) :: lower, upper
36 END FUNCTION c_allocate
37
38 FUNCTION c_establish(a) BIND(C, NAME="establish_c") RESULT(err)
39 USE, INTRINSIC :: ISO_C_BINDING
40 import
41 INTEGER(C_INT) :: err
42 type (T), pointer, DIMENSION(..), intent(out) :: a
43 END FUNCTION c_establish
44
45 FUNCTION c_contiguous(a) BIND(C, NAME="contiguous_c") RESULT(err)
46 USE, INTRINSIC :: ISO_C_BINDING
47 INTEGER(C_INT) :: err
48 type(*), DIMENSION(..) :: a
49 END FUNCTION c_contiguous
50
51 FUNCTION c_section(std_case, a, lower, strides) BIND(C, NAME="section_c") RESULT(ans)
52 USE, INTRINSIC :: ISO_C_BINDING
53 real(C_FLOAT) :: ans
54 INTEGER(C_INT) :: std_case
55 INTEGER(C_INT), dimension(15) :: lower
56 INTEGER(C_INT), dimension(15) :: strides
57 type(*), DIMENSION(..) :: a
58 END FUNCTION c_section
59
60 FUNCTION c_select_part(a) BIND(C, NAME="select_part_c") RESULT(ans)
61 USE, INTRINSIC :: ISO_C_BINDING
62 real(C_DOUBLE) :: ans
63 type(*), DIMENSION(..) :: a
64 END FUNCTION c_select_part
65
66 FUNCTION c_setpointer(a, lbounds) BIND(C, NAME="setpointer_c") RESULT(err)
67 USE, INTRINSIC :: ISO_C_BINDING
68 INTEGER(C_INT) :: err
69 INTEGER(C_INT), dimension(2) :: lbounds
70 INTEGER(C_INT), DIMENSION(..), pointer :: a
71 END FUNCTION c_setpointer
72
73 FUNCTION c_assumed_size(a) BIND(C, NAME="assumed_size_c") RESULT(err)
74 USE, INTRINSIC :: ISO_C_BINDING
75 INTEGER(C_INT) :: err
76 type(*), DIMENSION(..) :: a
77 END FUNCTION c_assumed_size
78
79 END INTERFACE
80
81 integer, dimension(:,:), allocatable :: x, y, z
82 integer, dimension(2,2) :: a, b, c
83 integer, dimension(4,4) :: d
84 integer :: i = 42, j, k
85 integer(C_INTPTR_T), dimension(15) :: lower, upper
86 real, dimension(10,10) :: arg
87 type (mytype), dimension(2,2) :: der
88
89 allocate (x, source = reshape ([4,3,2,1], [2,2]))
90 allocate (y, source = reshape ([2,3,4,5], [2,2]))
91 allocate (z, source = reshape ([0,0,0,0], [2,2]))
92
93 call test_CFI_address
94 call test_CFI_deallocate
95 call test_CFI_allocate
96 call test_CFI_establish
97 call test_CFI_contiguous (a)
98 call test_CFI_section (arg)
99 call test_CFI_select_part
100 call test_CFI_setpointer
101 call test_assumed_size (a)
102 contains
103 subroutine test_CFI_address
104 ! Basic test that CFI_desc_t can be passed and that CFI_address works
105 if (elemental_mult (z, x, y) .ne. 0) stop 1
106 if (any (z .ne. reshape ([8,9,8,5], [2,2]))) stop 2
107
108 a = reshape ([4,3,2,1], [2,2])
109 b = reshape ([2,3,4,5], [2,2])
110 c = 0
111 ! Verify that components of arrays of derived types are OK.
112 der%j = a
113 ! Check that non-pointer/non-allocatable arguments are OK
114 if (elemental_mult (c, der%j, b) .ne. 0) stop 3
115 if (any (c .ne. reshape ([8,9,8,5], [2,2]))) stop 4
116
117 ! Check array sections
118 d = 0
119 d(4:2:-2, 1:3:2) = b
120 if (elemental_mult (c, a, d(4:2:-2, 1:3:2)) .ne. 0) stop 5
121 if (any (c .ne. reshape ([8,9,8,5], [2,2]))) stop 6
122
123 ! If a scalar result is passed to 'elemental_mult' it is returned
124 ! as the function result and then zeroed. This tests that scalars
125 ! are correctly converted to CF_desc_t.
126 if ((elemental_mult (i, a, b) .ne. 42) &
127 .or. (i .ne. 0)) stop 7
128 deallocate (y,z)
129 end subroutine test_CFI_address
130
131 subroutine test_CFI_deallocate
132 ! Test CFI_deallocate.
133 if (c_deallocate (x) .ne. 0) stop 8
134 if (allocated (x)) stop 9
135 end subroutine test_CFI_deallocate
136
137 subroutine test_CFI_allocate
138 ! Test CFI_allocate.
139 lower(1:2) = [2,2]
140 upper(1:2) = [10,10]
141
142 if (c_allocate (x, lower, upper) .ne. 0) stop 10
143 if (.not.allocated (x)) stop 11
144 if (any (lbound (x) .ne. lower(1:2))) stop 12
145 if (any (ubound (x) .ne. upper(1:2))) stop 13
146
147 ! Elements are filled by 'c_allocate' with the product of the fortran indices
148 do j = lower(1) , upper(1)
149 do k = lower(2) , upper(2)
150 x(j,k) = x(j,k) - j * k
151 end do
152 end do
153 if (any (x .ne. 0)) stop 14
154 deallocate (x)
155 end subroutine test_CFI_allocate
156
157 subroutine test_CFI_establish
158 ! Test CFI_establish.
159 type(T), pointer :: case2(:) => null()
160 if (c_establish(case2) .ne. 0) stop 14
161 if (ubound(case2, 1) .ne. 9) stop 15
162 if (.not.associated(case2)) stop 16
163 if (sizeof(case2) .ne. 240) stop 17
164 if (int (sum (case2%x)) .ne. 55) stop 18
165 if (int (sum (imag (case2%y))) .ne. 110) stop 19
166 deallocate (case2)
167 end subroutine test_CFI_establish
168
169 subroutine test_CFI_contiguous (arg)
170 integer, dimension (2,*) :: arg
171 character(4), dimension(2) :: chr
172 ! These are contiguous
173 if (c_contiguous (arg) .ne. 1) stop 20
174 if (.not.allocated (x)) allocate (x(2, 2))
175 if (c_contiguous (x) .ne. 1) stop 22
176 deallocate (x)
177 if (c_contiguous (chr) .ne. 1) stop 23
178 ! These are not contiguous
179 if (c_contiguous (der%i) .eq. 1) stop 24
180 if (c_contiguous (arg(1:1,1:2)) .eq. 1) stop 25
181 if (c_contiguous (d(4:2:-2, 1:3:2)) .eq. 1) stop 26
182 if (c_contiguous (chr(:)(2:3)) .eq. 1) stop 27
183 end subroutine test_CFI_contiguous
184
185 subroutine test_CFI_section (arg)
186 real, dimension (100) :: a
187 real, dimension (10,*) :: arg
188 integer, dimension(15) :: lower, strides
189 integer :: i
190
191 ! Case (i) from F2018:18.5.5.7.
192 a = [(real(i), i = 1, 100)]
193 lower(1) = 10
194 strides(1) = 5
195 ! Remember, 'a' being non pointer, non-allocatable, the C descriptor
196 ! lbounds are set to zero.
197 if (int (sum(a(lower(1)+1::strides(1))) &
198 - c_section(1, a, lower, strides)) .ne. 0) stop 28
199 ! Case (ii) from F2018:18.5.5.7.
200 arg(:,1:10) = reshape ([(real(i), i = 1, 100)], [10,10])
201 lower(1) = 1
202 lower(2) = 5
203 strides(1) = 1
204 strides(2) = 0
205 if (int (sum(arg(:,5)) &
206 - c_section (2, arg, lower, strides)) .ne. 0) stop 29
207 end subroutine test_CFI_section
208
209 subroutine test_CFI_select_part
210 ! Test the example from F2018:18.5.5.8.
211 ! Modify to take rank 2 and sum the section type_t(5, :)%y%im
212 ! Note that sum_z_5 = sum (type_t(5, :)%y%im) is broken on Darwin.
213 !
214 type (t), dimension(10, 10) :: type_t
215 real(kind(type_t%x)) :: v, sum_z_5 = 0.0
216 complex(kind(type_t%y)) :: z
217 ! Set the array 'type_t'.
218 do j = 1, 10
219 do k = 1, 10
220 v = dble (j * k)
221 z = cmplx (2 * v, 3 * v)
222 type_t(j, k) = t (v, z)
223 if (j .eq. 5) sum_z_5 = sum_z_5 + imag (z)
224 end do
225 end do
226 ! Now do the test.
227 if (int (c_select_part (type_t) - sum_z_5) .ne. 0) stop 30
228 end subroutine test_CFI_select_part
229
230 subroutine test_CFI_setpointer
231 ! Test the example from F2018:18.5.5.9.
232 integer, dimension(:,:), pointer :: ptr => NULL ()
233 integer, dimension(2,2), target :: tgt
234 integer, dimension(2) :: lbounds = [-1, -2]
235 ! The C-function resets the lbounds
236 ptr(1:, 1:) => tgt
237 if (c_setpointer (ptr, lbounds) .ne. 0) stop 31
238 if (any (lbound(ptr) .ne. lbounds)) stop 32
239 end subroutine test_CFI_setpointer
240
241 subroutine test_assumed_size (arg)
242 integer, dimension(2,*) :: arg
243 ! The C-function checks contiguousness and that extent[1] == -1.
244 if (c_assumed_size (arg) .ne. 0) stop 33
245 end subroutine
246 end