Debian Codegraph @

gcc/testsuite/gfortran.dg/out_of_range_2.f90

raw
1 ! { dg-do run }
2 ! { dg-additional-options "-funsigned" }
3 !
4 ! PR fortran/115788 - OUT_OF_RANGE
5
6 program p
7 use iso_fortran_env, only: int8, int64, uint8, uint64, real32, real64
8 implicit none
9 integer :: i
10 integer(int8) :: i1
11 integer(int64) :: i8
12 unsigned :: u
13 unsigned(uint8) :: u1
14 unsigned(uint64) :: u8
15 real(real32) :: r
16 real(real64) :: d
17 logical :: t = .true., f = .false.
18
19 real, parameter :: a(*) = [-0.5, 0.5, 254.5, 255.5]
20 logical, parameter :: l1(*) = OUT_OF_RANGE (a, 0U_uint8)
21 logical, parameter :: l2(*) = OUT_OF_RANGE (a, 0U_uint8, .true.)
22 logical, parameter :: expect1(*) = [.false.,.false.,.false.,.false.]
23 logical, parameter :: expect2(*) = [.true. ,.false.,.false.,.true. ]
24 real :: b(size(a)) = a
25
26 ! Check for correct truncation or rounding, compile-time
27 if (any (l1 .neqv. expect1)) stop 1
28 if (any (l2 .neqv. expect2)) stop 2
29
30 ! Check for correct truncation or rounding, run-time
31 if (any (OUT_OF_RANGE (a, 0U_uint8, f) .neqv. expect1)) stop 3
32 if (any (OUT_OF_RANGE (a, 0U_uint8, t) .neqv. expect2)) stop 4
33
34 if (any (OUT_OF_RANGE (b, 0U_uint8) .neqv. expect1)) stop 5
35 if (any (OUT_OF_RANGE (b, 0U_uint8, .false.) .neqv. expect1)) stop 6
36 if (any (OUT_OF_RANGE (b, 0U_uint8, .true.) .neqv. expect2)) stop 7
37 if (any (OUT_OF_RANGE (b, 0U_uint8, f) .neqv. expect1)) stop 8
38 if (any (OUT_OF_RANGE (b, 0U_uint8, t) .neqv. expect2)) stop 9
39
40 ! Miscellaneous "obvious" special cases
41 u1 = huge (0U_uint8)
42 u = huge (0U)
43 u8 = huge (0U_uint64)
44 r = huge (0._real32)
45 d = real (r, real64)
46 if (OUT_OF_RANGE (huge (0U_uint8), r)) stop 10
47 if (OUT_OF_RANGE (huge (0U_uint8), d)) stop 11
48 if (OUT_OF_RANGE (huge (0U_uint8), u)) stop 12
49 if (OUT_OF_RANGE (u1, u)) stop 13
50 if (OUT_OF_RANGE (r, d)) stop 14
51 if (OUT_OF_RANGE (d, r)) stop 15
52 if (OUT_OF_RANGE (u, r)) stop 16
53 if (OUT_OF_RANGE (u8, r)) stop 17
54 if (OUT_OF_RANGE (u, u8)) stop 18
55
56 if (OUT_OF_RANGE (real (u1), u1,f)) stop 19
57 if (OUT_OF_RANGE (real (u,real64), u,f)) stop 20
58
59 if (.not. OUT_OF_RANGE (u, u1)) stop 21
60 if (.not. OUT_OF_RANGE (u8, u)) stop 22
61 if (.not. OUT_OF_RANGE (r, u8)) stop 23
62 if (.not. OUT_OF_RANGE (d, u8)) stop 24
63
64 ! Check passing of optional argument
65 if (any (out_of_range_1 (b, f) .neqv. OUT_OF_RANGE (b, 0U_uint8, f))) stop 25
66 if (any (out_of_range_1 (b, t) .neqv. OUT_OF_RANGE (b, 0U_uint8, t))) stop 26
67 if (any (out_of_range_1 (b) .neqv. OUT_OF_RANGE (b, 0U_uint8) )) stop 27
68
69 if (any (out_of_range_2 (b,u1,f) .neqv. OUT_OF_RANGE (b,0U_uint8,f))) stop 28
70 if (any (out_of_range_2 (b,u1,t) .neqv. OUT_OF_RANGE (b,0U_uint8,t))) stop 29
71 if (any (out_of_range_2 (b,u1) .neqv. OUT_OF_RANGE (b,0U_uint8) )) stop 30
72
73 ! Conversions between integer and unsigned
74 i1 = huge (0_int8)
75 i = huge (0)
76 i8 = huge (0_int64)
77
78 if (OUT_OF_RANGE (i1, u1)) stop 31
79 if (OUT_OF_RANGE (i, u)) stop 32
80 if (OUT_OF_RANGE (i8, u8)) stop 33
81 if (OUT_OF_RANGE (u1, i)) stop 34
82
83 if (.not. OUT_OF_RANGE (-i1, u1)) stop 35
84 if (.not. OUT_OF_RANGE (-i, u)) stop 36
85 if (.not. OUT_OF_RANGE (-i8, u8)) stop 37
86
87 if (.not. OUT_OF_RANGE (u1, i1)) stop 38
88 if (.not. OUT_OF_RANGE (u, i)) stop 39
89 if (.not. OUT_OF_RANGE (u8, i8)) stop 40
90
91 contains
92
93 elemental logical function out_of_range_1 (x, round)
94 real, intent(in) :: x
95 logical, intent(in), optional :: round
96
97 out_of_range_1 = out_of_range (x, 0U_uint8, round)
98 end function out_of_range_1
99
100 elemental logical function out_of_range_2 (x, mold, round) result (res)
101 real, intent(in) :: x
102 class(*), intent(in) :: mold
103 logical, intent(in), optional :: round
104
105 select type (mold)
106 type is (integer(int8))
107 res = out_of_range (x, 0_int8, round)
108 type is (unsigned(uint8))
109 res = out_of_range (x, 0U_uint8, round)
110 class default
111 error stop 99
112 end select
113 end function out_of_range_2
114
115 end