end.phpt 5.2 KB

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