2009-10-05 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / testsuite / gfortran.dg / int_1.f90
blob853578e26921704a3c16e580e1b873410040b32a
1 ! { dg-do run }
2 ! { dg-options "-std=gnu" }
4 ! 13.7.53 INT(A [, KIND])
6 ! Description. Convert to integer type.
7 ! Class. Elemental function.
8 ! Arguments.
9 ! A shall be of type integer, real, or complex,
10 ! or a boz-literal-constant .
11 ! KIND (optional) shall be a scalar integer initialization expression.
13 ! Result Characteristics. Integer. If KIND is present, the kind type
14 ! parameter is that specified by the value of KIND; otherwise, the
15 ! kind type parameter is that of default integer type.
17 ! Result Value.
19 ! Case (1): If A is of type integer, INT (A) = A.
21 ! Case (2): If A is of type real, there are two cases:
22 ! (a) if |A| < 1, INT (A) has the value 0
23 ! (b) if |A| .ge. 1, INT (A) is the integer whose magnitude is the
24 ! largest integer that does not exceed the magnitude of A and
25 ! whose sign is the same as the sign of A.
27 ! Case (3): If A is of type complex, INT(A) = INT(REAL(A, KIND(A))).
29 ! Case (4): If A is a boz-literal-constant, it is treated as if it were
30 ! an int-literal-constant with a kind-param that specifies the
31 ! representation method with the largest decimal exponent range
32 ! supported by the processor.
34 ! Example. INT (­3.7) has the value ­3.
36 module mykinds
37 integer, parameter :: ik1 = selected_int_kind(2)
38 integer, parameter :: ik2 = selected_int_kind(4)
39 integer, parameter :: ik4 = selected_int_kind(9)
40 integer, parameter :: ik8 = selected_int_kind(18)
41 integer, parameter :: sp = selected_real_kind(6,30)
42 integer, parameter :: dp = selected_real_kind(15,300)
43 integer, parameter :: ck = kind('a')
44 end module mykinds
46 program test_int
48 use mykinds
50 integer(ik1) i1
51 integer(ik2) i2
52 integer(ik4) i4
53 integer(ik8) i8
54 real(sp) r4
55 real(dp) r8
56 complex(sp) c4
57 complex(dp) c8
59 ! Case 1
61 i1 = int(-3)
62 i2 = int(-3)
63 i4 = int(-3)
64 i8 = int(-3)
65 if (i1 /= -3_ik1 .or. i2 /= -3_ik2) call abort
66 if (i4 /= -3_ik4 .or. i8 /= -3_ik8) call abort
68 i1 = int(5, ik1)
69 i2 = int(i1, ik2)
70 i4 = int(i1, ik4)
71 i8 = int(i1, ik8)
72 if (i1 /= 5_ik1 .or. i2 /= 5_ik2) call abort
73 if (i4 /= 5_ik4 .or. i8 /= 5_ik8) call abort
75 i8 = int(10, ik8)
76 i1 = int(i8, ik1)
77 i2 = int(i8, ik2)
78 i4 = int(i8, ik4)
79 if (i1 /= 10_ik1 .or. i2 /= 10_ik2) call abort
80 if (i4 /= 10_ik4 .or. i8 /= 10_ik8) call abort
82 ! case 2(b)
84 r4 = -3.7_sp
85 i1 = int(r4, ik1)
86 i2 = int(r4, ik2)
87 i4 = int(r4, ik4)
88 i8 = int(r4, ik8)
89 if (i1 /= -3_ik1 .or. i2 /= -3_ik2) call abort
90 if (i4 /= -3_ik4 .or. i8 /= -3_ik8) call abort
92 r8 = -3.7_dp
93 i1 = int(r8, ik1)
94 i2 = int(r8, ik2)
95 i4 = int(r8, ik4)
96 i8 = int(r8, ik8)
97 if (i1 /= -3_ik1 .or. i2 /= -3_ik2) call abort
98 if (i4 /= -3_ik4 .or. i8 /= -3_ik8) call abort
100 ! Case 2(a)
102 r4 = -3.7E-1_sp
103 i1 = int(r4, ik1)
104 i2 = int(r4, ik2)
105 i4 = int(r4, ik4)
106 i8 = int(r4, ik8)
107 if (i1 /= 0_ik1 .or. i2 /= 0_ik2) call abort
108 if (i4 /= 0_ik4 .or. i8 /= 0_ik8) call abort
110 r8 = -3.7E-1_dp
111 i1 = int(r8, ik1)
112 i2 = int(r8, ik2)
113 i4 = int(r8, ik4)
114 i8 = int(r8, ik8)
115 if (i1 /= 0_ik1 .or. i2 /= 0_ik2) call abort
116 if (i4 /= 0_ik4 .or. i8 /= 0_ik8) call abort
118 ! Case 3
120 c4 = (-3.7E-1_sp,3.7E-1_sp)
121 i1 = int(c4, ik1)
122 i2 = int(c4, ik2)
123 i4 = int(c4, ik4)
124 i8 = int(c4, ik8)
125 if (i1 /= 0_ik1 .or. i2 /= 0_ik2) call abort
126 if (i4 /= 0_ik4 .or. i8 /= 0_ik8) call abort
128 c8 = (-3.7E-1_dp,3.7E-1_dp)
129 i1 = int(c8, ik1)
130 i2 = int(c8, ik2)
131 i4 = int(c8, ik4)
132 i8 = int(c8, ik8)
133 if (i1 /= 0_ik1 .or. i2 /= 0_ik2) call abort
134 if (i4 /= 0_ik4 .or. i8 /= 0_ik8) call abort
136 c4 = (-3.7_sp,3.7_sp)
137 i1 = int(c4, ik1)
138 i2 = int(c4, ik2)
139 i4 = int(c4, ik4)
140 i8 = int(c4, ik8)
141 if (i1 /= -3_ik1 .or. i2 /= -3_ik2) call abort
142 if (i4 /= -3_ik4 .or. i8 /= -3_ik8) call abort
144 c8 = (3.7_dp,3.7_dp)
145 i1 = int(c8, ik1)
146 i2 = int(c8, ik2)
147 i4 = int(c8, ik4)
148 i8 = int(c8, ik8)
149 if (i1 /= 3_ik1 .or. i2 /= 3_ik2) call abort
150 if (i4 /= 3_ik4 .or. i8 /= 3_ik8) call abort
152 ! Case 4
154 i1 = int(b'0011', ik1)
155 i2 = int(b'0011', ik2)
156 i4 = int(b'0011', ik4)
157 i8 = int(b'0011', ik8)
158 if (i1 /= 3_ik1 .or. i2 /= 3_ik2) call abort
159 if (i4 /= 3_ik4 .or. i8 /= 3_ik8) call abort
160 i1 = int(o'0011', ik1)
161 i2 = int(o'0011', ik2)
162 i4 = int(o'0011', ik4)
163 i8 = int(o'0011', ik8)
164 if (i1 /= 9_ik1 .or. i2 /= 9_ik2) call abort
165 if (i4 /= 9_ik4 .or. i8 /= 9_ik8) call abort
166 i1 = int(z'0011', ik1)
167 i2 = int(z'0011', ik2)
168 i4 = int(z'0011', ik4)
169 i8 = int(z'0011', ik8)
170 if (i1 /= 17_ik1 .or. i2 /= 17_ik2) call abort
171 if (i4 /= 17_ik4 .or. i8 /= 17_ik8) call abort
173 end program test_int
175 ! { dg-final { cleanup-modules "mykinds" } }