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