xref: /PHP-5.4/ext/standard/tests/array/end.phpt (revision c80613cc)
1--TEST--
2Test end() function
3--SKIPIF--
4<?php
5if (PHP_INT_SIZE != 4) die("skip this test is for 32bit 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/* cleaning resource handles */
128fclose( $file_handle );  //file resource handle deleted
129closedir( $dir_handle );  //dir resource handle deleted
130
131?>
132--EXPECTF--
133*** Testing end() on different arrays ***
134-- Iteration 1 --
135int(0)
136int(0)
137-- Iteration 2 --
138int(100)
139int(100)
140-- Iteration 3 --
141string(1) "y"
142string(1) "y"
143-- Iteration 4 --
144NULL
145NULL
146-- Iteration 5 --
147int(5)
148int(5)
149-- Iteration 6 --
150float(-0.9)
151float(-0.9)
152-- Iteration 7 --
153float(-4.9999999)
154float(-4.9999999)
155-- Iteration 8 --
156bool(false)
157bool(false)
158-- Iteration 9 --
159string(3) "SOA"
160string(3) "SOA"
161-- Iteration 10 --
162array(0) {
163}
164array(0) {
165}
166-- Iteration 11 --
167string(0) ""
168string(0) ""
169-- Iteration 12 --
170string(1) " "
171string(1) " "
172-- Iteration 13 --
173float(-2147483648)
174float(-2147483648)
175-- Iteration 14 --
176float(-2147483648)
177float(-2147483648)
178-- Iteration 15 --
179float(2)
180float(2)
181
182*** Testing end() with sub-arrays ***
183array(3) {
184  [1]=>
185  string(3) "one"
186  ["two"]=>
187  int(2)
188  [""]=>
189  string(1) "f"
190}
191string(1) "f"
192
193*** Testing end() when array elements are deleted ***
194
195-- Remove first element from array --
196string(7) "neg.008"
197
198-- Remove last element from array --
199int(-4)
200int(-4)
201
202-- Remove any element from array apart from first and last element --
203int(-4)
204string(1) "b"
205int(-4)
206
207*** Testing end() on objects ***
208object(foo1)#%d (0) {
209}
210array(2) {
211  [0]=>
212  &object(foo)#%d (0) {
213  }
214  [1]=>
215  &object(foo1)#%d (0) {
216  }
217}
218
219*** Testing end() on resource type ***
220resource(%d) of type (stream)
221resource(%d) of type (stream)
222
223*** Testing error conditions ***
224
225Warning: end() expects exactly 1 parameter, 0 given in %s on line %d
226NULL
227
228Warning: end() expects exactly 1 parameter, 2 given in %s on line %d
229NULL
230
231Warning: end() expects parameter 1 to be array, integer given in %s on line %d
232NULL
233
234Warning: end() expects parameter 1 to be array, string given in %s on line %d
235NULL
236bool(false)
237Done
238