1--TEST--
2Test intval() function
3--SKIPIF--
4<?php
5if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only");
6?>
7--FILE--
8<?php
9echo "*** Testing intval() with valid integer values ***\n";
10// different valid  integer values
11$valid_ints = array(
12                '0',
13                '1',
14                '-1',
15                '-2147483648', // max negative integer value
16                '-2147483647',
17                2147483647,  // max positive integer value
18                2147483640,
19                0x123B,      // integer as hexadecimal
20                '0x12ab',
21                '0Xfff',
22                '0XFA',
23                -0x80000000, // max negative integer as hexadecimal
24                '0x7fffffff',  // max positive integer as hexadecimal
25                0x7FFFFFFF,  // max positive integer as hexadecimal
26                '0123',        // integer as octal
27                01,       // should be quivalent to octal 1
28                -020000000000, // max negative integer as octal
29                017777777777,  // max positive integer as octal
30               );
31
32/* loop to check that intval() recognizes different
33   integer values, expected output:integer value in decimal notation for valid integer */
34
35echo "\n***Output with default base value ie 10 ***\n";
36foreach ($valid_ints as $value ) {
37   var_dump( intval($value) );
38}
39
40
41echo "\n***Output with base value of 10( explicitly passed as argument) ***\n";
42foreach ($valid_ints as $value ) {
43   var_dump( intval($value, 10) );
44}
45
46
47echo "\n***Output with base value  of 16 ***\n";
48foreach ($valid_ints as $value ) {
49   var_dump( intval($value, 16) );
50}
51
52echo "\n***Output with base value of 8 ***\n";
53foreach ($valid_ints as $value ) {
54   var_dump( intval($value, 8) );
55}
56
57echo "\n*** Testing intval() on non integer types ***\n";
58
59// get a resource type variable
60$fp = fopen (__FILE__, "r");
61fclose($fp);
62$dfp = opendir ( __DIR__ );
63closedir($dfp);
64
65// unset variable
66
67$unset_var = 10;
68unset ($unset_var);
69
70// other types in a array
71$not_int_types = array (
72  /* float values */
73  '-2147483649', // float value
74  '2147483648',  // float value
75  '-0x80000001', // float value, beyond max negative int
76  '0x800000001', // float value, beyond max positive int
77  '020000000001', // float value, beyond max positive int
78  '-020000000001', // float value, beyond max negative int
79  0.0,
80  -0.1,
81  1.0,
82  1e5,
83  -1e6,
84  1E8,
85  -1E9,
86  10.0000000000000000005,
87  10.5e+5,
88
89  /* resources */
90  $fp,
91  $dfp,
92
93  /* arrays */
94  array(),
95  array(0),
96  array(1),
97  array(NULL),
98  array(null),
99  array("string"),
100  array(true),
101  array(TRUE),
102  array(false),
103  array(FALSE),
104  array(1,2,3,4),
105  array(1 => "One", "two" => 2),
106
107  /* strings */
108  "",
109  '',
110  "0",
111  '0',
112  "1",
113  '1',
114  "\x01",
115  '\x01',
116  "\01",
117  '\01',
118  'string',
119  "string",
120  "true",
121  "FALSE",
122  'false',
123  'TRUE',
124  "NULL",
125  'null',
126
127  /* booleans */
128  true,
129  false,
130  TRUE,
131  FALSE,
132
133  /* undefined and unset vars */
134  @$unset_var,
135  @$undefined_var
136);
137
138
139/* loop through the $not_int_types to see working of
140   intval() on non integer types, expected output: integer value in decimal notation for valid integers */
141foreach ($not_int_types as $type ) {
142   var_dump( intval($type) );
143}
144
145echo "\n--- Done ---\n";
146
147
148?>
149--EXPECTF--
150*** Testing intval() with valid integer values ***
151
152***Output with default base value ie 10 ***
153int(0)
154int(1)
155int(-1)
156int(-2147483648)
157int(-2147483647)
158int(2147483647)
159int(2147483640)
160int(4667)
161int(0)
162int(0)
163int(0)
164int(-2147483648)
165int(0)
166int(2147483647)
167int(123)
168int(1)
169int(-2147483648)
170int(2147483647)
171
172***Output with base value of 10( explicitly passed as argument) ***
173int(0)
174int(1)
175int(-1)
176int(-2147483648)
177int(-2147483647)
178int(2147483647)
179int(2147483640)
180int(4667)
181int(0)
182int(0)
183int(0)
184int(-2147483648)
185int(0)
186int(2147483647)
187int(123)
188int(1)
189int(-2147483648)
190int(2147483647)
191
192***Output with base value  of 16 ***
193int(0)
194int(1)
195int(-1)
196int(-2147483648)
197int(-2147483648)
198int(2147483647)
199int(2147483640)
200int(4667)
201int(4779)
202int(4095)
203int(250)
204int(-2147483648)
205int(2147483647)
206int(2147483647)
207int(291)
208int(1)
209int(-2147483648)
210int(2147483647)
211
212***Output with base value of 8 ***
213int(0)
214int(1)
215int(-1)
216int(-9020)
217int(-9020)
218int(2147483647)
219int(2147483640)
220int(4667)
221int(0)
222int(0)
223int(0)
224int(-2147483648)
225int(0)
226int(2147483647)
227int(83)
228int(1)
229int(-2147483648)
230int(2147483647)
231
232*** Testing intval() on non integer types ***
233int(-2147483648)
234int(2147483647)
235int(0)
236int(0)
237int(2147483647)
238int(-2147483648)
239int(0)
240int(0)
241int(1)
242int(100000)
243int(-1000000)
244int(100000000)
245int(-1000000000)
246int(10)
247int(1050000)
248int(%d)
249int(%d)
250int(0)
251int(1)
252int(1)
253int(1)
254int(1)
255int(1)
256int(1)
257int(1)
258int(1)
259int(1)
260int(1)
261int(1)
262int(0)
263int(0)
264int(0)
265int(0)
266int(1)
267int(1)
268int(0)
269int(0)
270int(0)
271int(0)
272int(0)
273int(0)
274int(0)
275int(0)
276int(0)
277int(0)
278int(0)
279int(0)
280int(1)
281int(0)
282int(1)
283int(0)
284int(0)
285int(0)
286
287--- Done ---
288