2 Test array_push() function
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.
11 $empty_array = array();
16 /* Various combinations of arrays to be used for the test */
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 )
33 /* Error Conditions */
34 echo "\n*** Testing Edge Conditions ***\n";
36 /* Invalid Number of arguments */
37 var_dump( array_push($mixed_array[1],1,2) );
39 /* Empty Array as argument */
40 var_dump( array_push($empty_array, 2) );
43 /* Loop to test normal functionality with different arrays inputs */
44 echo "\n*** Testing with various array inputs ***\n";
47 foreach( $mixed_array as $sub_array )
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") );
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] );
64 *** Testing Edge Conditions ***
68 *** Testing with various array inputs ***
70 -- Input Array for Iteration 1 is --
75 Output after push is :
78 -- Input Array for Iteration 2 is --
94 Output after push is :
97 -- Input Array for Iteration 3 is --
107 Output after push is :
110 -- Input Array for Iteration 4 is --
123 Output after push is :
126 -- Input Array for Iteration 5 is --
136 Output after push is :
139 -- Input Array for Iteration 6 is --
149 Output after push is :
152 -- Input Array for Iteration 7 is --
162 Output after push is :
165 -- Input Array for Iteration 8 is --
182 Output after push is :
185 -- Input Array for Iteration 9 is --
194 Output after push is :
197 -- Input Array for Iteration 10 is --
222 Output after push is :
225 -- Input Array for Iteration 11 is --
240 Output after push is :
243 *** Checking for return value and the new array formed from push operation ***