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