diff options
author | Raghubansh Kumar <kraghuba@php.net> | 2007-12-11 10:45:25 +0000 |
---|---|---|
committer | Raghubansh Kumar <kraghuba@php.net> | 2007-12-11 10:45:25 +0000 |
commit | b2def646b6d5fa24e4c7a3cc3e1934c80b107e4f (patch) | |
tree | ffe4a25b347e4f12e01398fe27c4405006e25fe5 /ext/standard | |
parent | 9a4de086cbc6bd19bc9fb99810c232ac98d3e078 (diff) | |
download | php-git-b2def646b6d5fa24e4c7a3cc3e1934c80b107e4f.tar.gz |
New testcases for array_walk() function
Diffstat (limited to 'ext/standard')
-rw-r--r-- | ext/standard/tests/array/array_walk_basic1.phpt | 80 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_basic2.phpt | 105 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_error1.phpt | 48 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_error2.phpt | 59 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_object1.phpt | bin | 0 -> 1391 bytes | |||
-rw-r--r-- | ext/standard/tests/array/array_walk_object2.phpt | 104 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_variation1.phpt | 250 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_variation2.phpt | 268 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_variation3.phpt | 123 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_variation4.phpt | 87 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_variation5.phpt | 64 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_variation6.phpt | 143 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_variation7.phpt | 93 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_variation8.phpt | 42 | ||||
-rw-r--r-- | ext/standard/tests/array/array_walk_variation9.phpt | 99 |
15 files changed, 1565 insertions, 0 deletions
diff --git a/ext/standard/tests/array/array_walk_basic1.phpt b/ext/standard/tests/array/array_walk_basic1.phpt new file mode 100644 index 0000000000..e4bdf1b895 --- /dev/null +++ b/ext/standard/tests/array/array_walk_basic1.phpt @@ -0,0 +1,80 @@ +--TEST-- +Test array_walk() function : basic functionality - regular array +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +echo "*** Testing array_walk() : basic functionality ***\n"; + +// regular array +$fruits = array("lemon", "orange", "banana", "apple"); + +/* Prototype : test_print(mixed $item, mixed $key) + * Parameters : item - item in key/item pair + * key - key in key/item pair + * Description : prints the array values with keys + */ +function test_print($item, $key) +{ + // dump the arguments to check that they are passed + // with proper type + var_dump($item); // value + var_dump($key); // key + echo "\n"; // new line to separate the output between each element +} +function with_userdata($item, $key, $user_data) +{ + // dump the arguments to check that they are passed + // with proper type + var_dump($item); // value + var_dump($key); // key + var_dump($user_data); // user supplied data + echo "\n"; // new line to separate the output between each element +} + +echo "-- Using array_walk() with default parameters to show array contents --\n"; +var_dump( array_walk($fruits, 'test_print')); + +echo "-- Using array_walk() with all parameters --\n"; +var_dump( array_walk($fruits, 'with_userdata', "Added")); + +echo "Done"; +?> +--EXPECT-- +*** Testing array_walk() : basic functionality *** +-- Using array_walk() with default parameters to show array contents -- +string(5) "lemon" +int(0) + +string(6) "orange" +int(1) + +string(6) "banana" +int(2) + +string(5) "apple" +int(3) + +bool(true) +-- Using array_walk() with all parameters -- +string(5) "lemon" +int(0) +string(5) "Added" + +string(6) "orange" +int(1) +string(5) "Added" + +string(6) "banana" +int(2) +string(5) "Added" + +string(5) "apple" +int(3) +string(5) "Added" + +bool(true) +Done diff --git a/ext/standard/tests/array/array_walk_basic2.phpt b/ext/standard/tests/array/array_walk_basic2.phpt new file mode 100644 index 0000000000..e856b580ba --- /dev/null +++ b/ext/standard/tests/array/array_walk_basic2.phpt @@ -0,0 +1,105 @@ +--TEST-- +Test array_walk() function : basic functionality - associative array +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +echo "*** Testing array_walk() : basic functionality ***\n"; + +// associative array +$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); + +// User defined callback functions +/* Prototype : test_alter(mixed $item, mixed $key, string $prefix) + * Parameters : item - value in key/value pair + * key - key in key/value pair + * prefix - string to be added + * Description : alters the array values by appending prefix string + */ +function test_alter(&$item, $key, $prefix) +{ + // dump the arguments to check that they are passed + // with proper type + var_dump($item); // value + var_dump($key); // key + var_dump($prefix); // additional agument passed to callback function + echo "\n"; // new line to separate the output between each element +} + +/* Prototype : test_print(mixed $item, mixed $key) + * Parameters : item - value in key/value pair + * key - key in key/value pair + * Description : prints the array values with keys + */ +function test_print($item, $key) +{ + // dump the arguments to check that they are passed + // with proper type + var_dump($item); // value + var_dump($key); // key + echo "\n"; // new line to separate the output between each element +} + +echo "-- Using array_walk with default parameters to show array contents --\n"; +var_dump(array_walk($fruits, 'test_print')); + +echo "-- Using array_walk with one optional parameter to modify contents --\n"; +var_dump (array_walk($fruits, 'test_alter', 'fruit')); + +echo "-- Using array_walk with default parameters to show modified array contents --\n"; +var_dump (array_walk($fruits, 'test_print')); + +echo "Done"; +?> +--EXPECT-- +*** Testing array_walk() : basic functionality *** +-- Using array_walk with default parameters to show array contents -- +string(5) "lemon" +string(1) "d" + +string(6) "orange" +string(1) "a" + +string(6) "banana" +string(1) "b" + +string(5) "apple" +string(1) "c" + +bool(true) +-- Using array_walk with one optional parameter to modify contents -- +string(5) "lemon" +string(1) "d" +string(5) "fruit" + +string(6) "orange" +string(1) "a" +string(5) "fruit" + +string(6) "banana" +string(1) "b" +string(5) "fruit" + +string(5) "apple" +string(1) "c" +string(5) "fruit" + +bool(true) +-- Using array_walk with default parameters to show modified array contents -- +string(5) "lemon" +string(1) "d" + +string(6) "orange" +string(1) "a" + +string(6) "banana" +string(1) "b" + +string(5) "apple" +string(1) "c" + +bool(true) +Done diff --git a/ext/standard/tests/array/array_walk_error1.phpt b/ext/standard/tests/array/array_walk_error1.phpt new file mode 100644 index 0000000000..ac64cbfd1f --- /dev/null +++ b/ext/standard/tests/array/array_walk_error1.phpt @@ -0,0 +1,48 @@ +--TEST-- +Test array_walk() function : error conditions(Bug#43558) +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +$input = array(1, 2); + +/* Prototype : callback(mixed value, mixed key, mixed user_data) + * Parameters : value - value in key/value pair + * key - key in key/value pair + * user_data - extra parameter + */ +function callback ($value, $key, $user_data) { + echo "\ncallback() invoked \n"; +} + +echo "*** Testing array_walk() : error conditions ***\n"; + +echo "-- Testing array_walk() function with zero arguments --\n"; +var_dump( array_walk() ); + +echo "-- Testing array_walk() function with one argument --\n"; +var_dump( array_walk($input) ); + +echo "-- Testing array_walk() function with non existent callback function --\n"; +var_dump( array_walk($input, "non_existent") ); + +echo "Done"; +?> +--EXPECTF-- +*** Testing array_walk() : error conditions *** +-- Testing array_walk() function with zero arguments -- + +Warning: array_walk() expects at least 2 parameters, 0 given in %s on line %d +NULL +-- Testing array_walk() function with one argument -- + +Warning: array_walk() expects at least 2 parameters, 1 given in %s on line %d +NULL +-- Testing array_walk() function with non existent callback function -- + +Warning: array_walk() expects parameter 2 to be valid callback, string given in %s on line %d +bool(false) +Done diff --git a/ext/standard/tests/array/array_walk_error2.phpt b/ext/standard/tests/array/array_walk_error2.phpt new file mode 100644 index 0000000000..e1bd2a5d46 --- /dev/null +++ b/ext/standard/tests/array/array_walk_error2.phpt @@ -0,0 +1,59 @@ +--TEST-- +Test array_walk() function : error conditions - callback parameters(Bug#43558) +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* + * Testing array_walk() by passing more number of parameters to callback function + */ +$input = array(1); + +function callback1($value, $key, $user_data ) { + echo "\ncallback1() invoked \n"; +} + +function callback2($value, $key, $user_data1, $user_data2) { + echo "\ncallback2() invoked \n"; +} +echo "*** Testing array_walk() : error conditions - callback parameters ***\n"; + +// expected: Missing argument Warning +var_dump( array_walk($input, "callback1") ); +var_dump( array_walk($input, "callback2", 4) ); + +// expected: Warning is supressed +var_dump( @array_walk($input, "callback1") ); +var_dump( @array_walk($input, "callback2", 4) ); + +echo "-- Testing array_walk() function with too many callback parameters --\n"; +var_dump( array_walk($input, "callback1", 20, 10) ); + +echo "Done"; +?> +--EXPECTF-- +*** Testing array_walk() : error conditions - callback parameters *** + +Warning: Missing argument 3 for callback1() in %s on line %d + +callback1() invoked +bool(false) + +Warning: Missing argument 4 for callback2() in %s on line %d + +callback2() invoked +bool(false) + +callback1() invoked +bool(true) + +callback2() invoked +bool(true) +-- Testing array_walk() function with too many callback parameters -- + +Warning: array_walk() expects at most 3 parameters, 4 given in %s on line %d +NULL +Done diff --git a/ext/standard/tests/array/array_walk_object1.phpt b/ext/standard/tests/array/array_walk_object1.phpt Binary files differnew file mode 100644 index 0000000000..9a76410c62 --- /dev/null +++ b/ext/standard/tests/array/array_walk_object1.phpt diff --git a/ext/standard/tests/array/array_walk_object2.phpt b/ext/standard/tests/array/array_walk_object2.phpt new file mode 100644 index 0000000000..61d0529753 --- /dev/null +++ b/ext/standard/tests/array/array_walk_object2.phpt @@ -0,0 +1,104 @@ +--TEST-- +Test array_walk() function : object functionality - array of objects +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* +* Testing array_walk() with an array of objects +*/ + +echo "*** Testing array_walk() : array of objects ***\n"; + +/* + * Prototype : callback(mixed $value, mixed $key, int $addvalue + * Parameters : $value - values in given input array + * $key - keys in given input array + * $addvalue - value to be added + * Description : Function adds the addvalue to each element of an array +*/ +function callback_private($value, $key, $addValue) +{ + echo "value : "; + var_dump($value->getValue()); + echo "key : "; + var_dump($key); +} + +function callback_public($value, $key) +{ + echo "value : "; + var_dump($value->pub_value); +} +function callback_protected($value, $key) +{ + echo "value : "; + var_dump($value->get_pro_value()); +} + +class MyClass +{ + private $pri_value; + public $pub_value; + protected $pro_value; + public function __construct($setVal) + { + $this->pri_value = $setVal; + $this->pub_value = $setVal; + $this->pro_value = $setVal; + } + public function getValue() + { + return $this->pri_value; + } + public function get_pro_value() + { + return $this->pro_value; + } +}; + +// array containing objects of MyClass +$input = array ( + new MyClass(3), + new MyClass(10), + new MyClass(20), + new MyClass(-10) +); + +echo "-- For private member --\n"; +var_dump( array_walk($input, "callback_private", 1)); +echo "-- For public member --\n"; +var_dump( array_walk($input, "callback_public")); +echo "-- For protected member --\n"; +var_dump( array_walk($input, "callback_protected")); + +echo "Done" +?> +--EXPECTF-- +*** Testing array_walk() : array of objects *** +-- For private member -- +value : int(3) +key : int(0) +value : int(10) +key : int(1) +value : int(20) +key : int(2) +value : int(-10) +key : int(3) +bool(true) +-- For public member -- +value : int(3) +value : int(10) +value : int(20) +value : int(-10) +bool(true) +-- For protected member -- +value : int(3) +value : int(10) +value : int(20) +value : int(-10) +bool(true) +Done diff --git a/ext/standard/tests/array/array_walk_variation1.phpt b/ext/standard/tests/array/array_walk_variation1.phpt new file mode 100644 index 0000000000..ca0effd4f2 --- /dev/null +++ b/ext/standard/tests/array/array_walk_variation1.phpt @@ -0,0 +1,250 @@ +--TEST-- +Test array_walk() function : usage variations - unexpected values for 'input' argument +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* + * Passing different scalar/nonscalar values in place of 'input' argument +*/ + +echo "*** Testing array_walk() : unexpected values for 'input' argument ***\n"; + +// callback function +/* Prototype : callback(mixed $value, mixed $key) + * Parameters : $value - values given in input array + * $key - keys given in input array + * Description : Function prints each element of an array with key + */ +function callback($value, $key) +{ + echo "key : "; + var_dump($key); + echo "value : "; + var_dump($value); +} + +// extra parameter passed to array_walk() +$user_data = 10; + +// get an unset variable +$unset_var = 10; +unset ($unset_var); + +// get resource variable +$fp = fopen(__FILE__, 'r'); + +// different scalar/nonscalar values to be used in place of an 'input' argument +$input_values = array( + + // int data +/* 1*/ 0, + 1, + 12345, + -2345, + + // float data +/* 5*/ 10.5, + -10.5, + 10.1234567e8, + 10.7654321E-8, + .5, + + // null data +/* 10*/ NULL, + null, + + // boolean data +/* 12*/ true, + false, + TRUE, + FALSE, + + // empty data +/* 16*/ "", + '', + + // string data +/* 18*/ "string", + 'string', + + // resource data + $fp, + + // undefined data + @$undefined_var, + + // unset data +/* 22*/ @$unset_var, +); + + +for($count = 0; $count < count($input_values); $count++) { + echo "-- Iteration ".($count + 1)." --\n"; + var_dump( array_walk($input_values[$count], "callback") ); + var_dump( array_walk($input_values[$count], "callback", $user_data) ); +} + +fclose($fp); +echo "Done" +?> +--EXPECTF-- +*** Testing array_walk() : unexpected values for 'input' argument *** +-- Iteration 1 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 2 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 3 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 4 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 5 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 6 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 7 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 8 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 9 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 10 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 11 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 12 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 13 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 14 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 15 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 16 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 17 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 18 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 19 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 20 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 21 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +-- Iteration 22 -- + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_walk(): The argument should be an array in %s on line %d +bool(false) +Done diff --git a/ext/standard/tests/array/array_walk_variation2.phpt b/ext/standard/tests/array/array_walk_variation2.phpt new file mode 100644 index 0000000000..1b76639e28 --- /dev/null +++ b/ext/standard/tests/array/array_walk_variation2.phpt @@ -0,0 +1,268 @@ +--TEST-- +Test array_walk() function : usage variations - unexpected values in place of 'funcname' argument(Bug#43558) +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* + * Passing different scalar/nonscalar values in place of 'funcname' argument +*/ + +echo "*** Testing array_walk() : unexpected values for 'funcname' argument ***\n"; + +$input = array(1, 2); + +//get an unset variable +$unset_var = 10; +unset ($unset_var); + +$user_data = 20; + +// class definition +class MyClass +{ + public function __toString() + { + return 'object'; + } +} + +// different scalar/nonscalar values to be used in place of callback function +$funcname_values = array( + + // int data +/* 1*/ 0, + 1, + 12345, + -2345, + + // float data +/* 5*/ 10.5, + -10.5, + 10.1234567e8, + 10.7654321E-8, + .5, + + // array data +/* 10*/ array(), + array(0), + array(1), + array('color' => 'red', 'item' => 'pen'), + + // null data +/* 14*/ NULL, + null, + + // boolean data +/* 16*/ true, + false, + TRUE, + FALSE, + + // empty data +/* 20*/ "", + '', + + // object data + new MyClass(), + + // resource data +/* 23*/ $fp = fopen(__FILE__, 'r'), + + // undefined data + @$undefined_var, + + // unset data +/* 25*/ @$unset_var, +); + +for($count = 0; $count < count($funcname_values); $count++) { + echo "-- Iteration ".($count + 1)." --\n"; + var_dump( array_walk($input, $funcname_values[$count]) ); + var_dump( array_walk($input, $funcname_values[$count], $user_data )); +} + +fclose($fp); +echo "Done" +?> +--EXPECTF-- +*** Testing array_walk() : unexpected values for 'funcname' argument *** +-- Iteration 1 -- + +Warning: array_walk() expects parameter 2 to be valid callback, integer given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, integer given in %s on line %d +bool(false) +-- Iteration 2 -- + +Warning: array_walk() expects parameter 2 to be valid callback, integer given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, integer given in %s on line %d +bool(false) +-- Iteration 3 -- + +Warning: array_walk() expects parameter 2 to be valid callback, integer given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, integer given in %s on line %d +bool(false) +-- Iteration 4 -- + +Warning: array_walk() expects parameter 2 to be valid callback, integer given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, integer given in %s on line %d +bool(false) +-- Iteration 5 -- + +Warning: array_walk() expects parameter 2 to be valid callback, double given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, double given in %s on line %d +bool(false) +-- Iteration 6 -- + +Warning: array_walk() expects parameter 2 to be valid callback, double given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, double given in %s on line %d +bool(false) +-- Iteration 7 -- + +Warning: array_walk() expects parameter 2 to be valid callback, double given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, double given in %s on line %d +bool(false) +-- Iteration 8 -- + +Warning: array_walk() expects parameter 2 to be valid callback, double given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, double given in %s on line %d +bool(false) +-- Iteration 9 -- + +Warning: array_walk() expects parameter 2 to be valid callback, double given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, double given in %s on line %d +bool(false) +-- Iteration 10 -- + +Warning: array_walk() expects parameter 2 to be valid callback, array given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, array given in %s on line %d +bool(false) +-- Iteration 11 -- + +Warning: array_walk() expects parameter 2 to be valid callback, array given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, array given in %s on line %d +bool(false) +-- Iteration 12 -- + +Warning: array_walk() expects parameter 2 to be valid callback, array given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, array given in %s on line %d +bool(false) +-- Iteration 13 -- + +Warning: array_walk() expects parameter 2 to be valid callback, array given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, array given in %s on line %d +bool(false) +-- Iteration 14 -- + +Warning: array_walk() expects parameter 2 to be valid callback, null given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, null given in %s on line %d +bool(false) +-- Iteration 15 -- + +Warning: array_walk() expects parameter 2 to be valid callback, null given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, null given in %s on line %d +bool(false) +-- Iteration 16 -- + +Warning: array_walk() expects parameter 2 to be valid callback, boolean given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, boolean given in %s on line %d +bool(false) +-- Iteration 17 -- + +Warning: array_walk() expects parameter 2 to be valid callback, boolean given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, boolean given in %s on line %d +bool(false) +-- Iteration 18 -- + +Warning: array_walk() expects parameter 2 to be valid callback, boolean given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, boolean given in %s on line %d +bool(false) +-- Iteration 19 -- + +Warning: array_walk() expects parameter 2 to be valid callback, boolean given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, boolean given in %s on line %d +bool(false) +-- Iteration 20 -- + +Warning: array_walk() expects parameter 2 to be valid callback, string given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, string given in %s on line %d +bool(false) +-- Iteration 21 -- + +Warning: array_walk() expects parameter 2 to be valid callback, string given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, string given in %s on line %d +bool(false) +-- Iteration 22 -- + +Warning: array_walk() expects parameter 2 to be valid callback, object given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, object given in %s on line %d +bool(false) +-- Iteration 23 -- + +Warning: array_walk() expects parameter 2 to be valid callback, resource given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, resource given in %s on line %d +bool(false) +-- Iteration 24 -- + +Warning: array_walk() expects parameter 2 to be valid callback, null given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, null given in %s on line %d +bool(false) +-- Iteration 25 -- + +Warning: array_walk() expects parameter 2 to be valid callback, null given in %s on line %d +bool(false) + +Warning: array_walk() expects parameter 2 to be valid callback, null given in %s on line %d +bool(false) +Done diff --git a/ext/standard/tests/array/array_walk_variation3.phpt b/ext/standard/tests/array/array_walk_variation3.phpt new file mode 100644 index 0000000000..9ff7d5dfac --- /dev/null +++ b/ext/standard/tests/array/array_walk_variation3.phpt @@ -0,0 +1,123 @@ +--TEST-- +Test array_walk() function : usage variations - 'input' array with different values +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* + * Testing array_walk() with following types of 'input' arrays: + * integer, float, string, bool, null, empty & mixed +*/ + +// callback function +/* + * Prototype : print_value(mixed $value, int $key, int $count) + * Parameters : $value - array entries(values) + * $key - keys in given input array + * $count - extra parameter used as an index + * Description : prints the array values with keys and count value + */ +function print_value($value, $key, $count) +{ + echo $count." : ".$key." ".$value."\n"; +} + +echo "*** Testing array_walk() : 'input' array with different values***\n"; + +// different arrays as input +$input_values = array( + + // integer values +/*1*/ array(1, 0, -10, 023, -041, 0x5A, 0X1F, -0x6E), + + // float value + array(3.4, 0.8, -2.9, 6.25e2, 8.20E-3), + + // string values + array('Mango', "Apple", 'Orange', "Lemon"), + + // bool values +/*4*/ array(true, false, TRUE, FALSE), + + // null values + array(null, NULL), + + // empty array + array(), + + // binary array + array(b"binary"), + + // mixed array +/*8*/ array(16, 8.345, "Fruits", true, null, FALSE, -98, 0.005, 'banana') +); + +for($count = 0; $count < count($input_values); $count++) { + echo "\n-- Iteration ".($count + 1)." --\n"; + var_dump( array_walk($input_values[$count], "print_value", $count+1)); +} +echo "Done" +?> +--EXPECTF-- +*** Testing array_walk() : 'input' array with different values*** + +-- Iteration 1 -- +1 : 0 1 +1 : 1 0 +1 : 2 -10 +1 : 3 19 +1 : 4 -33 +1 : 5 90 +1 : 6 31 +1 : 7 -110 +bool(true) + +-- Iteration 2 -- +2 : 0 3.4 +2 : 1 0.8 +2 : 2 -2.9 +2 : 3 625 +2 : 4 0.0082 +bool(true) + +-- Iteration 3 -- +3 : 0 Mango +3 : 1 Apple +3 : 2 Orange +3 : 3 Lemon +bool(true) + +-- Iteration 4 -- +4 : 0 1 +4 : 1 +4 : 2 1 +4 : 3 +bool(true) + +-- Iteration 5 -- +5 : 0 +5 : 1 +bool(true) + +-- Iteration 6 -- +bool(true) + +-- Iteration 7 -- +7 : 0 binary +bool(true) + +-- Iteration 8 -- +8 : 0 16 +8 : 1 8.345 +8 : 2 Fruits +8 : 3 1 +8 : 4 +8 : 5 +8 : 6 -98 +8 : 7 0.005 +8 : 8 banana +bool(true) +Done diff --git a/ext/standard/tests/array/array_walk_variation4.phpt b/ext/standard/tests/array/array_walk_variation4.phpt new file mode 100644 index 0000000000..868732daaf --- /dev/null +++ b/ext/standard/tests/array/array_walk_variation4.phpt @@ -0,0 +1,87 @@ +--TEST-- +Test array_walk() function : usage variations - 'input' array with subarray +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* + * Testing array_walk() with an array having subarrays as elements +*/ + +echo "*** Testing array_walk() : array with subarray ***\n"; + +// callback function +/* Prototype : callback(mixed $value, mixed $key) + * Parameters : $value - values in given 'input' array + * $key - keys in given 'input' array + * Description : It prints the count of an array elements, passed as argument + */ +function callback($value, $key) +{ + // dump the arguments to check that they are passed + // with proper type + var_dump($key); // key + var_dump($value); // value + echo "\n"; // new line to separate the output between each element +} + +$input = array( + array(), + array(1), + array(1,2,3), + array("Mango", "Orange"), + array(array(1, 2, 3)) +); + +var_dump( array_walk( $input, "callback")); + +echo "Done" +?> +--EXPECTF-- +*** Testing array_walk() : array with subarray *** +int(0) +array(0) { +} + +int(1) +array(1) { + [0]=> + int(1) +} + +int(2) +array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) +} + +int(3) +array(2) { + [0]=> + string(5) "Mango" + [1]=> + string(6) "Orange" +} + +int(4) +array(1) { + [0]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + } +} + +bool(true) +Done diff --git a/ext/standard/tests/array/array_walk_variation5.phpt b/ext/standard/tests/array/array_walk_variation5.phpt new file mode 100644 index 0000000000..b5d262a733 --- /dev/null +++ b/ext/standard/tests/array/array_walk_variation5.phpt @@ -0,0 +1,64 @@ +--TEST-- +Test array_walk() function : usage variations - 'input' argument containing reference variables +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* + * Testing array_walk() with an array having reference variables +*/ + +echo "*** Testing array_walk() : array with references ***\n"; + +$value1 = 10; +$value2 = -20; +$value3 = &$value1; +$value4 = 50; + +// 'input' array containing references to above variables +$input = array(&$value1, &$value2, -35, &$value3, 0, &$value4); + +// callback function +/* Prototype : callback(int $value, mixed $key) + * Parameters : $value - values in given input array + * $key - keys in given input array + * Description : function checks for the value whether positive or negative and displays according to that + */ +function callback($value, $key) +{ + // dump the arguments to check that they are passed + // with proper type + var_dump($key); // key + var_dump($value); // value + echo "\n"; // new line to separate the output between each element +} + +var_dump( array_walk($input, "callback")); + +echo "Done" +?> +--EXPECTF-- +*** Testing array_walk() : array with references *** +int(0) +int(10) + +int(1) +int(-20) + +int(2) +int(-35) + +int(3) +int(10) + +int(4) +int(0) + +int(5) +int(50) + +bool(true) +Done diff --git a/ext/standard/tests/array/array_walk_variation6.phpt b/ext/standard/tests/array/array_walk_variation6.phpt new file mode 100644 index 0000000000..c1f23233ec --- /dev/null +++ b/ext/standard/tests/array/array_walk_variation6.phpt @@ -0,0 +1,143 @@ +--TEST-- +Test array_walk() function : usage variations - 'input' argument as diff. associative arrays +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* + * Passing 'input' argument as an associative array + * with Numeric & string keys +*/ + +echo "*** Testing array_walk() : 'input' as an associative array ***\n"; + +// callback functions +/* Prototype : for_numeric( int $value, int $key, int $user_data) + * Parameters : $value - value from key/value pair of the array + * $key - key from key/value pair of the array + * $user_data - data to be added to 'value' + * Description : Function adds values with keys & user_data + */ +function for_numeric($value, $key, $user_data) +{ + // dump the input values to see if they are + // passed with correct type + var_dump($key); + var_dump($value); + var_dump($user_data); + echo "\n"; // new line to separate the output between each element +} + +/* Prototype : for_string( string $value, string $key) + * Parameters : $value - values in given input array + * $key - keys in given input array + * Description : Function appends key to the value + */ +function for_string($value, $key) +{ + // dump the input values to see if they are + // passed with correct type + var_dump($key); + var_dump($value); + echo "\n"; // new line to separate the output between each element +} + +/* Prototype : for_mixed( mixed $value, mixed $key) + * Parameters : $value - values in given input array + * $key - keys in given input array + * Description : Function displays each element of an array with keys + */ +function for_mixed($value, $key) +{ + // dump the input values to see if they are + // passed with correct type + var_dump($key); + var_dump($value); + echo "\n"; // new line to separate the output between each element +} + +// Numeric keys +$input = array( 1 => 25, 5 => 12, 0 => -80, -2 => 100, 5 => 30); +echo "-- Associative array with numeric keys --\n"; +var_dump( array_walk($input, "for_numeric", 10)); + +// String keys +$input = array( "a" => "Apple", 'b' => 'Bananna', "c" => "carrot", 'o' => "Orange"); +echo "-- Associative array with string keys --\n"; +var_dump( array_walk($input, "for_string")); + +// binary keys +$input = array( b"a" => "Apple", b"b" => "Banana"); +echo "-- Associative array with binary keys --\n"; +var_dump( array_walk($input, "for_string")); + +// Mixed keys - numeric/string +$input = array( 0 => 1, 1 => 2, "a" => "Apple", "b" => "Banana", 2 =>3); +echo "-- Associative array with numeric/string keys --\n"; +var_dump( array_walk($input, "for_mixed")); + +echo "Done" +?> +--EXPECTF-- +*** Testing array_walk() : 'input' as an associative array *** +-- Associative array with numeric keys -- +int(1) +int(25) +int(10) + +int(5) +int(30) +int(10) + +int(0) +int(-80) +int(10) + +int(-2) +int(100) +int(10) + +bool(true) +-- Associative array with string keys -- +string(1) "a" +string(5) "Apple" + +string(1) "b" +string(7) "Bananna" + +string(1) "c" +string(6) "carrot" + +string(1) "o" +string(6) "Orange" + +bool(true) +-- Associative array with binary keys -- +string(1) "a" +string(5) "Apple" + +string(1) "b" +string(6) "Banana" + +bool(true) +-- Associative array with numeric/string keys -- +int(0) +int(1) + +int(1) +int(2) + +string(1) "a" +string(5) "Apple" + +string(1) "b" +string(6) "Banana" + +int(2) +int(3) + +bool(true) +Done diff --git a/ext/standard/tests/array/array_walk_variation7.phpt b/ext/standard/tests/array/array_walk_variation7.phpt new file mode 100644 index 0000000000..da85958b8e --- /dev/null +++ b/ext/standard/tests/array/array_walk_variation7.phpt @@ -0,0 +1,93 @@ +--TEST-- +Test array_walk() function : usage variations - anonymous callback function +--FILE-- +<?php +/* Prototype : proto bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* +* Passing anonymous(run-time) callback function with following variations: +* with one parameter +* two parameters +* three parameters +* extra parameters +* without parameters +*/ + +echo "*** Testing array_walk() : anonymous function as callback ***\n"; + +$input = array(2, 5, 10, 0); + +echo "-- Anonymous function with one argument --\n"; +var_dump( array_walk($input, create_function('$value', 'var_dump($value); echo "\n";'))); + +echo "-- Anonymous function with two arguments --\n"; +var_dump( array_walk($input, create_function('$value, $key', 'var_dump($key); var_dump($value); echo "\n";'))); + +echo "-- Anonymous function with three arguments --\n"; +var_dump( array_walk($input, create_function('$value, $key, $user_data', 'var_dump($key); var_dump($value); var_dump($user_data); echo "\n";'), 10)); + +echo "-- Anonymous function with one more argument --\n"; +var_dump( array_walk($input, create_function('$value, $key, $user_data', 'var_dump($key); var_dump($value); var_dump($user_data); echo "\n";'), 20, 30)); + +echo "-- Anonymous function with null argument --\n"; +var_dump( array_walk( $input, create_function(null, 'echo "1\n";'))); +echo "Done" +?> +--EXPECTF-- +*** Testing array_walk() : anonymous function as callback *** +-- Anonymous function with one argument -- +int(2) + +int(5) + +int(10) + +int(0) + +bool(true) +-- Anonymous function with two arguments -- +int(0) +int(2) + +int(1) +int(5) + +int(2) +int(10) + +int(3) +int(0) + +bool(true) +-- Anonymous function with three arguments -- +int(0) +int(2) +int(10) + +int(1) +int(5) +int(10) + +int(2) +int(10) +int(10) + +int(3) +int(0) +int(10) + +bool(true) +-- Anonymous function with one more argument -- + +Warning: array_walk() expects at most 3 parameters, 4 given in %s on line %d +NULL +-- Anonymous function with null argument -- +1 +1 +1 +1 +bool(true) +Done diff --git a/ext/standard/tests/array/array_walk_variation8.phpt b/ext/standard/tests/array/array_walk_variation8.phpt new file mode 100644 index 0000000000..97b1a1c86f --- /dev/null +++ b/ext/standard/tests/array/array_walk_variation8.phpt @@ -0,0 +1,42 @@ +--TEST-- +Test array_walk() function : usage variations - buit-in function as callback(Bug#43558) +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* + * Passing different buit-in functionns as callback function + * pow function + * min function + * echo language construct +*/ + +echo "*** Testing array_walk() : built-in function as callback ***\n"; + +$input = array(2 => 1, 65, 98, 100, 6 => -4); + +echo "-- With 'pow' built-in function --\n"; +var_dump( array_walk($input, 'pow')); + +echo "-- With 'min' built-in function --\n"; +var_dump( array_walk($input, "min")); + +echo "-- With 'echo' language construct --\n"; +var_dump( array_walk($input, "echo")); + +echo "Done" +?> +--EXPECTF-- +*** Testing array_walk() : built-in function as callback *** +-- With 'pow' built-in function -- +bool(true) +-- With 'min' built-in function -- +bool(true) +-- With 'echo' language construct -- + +Warning: array_walk() expects parameter 2 to be valid callback, string given in %s on line %d +bool(false) +Done diff --git a/ext/standard/tests/array/array_walk_variation9.phpt b/ext/standard/tests/array/array_walk_variation9.phpt new file mode 100644 index 0000000000..37077841af --- /dev/null +++ b/ext/standard/tests/array/array_walk_variation9.phpt @@ -0,0 +1,99 @@ +--TEST-- +Test array_walk() function : usage variations - different callback functions +--FILE-- +<?php +/* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata]) + * Description: Apply a user function to every member of an array + * Source code: ext/standard/array.c +*/ + +/* + * Passing different types of callback functions to array_walk() + * without parameters + * with less and more parameters +*/ + +echo "*** Testing array_walk() : callback function variation ***\n"; + +$input = array('Apple', 'Banana', 'Mango', 'Orange'); + +echo "-- callback function with both parameters --\n"; +function callback_two_parameter($value, $key) +{ + // dump the arguments to check that they are passed + // with proper type + var_dump($key); // key + var_dump($value); // value + echo "\n"; // new line to separate the output between each element +} +var_dump( array_walk($input, 'callback_two_parameter')); + +echo "-- callback function with only one parameter --\n"; +function callback_one_parameter($value) +{ + // dump the arguments to check that they are passed + // with proper type + var_dump($value); // value + echo "\n"; // new line to separate the output between each element +} +var_dump( array_walk($input, 'callback_one_parameter')); + +echo "-- callback function without parameters --\n"; +function callback_no_parameter() +{ + echo "callback3() called\n"; +} +var_dump( array_walk($input, 'callback_no_parameter')); + +echo "-- passing one more parameter to function with two parameters --\n"; +var_dump( array_walk($input, 'callback_two_parameter', 10)); + +echo "Done" +?> +--EXPECTF-- +*** Testing array_walk() : callback function variation *** +-- callback function with both parameters -- +int(0) +string(5) "Apple" + +int(1) +string(6) "Banana" + +int(2) +string(5) "Mango" + +int(3) +string(6) "Orange" + +bool(true) +-- callback function with only one parameter -- +string(5) "Apple" + +string(6) "Banana" + +string(5) "Mango" + +string(6) "Orange" + +bool(true) +-- callback function without parameters -- +callback3() called +callback3() called +callback3() called +callback3() called +bool(true) +-- passing one more parameter to function with two parameters -- +int(0) +string(5) "Apple" + +int(1) +string(6) "Banana" + +int(2) +string(5) "Mango" + +int(3) +string(6) "Orange" + +bool(true) +Done |