mirror of
https://github.com/php/php-src.git
synced 2025-01-08 20:17:28 +08:00
New testcases for array_filter() function
This commit is contained in:
parent
f1187f0fca
commit
63b1e9fb86
55
ext/standard/tests/array/array_filter_basic.phpt
Normal file
55
ext/standard/tests/array/array_filter_basic.phpt
Normal file
@ -0,0 +1,55 @@
|
||||
--TEST--
|
||||
Test array_filter() function : basic functionality
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
|
||||
echo "*** Testing array_filter() : basic functionality ***\n";
|
||||
|
||||
|
||||
// Initialise all required variables
|
||||
$input = array(1, 2, 3, 0, -1); // 0 will be considered as FALSE and removed in default callback
|
||||
|
||||
/* Callback function
|
||||
* Prototype : bool even(array $input)
|
||||
* Parameters : $input - input array each element of which will be checked in function even()
|
||||
* Return type : boolean - true if element is even and false otherwise
|
||||
* Description : This function takes array as parameter and checks for each element of array.
|
||||
* It returns true if the element is even number else returns false
|
||||
*/
|
||||
function even($input)
|
||||
{
|
||||
return ($input % 2 == 0);
|
||||
}
|
||||
|
||||
// with all possible arguments
|
||||
var_dump( array_filter($input,"even") );
|
||||
|
||||
// with default arguments
|
||||
var_dump( array_filter($input) );
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : basic functionality ***
|
||||
array(2) {
|
||||
[1]=>
|
||||
int(2)
|
||||
[3]=>
|
||||
int(0)
|
||||
}
|
||||
array(4) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
[4]=>
|
||||
int(-1)
|
||||
}
|
||||
Done
|
50
ext/standard/tests/array/array_filter_error.phpt
Normal file
50
ext/standard/tests/array/array_filter_error.phpt
Normal file
@ -0,0 +1,50 @@
|
||||
--TEST--
|
||||
Test array_filter() function : error conditions
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
echo "*** Testing array_filter() : error conditions ***\n";
|
||||
|
||||
// zero arguments
|
||||
echo "-- Testing array_filter() function with Zero arguments --";
|
||||
var_dump( array_filter() );
|
||||
|
||||
$input = array(0, 1, 2, 3, 5);
|
||||
/* callback function
|
||||
* Prototype : bool odd(array $input)
|
||||
* Parameters : $input - array for which each elements should be checked into the function
|
||||
* Return Type : bool - true if element is odd and returns false otherwise
|
||||
* Description : Function takes array as input and checks for its each elements.
|
||||
*/
|
||||
function odd($input)
|
||||
{
|
||||
return ($input % 2 != 0);
|
||||
}
|
||||
$extra_arg = 10;
|
||||
|
||||
// with one more than the expected number of arguments
|
||||
echo "-- Testing array_filter() function with more than expected no. of arguments --";
|
||||
var_dump( array_filter($input, "odd", $extra_arg) );
|
||||
|
||||
// with incorrect callback function
|
||||
echo "-- Testing array_filter() function with incorrect callback --";
|
||||
var_dump( array_filter($input, "even") );
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : error conditions ***
|
||||
-- Testing array_filter() function with Zero arguments --
|
||||
Warning: Wrong parameter count for array_filter() in %s on line %d
|
||||
NULL
|
||||
-- Testing array_filter() function with more than expected no. of arguments --
|
||||
Warning: Wrong parameter count for array_filter() in %s on line %d
|
||||
NULL
|
||||
-- Testing array_filter() function with incorrect callback --
|
||||
Warning: array_filter(): The second argument, 'even', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
Done
|
195
ext/standard/tests/array/array_filter_variation1.phpt
Normal file
195
ext/standard/tests/array/array_filter_variation1.phpt
Normal file
@ -0,0 +1,195 @@
|
||||
--TEST--
|
||||
Test array_filter() function : usage variations - Unexpected values for 'input' argument
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/* Testing different scalar and non-scalar values for 'input' argument
|
||||
*/
|
||||
echo "*** Testing array_filter() : usage variations - unexpected values for 'input'***\n";
|
||||
|
||||
/* Callback function
|
||||
* Prototype : bool always_true(array $input)
|
||||
* Parameters : array for which each elements needs to be used in function
|
||||
* Return value : Returns true for each element
|
||||
* Discription : function applied to each element of the passed array and returns true
|
||||
*/
|
||||
function always_true($input)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// get an unset variable
|
||||
$unset_var = 10;
|
||||
unset ($unset_var);
|
||||
|
||||
// class definition for object variable
|
||||
class MyClass
|
||||
{
|
||||
public function __toString()
|
||||
{
|
||||
return "object";
|
||||
}
|
||||
}
|
||||
|
||||
// resource variable
|
||||
$fp = fopen(__FILE__, 'r');
|
||||
|
||||
// different values for 'input' argument
|
||||
$input_values = array(
|
||||
|
||||
// int data
|
||||
/*1*/ 0,
|
||||
1,
|
||||
12345,
|
||||
-2345,
|
||||
|
||||
// float data
|
||||
/*5*/ 10.5,
|
||||
-10.5,
|
||||
12.3456789000e10,
|
||||
12.3456789000E-10,
|
||||
.5,
|
||||
|
||||
|
||||
// null data
|
||||
/*10*/ NULL,
|
||||
null,
|
||||
|
||||
// boolean data
|
||||
/*12*/ true,
|
||||
false,
|
||||
TRUE,
|
||||
FALSE,
|
||||
|
||||
// empty data
|
||||
/*16*/ "",
|
||||
'',
|
||||
|
||||
// string data
|
||||
/*18*/ "string",
|
||||
'string',
|
||||
|
||||
// object data
|
||||
/*20*/ new MyClass(),
|
||||
|
||||
// resource data
|
||||
$fp,
|
||||
|
||||
// undefined data
|
||||
@$undefined_var,
|
||||
|
||||
// unset data
|
||||
/*23*/ @$unset_var,
|
||||
);
|
||||
|
||||
// loop through each element of the array for input
|
||||
for($count = 0; $count < count($input_values); $count++) {
|
||||
echo "-- Iteration ".($count + 1)." --\n";
|
||||
var_dump( array_filter($input_values[$count],"always_true") );
|
||||
};
|
||||
|
||||
// closing resource
|
||||
fclose($fp);
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : usage variations - unexpected values for 'input'***
|
||||
-- Iteration 1 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 2 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 3 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 4 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 5 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 6 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 7 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 8 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 9 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 10 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 11 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 12 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 13 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 14 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 15 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 16 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 17 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 18 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 19 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 20 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 21 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 22 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
-- Iteration 23 --
|
||||
|
||||
Warning: array_filter(): The first argument should be an array in %s on line %d
|
||||
NULL
|
||||
Done
|
184
ext/standard/tests/array/array_filter_variation2.phpt
Normal file
184
ext/standard/tests/array/array_filter_variation2.phpt
Normal file
@ -0,0 +1,184 @@
|
||||
--TEST--
|
||||
Test array_filter() function : usage variations - Unexpected values for 'callback' function argument
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/* Testing different scalar non-scalar values in place of 'callback' argument
|
||||
*/
|
||||
echo "*** Testing array_filter() : usage variations - unexpected values for 'callback' function***\n";
|
||||
|
||||
// Initialise variables
|
||||
$input = array('value1', 'value2', 'value3', 'value4');
|
||||
|
||||
//get an unset variable
|
||||
$unset_var = 10;
|
||||
unset ($unset_var);
|
||||
|
||||
// class definition for object variable
|
||||
class MyClass
|
||||
{
|
||||
public function __toString()
|
||||
{
|
||||
return 'object';
|
||||
}
|
||||
}
|
||||
|
||||
// resource variable
|
||||
$fp = fopen(__FILE__, 'r');
|
||||
|
||||
// different scalar/non-scalar values inplace of 'callback'
|
||||
$values = array(
|
||||
|
||||
// int data
|
||||
/*1*/ 0,
|
||||
1,
|
||||
12345,
|
||||
-2345,
|
||||
|
||||
// float data
|
||||
/*5*/ 10.5,
|
||||
-10.5,
|
||||
12.3456789000e10,
|
||||
12.3456789000E-10,
|
||||
.5,
|
||||
|
||||
// array data
|
||||
/*10*/ array(),
|
||||
array(0),
|
||||
array(1),
|
||||
array(1, 2),
|
||||
array('color' => 'red', 'item' => 'pen'),
|
||||
|
||||
// null data
|
||||
/*15*/ NULL,
|
||||
null,
|
||||
|
||||
// boolean data
|
||||
/*17*/ true,
|
||||
false,
|
||||
TRUE,
|
||||
FALSE,
|
||||
|
||||
// empty data
|
||||
/*21*/ "",
|
||||
'',
|
||||
|
||||
// string data
|
||||
/*23*/ "string",
|
||||
'string',
|
||||
|
||||
// object data
|
||||
/*25*/ new MyClass(),
|
||||
|
||||
// resource data
|
||||
$fp,
|
||||
|
||||
// undefined data
|
||||
@$undefined_var,
|
||||
|
||||
// unset data
|
||||
/*28*/ @$unset_var,
|
||||
);
|
||||
|
||||
// loop through each element of the 'values' for callback
|
||||
for($count = 0; $count < count($values); $count++) {
|
||||
echo "-- Iteration ".($count + 1)." --";
|
||||
var_dump( array_filter($input, $values[$count]) );
|
||||
};
|
||||
|
||||
// closing resource
|
||||
fclose($fp);
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : usage variations - unexpected values for 'callback' function***
|
||||
-- Iteration 1 --
|
||||
Warning: array_filter(): The second argument, '0', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 2 --
|
||||
Warning: array_filter(): The second argument, '1', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 3 --
|
||||
Warning: array_filter(): The second argument, '12345', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 4 --
|
||||
Warning: array_filter(): The second argument, '-2345', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 5 --
|
||||
Warning: array_filter(): The second argument, '10.5', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 6 --
|
||||
Warning: array_filter(): The second argument, '-10.5', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 7 --
|
||||
Warning: array_filter(): The second argument, '123456789000', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 8 --
|
||||
Warning: array_filter(): The second argument, '1.23456789E-9', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 9 --
|
||||
Warning: array_filter(): The second argument, '0.5', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 10 --
|
||||
Warning: array_filter(): The second argument, 'Array', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 11 --
|
||||
Warning: array_filter(): The second argument, 'Array', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 12 --
|
||||
Warning: array_filter(): The second argument, 'Array', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 13 --
|
||||
Warning: array_filter(): The second argument, 'Array', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 14 --
|
||||
Warning: array_filter(): The second argument, 'Array', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 15 --
|
||||
Warning: array_filter(): The second argument, '', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 16 --
|
||||
Warning: array_filter(): The second argument, '', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 17 --
|
||||
Warning: array_filter(): The second argument, '1', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 18 --
|
||||
Warning: array_filter(): The second argument, '', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 19 --
|
||||
Warning: array_filter(): The second argument, '1', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 20 --
|
||||
Warning: array_filter(): The second argument, '', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 21 --
|
||||
Warning: array_filter(): The second argument, '', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 22 --
|
||||
Warning: array_filter(): The second argument, '', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 23 --
|
||||
Warning: array_filter(): The second argument, 'string', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 24 --
|
||||
Warning: array_filter(): The second argument, 'string', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 25 --
|
||||
Warning: array_filter(): The second argument, 'object', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 26 --
|
||||
Warning: array_filter(): The second argument, %s, should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 27 --
|
||||
Warning: array_filter(): The second argument, '', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
-- Iteration 28 --
|
||||
Warning: array_filter(): The second argument, '', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
Done
|
216
ext/standard/tests/array/array_filter_variation3.phpt
Normal file
216
ext/standard/tests/array/array_filter_variation3.phpt
Normal file
@ -0,0 +1,216 @@
|
||||
--TEST--
|
||||
Test array_filter() function : usage variations - Different types of array for 'input' argument
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing different types of array as 'input' argument.
|
||||
*/
|
||||
|
||||
// callback function returning always false
|
||||
function always_false($input)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// callback function returning always true
|
||||
function always_true($input)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
echo "*** Testing array_filter() : usage variations - different types of array for 'input' argument***\n";
|
||||
|
||||
// different types of 'input' array
|
||||
$input_values = array(
|
||||
array(0, 1, 2, -1, 034, 0X4A), // integer values
|
||||
array(0.0, 1.2, 1.2e3, 1.2e-3), // float values
|
||||
array('value1', "value2", '', " ", ""), // string values
|
||||
array(true, false, TRUE, FALSE), // bool values
|
||||
array(null, NULL), // null values
|
||||
array(1 => 'one', 'zero' => 0, -2 => "value"), //associative array
|
||||
array("one" => 1, null => 'null', 5.2 => "float", true => 1, "" => 'empty'), // associative array with different keys
|
||||
array(1 => 'one', 2, "key" => 'value') // combinition of associative and non-associative array
|
||||
|
||||
);
|
||||
|
||||
// loop through each element of 'input' with default callback
|
||||
for($count = 0; $count < count($input_values); $count++)
|
||||
{
|
||||
echo "-- Iteration ".($count + 1). " --\n";
|
||||
var_dump( array_filter($input_values[$count]) );
|
||||
var_dump( array_filter($input_values[$count], 'always_true') );
|
||||
var_dump( array_filter($input_values[$count], 'always_false') );
|
||||
}
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : usage variations - different types of array for 'input' argument***
|
||||
-- Iteration 1 --
|
||||
array(5) {
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(2)
|
||||
[3]=>
|
||||
int(-1)
|
||||
[4]=>
|
||||
int(28)
|
||||
[5]=>
|
||||
int(74)
|
||||
}
|
||||
array(6) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(2)
|
||||
[3]=>
|
||||
int(-1)
|
||||
[4]=>
|
||||
int(28)
|
||||
[5]=>
|
||||
int(74)
|
||||
}
|
||||
array(0) {
|
||||
}
|
||||
-- Iteration 2 --
|
||||
array(3) {
|
||||
[1]=>
|
||||
float(1.2)
|
||||
[2]=>
|
||||
float(1200)
|
||||
[3]=>
|
||||
float(0.0012)
|
||||
}
|
||||
array(4) {
|
||||
[0]=>
|
||||
float(0)
|
||||
[1]=>
|
||||
float(1.2)
|
||||
[2]=>
|
||||
float(1200)
|
||||
[3]=>
|
||||
float(0.0012)
|
||||
}
|
||||
array(0) {
|
||||
}
|
||||
-- Iteration 3 --
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(6) "value1"
|
||||
[1]=>
|
||||
string(6) "value2"
|
||||
[3]=>
|
||||
string(1) " "
|
||||
}
|
||||
array(5) {
|
||||
[0]=>
|
||||
string(6) "value1"
|
||||
[1]=>
|
||||
string(6) "value2"
|
||||
[2]=>
|
||||
string(0) ""
|
||||
[3]=>
|
||||
string(1) " "
|
||||
[4]=>
|
||||
string(0) ""
|
||||
}
|
||||
array(0) {
|
||||
}
|
||||
-- Iteration 4 --
|
||||
array(2) {
|
||||
[0]=>
|
||||
bool(true)
|
||||
[2]=>
|
||||
bool(true)
|
||||
}
|
||||
array(4) {
|
||||
[0]=>
|
||||
bool(true)
|
||||
[1]=>
|
||||
bool(false)
|
||||
[2]=>
|
||||
bool(true)
|
||||
[3]=>
|
||||
bool(false)
|
||||
}
|
||||
array(0) {
|
||||
}
|
||||
-- Iteration 5 --
|
||||
array(0) {
|
||||
}
|
||||
array(2) {
|
||||
[0]=>
|
||||
NULL
|
||||
[1]=>
|
||||
NULL
|
||||
}
|
||||
array(0) {
|
||||
}
|
||||
-- Iteration 6 --
|
||||
array(2) {
|
||||
[1]=>
|
||||
string(3) "one"
|
||||
[-2]=>
|
||||
string(5) "value"
|
||||
}
|
||||
array(3) {
|
||||
[1]=>
|
||||
string(3) "one"
|
||||
["zero"]=>
|
||||
int(0)
|
||||
[-2]=>
|
||||
string(5) "value"
|
||||
}
|
||||
array(0) {
|
||||
}
|
||||
-- Iteration 7 --
|
||||
array(4) {
|
||||
["one"]=>
|
||||
int(1)
|
||||
[""]=>
|
||||
string(5) "empty"
|
||||
[5]=>
|
||||
string(5) "float"
|
||||
[1]=>
|
||||
int(1)
|
||||
}
|
||||
array(4) {
|
||||
["one"]=>
|
||||
int(1)
|
||||
[""]=>
|
||||
string(5) "empty"
|
||||
[5]=>
|
||||
string(5) "float"
|
||||
[1]=>
|
||||
int(1)
|
||||
}
|
||||
array(0) {
|
||||
}
|
||||
-- Iteration 8 --
|
||||
array(3) {
|
||||
[1]=>
|
||||
string(3) "one"
|
||||
[2]=>
|
||||
int(2)
|
||||
["key"]=>
|
||||
string(5) "value"
|
||||
}
|
||||
array(3) {
|
||||
[1]=>
|
||||
string(3) "one"
|
||||
[2]=>
|
||||
int(2)
|
||||
["key"]=>
|
||||
string(5) "value"
|
||||
}
|
||||
array(0) {
|
||||
}
|
||||
Done
|
97
ext/standard/tests/array/array_filter_variation4.phpt
Normal file
97
ext/standard/tests/array/array_filter_variation4.phpt
Normal file
@ -0,0 +1,97 @@
|
||||
--TEST--
|
||||
Test array_filter() function : usage variations - Different types of 'callback' function
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing different types of callback functions to array_filter()
|
||||
* with parameter and return
|
||||
* with paramter and without return
|
||||
* without parameter and with return
|
||||
* without parameter and without return
|
||||
*/
|
||||
|
||||
echo "*** Testing array_filter() : usage variation - different 'callback' functions***\n";
|
||||
|
||||
// Initialize variables
|
||||
$input = array(0, -1, 2, 3.4E-3, 'hello', "value", "key" => 4, 'null' => NULL);
|
||||
|
||||
// callback function without parameters and with return value
|
||||
function callback1()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
echo "-- Callback function without parameter and with return --\n";
|
||||
var_dump( array_filter($input, "callback1") );
|
||||
|
||||
// callback function with parameter and without return value
|
||||
function callback2($input)
|
||||
{
|
||||
}
|
||||
echo "-- Callback funciton with parameter and without return --\n";
|
||||
var_dump( array_filter($input, "callback2") );
|
||||
|
||||
|
||||
// callback function without parameter and without return value
|
||||
function callback3()
|
||||
{
|
||||
}
|
||||
echo "-- Callback function without parameter and return --\n";
|
||||
var_dump( array_filter($input, "callback3") );
|
||||
|
||||
// callback function with parameter and with return value
|
||||
function callback4($input)
|
||||
{
|
||||
if($input > 0 ) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
echo "-- Callback function with parameter and return --\n";
|
||||
var_dump( array_filter($input, "callback4") );
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : usage variation - different 'callback' functions***
|
||||
-- Callback function without parameter and with return --
|
||||
array(8) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(-1)
|
||||
[2]=>
|
||||
int(2)
|
||||
[3]=>
|
||||
float(0.0034)
|
||||
[4]=>
|
||||
string(5) "hello"
|
||||
[5]=>
|
||||
string(5) "value"
|
||||
["key"]=>
|
||||
int(4)
|
||||
["null"]=>
|
||||
NULL
|
||||
}
|
||||
-- Callback funciton with parameter and without return --
|
||||
array(0) {
|
||||
}
|
||||
-- Callback function without parameter and return --
|
||||
array(0) {
|
||||
}
|
||||
-- Callback function with parameter and return --
|
||||
array(3) {
|
||||
[2]=>
|
||||
int(2)
|
||||
[3]=>
|
||||
float(0.0034)
|
||||
["key"]=>
|
||||
int(4)
|
||||
}
|
||||
Done
|
109
ext/standard/tests/array/array_filter_variation5.phpt
Normal file
109
ext/standard/tests/array/array_filter_variation5.phpt
Normal file
@ -0,0 +1,109 @@
|
||||
--TEST--
|
||||
Test array_filter() function : usage variations - 'input' argument with different false entries
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* With default callback function argument, array_filter() removes elements which are interpreted as false
|
||||
* Here Testing all the false array element possibilities
|
||||
*/
|
||||
|
||||
// callback function always_true
|
||||
function always_true($input)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// callback function always_false
|
||||
function always_false($input)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
echo "*** Testing array_filter() : usage variations - different false elements in 'input' ***\n";
|
||||
|
||||
// unset variable
|
||||
$unset_var = 10;
|
||||
unset($unset_var);
|
||||
|
||||
// empty heredoc string
|
||||
$empty_heredoc =<<<EOT
|
||||
EOT;
|
||||
|
||||
// input array with different false elements
|
||||
$input = array(
|
||||
false,
|
||||
False,
|
||||
'',
|
||||
"",
|
||||
0,
|
||||
0.0,
|
||||
null,
|
||||
NULL,
|
||||
"0",
|
||||
'0',
|
||||
array(),
|
||||
!1,
|
||||
1==2,
|
||||
$empty_heredoc,
|
||||
@$unset_var,
|
||||
@$undefined_var,
|
||||
);
|
||||
|
||||
// With default callback function
|
||||
var_dump( array_filter($input) );
|
||||
|
||||
// With callback function which returns always true
|
||||
var_dump( array_filter($input, 'always_true') );
|
||||
|
||||
// With callback function which returns always false
|
||||
var_dump( array_filter($input, 'always_false') );
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : usage variations - different false elements in 'input' ***
|
||||
array(0) {
|
||||
}
|
||||
array(16) {
|
||||
[0]=>
|
||||
bool(false)
|
||||
[1]=>
|
||||
bool(false)
|
||||
[2]=>
|
||||
string(0) ""
|
||||
[3]=>
|
||||
string(0) ""
|
||||
[4]=>
|
||||
int(0)
|
||||
[5]=>
|
||||
float(0)
|
||||
[6]=>
|
||||
NULL
|
||||
[7]=>
|
||||
NULL
|
||||
[8]=>
|
||||
string(1) "0"
|
||||
[9]=>
|
||||
string(1) "0"
|
||||
[10]=>
|
||||
array(0) {
|
||||
}
|
||||
[11]=>
|
||||
bool(false)
|
||||
[12]=>
|
||||
bool(false)
|
||||
[13]=>
|
||||
string(0) ""
|
||||
[14]=>
|
||||
NULL
|
||||
[15]=>
|
||||
NULL
|
||||
}
|
||||
array(0) {
|
||||
}
|
||||
Done
|
94
ext/standard/tests/array/array_filter_variation6.phpt
Normal file
94
ext/standard/tests/array/array_filter_variation6.phpt
Normal file
@ -0,0 +1,94 @@
|
||||
--TEST--
|
||||
Test array_filter() function : usage variations - 'input' array containing references
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing 'input' array which contains elements as reference to other data
|
||||
*/
|
||||
|
||||
echo "*** Testing array_filter() : usage variations - 'input' containing references ***\n";
|
||||
|
||||
// Callback function
|
||||
/* Prototype : bool callback(array $input)
|
||||
* Parameter : $input - array of which each element need to be checked in function
|
||||
* Return Type : returns true or false
|
||||
* Description : This function checks each element of an input array if element > 5 then
|
||||
* returns true else returns false
|
||||
*/
|
||||
function callback($input)
|
||||
{
|
||||
if($input > 5) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// initializing variables
|
||||
$value1 = array(1, 2, 8);
|
||||
$value2 = array(5, 6, 4);
|
||||
$input = array(&$value1, 10, &$value2, 'value');
|
||||
|
||||
// with 'callback' argument
|
||||
var_dump( array_filter($input, 'callback') );
|
||||
|
||||
// with default 'callback' argument
|
||||
var_dump( array_filter($input) );
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : usage variations - 'input' containing references ***
|
||||
array(3) {
|
||||
[0]=>
|
||||
&array(3) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(8)
|
||||
}
|
||||
[1]=>
|
||||
int(10)
|
||||
[2]=>
|
||||
&array(3) {
|
||||
[0]=>
|
||||
int(5)
|
||||
[1]=>
|
||||
int(6)
|
||||
[2]=>
|
||||
int(4)
|
||||
}
|
||||
}
|
||||
array(4) {
|
||||
[0]=>
|
||||
&array(3) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(8)
|
||||
}
|
||||
[1]=>
|
||||
int(10)
|
||||
[2]=>
|
||||
&array(3) {
|
||||
[0]=>
|
||||
int(5)
|
||||
[1]=>
|
||||
int(6)
|
||||
[2]=>
|
||||
int(4)
|
||||
}
|
||||
[3]=>
|
||||
string(5) "value"
|
||||
}
|
||||
Done
|
80
ext/standard/tests/array/array_filter_variation7.phpt
Normal file
80
ext/standard/tests/array/array_filter_variation7.phpt
Normal file
@ -0,0 +1,80 @@
|
||||
--TEST--
|
||||
Test array_filter() function : usage variations - anonymous callback functions
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing different anonymous callback functions with passed by value and reference arguments,
|
||||
*/
|
||||
|
||||
echo "*** Testing array_filter() : usage variations - Anonymous callback functions ***\n";
|
||||
|
||||
$input = array(0, 1, -1, 10, 100, 1000, 'Hello', null);
|
||||
|
||||
// anonymous callback function
|
||||
echo "Anonymous callback function with regular parameter and statement\n";
|
||||
var_dump( array_filter($input, create_function('$input', 'return ($input > 1);') ) );
|
||||
|
||||
// anonymous callback function with reference
|
||||
echo "Anonymous callback function with reference parameter\n";
|
||||
var_dump( array_filter($input, create_function('&$input', 'return ($input < 1);') ) );
|
||||
|
||||
// anonymous callback function with null argument
|
||||
echo "Anonymous callback funciton with null argument\n";
|
||||
var_dump( array_filter($input, create_function(null, 'return true;') ) );
|
||||
|
||||
// anonymous callback function with argument and null statement
|
||||
echo "Anonymous callback function with regular argument and null statement\n";
|
||||
var_dump( array_filter($input, create_function('$input', null) ) );
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : usage variations - Anonymous callback functions ***
|
||||
Anonymous callback function with regular parameter and statement
|
||||
array(3) {
|
||||
[3]=>
|
||||
int(10)
|
||||
[4]=>
|
||||
int(100)
|
||||
[5]=>
|
||||
int(1000)
|
||||
}
|
||||
Anonymous callback function with reference parameter
|
||||
array(4) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[2]=>
|
||||
int(-1)
|
||||
[6]=>
|
||||
string(5) "Hello"
|
||||
[7]=>
|
||||
NULL
|
||||
}
|
||||
Anonymous callback funciton with null argument
|
||||
array(8) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(-1)
|
||||
[3]=>
|
||||
int(10)
|
||||
[4]=>
|
||||
int(100)
|
||||
[5]=>
|
||||
int(1000)
|
||||
[6]=>
|
||||
string(5) "Hello"
|
||||
[7]=>
|
||||
NULL
|
||||
}
|
||||
Anonymous callback function with regular argument and null statement
|
||||
array(0) {
|
||||
}
|
||||
Done
|
151
ext/standard/tests/array/array_filter_variation8.phpt
Normal file
151
ext/standard/tests/array/array_filter_variation8.phpt
Normal file
@ -0,0 +1,151 @@
|
||||
--TEST--
|
||||
Test array_filter() function : usage variations - Callback function with different return values
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* callback functions are expected to return bool value for array_filter()
|
||||
* here testing callback functions for return values other than bool
|
||||
*/
|
||||
|
||||
echo "*** Testing array_filter() : usage variations - callback function with different return values***\n";
|
||||
|
||||
$input = array(0, 1, -1, 10, 100, 1000, 'Hello', null, true);
|
||||
|
||||
// callback functions
|
||||
// int as return value
|
||||
function callback1($input)
|
||||
{
|
||||
return 5;
|
||||
}
|
||||
echo "callback function with int return value\n";
|
||||
var_dump( array_filter($input, 'callback1') );
|
||||
|
||||
// float as return value
|
||||
function callback2($input)
|
||||
{
|
||||
return 3.4;
|
||||
}
|
||||
echo "callback function with float return value\n";
|
||||
var_dump( array_filter($input, 'callback2') );
|
||||
|
||||
// string as return value
|
||||
function callback3($input)
|
||||
{
|
||||
return 'value';
|
||||
}
|
||||
echo "callback function with string return value\n";
|
||||
var_dump( array_filter($input, 'callback3') );
|
||||
|
||||
// null as return value
|
||||
function callback4($input)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
echo "callback function with null return value\n";
|
||||
var_dump( array_filter($input, 'callback4') );
|
||||
|
||||
// array as return value
|
||||
function callback5($input)
|
||||
{
|
||||
return array(8);
|
||||
}
|
||||
echo "callback function with array as return value\n";
|
||||
var_dump( array_filter($input, 'callback5') );
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : usage variations - callback function with different return values***
|
||||
callback function with int return value
|
||||
array(9) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(-1)
|
||||
[3]=>
|
||||
int(10)
|
||||
[4]=>
|
||||
int(100)
|
||||
[5]=>
|
||||
int(1000)
|
||||
[6]=>
|
||||
string(5) "Hello"
|
||||
[7]=>
|
||||
NULL
|
||||
[8]=>
|
||||
bool(true)
|
||||
}
|
||||
callback function with float return value
|
||||
array(9) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(-1)
|
||||
[3]=>
|
||||
int(10)
|
||||
[4]=>
|
||||
int(100)
|
||||
[5]=>
|
||||
int(1000)
|
||||
[6]=>
|
||||
string(5) "Hello"
|
||||
[7]=>
|
||||
NULL
|
||||
[8]=>
|
||||
bool(true)
|
||||
}
|
||||
callback function with string return value
|
||||
array(9) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(-1)
|
||||
[3]=>
|
||||
int(10)
|
||||
[4]=>
|
||||
int(100)
|
||||
[5]=>
|
||||
int(1000)
|
||||
[6]=>
|
||||
string(5) "Hello"
|
||||
[7]=>
|
||||
NULL
|
||||
[8]=>
|
||||
bool(true)
|
||||
}
|
||||
callback function with null return value
|
||||
array(0) {
|
||||
}
|
||||
callback function with array as return value
|
||||
array(9) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(-1)
|
||||
[3]=>
|
||||
int(10)
|
||||
[4]=>
|
||||
int(100)
|
||||
[5]=>
|
||||
int(1000)
|
||||
[6]=>
|
||||
string(5) "Hello"
|
||||
[7]=>
|
||||
NULL
|
||||
[8]=>
|
||||
bool(true)
|
||||
}
|
||||
Done
|
72
ext/standard/tests/array/array_filter_variation9.phpt
Normal file
72
ext/standard/tests/array/array_filter_variation9.phpt
Normal file
@ -0,0 +1,72 @@
|
||||
--TEST--
|
||||
Test array_filter() function : usage variations - built-in functions as 'callback' argument
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : array array_filter(array $input [, callback $callback])
|
||||
* Description: Filters elements from the array via the callback.
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Passing built-in functions and different language constructs as 'callback' argument
|
||||
*/
|
||||
|
||||
echo "*** Testing array_filter() : usage variations - built-in functions as 'callback' argument ***\n";
|
||||
|
||||
$input = array(0, 1, -1, 10, 100, 1000, 'Hello', null);
|
||||
|
||||
// using built-in function 'is_int' as 'callback'
|
||||
var_dump( array_filter($input, 'is_int') );
|
||||
|
||||
// using built-in function 'chr' as 'callback'
|
||||
var_dump( array_filter($input, 'chr') );
|
||||
|
||||
// using language construct 'echo' as 'callback'
|
||||
var_dump( array_filter($input, 'echo') );
|
||||
|
||||
// using language construct 'exit' as 'callback'
|
||||
var_dump( array_filter($input, 'exit') );
|
||||
|
||||
echo "Done"
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_filter() : usage variations - built-in functions as 'callback' argument ***
|
||||
array(6) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(-1)
|
||||
[3]=>
|
||||
int(10)
|
||||
[4]=>
|
||||
int(100)
|
||||
[5]=>
|
||||
int(1000)
|
||||
}
|
||||
array(8) {
|
||||
[0]=>
|
||||
int(0)
|
||||
[1]=>
|
||||
int(1)
|
||||
[2]=>
|
||||
int(-1)
|
||||
[3]=>
|
||||
int(10)
|
||||
[4]=>
|
||||
int(100)
|
||||
[5]=>
|
||||
int(1000)
|
||||
[6]=>
|
||||
string(5) "Hello"
|
||||
[7]=>
|
||||
NULL
|
||||
}
|
||||
|
||||
Warning: array_filter(): The second argument, 'echo', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
|
||||
Warning: array_filter(): The second argument, 'exit', should be a valid callback in %s on line %d
|
||||
NULL
|
||||
Done
|
Loading…
Reference in New Issue
Block a user