1--TEST--
2Test end() function
3--SKIPIF--
4<?php
5if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only");
6?>
7--INI--
8precision=14
9--FILE--
10<?php
11/* Prototype: mixed end ( array &$array );
12   Description: Advances internal pointer of array to last element, and returns its value.
13*/
14
15$arrays = array (
16  array( 0 ),
17  range(1, 100 ),
18  range('a', 'z', 2 ),
19  array("a" => "A", 2 => "B", "C" => 3, 4 => 4, "one" => 1, "" => NULL ),
20  array(1, array(1, 2 => 3 ), "one" => 1, "5" => 5 ),
21  array(-1, -2, -3, -4, "-0.005" => "neg0.005", 2.0 => "float2", "neg.9" => -.9 ),
22  array(1.0005, 2.000000, -3.000000, -4.9999999 ),
23  array(true, false),
24  array("PHP", "Web2.0", "SOA"),
25  array(1, array() ),
26  array(1, 2, "" ),
27  array(" "),
28  array(2147483647, 2147483648, -2147483647, -2147483648 ),
29  array(0x7FFFFFFF, -0x80000000, 017777777777, -020000000000 ),
30  array(-.6700000E+3, -4.10003E+3, 1e-5, -1E+5, 000002.00 )
31);
32/* loop through $arrays to print the last element of each sub-array */
33echo "*** Testing end() on different arrays ***\n";
34$counter = 1;
35foreach ($arrays as $sub_array){
36  echo "-- Iteration $counter --\n";
37  var_dump( end($sub_array) );
38  /* ensure that internal pointer is moved to last element */
39  var_dump( current($sub_array) );
40  $counter++;
41}
42
43/* checking for end() on sub-arrays */
44echo "\n*** Testing end() with sub-arrays ***\n";
45$test_array = array(1, array(1 => "one", "two" => 2, "" => "f") );
46var_dump( end($test_array) );
47var_dump( end($test_array[1]) );
48
49/* checking working of end() when array elements are deleted */
50echo "\n*** Testing end() when array elements are deleted ***\n";
51$array_test = array("a", "b", "d", 7, "u" => "U", -4, "-.008" => "neg.008");
52
53// remove first element from array
54echo "\n-- Remove first element from array --\n";
55unset($array_test[0]);
56var_dump( end($array_test) );
57
58// remove last element from array, rewind and check end()
59echo "\n-- Remove last element from array --\n";
60unset($array_test['-.008']);
61var_dump( end($array_test) );
62reset( $array_test );
63var_dump( end($array_test) );
64
65// remove any element  !first, !last, rewind and check end()
66echo "\n-- Remove any element from array apart from first and last element --\n";
67unset($array_test[7]);
68var_dump( end($array_test) );
69var_dump( reset($array_test) );
70var_dump( end($array_test) );
71
72/* Checking on OBJECTS type */
73echo "\n*** Testing end() on objects ***\n";
74class foo
75{
76  function __toString() {
77    return "Object";
78  }
79}
80class foo1
81{
82  function __toString() {
83    return "Object1";
84  }
85}
86
87$object1 = new foo(); //new object created
88$object2 = new foo1();
89
90$array_object = array();
91$array_object[0] = &$object1;
92$array_object[1] = &$object2;
93var_dump( end($array_object) );
94var_dump($array_object);
95
96/* Checking on RESOURCE type */
97echo "\n*** Testing end() on resource type ***\n";
98//file type resource
99$file_handle = fopen(__FILE__, "r");
100
101//directory type resource
102$dir_handle = opendir( dirname(__FILE__) );
103
104//store resources in array
105$resources = array($file_handle, $dir_handle);
106var_dump( end($resources) );
107var_dump( current($resources) );
108
109echo "\n*** Testing error conditions ***\n";
110/* checking for unexpected number of arguments */
111var_dump( end() );
112var_dump( end($array[0], $array[0]) );
113
114/* checking for unexpected type of arguments */
115$var=1;
116$var1="string";
117var_dump( end($var) );
118var_dump( end($var1) );
119
120/* checking null array */
121$null_array = array();
122var_dump( end($null_array) );
123
124echo "Done\n";
125
126?>
127--CLEAN--
128/* cleaning resource handles */
129fclose( $file_handle );  //file resource handle deleted
130closedir( $dir_handle );  //dir resource handle deleted
131--EXPECTF--
132*** Testing end() on different arrays ***
133-- Iteration 1 --
134int(0)
135int(0)
136-- Iteration 2 --
137int(100)
138int(100)
139-- Iteration 3 --
140string(1) "y"
141string(1) "y"
142-- Iteration 4 --
143NULL
144NULL
145-- Iteration 5 --
146int(5)
147int(5)
148-- Iteration 6 --
149float(-0.9)
150float(-0.9)
151-- Iteration 7 --
152float(-4.9999999)
153float(-4.9999999)
154-- Iteration 8 --
155bool(false)
156bool(false)
157-- Iteration 9 --
158string(3) "SOA"
159string(3) "SOA"
160-- Iteration 10 --
161array(0) {
162}
163array(0) {
164}
165-- Iteration 11 --
166string(0) ""
167string(0) ""
168-- Iteration 12 --
169string(1) " "
170string(1) " "
171-- Iteration 13 --
172int(-2147483648)
173int(-2147483648)
174-- Iteration 14 --
175int(-2147483648)
176int(-2147483648)
177-- Iteration 15 --
178float(2)
179float(2)
180
181*** Testing end() with sub-arrays ***
182array(3) {
183  [1]=>
184  string(3) "one"
185  ["two"]=>
186  int(2)
187  [""]=>
188  string(1) "f"
189}
190string(1) "f"
191
192*** Testing end() when array elements are deleted ***
193
194-- Remove first element from array --
195string(7) "neg.008"
196
197-- Remove last element from array --
198int(-4)
199int(-4)
200
201-- Remove any element from array apart from first and last element --
202int(-4)
203string(1) "b"
204int(-4)
205
206*** Testing end() on objects ***
207object(foo1)#%d (0) {
208}
209array(2) {
210  [0]=>
211  &object(foo)#%d (0) {
212  }
213  [1]=>
214  &object(foo1)#%d (0) {
215  }
216}
217
218*** Testing end() on resource type ***
219resource(%d) of type (stream)
220resource(%d) of type (stream)
221
222*** Testing error conditions ***
223
224Warning: end() expects exactly 1 parameter, 0 given in %s on line %d
225NULL
226
227Warning: end() expects exactly 1 parameter, 2 given in %s on line %d
228NULL
229
230Warning: end() expects parameter 1 to be array, int given in %s on line %d
231NULL
232
233Warning: end() expects parameter 1 to be array, string given in %s on line %d
234NULL
235bool(false)
236Done
237