--- /dev/null
+--TEST--
+Test array_merge() function
+--FILE--
+<?php
+/* Prototype: array array_merge(array $array1 [, array $array2 [, array $...]]);
+ Description: Merge one or more arrays
+*/
+
+echo "\n*** Testing array_merge() basic functionality ***";
+$begin_array = array(
+ array(),
+ array( 1 => "string"),
+ array( "" => "string"),
+ array( -2.44444 => 12),
+ array( "a" => 1, "b" => -2.344, "b" => "string", "c" => NULL, "d" => -2.344),
+ array( 4 => 1, 3 => -2.344, "3" => "string", "2" => NULL,1 => -2.344),
+ array( NULL, 1.23 => "Hi", "string" => "hello",
+ array("" => "World", "-2.34" => "a", "0" => "b"))
+);
+
+$end_array = array(
+ array(),
+ array( 1 => "string"),
+ array( "" => "string"),
+ array( -2.44444 => 12),
+ array( "a" => 1, "b" => -2.344, "b" => "string", "c" => NULL, "d" => -2.344),
+ array( 4 => 1, 3 => -2.344, "3" => "string", "2" => NULL, 1=> -2.344),
+ array( NULL, 1.23 => "Hi", "string" => "hello",
+ array("" => "World", "-2.34" => "a", "0" => "b"))
+);
+
+/* loop through to merge two arrays */
+$count_outer = 0;
+foreach($begin_array as $first) {
+ echo "\n\n--- Iteration $count_outer ---";
+ $count_inner = 0;
+ foreach($end_array as $second) {
+ echo "\n-- Inner iteration $count_inner of Iteration $count_outer --\n";
+ $result = array_merge($first, $second);
+ print_r($result);
+ $count_inner++;
+ }
+ $count_outer++;
+}
+
+
+echo "\n*** Testing array_merge() with three or more arrays ***\n";
+var_dump( array_merge( $end_array[0],
+ $end_array[5],
+ $end_array[4],
+ $end_array[6]
+ )
+ );
+
+var_dump( array_merge( $end_array[0],
+ $end_array[5],
+ array("array on fly"),
+ array("nullarray" => array())
+ )
+ );
+
+
+echo "\n*** Testing single array argument ***\n";
+/* Empty array */
+var_dump(array_merge(array()));
+
+/* associative array with string keys, which will not be re-indexed */
+var_dump(array_merge($begin_array[4]));
+
+/* associative array with numeric keys, which will be re-indexed */
+var_dump(array_merge($begin_array[5]));
+
+/* associative array with mixed keys and sub-array as element */
+var_dump(array_merge($begin_array[6]));
+
+echo "\n*** Testing array_merge() with typecasting non-array to array ***\n";
+var_dump(array_merge($begin_array[4], (array)"type1", (array)10, (array)12.34));
+
+echo "\n*** Testing error conditions ***";
+/* Invalid argumens */
+var_dump(array_merge());
+var_dump(array_merge(100, 200));
+var_dump(array_merge($begin_array[0], $begin_array[1], 100));
+var_dump(array_merge($begin_array[0], $begin_array[1], $arr4));
+
+echo "Done\n";
+?>
+--EXPECTF--
+*** Testing array_merge() basic functionality ***
+
+--- Iteration 0 ---
+-- Inner iteration 0 of Iteration 0 --
+Array
+(
+)
+
+-- Inner iteration 1 of Iteration 0 --
+Array
+(
+ [0] => string
+)
+
+-- Inner iteration 2 of Iteration 0 --
+Array
+(
+ [] => string
+)
+
+-- Inner iteration 3 of Iteration 0 --
+Array
+(
+ [0] => 12
+)
+
+-- Inner iteration 4 of Iteration 0 --
+Array
+(
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+)
+
+-- Inner iteration 5 of Iteration 0 --
+Array
+(
+ [0] => 1
+ [1] => string
+ [2] =>
+ [3] => -2.344
+)
+
+-- Inner iteration 6 of Iteration 0 --
+Array
+(
+ [0] =>
+ [1] => Hi
+ [string] => hello
+ [2] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+)
+
+
+--- Iteration 1 ---
+-- Inner iteration 0 of Iteration 1 --
+Array
+(
+ [0] => string
+)
+
+-- Inner iteration 1 of Iteration 1 --
+Array
+(
+ [0] => string
+ [1] => string
+)
+
+-- Inner iteration 2 of Iteration 1 --
+Array
+(
+ [0] => string
+ [] => string
+)
+
+-- Inner iteration 3 of Iteration 1 --
+Array
+(
+ [0] => string
+ [1] => 12
+)
+
+-- Inner iteration 4 of Iteration 1 --
+Array
+(
+ [0] => string
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+)
+
+-- Inner iteration 5 of Iteration 1 --
+Array
+(
+ [0] => string
+ [1] => 1
+ [2] => string
+ [3] =>
+ [4] => -2.344
+)
+
+-- Inner iteration 6 of Iteration 1 --
+Array
+(
+ [0] => string
+ [1] =>
+ [2] => Hi
+ [string] => hello
+ [3] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+)
+
+
+--- Iteration 2 ---
+-- Inner iteration 0 of Iteration 2 --
+Array
+(
+ [] => string
+)
+
+-- Inner iteration 1 of Iteration 2 --
+Array
+(
+ [] => string
+ [0] => string
+)
+
+-- Inner iteration 2 of Iteration 2 --
+Array
+(
+ [] => string
+)
+
+-- Inner iteration 3 of Iteration 2 --
+Array
+(
+ [] => string
+ [0] => 12
+)
+
+-- Inner iteration 4 of Iteration 2 --
+Array
+(
+ [] => string
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+)
+
+-- Inner iteration 5 of Iteration 2 --
+Array
+(
+ [] => string
+ [0] => 1
+ [1] => string
+ [2] =>
+ [3] => -2.344
+)
+
+-- Inner iteration 6 of Iteration 2 --
+Array
+(
+ [] => string
+ [0] =>
+ [1] => Hi
+ [string] => hello
+ [2] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+)
+
+
+--- Iteration 3 ---
+-- Inner iteration 0 of Iteration 3 --
+Array
+(
+ [0] => 12
+)
+
+-- Inner iteration 1 of Iteration 3 --
+Array
+(
+ [0] => 12
+ [1] => string
+)
+
+-- Inner iteration 2 of Iteration 3 --
+Array
+(
+ [0] => 12
+ [] => string
+)
+
+-- Inner iteration 3 of Iteration 3 --
+Array
+(
+ [0] => 12
+ [1] => 12
+)
+
+-- Inner iteration 4 of Iteration 3 --
+Array
+(
+ [0] => 12
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+)
+
+-- Inner iteration 5 of Iteration 3 --
+Array
+(
+ [0] => 12
+ [1] => 1
+ [2] => string
+ [3] =>
+ [4] => -2.344
+)
+
+-- Inner iteration 6 of Iteration 3 --
+Array
+(
+ [0] => 12
+ [1] =>
+ [2] => Hi
+ [string] => hello
+ [3] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+)
+
+
+--- Iteration 4 ---
+-- Inner iteration 0 of Iteration 4 --
+Array
+(
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+)
+
+-- Inner iteration 1 of Iteration 4 --
+Array
+(
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+ [0] => string
+)
+
+-- Inner iteration 2 of Iteration 4 --
+Array
+(
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+ [] => string
+)
+
+-- Inner iteration 3 of Iteration 4 --
+Array
+(
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+ [0] => 12
+)
+
+-- Inner iteration 4 of Iteration 4 --
+Array
+(
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+)
+
+-- Inner iteration 5 of Iteration 4 --
+Array
+(
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+ [0] => 1
+ [1] => string
+ [2] =>
+ [3] => -2.344
+)
+
+-- Inner iteration 6 of Iteration 4 --
+Array
+(
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+ [0] =>
+ [1] => Hi
+ [string] => hello
+ [2] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+)
+
+
+--- Iteration 5 ---
+-- Inner iteration 0 of Iteration 5 --
+Array
+(
+ [0] => 1
+ [1] => string
+ [2] =>
+ [3] => -2.344
+)
+
+-- Inner iteration 1 of Iteration 5 --
+Array
+(
+ [0] => 1
+ [1] => string
+ [2] =>
+ [3] => -2.344
+ [4] => string
+)
+
+-- Inner iteration 2 of Iteration 5 --
+Array
+(
+ [0] => 1
+ [1] => string
+ [2] =>
+ [3] => -2.344
+ [] => string
+)
+
+-- Inner iteration 3 of Iteration 5 --
+Array
+(
+ [0] => 1
+ [1] => string
+ [2] =>
+ [3] => -2.344
+ [4] => 12
+)
+
+-- Inner iteration 4 of Iteration 5 --
+Array
+(
+ [0] => 1
+ [1] => string
+ [2] =>
+ [3] => -2.344
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+)
+
+-- Inner iteration 5 of Iteration 5 --
+Array
+(
+ [0] => 1
+ [1] => string
+ [2] =>
+ [3] => -2.344
+ [4] => 1
+ [5] => string
+ [6] =>
+ [7] => -2.344
+)
+
+-- Inner iteration 6 of Iteration 5 --
+Array
+(
+ [0] => 1
+ [1] => string
+ [2] =>
+ [3] => -2.344
+ [4] =>
+ [5] => Hi
+ [string] => hello
+ [6] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+)
+
+
+--- Iteration 6 ---
+-- Inner iteration 0 of Iteration 6 --
+Array
+(
+ [0] =>
+ [1] => Hi
+ [string] => hello
+ [2] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+)
+
+-- Inner iteration 1 of Iteration 6 --
+Array
+(
+ [0] =>
+ [1] => Hi
+ [string] => hello
+ [2] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+ [3] => string
+)
+
+-- Inner iteration 2 of Iteration 6 --
+Array
+(
+ [0] =>
+ [1] => Hi
+ [string] => hello
+ [2] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+ [] => string
+)
+
+-- Inner iteration 3 of Iteration 6 --
+Array
+(
+ [0] =>
+ [1] => Hi
+ [string] => hello
+ [2] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+ [3] => 12
+)
+
+-- Inner iteration 4 of Iteration 6 --
+Array
+(
+ [0] =>
+ [1] => Hi
+ [string] => hello
+ [2] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+ [a] => 1
+ [b] => string
+ [c] =>
+ [d] => -2.344
+)
+
+-- Inner iteration 5 of Iteration 6 --
+Array
+(
+ [0] =>
+ [1] => Hi
+ [string] => hello
+ [2] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+ [3] => 1
+ [4] => string
+ [5] =>
+ [6] => -2.344
+)
+
+-- Inner iteration 6 of Iteration 6 --
+Array
+(
+ [0] =>
+ [1] => Hi
+ [string] => hello
+ [2] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+ [3] =>
+ [4] => Hi
+ [5] => Array
+ (
+ [] => World
+ [-2.34] => a
+ [0] => b
+ )
+
+)
+
+*** Testing array_merge() with three or more arrays ***
+array(12) {
+ [0]=>
+ int(1)
+ [1]=>
+ string(6) "string"
+ [2]=>
+ NULL
+ [3]=>
+ float(-2.344)
+ ["a"]=>
+ int(1)
+ ["b"]=>
+ string(6) "string"
+ ["c"]=>
+ NULL
+ ["d"]=>
+ float(-2.344)
+ [4]=>
+ NULL
+ [5]=>
+ string(2) "Hi"
+ ["string"]=>
+ string(5) "hello"
+ [6]=>
+ array(3) {
+ [""]=>
+ string(5) "World"
+ ["-2.34"]=>
+ string(1) "a"
+ [0]=>
+ string(1) "b"
+ }
+}
+array(6) {
+ [0]=>
+ int(1)
+ [1]=>
+ string(6) "string"
+ [2]=>
+ NULL
+ [3]=>
+ float(-2.344)
+ [4]=>
+ string(12) "array on fly"
+ ["nullarray"]=>
+ array(0) {
+ }
+}
+
+*** Testing single array argument ***
+array(0) {
+}
+array(4) {
+ ["a"]=>
+ int(1)
+ ["b"]=>
+ string(6) "string"
+ ["c"]=>
+ NULL
+ ["d"]=>
+ float(-2.344)
+}
+array(4) {
+ [0]=>
+ int(1)
+ [1]=>
+ string(6) "string"
+ [2]=>
+ NULL
+ [3]=>
+ float(-2.344)
+}
+array(4) {
+ [0]=>
+ NULL
+ [1]=>
+ string(2) "Hi"
+ ["string"]=>
+ string(5) "hello"
+ [2]=>
+ array(3) {
+ [""]=>
+ string(5) "World"
+ ["-2.34"]=>
+ string(1) "a"
+ [0]=>
+ string(1) "b"
+ }
+}
+
+*** Testing array_merge() with typecasting non-array to array ***
+array(7) {
+ ["a"]=>
+ int(1)
+ ["b"]=>
+ string(6) "string"
+ ["c"]=>
+ NULL
+ ["d"]=>
+ float(-2.344)
+ [0]=>
+ string(5) "type1"
+ [1]=>
+ int(10)
+ [2]=>
+ float(12.34)
+}
+
+*** Testing error conditions ***
+Warning: Wrong parameter count for array_merge() in %s on line %d
+NULL
+
+Warning: array_merge(): Argument #1 is not an array in %s on line %d
+
+Warning: array_merge(): Argument #2 is not an array in %s on line %d
+NULL
+
+Warning: array_merge(): Argument #3 is not an array in %s on line %d
+NULL
+
+Notice: Undefined variable: arr4 in %s on line %d
+
+Warning: array_merge(): Argument #3 is not an array in %s on line %d
+NULL
+Done
--- /dev/null
+--TEST--
+Test array_push() function
+--FILE--
+<?php
+
+/* Prototype: int array_push( array &array );
+ * Description: Push one or more elements onto the end of array
+ and returns the new number of elements in the array.
+ */
+
+$empty_array = array();
+$number = 5;
+$str = "abc";
+
+
+/* Various combinations of arrays to be used for the test */
+$mixed_array = array(
+ array(),
+ array( 1,2,3,4,5,6,7,8,9 ),
+ array( "One", "_Two", "Three", "Four", "Five" ),
+ array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ),
+ array( "a" => "aaa", "A" => "AAA", "c" => "ccc", "d" => "ddd", "e" => "eee" ),
+ array( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five" ),
+ array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ),
+ array( "f" => "fff", "1" => "one", 4 => 6, "" => "blank", 2.4 => "float", "F" => "FFF",
+ "blank" => "", 3.7 => 3.7, 5.4 => 7, 6 => 8.6, '5' => "Five", "4name" => "jonny", "a" => NULL, NULL => 3 ),
+ array( 12, "name", 'age', '45' ),
+ array( array("oNe", "tWo", 4), array(10, 20, 30, 40, 50), array() ),
+ array( "one" => 1, "one" => 2, "three" => 3, 3, 4, 3 => 33, 4 => 44, 5, 6,
+ 5.4 => 54, 5.7 => 57, "5.4" => 554, "5.7" => 557 )
+);
+
+/* Error Conditions */
+echo "\n*** Testing Error Conditions ***\n";
+
+/* Zero argument */
+var_dump( array_push() );
+
+/* Scalar argument */
+var_dump( array_push($number, 22) );
+
+/* String argument */
+var_dump( array_push($str, 22) );
+
+/* Invalid Number of arguments */
+var_dump( array_push($mixed_array[1],1,2) );
+
+/* Empty Array as argument */
+var_dump( array_push($empty_array, 2) );
+
+
+/* Loop to test normal functionality with different arrays inputs */
+echo "\n*** Testing with various array inputs ***\n";
+
+$counter = 1;
+foreach( $mixed_array as $sub_array )
+{
+ echo "\n-- Input Array for Iteration $counter is --\n";
+ print_r( $sub_array );
+ echo "\nOutput after push is :\n";
+ var_dump( array_push($sub_array, 22, "abc") );
+ $counter++;
+}
+
+/* Checking for return value and the new array formed from push operation */
+echo "\n*** Checking for return value and the new array formed from push operation ***\n";
+var_dump( array_push($mixed_array[2], 22, 33, "44") );
+var_dump( $mixed_array[2] );
+
+echo"\nDone";
+?>
+--EXPECTF--
+*** Testing Error Conditions ***
+
+Warning: Wrong parameter count for array_push() in %s on line %d
+NULL
+
+Warning: array_push(): First argument should be an array in %s on line %d
+bool(false)
+
+Warning: array_push(): First argument should be an array in %s on line %d
+bool(false)
+int(11)
+int(1)
+
+*** Testing with various array inputs ***
+
+-- Input Array for Iteration 1 is --
+Array
+(
+)
+
+Output after push is :
+int(2)
+
+-- Input Array for Iteration 2 is --
+Array
+(
+ [0] => 1
+ [1] => 2
+ [2] => 3
+ [3] => 4
+ [4] => 5
+ [5] => 6
+ [6] => 7
+ [7] => 8
+ [8] => 9
+ [9] => 1
+ [10] => 2
+)
+
+Output after push is :
+int(13)
+
+-- Input Array for Iteration 3 is --
+Array
+(
+ [0] => One
+ [1] => _Two
+ [2] => Three
+ [3] => Four
+ [4] => Five
+)
+
+Output after push is :
+int(7)
+
+-- Input Array for Iteration 4 is --
+Array
+(
+ [0] => 6
+ [1] => six
+ [2] => 7
+ [3] => seven
+ [4] => 8
+ [5] => eight
+ [6] => 9
+ [7] => nine
+)
+
+Output after push is :
+int(10)
+
+-- Input Array for Iteration 5 is --
+Array
+(
+ [a] => aaa
+ [A] => AAA
+ [c] => ccc
+ [d] => ddd
+ [e] => eee
+)
+
+Output after push is :
+int(7)
+
+-- Input Array for Iteration 6 is --
+Array
+(
+ [1] => one
+ [2] => two
+ [3] => three
+ [4] => four
+ [5] => five
+)
+
+Output after push is :
+int(7)
+
+-- Input Array for Iteration 7 is --
+Array
+(
+ [1] => one
+ [2] => two
+ [3] => 7
+ [4] => four
+ [5] => five
+)
+
+Output after push is :
+int(7)
+
+-- Input Array for Iteration 8 is --
+Array
+(
+ [f] => fff
+ [1] => one
+ [4] => 6
+ [] => 3
+ [2] => float
+ [F] => FFF
+ [blank] =>
+ [3] => 3.7
+ [5] => Five
+ [6] => 8.6
+ [4name] => jonny
+ [a] =>
+)
+
+Output after push is :
+int(14)
+
+-- Input Array for Iteration 9 is --
+Array
+(
+ [0] => 12
+ [1] => name
+ [2] => age
+ [3] => 45
+)
+
+Output after push is :
+int(6)
+
+-- Input Array for Iteration 10 is --
+Array
+(
+ [0] => Array
+ (
+ [0] => oNe
+ [1] => tWo
+ [2] => 4
+ )
+
+ [1] => Array
+ (
+ [0] => 10
+ [1] => 20
+ [2] => 30
+ [3] => 40
+ [4] => 50
+ )
+
+ [2] => Array
+ (
+ )
+
+)
+
+Output after push is :
+int(5)
+
+-- Input Array for Iteration 11 is --
+Array
+(
+ [one] => 2
+ [three] => 3
+ [0] => 3
+ [1] => 4
+ [3] => 33
+ [4] => 44
+ [5] => 57
+ [6] => 6
+ [5.4] => 554
+ [5.7] => 557
+)
+
+Output after push is :
+int(12)
+
+*** Checking for return value and the new array formed from push operation ***
+int(8)
+array(8) {
+ [0]=>
+ string(3) "One"
+ [1]=>
+ string(4) "_Two"
+ [2]=>
+ string(5) "Three"
+ [3]=>
+ string(4) "Four"
+ [4]=>
+ string(4) "Five"
+ [5]=>
+ int(22)
+ [6]=>
+ int(33)
+ [7]=>
+ string(2) "44"
+}
+
+Done
--TEST--
-count
+Test count() function
--FILE--
<?php
-print "Testing NULL...\n";
+/* Prototype: int count ( mixed $var [, int $mode] );
+ Discription: Count elements in an array, or properties in an object
+*/
+
+echo "*** Testing basic functionality of count() function ***\n";
+print "-- Testing NULL --\n";
$arr = NULL;
print "COUNT_NORMAL: should be 0, is ".count($arr, COUNT_NORMAL)."\n";
print "COUNT_RECURSIVE: should be 0, is ".count($arr, COUNT_RECURSIVE)."\n";
-print "Testing arrays...\n";
+print "-- Testing arrays --\n";
$arr = array(1, array(3, 4, array(6, array(8))));
print "COUNT_NORMAL: should be 2, is ".count($arr, COUNT_NORMAL)."\n";
print "COUNT_RECURSIVE: should be 8, is ".count($arr, COUNT_RECURSIVE)."\n";
-print "Testing hashes...\n";
+print "-- Testing hashes --\n";
$arr = array("a" => 1, "b" => 2, array("c" => 3, array("d" => 5)));
print "COUNT_NORMAL: should be 3, is ".count($arr, COUNT_NORMAL)."\n";
print "COUNT_RECURSIVE: should be 6, is ".count($arr, COUNT_RECURSIVE)."\n";
-print "Testing strings...\n";
+print "-- Testing strings --\n";
print "COUNT_NORMAL: should be 1, is ".count("string", COUNT_NORMAL)."\n";
print "COUNT_RECURSIVE: should be 1, is ".count("string", COUNT_RECURSIVE)."\n";
-print "Testing various types with no second argument.\n";
+print "-- Testing various types with no second argument --\n";
print "COUNT_NORMAL: should be 1, is ".count("string")."\n";
print "COUNT_NORMAL: should be 2, is ".count(array("a", array("b")))."\n";
$arr = array('a'=>array(NULL, NULL, NULL), 1=>array(NULL=>1, 1=>NULL),
array(array(array(array(array(NULL))))));
-print "Testing really cool arrays ;)\n";
+print "-- Testing really cool arrays --\n";
print "COUNT_NORMAL: should be 3, is ".count($arr, COUNT_NORMAL)."\n";
print "COUNT_RECURSIVE: should be 13, is ".count($arr, COUNT_RECURSIVE)."\n";
+
+echo "\n*** Testing possible variations of count() function on arrays ***";
+$count_array = array(
+ array(),
+ array( 1 => "string"),
+ array( "" => "string", 0 => "a", NULL => "b", -1.00 => "c",
+ array(array(array(NULL)))),
+ array( -2.44444 => 12, array(array(1, 2, array(array("0"))))),
+ array( "a" => 1, "b" => -2.344, "b" => "string", "c" => NULL, "d" => -2.344),
+ array( 4 => 1, 3 => -2.344, "3" => "string", "2" => NULL,
+ 1 => -2.344, array()),
+ array( TRUE => TRUE, FALSE => FALSE, "" => "", " " => " ",
+ NULL => NULL, "\x000" => "\x000", "\000" => "\000"),
+ array( NULL, 1.23 => "Hi", "string" => "hello",
+ array("" => "World", "-2.34" => "a", "0" => "b"))
+);
+
+$i = 0;
+foreach ($count_array as $count_value) {
+ echo "\n-- Iteration $i --\n";
+ print "COUNT_NORMAL is ".count($count_value, COUNT_NORMAL)."\n";
+ print "COUNT_RECURSIVE is ".count($count_value, COUNT_RECURSIVE)."\n";
+ $i++;
+}
+
+
+/* Testing count() by passing constant with no second argument */
+print "\n-- Testing count() on constants with no second argument --\n";
+print "COUNT_NORMAL: should be 1, is ".count(100)."\n";
+print "COUNT_NORMAL: should be 1, is ".count(-23.45)."\n";
+
+print "\n-- Testing count() on NULL and Unset variables --\n";
+print "COUNT_NORMAL: should be 0, is ".count(NULL)."\n";
+print "COUNT_NORMAL: should be 1, is ".count("")."\n";
+print "COUNT_NORMAL: should be 0, is ".@count($a)."\n";
+
+
+print "\n-- Testing count() on an empty sub-array --\n";
+$arr = array(1, array(3, 4, array()));
+print "COUNT_NORMAL: should be 2, is ".count($arr, COUNT_NORMAL)."\n";
+print "COUNT_RECURSIVE: should be 5, is ".count($arr, COUNT_RECURSIVE)."\n";
+
+echo "\n-- Testing count() on objects with Countable interface --\n";
+class count_class implements Countable {
+ private $var_private;
+ public $var_public;
+ protected $var_protected;
+
+ public function count() {
+ return 3;
+ }
+}
+
+$obj = new count_class();
+print "COUNT_NORMAL: should be 3, is ".count($obj)."\n";
+
+
+echo "\n-- Testing count() on resource type --\n";
+$resource1 = fopen( __FILE__, "r" ); // Creating file(stream type) resource
+$resource2 = opendir( "." ); // Creating dir resource
+
+/* creating an array with resources as elements */
+$arr_resource = array("a" => $resource1, "b" => $resource2);
+var_dump(count($arr_resource));
+
+echo "\n-- Testing count() on arrays containing references --\n";
+$arr = array(1, array("a", "b", "c"));
+$arr[2] = &$arr[1];
+
+$mode_arr = array( COUNT_NORMAL, COUNT_RECURSIVE, 0, 1, -1, -1.45, 2, TRUE,
+ FALSE, NULL);
+for( $i =0; $i < count( $mode_arr ); $i++) {
+ echo "For mode '$mode_arr[$i]' count is => ";
+ var_dump(count($arr, $mode_arr[$i]));
+}
+
+
+echo "\n-- Testing error conditions --";
+var_dump( count() ); // No. of args = 0
+var_dump( count(array(), COUNT_NORMAL, 100) ); // No. of args > expected
+
+/* Testing Invalid type arguments */
+var_dump( count("string", ABCD) );
+var_dump( count(100, "string") );
+var_dump( count(array(), "") );
+
+echo "\nDone";
+
+--CLEAN--
+/* closing the resource handles */
+fclose( $resource1 );
+closedir( $resource2 );
?>
---EXPECT--
-Testing NULL...
+--EXPECTF--
+*** Testing basic functionality of count() function ***
+-- Testing NULL --
COUNT_NORMAL: should be 0, is 0
COUNT_RECURSIVE: should be 0, is 0
-Testing arrays...
+-- Testing arrays --
COUNT_NORMAL: should be 2, is 2
COUNT_RECURSIVE: should be 8, is 8
-Testing hashes...
+-- Testing hashes --
COUNT_NORMAL: should be 3, is 3
COUNT_RECURSIVE: should be 6, is 6
-Testing strings...
+-- Testing strings --
COUNT_NORMAL: should be 1, is 1
COUNT_RECURSIVE: should be 1, is 1
-Testing various types with no second argument.
+-- Testing various types with no second argument --
COUNT_NORMAL: should be 1, is 1
COUNT_NORMAL: should be 2, is 2
-Testing really cool arrays ;)
+-- Testing really cool arrays --
COUNT_NORMAL: should be 3, is 3
COUNT_RECURSIVE: should be 13, is 13
+
+*** Testing possible variations of count() function on arrays ***
+-- Iteration 0 --
+COUNT_NORMAL is 0
+COUNT_RECURSIVE is 0
+
+-- Iteration 1 --
+COUNT_NORMAL is 1
+COUNT_RECURSIVE is 1
+
+-- Iteration 2 --
+COUNT_NORMAL is 4
+COUNT_RECURSIVE is 7
+
+-- Iteration 3 --
+COUNT_NORMAL is 2
+COUNT_RECURSIVE is 8
+
+-- Iteration 4 --
+COUNT_NORMAL is 4
+COUNT_RECURSIVE is 4
+
+-- Iteration 5 --
+COUNT_NORMAL is 5
+COUNT_RECURSIVE is 5
+
+-- Iteration 6 --
+COUNT_NORMAL is 6
+COUNT_RECURSIVE is 6
+
+-- Iteration 7 --
+COUNT_NORMAL is 4
+COUNT_RECURSIVE is 7
+
+-- Testing count() on constants with no second argument --
+COUNT_NORMAL: should be 1, is 1
+COUNT_NORMAL: should be 1, is 1
+
+-- Testing count() on NULL and Unset variables --
+COUNT_NORMAL: should be 0, is 0
+COUNT_NORMAL: should be 1, is 1
+COUNT_NORMAL: should be 0, is 0
+
+-- Testing count() on an empty sub-array --
+COUNT_NORMAL: should be 2, is 2
+COUNT_RECURSIVE: should be 5, is 5
+
+-- Testing count() on objects with Countable interface --
+COUNT_NORMAL: should be 3, is 3
+
+-- Testing count() on resource type --
+int(2)
+
+-- Testing count() on arrays containing references --
+For mode '0' count is => int(3)
+For mode '1' count is => int(9)
+For mode '0' count is => int(3)
+For mode '1' count is => int(9)
+For mode '-1' count is => int(3)
+For mode '-1.45' count is => int(3)
+For mode '2' count is => int(3)
+For mode '1' count is => int(9)
+For mode '' count is => int(3)
+For mode '' count is => int(3)
+
+-- Testing error conditions --
+Warning: count() expects at least 1 parameter, 0 given in %s on line %d
+NULL
+
+Warning: count() expects at most 2 parameters, 3 given in %s on line %d
+NULL
+
+Notice: Use of undefined constant ABCD - assumed 'ABCD' in %s on line %d
+
+Warning: count() expects parameter 2 to be long, string given in %s on line %d
+NULL
+
+Warning: count() expects parameter 2 to be long, string given in %s on line %d
+NULL
+
+Warning: count() expects parameter 2 to be long, string given in %s on line %d
+NULL
+
+Done