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$arrays = array (
12  array( 0 ),
13  range(1, 100 ),
14  range('a', 'z', 2 ),
15  array("a" => "A", 2 => "B", "C" => 3, 4 => 4, "one" => 1, "" => NULL ),
16  array(1, array(1, 2 => 3 ), "one" => 1, "5" => 5 ),
17  array(-1, -2, -3, -4, "-0.005" => "neg0.005", 2.0 => "float2", "neg.9" => -.9 ),
18  array(1.0005, 2.000000, -3.000000, -4.9999999 ),
19  array(true, false),
20  array("PHP", "Web2.0", "SOA"),
21  array(1, array() ),
22  array(1, 2, "" ),
23  array(" "),
24  array(2147483647, 2147483648, -2147483647, -2147483648 ),
25  array(0x7FFFFFFF, -0x80000000, 017777777777, -020000000000 ),
26  array(-.6700000E+3, -4.10003E+3, 1e-5, -1E+5, 000002.00 )
27);
28/* loop through $arrays to print the last element of each sub-array */
29echo "*** Testing end() on different arrays ***\n";
30$counter = 1;
31foreach ($arrays as $sub_array){
32  echo "-- Iteration $counter --\n";
33  var_dump( end($sub_array) );
34  /* ensure that internal pointer is moved to last element */
35  var_dump( current($sub_array) );
36  $counter++;
37}
38
39/* checking for end() on sub-arrays */
40echo "\n*** Testing end() with sub-arrays ***\n";
41$test_array = array(1, array(1 => "one", "two" => 2, "" => "f") );
42var_dump( end($test_array) );
43var_dump( end($test_array[1]) );
44
45/* checking working of end() when array elements are deleted */
46echo "\n*** Testing end() when array elements are deleted ***\n";
47$array_test = array("a", "b", "d", 7, "u" => "U", -4, "-.008" => "neg.008");
48
49// remove first element from array
50echo "\n-- Remove first element from array --\n";
51unset($array_test[0]);
52var_dump( end($array_test) );
53
54// remove last element from array, rewind and check end()
55echo "\n-- Remove last element from array --\n";
56unset($array_test['-.008']);
57var_dump( end($array_test) );
58reset( $array_test );
59var_dump( end($array_test) );
60
61// remove any element  !first, !last, rewind and check end()
62echo "\n-- Remove any element from array apart from first and last element --\n";
63unset($array_test[7]);
64var_dump( end($array_test) );
65var_dump( reset($array_test) );
66var_dump( end($array_test) );
67
68/* Checking on OBJECTS type */
69echo "\n*** Testing end() on objects ***\n";
70class foo
71{
72  function __toString() {
73    return "Object";
74  }
75}
76class foo1
77{
78  function __toString() {
79    return "Object1";
80  }
81}
82
83$object1 = new foo(); //new object created
84$object2 = new foo1();
85
86$array_object = array();
87$array_object[0] = &$object1;
88$array_object[1] = &$object2;
89var_dump( end($array_object) );
90var_dump($array_object);
91
92/* Checking on RESOURCE type */
93echo "\n*** Testing end() on resource type ***\n";
94//file type resource
95$file_handle = fopen(__FILE__, "r");
96
97//directory type resource
98$dir_handle = opendir( __DIR__ );
99
100//store resources in array
101$resources = array($file_handle, $dir_handle);
102var_dump( end($resources) );
103var_dump( current($resources) );
104
105echo "Done\n";
106
107?>
108--EXPECTF--
109*** Testing end() on different arrays ***
110-- Iteration 1 --
111int(0)
112int(0)
113-- Iteration 2 --
114int(100)
115int(100)
116-- Iteration 3 --
117string(1) "y"
118string(1) "y"
119-- Iteration 4 --
120NULL
121NULL
122-- Iteration 5 --
123int(5)
124int(5)
125-- Iteration 6 --
126float(-0.9)
127float(-0.9)
128-- Iteration 7 --
129float(-4.9999999)
130float(-4.9999999)
131-- Iteration 8 --
132bool(false)
133bool(false)
134-- Iteration 9 --
135string(3) "SOA"
136string(3) "SOA"
137-- Iteration 10 --
138array(0) {
139}
140array(0) {
141}
142-- Iteration 11 --
143string(0) ""
144string(0) ""
145-- Iteration 12 --
146string(1) " "
147string(1) " "
148-- Iteration 13 --
149int(-2147483648)
150int(-2147483648)
151-- Iteration 14 --
152int(-2147483648)
153int(-2147483648)
154-- Iteration 15 --
155float(2)
156float(2)
157
158*** Testing end() with sub-arrays ***
159array(3) {
160  [1]=>
161  string(3) "one"
162  ["two"]=>
163  int(2)
164  [""]=>
165  string(1) "f"
166}
167string(1) "f"
168
169*** Testing end() when array elements are deleted ***
170
171-- Remove first element from array --
172string(7) "neg.008"
173
174-- Remove last element from array --
175int(-4)
176int(-4)
177
178-- Remove any element from array apart from first and last element --
179int(-4)
180string(1) "b"
181int(-4)
182
183*** Testing end() on objects ***
184object(foo1)#%d (0) {
185}
186array(2) {
187  [0]=>
188  &object(foo)#%d (0) {
189  }
190  [1]=>
191  &object(foo1)#%d (0) {
192  }
193}
194
195*** Testing end() on resource type ***
196resource(%d) of type (stream)
197resource(%d) of type (stream)
198Done
199