Debian Codegraph @

gcc/testsuite/gfortran.dg/reduce_1.f90

raw
1 ! { dg-do run }
2 !
3 ! Test results from the F2018 intrinsic REDUCE
4 !
5 ! Contributed by Paul Thomas <[email protected]>
6 !
7
8 module operations
9 type :: s
10 integer, allocatable :: i
11 integer :: j
12 end type s
13
14 contains
15
16 pure function add(i,j) result(sum_ij)
17 integer, intent(in) :: i, j
18 integer :: sum_ij
19 sum_ij = i + j
20 end function add
21 !
22 pure function mult(i,j) result(prod_ij)
23 integer, intent(in) :: i, j
24 integer :: prod_ij
25 prod_ij = i * j
26 end function mult
27
28 pure function mult_by_val(i,j) result(prod_ij)
29 integer, intent(in), value :: i, j
30 integer :: prod_ij
31 prod_ij = i * j
32 end function mult_by_val
33
34 pure function non_com(i,j) result(nc_ij)
35 integer, intent(in) :: i, j
36 integer :: nc_ij
37 if (i > j) then
38 nc_ij = i - j
39 else
40 nc_ij = i + j
41 endif
42 end function non_com
43
44 pure function c_op (i, j) result (ij)
45 character(8), intent(in) :: i, j
46 character(8) :: ij
47 integer :: n
48 ij = i
49 do n = 1, 8
50 if (i(n:n) .ne. j(n:n)) ij(n:n) = '!'
51 end do
52 end function c_op
53
54 pure function t_op (i, j) result (ij)
55 type(s), intent(in) :: i, j
56 type(s) :: ij
57 ij%i = non_com (i%i, j%i)
58 ij%j = non_com (j%j, i%j)
59 end function t_op
60
61 pure function t_add (i, j) result (ij)
62 type(s), intent(in) :: i, j
63 type(s) :: ij
64 ij%i = i%i + j%i
65 ij%j = j%j + i%j
66 end function t_add
67 end module operations
68
69 program test_reduce
70 use operations
71 implicit none
72 integer :: i
73 integer, parameter :: n = 3
74 integer, parameter :: vec(n) = [2, 5, 10]
75 integer, parameter :: mat(n,2) = reshape([vec,2*vec],shape=[size(vec),2])
76 integer :: res0
77 integer, dimension(:), allocatable :: res1
78 integer, dimension(:,:), allocatable :: res2
79 logical, parameter :: t = .true., f = .false.
80 LOGICAL, PARAMETER :: keep(n) = [t,f,t]
81 logical, parameter :: keepM(n,2) = reshape([keep,keep],shape=[n,2])
82 logical, parameter :: all_false(n,2) = reshape ([(f, i = 1,2*n)],[n,2])
83 character(*), parameter :: carray (4) = ['abctefgh', 'atcdefgh', &
84 'abcdefth', 'abcdtfgh']
85 character(:), allocatable :: cres0, cres1(:)
86 type(s), allocatable :: tres1(:)
87 type(s), allocatable :: tres2(:,:)
88 type(s) :: tres2_na(2, 4)
89 type(s), allocatable :: tarray(:,:,:)
90 type(s), allocatable :: tvec(:)
91 type(s), allocatable :: tres0
92 integer, allocatable :: ires(:)
93
94 ! Simple cases with and without DIM
95 res0 = reduce (vec, add, dim=1)
96 if (res0 /= 17) stop 1
97 res0 = reduce (vec, mult, 1)
98 if (res0 /= 100) stop 2
99 res1 = reduce (mat, add, 1)
100 if (any (res1 /= [17, 34])) stop 3
101 res1 = reduce (mat, mult, 1)
102 if (any (res1 /= [100, 800])) stop 4
103 res1 = reduce (mat, add, 2)
104 if (any (res1 /= [6, 15, 30])) stop 5
105 res1 = reduce (mat, mult, 2)
106 if (any (res1 /= [8, 50, 200])) stop 6
107 res0 = reduce (mat, add)
108 if (res0 /= 51) stop 7
109 res0 = reduce (mat, mult)
110 if (res0 /= 80000) stop 8
111 ! Repeat previous test with arguments passed by value to operation
112 res0 = reduce (mat, mult_by_val)
113 if (res0 /= 80000) stop 9
114
115 ! Using MASK and IDENTITY
116 res0 = reduce (vec,add, mask=keep, identity = 1)
117 if (res0 /= 12) stop 10
118 res0 = reduce (vec,mult, mask=keep, identity = 1)
119 if (res0 /= 20) stop 11
120 res0 = reduce (mat, add, mask=keepM, identity = 1)
121 if (res0 /= 36) stop 12
122 res0 = reduce (mat, mult, mask=keepM, identity = 1)
123 if (res0 /= 1600) stop 13
124 res0 = reduce (mat, mult, mask=all_false, identity = -1)
125 if (res0 /= -1) stop 14
126
127 ! 3-D ARRAYs with and without DIM and MASK
128 res0 = reduce (reshape ([(i, i=1,8)], [2,2,2]),mult)
129 if (res0 /= 40320) stop 15
130 res2 = reduce (reshape ([(i, i=1,8)], [2,2,2]),mult,dim=2)
131 if (any (res2 /= reshape ([3,8,35,48], [2,2]))) stop 16
132 res2 = reduce (reshape ([(i, i=1,8)], [2,2,2]),mult,dim=2, &
133 mask=reshape ([t,f,t,f,t,f,t,f],[2,2,2]), identity=-1)
134 if (any (res2 /= reshape ([3,-1,35,-1], [2,2]))) stop 17
135 res2 = reduce (reshape([(i, i=1,16)], [2,4,2]), add, dim = 3, &
136 mask=reshape([f,t,t,f,t,t,t,t,t,t,t,t,t,t,t,t],[2,4,2]), &
137 identity=-1)
138 if (any (res2 /= reshape ([9,12,14,12,18,20,22,24], [2,4]))) stop 18
139 res1 = reduce (reshape([(i, i=1,16)], [4,4]),add, dim = 2, &
140 mask=reshape([f,t,t,f,t,t,t,t,t,t,t,t,t,t,t,t],[4,4]), &
141 identity=-1)
142 if (any (res1 /= [27,32,36,36])) stop 19
143
144 ! Verify that the library function treats non-comutative OPERATION in the
145 ! correct order. If this were incorrect,the result would be [9,8,8,12,8,8,8,8].
146 res2 = reduce (reshape([(i, i=1,16)], [2,4,2]), non_com, dim = 3, &
147 mask=reshape([f,t,t,f,t,t,t,t,t,t,t,t,t,t,t,t],[2,4,2]), &
148 identity=-1)
149 if (any (res2 /= reshape([9,12,14,12,18,20,22,24],shape(res2)))) stop 20
150
151 ! Character ARRAY and OPERATION
152 cres0 = reduce (carray, c_op); if (cres0 /= 'a!c!!f!h') stop 21
153 cres1 = reduce (reshape (carray, [2,2]), c_op, dim = 1)
154 if (any (cres1 /= ['a!c!efgh','abcd!f!h'])) stop 22
155
156 ! Derived type ARRAY and OPERATION - was checked for memory leaks of the
157 ! allocatable component.
158 ! tarray = reshape([(s(i, i), i = 1, 16)], [2,4,2]) leaks memory!
159 allocate (tvec(16))
160 do i = 1, 16
161 tvec(i)%i = i
162 tvec(i)%j = i
163 enddo
164 tarray = reshape(tvec, [2,4,2])
165
166 tres2 = reduce (tarray, t_op, dim = 3, &
167 mask=reshape([t,t,t,f,t,t,t,t,t,f,t,t,t,t,t,t],[2,4,2]), &
168 identity = s(NULL(),1))
169 ires = [10,2,14,12,18,20,22,24]
170 tres1 = reshape (tres2, [size (tres2, 1)* size (tres2, 2)])
171 do i = 1, size (tres2, 1)* size (tres2, 2)
172 if (tres1(i)%i /= ires(i)) stop 23
173 end do
174 if (any (tres2%j /= reshape([8,2,8,12,8,8,8,8],shape(tres2)))) stop 24
175
176 ! Check that the non-allocatable result with an allocatable component does not
177 ! leak memory from the allocatable component
178 tres2_na = reduce (tarray, t_op, dim = 3, &
179 mask=reshape([t,t,t,f,t,t,t,t,t,f,t,t,t,t,t,t],[2,4,2]), &
180 identity = s(NULL(),1))
181 tres1 = reshape (tres2_na, [size (tres2_na, 1)* size (tres2, 2)])
182 do i = 1, size (tres2_na, 1)* size (tres2_na, 2)
183 if (tres1(i)%i /= ires(i)) stop 25
184 end do
185 if (any (tres2_na%j /= reshape([8,2,8,12,8,8,8,8],shape(tres2_na)))) stop 26
186
187
188 tres0 = reduce (tarray, t_add)
189 if (tres0%i /= 136) stop 27
190 if (tres0%j /= 136) stop 28
191
192 ! Test array being a component of an array of derived types
193 i = reduce (tarray%j, add, &
194 mask=reshape([t,t,t,f,t,t,t,t,t,f,t,t,t,t,f,t],[2,4,2]), &
195 identity = 0)
196 if (i /= 107) stop 29
197
198
199 ! Deallocate the allocatable components and then the allocatable variables
200 tres2_na = reshape ([(s(NULL (), 0), i = 1, size (tres2_na))], shape (tres2_na))
201 deallocate (res1, res2, cres0, cres1, tarray, ires, tres0, tres1, tres2, tvec)
202 end