[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / flang / test / Evaluate / folding03.f90
blob26b0e4edff3e051166bda9d652f822412da7fe24
1 ! RUN: %python %S/test_folding.py %s %flang_fc1
2 ! Test operation folding edge case (both expected value and messages)
3 ! These tests make assumptions regarding real(4) and integer(4) extrema.
5 #define TEST_ISNAN(v) logical, parameter :: test_##v =.NOT.(v.EQ.v)
7 module integer_tests
8 integer(4), parameter :: i4_pmax = 2147483647_4
9 ! Fortran grammar rule R605 prevents from writing -2147483648_4 in an
10 ! expression because literal-constant are not signed so this would parse
11 ! to -(2147483648_4) and 2147483648_4 is not accepted as a literal-constant.
12 ! However, one can reach this value with operations.
13 integer(4), parameter :: i4_nmax = -2147483647_4 - 1_4
15 ! Integer division by zero are not tested here because they are handled as fatal
16 ! errors in constants.
18 !WARN: warning: INTEGER(4) negation overflowed
19 logical, parameter :: test_overflow_unary_minus1 = (-i4_nmax).EQ.i4_nmax
20 logical, parameter :: test_no_overflow_unary_minus1 = (-i4_pmax).EQ.(i4_nmax+1_4)
21 logical, parameter :: test_no_overflow_unary_plus1 = (+i4_pmax).EQ.i4_pmax
22 logical, parameter :: test_no_overflow_unary_plus2 = (+i4_nmax).EQ.i4_nmax
24 !WARN: warning: INTEGER(4) addition overflowed
25 logical, parameter :: test_overflow_add1 = (i4_pmax+1_4).EQ.i4_nmax
26 !WARN: warning: INTEGER(4) addition overflowed
27 logical, parameter :: test_overflow_add2 = (i4_nmax + (-1_4)).EQ.i4_pmax
28 !WARN: warning: INTEGER(4) addition overflowed
29 logical, parameter :: test_overflow_add3 = (i4_pmax + i4_pmax).EQ.(-2_4)
30 !WARN: warning: INTEGER(4) addition overflowed
31 logical, parameter :: test_overflow_add4 = (i4_nmax + i4_nmax).EQ.(0_4)
32 logical, parameter :: test_no_overflow_add1 = (i4_pmax + 0_4).EQ.i4_pmax
33 logical, parameter :: test_no_overflow_add2 = (i4_nmax + (-0_4)).EQ.i4_nmax
34 logical, parameter :: test_no_overflow_add3 = (i4_pmax + i4_nmax).EQ.(-1_4)
35 logical, parameter :: test_no_overflow_add4 = (i4_nmax + i4_pmax).EQ.(-1_4)
37 !WARN: warning: INTEGER(4) subtraction overflowed
38 logical, parameter :: test_overflow_sub1 = (i4_nmax - 1_4).EQ.i4_pmax
39 !WARN: warning: INTEGER(4) subtraction overflowed
40 logical, parameter :: test_overflow_sub2 = (i4_pmax - (-1_4)).EQ.i4_nmax
41 !WARN: warning: INTEGER(4) subtraction overflowed
42 logical, parameter :: test_overflow_sub3 = (i4_nmax - i4_pmax).EQ.(1_4)
43 !WARN: warning: INTEGER(4) subtraction overflowed
44 logical, parameter :: test_overflow_sub4 = (i4_pmax - i4_nmax).EQ.(-1_4)
45 logical, parameter :: test_no_overflow_sub1 = (i4_nmax - 0_4).EQ.i4_nmax
46 logical, parameter :: test_no_overflow_sub2 = (i4_pmax - (-0_4)).EQ.i4_pmax
47 logical, parameter :: test_no_overflow_sub3 = (i4_nmax - i4_nmax).EQ.0_4
48 logical, parameter :: test_no_overflow_sub4 = (i4_pmax - i4_pmax).EQ.0_4
51 !WARN: warning: INTEGER(4) multiplication overflowed
52 logical, parameter :: test_overflow_mult1 = (i4_pmax*2_4).EQ.(-2_4)
53 !WARN: warning: INTEGER(4) multiplication overflowed
54 logical, parameter :: test_overflow_mult2 = (i4_nmax*2_4).EQ.(0_4)
55 !WARN: warning: INTEGER(4) multiplication overflowed
56 logical, parameter :: test_overflow_mult3 = (i4_nmax*i4_nmax).EQ.(0_4)
57 !WARN: warning: INTEGER(4) multiplication overflowed
58 logical, parameter :: test_overflow_mult4 = (i4_pmax*i4_pmax).EQ.(1_4)
60 !WARN: warning: INTEGER(4) division overflowed
61 logical, parameter :: test_overflow_div1 = (i4_nmax/(-1_4)).EQ.(i4_nmax)
62 logical, parameter :: test_no_overflow_div1 = (i4_nmax/(-2_4)).EQ.(1_4 + i4_pmax/2_4)
63 logical, parameter :: test_no_overflow_div2 = (i4_nmax/i4_nmax).EQ.(1_4)
65 !WARN: warning: INTEGER(4) power overflowed
66 logical, parameter :: test_overflow_pow1 = (i4_pmax**2_4).EQ.(1_4)
67 !WARN: warning: INTEGER(4) power overflowed
68 logical, parameter :: test_overflow_pow3 = (i4_nmax**2_4).EQ.(0_4)
69 logical, parameter :: test_no_overflow_pow1 = ((-1_4)**i4_nmax).EQ.(1_4)
70 logical, parameter :: test_no_overflow_pow2 = ((-1_4)**i4_pmax).EQ.(-1_4)
72 end module
74 module real_tests
75 ! Test real(4) operation folding on edge cases (inf and NaN)
77 real(4), parameter :: r4_pmax = 3.4028235E38
78 real(4), parameter :: r4_nmax = -3.4028235E38
79 !WARN: warning: invalid argument on division
80 real(4), parameter :: r4_nan = 0._4/0._4
81 TEST_ISNAN(r4_nan)
82 !WARN: warning: division by zero
83 real(4), parameter :: r4_pinf = 1._4/0._4
84 !WARN: warning: division by zero
85 real(4), parameter :: r4_ninf = -1._4/0._4
87 logical, parameter :: test_r4_nan_parentheses1 = .NOT.(((r4_nan)).EQ.r4_nan)
88 logical, parameter :: test_r4_nan_parentheses2 = .NOT.(((r4_nan)).LT.r4_nan)
89 logical, parameter :: test_r4_nan_parentheses3 = .NOT.(((r4_nan)).GT.r4_nan)
90 logical, parameter :: test_r4_nan_parentheses4 = ((r4_nan)).NE.r4_nan
91 logical, parameter :: test_r4_pinf_parentheses = ((r4_pinf)).EQ.r4_pinf
92 logical, parameter :: test_r4_ninf_parentheses = ((r4_ninf)).EQ.r4_ninf
94 ! No warnings expected
95 logical, parameter :: test_r4_negation1 = (-r4_pmax).EQ.r4_nmax
96 logical, parameter :: test_r4_negation2 = (-r4_nmax).EQ.r4_pmax
97 logical, parameter :: test_r4_negation3 = (-r4_pinf).EQ.r4_ninf
98 logical, parameter :: test_r4_negation4 = (-r4_ninf).EQ.r4_pinf
99 logical, parameter :: test_r4_plus1 = (+r4_pmax).EQ.r4_pmax
100 logical, parameter :: test_r4_plus2 = (+r4_nmax).EQ.r4_nmax
101 logical, parameter :: test_r4_plus3 = (+r4_pinf).EQ.r4_pinf
102 logical, parameter :: test_r4_plus4 = (+r4_ninf).EQ.r4_ninf
103 ! NaN propagation , no warnings expected (quiet)
104 real(4), parameter :: r4_nan_minus = (-r4_nan)
105 TEST_ISNAN(r4_nan_minus)
106 real(4), parameter :: r4_nan_plus = (+r4_nan)
107 TEST_ISNAN(r4_nan_plus)
109 !WARN: warning: overflow on addition
110 logical, parameter :: test_inf_r4_add9 = (r4_pmax + r4_pmax).eq.(r4_pinf)
111 !WARN: warning: overflow on addition
112 logical, parameter :: test_inf_r4_add10 = (r4_nmax + r4_nmax).eq.(r4_ninf)
113 !WARN: warning: overflow on subtraction
114 logical, parameter :: test_inf_r4_sub9 = (r4_pmax - r4_nmax).eq.(r4_pinf)
115 !WARN: warning: overflow on subtraction
116 logical, parameter :: test_inf_r4_sub10 = (r4_nmax - r4_pmax).eq.(r4_ninf)
118 ! No warnings expected below (inf propagation).
119 logical, parameter :: test_inf_r4_add1 = (r4_pinf + r4_pinf).EQ.(r4_pinf)
120 logical, parameter :: test_inf_r4_add2 = (r4_ninf + r4_ninf).EQ.(r4_ninf)
121 logical, parameter :: test_inf_r4_add3 = (r4_pinf + r4_nmax).EQ.(r4_pinf)
122 logical, parameter :: test_inf_r4_add4 = (r4_pinf + r4_pmax).EQ.(r4_pinf)
123 logical, parameter :: test_inf_r4_add5 = (r4_ninf + r4_pmax).EQ.(r4_ninf)
124 logical, parameter :: test_inf_r4_add6 = (r4_ninf + r4_nmax).EQ.(r4_ninf)
125 logical, parameter :: test_inf_r4_add7 = (r4_ninf + 0._4).EQ.(r4_ninf)
126 logical, parameter :: test_inf_r4_add8 = (r4_pinf + 0._4).EQ.(r4_pinf)
128 !WARN: warning: invalid argument on subtraction
129 real(4), parameter :: r4_nan_sub1 = r4_pinf - r4_pinf
130 TEST_ISNAN(r4_nan_sub1)
131 !WARN: warning: invalid argument on subtraction
132 real(4), parameter :: r4_nan_sub2 = r4_ninf - r4_ninf
133 TEST_ISNAN(r4_nan_sub2)
134 !WARN: warning: invalid argument on addition
135 real(4), parameter :: r4_nan_add1 = r4_ninf + r4_pinf
136 TEST_ISNAN(r4_nan_add1)
137 !WARN: warning: invalid argument on addition
138 real(4), parameter :: r4_nan_add2 = r4_pinf + r4_ninf
139 TEST_ISNAN(r4_nan_add2)
141 ! No warnings expected here (quite NaN propagation)
142 real(4), parameter :: r4_nan_sub3 = 0._4 - r4_nan
143 TEST_ISNAN(r4_nan_sub3)
144 real(4), parameter :: r4_nan_sub4 = r4_nan - r4_pmax
145 TEST_ISNAN(r4_nan_sub4)
146 real(4), parameter :: r4_nan_sub5 = r4_nan - r4_nmax
147 TEST_ISNAN(r4_nan_sub5)
148 real(4), parameter :: r4_nan_sub6 = r4_nan - r4_nan
149 TEST_ISNAN(r4_nan_sub6)
150 real(4), parameter :: r4_nan_add3 = 0._4 + r4_nan
151 TEST_ISNAN(r4_nan_add3)
152 real(4), parameter :: r4_nan_add4 = r4_nan + r4_pmax
153 TEST_ISNAN(r4_nan_add4)
154 real(4), parameter :: r4_nan_add5 = r4_nmax + r4_nan
155 TEST_ISNAN(r4_nan_add5)
156 real(4), parameter :: r4_nan_add6 = r4_nan + r4_nan
157 TEST_ISNAN(r4_nan_add6)
159 !WARN: warning: overflow on multiplication
160 logical, parameter :: test_inf_r4_mult1 = (1.5_4*r4_pmax).eq.(r4_pinf)
161 !WARN: warning: overflow on multiplication
162 logical, parameter :: test_inf_r4_mult2 = (1.5_4*r4_nmax).eq.(r4_ninf)
163 !WARN: warning: overflow on division
164 logical, parameter :: test_inf_r4_div1 = (r4_nmax/(-0.5_4)).eq.(r4_pinf)
165 !WARN: warning: overflow on division
166 logical, parameter :: test_inf_r4_div2 = (r4_pmax/(-0.5_4)).eq.(r4_ninf)
168 ! No warnings expected below (inf propagation).
169 logical, parameter :: test_inf_r4_mult3 = (r4_pinf*r4_pinf).EQ.(r4_pinf)
170 logical, parameter :: test_inf_r4_mult4 = (r4_ninf*r4_ninf).EQ.(r4_pinf)
171 logical, parameter :: test_inf_r4_mult5 = (r4_pinf*0.1_4).EQ.(r4_pinf)
172 logical, parameter :: test_inf_r4_mult6 = (r4_ninf*r4_nmax).EQ.(r4_pinf)
173 logical, parameter :: test_inf_r4_div3 = (r4_pinf/0.).EQ.(r4_pinf)
174 logical, parameter :: test_inf_r4_div4 = (r4_ninf/0.).EQ.(r4_ninf)
175 logical, parameter :: test_inf_r4_div5 = (0./r4_pinf).EQ.(0.)
176 logical, parameter :: test_inf_r4_div6 = (0./r4_ninf).EQ.(0.)
177 logical, parameter :: test_inf_r4_div7 = (r4_pinf/r4_pmax).EQ.(r4_pinf)
178 logical, parameter :: test_inf_r4_div8 = (r4_pinf/r4_nmax).EQ.(r4_ninf)
179 logical, parameter :: test_inf_r4_div9 = (r4_nmax/r4_pinf).EQ.(0.)
180 logical, parameter :: test_inf_r4_div10 = (r4_nmax/r4_ninf).EQ.(0.)
182 !WARN: warning: invalid argument on division
183 real(4), parameter :: r4_nan_div1 = 0._4/0._4
184 TEST_ISNAN(r4_nan_div1)
185 !WARN: warning: invalid argument on division
186 real(4), parameter :: r4_nan_div2 = r4_ninf/r4_ninf
187 TEST_ISNAN(r4_nan_div2)
188 !WARN: warning: invalid argument on division
189 real(4), parameter :: r4_nan_div3 = r4_ninf/r4_pinf
190 TEST_ISNAN(r4_nan_div3)
191 !WARN: warning: invalid argument on division
192 real(4), parameter :: r4_nan_div4 = r4_pinf/r4_ninf
193 TEST_ISNAN(r4_nan_div4)
194 !WARN: warning: invalid argument on division
195 real(4), parameter :: r4_nan_div5 = r4_pinf/r4_pinf
196 TEST_ISNAN(r4_nan_div5)
197 !WARN: warning: invalid argument on multiplication
198 real(4), parameter :: r4_nan_mult1 = r4_pinf*0._4
199 TEST_ISNAN(r4_nan_mult1)
200 !WARN: warning: invalid argument on multiplication
201 real(4), parameter :: r4_nan_mult2 = 0._4*r4_ninf
202 TEST_ISNAN(r4_nan_mult2)
204 ! No warnings expected here (quite NaN propagation)
205 real(4), parameter :: r4_nan_div6 = 0._4/r4_nan
206 TEST_ISNAN(r4_nan_div6)
207 real(4), parameter :: r4_nan_div7 = r4_nan/r4_nan
208 TEST_ISNAN(r4_nan_div7)
209 real(4), parameter :: r4_nan_div8 = r4_nan/0._4
210 TEST_ISNAN(r4_nan_div8)
211 real(4), parameter :: r4_nan_div9 = r4_nan/1._4
212 TEST_ISNAN(r4_nan_div9)
213 real(4), parameter :: r4_nan_mult3 = r4_nan*1._4
214 TEST_ISNAN(r4_nan_mult3)
215 real(4), parameter :: r4_nan_mult4 = r4_nan*r4_nan
216 TEST_ISNAN(r4_nan_mult4)
217 real(4), parameter :: r4_nan_mult5 = 0._4*r4_nan
218 TEST_ISNAN(r4_nan_mult5)
220 ! TODO: ** operator folding
221 ! logical, parameter :: test_inf_r4_exp1 = (r4_pmax**2._4).EQ.(r4_pinf)
223 ! Relational operator edge cases (No warnings expected?)
224 logical, parameter :: test_inf_r4_eq1 = r4_pinf.EQ.r4_pinf
225 logical, parameter :: test_inf_r4_eq2 = r4_ninf.EQ.r4_ninf
226 logical, parameter :: test_inf_r4_eq3 = .NOT.(r4_pinf.EQ.r4_ninf)
227 logical, parameter :: test_inf_r4_eq4 = .NOT.(r4_pinf.EQ.r4_pmax)
229 logical, parameter :: test_inf_r4_ne1 = .NOT.(r4_pinf.NE.r4_pinf)
230 logical, parameter :: test_inf_r4_ne2 = .NOT.(r4_ninf.NE.r4_ninf)
231 logical, parameter :: test_inf_r4_ne3 = r4_pinf.NE.r4_ninf
232 logical, parameter :: test_inf_r4_ne4 = r4_pinf.NE.r4_pmax
234 logical, parameter :: test_inf_r4_gt1 = .NOT.(r4_pinf.GT.r4_pinf)
235 logical, parameter :: test_inf_r4_gt2 = .NOT.(r4_ninf.GT.r4_ninf)
236 logical, parameter :: test_inf_r4_gt3 = r4_pinf.GT.r4_ninf
237 logical, parameter :: test_inf_r4_gt4 = r4_pinf.GT.r4_pmax
239 logical, parameter :: test_inf_r4_lt1 = .NOT.(r4_pinf.LT.r4_pinf)
240 logical, parameter :: test_inf_r4_lt2 = .NOT.(r4_ninf.LT.r4_ninf)
241 logical, parameter :: test_inf_r4_lt3 = r4_ninf.LT.r4_pinf
242 logical, parameter :: test_inf_r4_lt4 = r4_pmax.LT.r4_pinf
244 logical, parameter :: test_inf_r4_ge1 = r4_pinf.GE.r4_pinf
245 logical, parameter :: test_inf_r4_ge2 = r4_ninf.GE.r4_ninf
246 logical, parameter :: test_inf_r4_ge3 = .NOT.(r4_ninf.GE.r4_pinf)
247 logical, parameter :: test_inf_r4_ge4 = .NOT.(r4_pmax.GE.r4_pinf)
249 logical, parameter :: test_inf_r4_le1 = r4_pinf.LE.r4_pinf
250 logical, parameter :: test_inf_r4_le2 = r4_ninf.LE.r4_ninf
251 logical, parameter :: test_inf_r4_le3 = .NOT.(r4_pinf.LE.r4_ninf)
252 logical, parameter :: test_inf_r4_le4 = .NOT.(r4_pinf.LE.r4_pmax)
254 ! Invalid relational argument
255 logical, parameter :: test_nan_r4_eq1 = .NOT.(r4_nan.EQ.r4_nan)
256 logical, parameter :: test_nan_r4_lt1 = .NOT.(r4_nan.LE.r4_nan)
257 logical, parameter :: test_nan_r4_gt1 = .NOT.(r4_nan.GT.r4_nan)
258 logical, parameter :: test_nan_r4_ne1 = r4_nan.NE.r4_nan
260 end module
262 ! TODO: edge case conversions
263 ! TODO: complex tests (or is real tests enough?)
265 ! Logical operation (with logical arguments) cannot overflow or be invalid.
266 ! CHARACTER folding operations may cause host memory exhaustion if the
267 ! string are very large. This will cause a fatal error for the program
268 ! doing folding (e.g. f18), so there is nothing very interesting to test here.