end.phpt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. $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 */
  29. echo "*** Testing end() on different arrays ***\n";
  30. $counter = 1;
  31. foreach ($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. /* checking for end() on sub-arrays */
  39. echo "\n*** Testing end() with sub-arrays ***\n";
  40. $test_array = array(1, array(1 => "one", "two" => 2, "" => "f") );
  41. var_dump( end($test_array) );
  42. var_dump( end($test_array[1]) );
  43. /* checking working of end() when array elements are deleted */
  44. echo "\n*** Testing end() when array elements are deleted ***\n";
  45. $array_test = array("a", "b", "d", 7, "u" => "U", -4, "-.008" => "neg.008");
  46. // remove first element from array
  47. echo "\n-- Remove first element from array --\n";
  48. unset($array_test[0]);
  49. var_dump( end($array_test) );
  50. // remove last element from array, rewind and check end()
  51. echo "\n-- Remove last element from array --\n";
  52. unset($array_test['-.008']);
  53. var_dump( end($array_test) );
  54. reset( $array_test );
  55. var_dump( end($array_test) );
  56. // remove any element !first, !last, rewind and check end()
  57. echo "\n-- Remove any element from array apart from first and last element --\n";
  58. unset($array_test[7]);
  59. var_dump( end($array_test) );
  60. var_dump( reset($array_test) );
  61. var_dump( end($array_test) );
  62. /* Checking on OBJECTS type */
  63. echo "\n*** Testing end() on objects ***\n";
  64. class foo
  65. {
  66. function __toString() {
  67. return "Object";
  68. }
  69. }
  70. class foo1
  71. {
  72. function __toString() {
  73. return "Object1";
  74. }
  75. }
  76. $object1 = new foo(); //new object created
  77. $object2 = new foo1();
  78. $array_object = array();
  79. $array_object[0] = &$object1;
  80. $array_object[1] = &$object2;
  81. var_dump( end($array_object) );
  82. var_dump($array_object);
  83. /* Checking on RESOURCE type */
  84. echo "\n*** Testing end() on resource type ***\n";
  85. //file type resource
  86. $file_handle = fopen(__FILE__, "r");
  87. //directory type resource
  88. $dir_handle = opendir( __DIR__ );
  89. //store resources in array
  90. $resources = array($file_handle, $dir_handle);
  91. var_dump( end($resources) );
  92. var_dump( current($resources) );
  93. echo "Done\n";
  94. /* cleaning resource handles */
  95. fclose( $file_handle ); //file resource handle deleted
  96. closedir( $dir_handle ); //dir resource handle deleted
  97. ?>
  98. --EXPECTF--
  99. *** Testing end() on different arrays ***
  100. -- Iteration 1 --
  101. int(0)
  102. int(0)
  103. -- Iteration 2 --
  104. int(100)
  105. int(100)
  106. -- Iteration 3 --
  107. string(1) "y"
  108. string(1) "y"
  109. -- Iteration 4 --
  110. NULL
  111. NULL
  112. -- Iteration 5 --
  113. int(5)
  114. int(5)
  115. -- Iteration 6 --
  116. float(-0.9)
  117. float(-0.9)
  118. -- Iteration 7 --
  119. float(-4.9999999)
  120. float(-4.9999999)
  121. -- Iteration 8 --
  122. bool(false)
  123. bool(false)
  124. -- Iteration 9 --
  125. string(3) "SOA"
  126. string(3) "SOA"
  127. -- Iteration 10 --
  128. array(0) {
  129. }
  130. array(0) {
  131. }
  132. -- Iteration 11 --
  133. string(0) ""
  134. string(0) ""
  135. -- Iteration 12 --
  136. string(1) " "
  137. string(1) " "
  138. -- Iteration 13 --
  139. float(-2147483648)
  140. float(-2147483648)
  141. -- Iteration 14 --
  142. float(-2147483648)
  143. float(-2147483648)
  144. -- Iteration 15 --
  145. float(2)
  146. float(2)
  147. *** Testing end() with sub-arrays ***
  148. array(3) {
  149. [1]=>
  150. string(3) "one"
  151. ["two"]=>
  152. int(2)
  153. [""]=>
  154. string(1) "f"
  155. }
  156. string(1) "f"
  157. *** Testing end() when array elements are deleted ***
  158. -- Remove first element from array --
  159. string(7) "neg.008"
  160. -- Remove last element from array --
  161. int(-4)
  162. int(-4)
  163. -- Remove any element from array apart from first and last element --
  164. int(-4)
  165. string(1) "b"
  166. int(-4)
  167. *** Testing end() on objects ***
  168. object(foo1)#%d (0) {
  169. }
  170. array(2) {
  171. [0]=>
  172. &object(foo)#%d (0) {
  173. }
  174. [1]=>
  175. &object(foo1)#%d (0) {
  176. }
  177. }
  178. *** Testing end() on resource type ***
  179. resource(%d) of type (stream)
  180. resource(%d) of type (stream)
  181. Done