005.phpt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. --TEST--
  2. Test array_shift() function
  3. --FILE--
  4. <?php
  5. /* Prototype: mixed array_shift( array &array );
  6. * Description: Shifts the first value of the array off and returns it.
  7. */
  8. array_shift($GLOBALS);
  9. $empty_array = array();
  10. $number = 5;
  11. $str = "abc";
  12. /* Various combinations of arrays to be used for the test */
  13. $mixed_array = array(
  14. array(),
  15. array( 1,2,3,4,5,6,7,8,9 ),
  16. array( "One", "_Two", "Three", "Four", "Five" ),
  17. array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ),
  18. array( "a" => "aaa", "A" => "AAA", "c" => "ccc", "d" => "ddd", "e" => "eee" ),
  19. array( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five" ),
  20. array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ),
  21. array( "f" => "fff", "1" => "one", 4 => 6, "" => "blank", 2.4 => "float", "F" => "FFF",
  22. "blank" => "", 3.7 => 3.7, 5.4 => 7, 6 => 8.6, '5' => "Five", "4name" => "jonny", "a" => NULL, NULL => 3 ),
  23. array( 12, "name", 'age', '45' ),
  24. array( array("oNe", "tWo", 4), array(10, 20, 30, 40, 50), array() ),
  25. array( "one" => 1, "one" => 2, "three" => 3, 3, 4, 3 => 33, 4 => 44, 5, 6,
  26. 5.4 => 54, 5.7 => 57, "5.4" => 554, "5.7" => 557 )
  27. );
  28. /* Testing Error Conditions */
  29. echo "\n*** Testing Error Conditions ***\n";
  30. /* Zero argument */
  31. var_dump( array_shift() );
  32. /* Scalar argument */
  33. var_dump( array_shift($number) );
  34. /* String argument */
  35. var_dump( array_shift($str) );
  36. /* Invalid Number of arguments */
  37. var_dump( array_shift($mixed_array[1],$mixed_array[2]) );
  38. /* Empty Array as argument */
  39. var_dump( array_shift($empty_array) );
  40. /* Loop to test normal functionality with different arrays inputs */
  41. echo "\n*** Testing with various array inputs ***\n";
  42. $counter = 1;
  43. foreach( $mixed_array as $sub_array ) {
  44. echo "\n-- Input Array for Iteration $counter is -- \n";
  45. print_r( $sub_array );
  46. echo "\nOutput after shift is :\n";
  47. var_dump( array_shift($sub_array) );
  48. $counter++;
  49. }
  50. /*Checking for internal array pointer beint reset when shift is called */
  51. echo"\n*** Checking for internal array pointer being reset when shift is called ***\n";
  52. echo "\nCurrent Element is : ";
  53. var_dump( current($mixed_array[1]) );
  54. echo "\nNext Element is : ";
  55. var_dump( next($mixed_array[1]) );
  56. echo "\nNext Element is : ";
  57. var_dump( next($mixed_array[1]) );
  58. echo "\nshifted Element is : ";
  59. var_dump( array_shift($mixed_array[1]) );
  60. echo "\nCurrent Element after shift operation is: ";
  61. var_dump( current($mixed_array[1]) );
  62. echo"Done";
  63. ?>
  64. --EXPECTF--
  65. *** Testing Error Conditions ***
  66. Warning: array_shift() expects exactly 1 parameter, 0 given in %s on line %d
  67. NULL
  68. Warning: array_shift() expects parameter 1 to be array, integer given in %s on line %d
  69. NULL
  70. Warning: array_shift() expects parameter 1 to be array, string given in %s on line %d
  71. NULL
  72. Warning: array_shift() expects exactly 1 parameter, 2 given in %s on line %d
  73. NULL
  74. NULL
  75. *** Testing with various array inputs ***
  76. -- Input Array for Iteration 1 is --
  77. Array
  78. (
  79. )
  80. Output after shift is :
  81. NULL
  82. -- Input Array for Iteration 2 is --
  83. Array
  84. (
  85. [0] => 1
  86. [1] => 2
  87. [2] => 3
  88. [3] => 4
  89. [4] => 5
  90. [5] => 6
  91. [6] => 7
  92. [7] => 8
  93. [8] => 9
  94. )
  95. Output after shift is :
  96. int(1)
  97. -- Input Array for Iteration 3 is --
  98. Array
  99. (
  100. [0] => One
  101. [1] => _Two
  102. [2] => Three
  103. [3] => Four
  104. [4] => Five
  105. )
  106. Output after shift is :
  107. string(3) "One"
  108. -- Input Array for Iteration 4 is --
  109. Array
  110. (
  111. [0] => 6
  112. [1] => six
  113. [2] => 7
  114. [3] => seven
  115. [4] => 8
  116. [5] => eight
  117. [6] => 9
  118. [7] => nine
  119. )
  120. Output after shift is :
  121. int(6)
  122. -- Input Array for Iteration 5 is --
  123. Array
  124. (
  125. [a] => aaa
  126. [A] => AAA
  127. [c] => ccc
  128. [d] => ddd
  129. [e] => eee
  130. )
  131. Output after shift is :
  132. string(3) "aaa"
  133. -- Input Array for Iteration 6 is --
  134. Array
  135. (
  136. [1] => one
  137. [2] => two
  138. [3] => three
  139. [4] => four
  140. [5] => five
  141. )
  142. Output after shift is :
  143. string(3) "one"
  144. -- Input Array for Iteration 7 is --
  145. Array
  146. (
  147. [1] => one
  148. [2] => two
  149. [3] => 7
  150. [4] => four
  151. [5] => five
  152. )
  153. Output after shift is :
  154. string(3) "one"
  155. -- Input Array for Iteration 8 is --
  156. Array
  157. (
  158. [f] => fff
  159. [1] => one
  160. [4] => 6
  161. [] => 3
  162. [2] => float
  163. [F] => FFF
  164. [blank] =>
  165. [3] => 3.7
  166. [5] => Five
  167. [6] => 8.6
  168. [4name] => jonny
  169. [a] =>
  170. )
  171. Output after shift is :
  172. string(3) "fff"
  173. -- Input Array for Iteration 9 is --
  174. Array
  175. (
  176. [0] => 12
  177. [1] => name
  178. [2] => age
  179. [3] => 45
  180. )
  181. Output after shift is :
  182. int(12)
  183. -- Input Array for Iteration 10 is --
  184. Array
  185. (
  186. [0] => Array
  187. (
  188. [0] => oNe
  189. [1] => tWo
  190. [2] => 4
  191. )
  192. [1] => Array
  193. (
  194. [0] => 10
  195. [1] => 20
  196. [2] => 30
  197. [3] => 40
  198. [4] => 50
  199. )
  200. [2] => Array
  201. (
  202. )
  203. )
  204. Output after shift is :
  205. array(3) {
  206. [0]=>
  207. string(3) "oNe"
  208. [1]=>
  209. string(3) "tWo"
  210. [2]=>
  211. int(4)
  212. }
  213. -- Input Array for Iteration 11 is --
  214. Array
  215. (
  216. [one] => 2
  217. [three] => 3
  218. [0] => 3
  219. [1] => 4
  220. [3] => 33
  221. [4] => 44
  222. [5] => 57
  223. [6] => 6
  224. [5.4] => 554
  225. [5.7] => 557
  226. )
  227. Output after shift is :
  228. int(2)
  229. *** Checking for internal array pointer being reset when shift is called ***
  230. Current Element is : int(1)
  231. Next Element is : int(2)
  232. Next Element is : int(3)
  233. shifted Element is : int(1)
  234. Current Element after shift operation is: int(2)
  235. Done