array_push.phpt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. --TEST--
  2. Test array_push() function
  3. --FILE--
  4. <?php
  5. $empty_array = array();
  6. $number = 5;
  7. $str = "abc";
  8. /* Various combinations of arrays to be used for the test */
  9. $mixed_array = array(
  10. array(),
  11. array( 1,2,3,4,5,6,7,8,9 ),
  12. array( "One", "_Two", "Three", "Four", "Five" ),
  13. array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ),
  14. array( "a" => "aaa", "A" => "AAA", "c" => "ccc", "d" => "ddd", "e" => "eee" ),
  15. array( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five" ),
  16. array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ),
  17. array( "f" => "fff", "1" => "one", 4 => 6, "" => "blank", 2 => "float", "F" => "FFF",
  18. "blank" => "", 3 => 3.7, 5 => 7, 6 => 8.6, '5' => "Five", "4name" => "jonny", "a" => NULL, NULL => 3 ),
  19. array( 12, "name", 'age', '45' ),
  20. array( array("oNe", "tWo", 4), array(10, 20, 30, 40, 50), array() ),
  21. array( "one" => 1, "one" => 2, "three" => 3, 3, 4, 3 => 33, 4 => 44, 5, 6,
  22. 5 => 57, "5.4" => 554, "5.7" => 557 )
  23. );
  24. /* Error Conditions */
  25. echo "\n*** Testing Edge Conditions ***\n";
  26. /* Invalid Number of arguments */
  27. var_dump( array_push($mixed_array[1],1,2) );
  28. /* Empty Array as argument */
  29. var_dump( array_push($empty_array, 2) );
  30. /* Loop to test normal functionality with different arrays inputs */
  31. echo "\n*** Testing with various array inputs ***\n";
  32. $counter = 1;
  33. foreach( $mixed_array as $sub_array )
  34. {
  35. echo "\n-- Input Array for Iteration $counter is --\n";
  36. var_dump( $sub_array );
  37. echo "\nOutput after push is :\n";
  38. var_dump( array_push($sub_array, 22, "abc") );
  39. $counter++;
  40. }
  41. /* Checking for return value and the new array formed from push operation */
  42. echo "\n*** Checking for return value and the new array formed from push operation ***\n";
  43. var_dump( array_push($mixed_array[2], 22, 33, "44") );
  44. var_dump( $mixed_array[2] );
  45. echo"\nDone";
  46. ?>
  47. --EXPECT--
  48. *** Testing Edge Conditions ***
  49. int(11)
  50. int(1)
  51. *** Testing with various array inputs ***
  52. -- Input Array for Iteration 1 is --
  53. array(0) {
  54. }
  55. Output after push is :
  56. int(2)
  57. -- Input Array for Iteration 2 is --
  58. array(11) {
  59. [0]=>
  60. int(1)
  61. [1]=>
  62. int(2)
  63. [2]=>
  64. int(3)
  65. [3]=>
  66. int(4)
  67. [4]=>
  68. int(5)
  69. [5]=>
  70. int(6)
  71. [6]=>
  72. int(7)
  73. [7]=>
  74. int(8)
  75. [8]=>
  76. int(9)
  77. [9]=>
  78. int(1)
  79. [10]=>
  80. int(2)
  81. }
  82. Output after push is :
  83. int(13)
  84. -- Input Array for Iteration 3 is --
  85. array(5) {
  86. [0]=>
  87. string(3) "One"
  88. [1]=>
  89. string(4) "_Two"
  90. [2]=>
  91. string(5) "Three"
  92. [3]=>
  93. string(4) "Four"
  94. [4]=>
  95. string(4) "Five"
  96. }
  97. Output after push is :
  98. int(7)
  99. -- Input Array for Iteration 4 is --
  100. array(8) {
  101. [0]=>
  102. int(6)
  103. [1]=>
  104. string(3) "six"
  105. [2]=>
  106. int(7)
  107. [3]=>
  108. string(5) "seven"
  109. [4]=>
  110. int(8)
  111. [5]=>
  112. string(5) "eight"
  113. [6]=>
  114. int(9)
  115. [7]=>
  116. string(4) "nine"
  117. }
  118. Output after push is :
  119. int(10)
  120. -- Input Array for Iteration 5 is --
  121. array(5) {
  122. ["a"]=>
  123. string(3) "aaa"
  124. ["A"]=>
  125. string(3) "AAA"
  126. ["c"]=>
  127. string(3) "ccc"
  128. ["d"]=>
  129. string(3) "ddd"
  130. ["e"]=>
  131. string(3) "eee"
  132. }
  133. Output after push is :
  134. int(7)
  135. -- Input Array for Iteration 6 is --
  136. array(5) {
  137. [1]=>
  138. string(3) "one"
  139. [2]=>
  140. string(3) "two"
  141. [3]=>
  142. string(5) "three"
  143. [4]=>
  144. string(4) "four"
  145. [5]=>
  146. string(4) "five"
  147. }
  148. Output after push is :
  149. int(7)
  150. -- Input Array for Iteration 7 is --
  151. array(5) {
  152. [1]=>
  153. string(3) "one"
  154. [2]=>
  155. string(3) "two"
  156. [3]=>
  157. int(7)
  158. [4]=>
  159. string(4) "four"
  160. [5]=>
  161. string(4) "five"
  162. }
  163. Output after push is :
  164. int(7)
  165. -- Input Array for Iteration 8 is --
  166. array(12) {
  167. ["f"]=>
  168. string(3) "fff"
  169. [1]=>
  170. string(3) "one"
  171. [4]=>
  172. int(6)
  173. [""]=>
  174. int(3)
  175. [2]=>
  176. string(5) "float"
  177. ["F"]=>
  178. string(3) "FFF"
  179. ["blank"]=>
  180. string(0) ""
  181. [3]=>
  182. float(3.7)
  183. [5]=>
  184. string(4) "Five"
  185. [6]=>
  186. float(8.6)
  187. ["4name"]=>
  188. string(5) "jonny"
  189. ["a"]=>
  190. NULL
  191. }
  192. Output after push is :
  193. int(14)
  194. -- Input Array for Iteration 9 is --
  195. array(4) {
  196. [0]=>
  197. int(12)
  198. [1]=>
  199. string(4) "name"
  200. [2]=>
  201. string(3) "age"
  202. [3]=>
  203. string(2) "45"
  204. }
  205. Output after push is :
  206. int(6)
  207. -- Input Array for Iteration 10 is --
  208. array(3) {
  209. [0]=>
  210. array(3) {
  211. [0]=>
  212. string(3) "oNe"
  213. [1]=>
  214. string(3) "tWo"
  215. [2]=>
  216. int(4)
  217. }
  218. [1]=>
  219. array(5) {
  220. [0]=>
  221. int(10)
  222. [1]=>
  223. int(20)
  224. [2]=>
  225. int(30)
  226. [3]=>
  227. int(40)
  228. [4]=>
  229. int(50)
  230. }
  231. [2]=>
  232. array(0) {
  233. }
  234. }
  235. Output after push is :
  236. int(5)
  237. -- Input Array for Iteration 11 is --
  238. array(10) {
  239. ["one"]=>
  240. int(2)
  241. ["three"]=>
  242. int(3)
  243. [0]=>
  244. int(3)
  245. [1]=>
  246. int(4)
  247. [3]=>
  248. int(33)
  249. [4]=>
  250. int(44)
  251. [5]=>
  252. int(57)
  253. [6]=>
  254. int(6)
  255. ["5.4"]=>
  256. int(554)
  257. ["5.7"]=>
  258. int(557)
  259. }
  260. Output after push is :
  261. int(12)
  262. *** Checking for return value and the new array formed from push operation ***
  263. int(8)
  264. array(8) {
  265. [0]=>
  266. string(3) "One"
  267. [1]=>
  268. string(4) "_Two"
  269. [2]=>
  270. string(5) "Three"
  271. [3]=>
  272. string(4) "Four"
  273. [4]=>
  274. string(4) "Five"
  275. [5]=>
  276. int(22)
  277. [6]=>
  278. int(33)
  279. [7]=>
  280. string(2) "44"
  281. }
  282. Done