mirror of
https://github.com/php/php-src.git
synced 2025-08-18 15:08:55 +02:00
New testcases for array_rand() function
This commit is contained in:
parent
dd465b457c
commit
36ca7b94e6
9 changed files with 1069 additions and 0 deletions
52
ext/standard/tests/array/array_rand_basic1.phpt
Normal file
52
ext/standard/tests/array/array_rand_basic1.phpt
Normal file
|
@ -0,0 +1,52 @@
|
|||
--TEST--
|
||||
Test array_rand() function : basic functionality - array with default keys
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_rand(array $input [, int $num_req])
|
||||
* Description: Return key/keys for random entry/entries in the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test array_rand() when array with default keys is passed to 'input' argument
|
||||
*/
|
||||
|
||||
echo "*** Testing array_rand() : array with default keys ***\n";
|
||||
|
||||
|
||||
// Initialise the 'input' and 'num_req' variables
|
||||
$input = array(10, 20, 30, 40, 50, 60, 70);
|
||||
$num_req = 6;
|
||||
|
||||
// Calling array_rand() with optional argument
|
||||
echo"\n-- with all default and optional arguments --\n";
|
||||
var_dump( array_rand($input,$num_req) );
|
||||
|
||||
// Calling array_rand() with default arguments
|
||||
echo"\n-- with default argument --\n";
|
||||
var_dump( array_rand($input) );
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_rand() : array with default keys ***
|
||||
|
||||
-- with all default and optional arguments --
|
||||
array(%d) {
|
||||
[0]=>
|
||||
int(%d)
|
||||
[1]=>
|
||||
int(%d)
|
||||
[2]=>
|
||||
int(%d)
|
||||
[3]=>
|
||||
int(%d)
|
||||
[4]=>
|
||||
int(%d)
|
||||
[5]=>
|
||||
int(%d)
|
||||
}
|
||||
|
||||
-- with default argument --
|
||||
int(%d)
|
||||
Done
|
57
ext/standard/tests/array/array_rand_basic2.phpt
Normal file
57
ext/standard/tests/array/array_rand_basic2.phpt
Normal file
|
@ -0,0 +1,57 @@
|
|||
--TEST--
|
||||
Test array_rand() function : basic functionality - with associative array for 'input' argument
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_rand(array $input [, int $num_req])
|
||||
* Description: Return key/keys for random entry/entries in the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test array_rand() when associative array is passed to 'input' argument
|
||||
*/
|
||||
|
||||
echo "*** Testing array_rand() : with associative array ***\n";
|
||||
|
||||
|
||||
// Initialise the 'input' and 'num_req' variables
|
||||
$input = array(
|
||||
'one' => 1, 'two' => 2, 'three' => 3,
|
||||
'FoUr' => 'four', '#5' => 5, 'SIX' => 'six',
|
||||
"seven" => 7, "#8" => "eight", "nine" => "NINE"
|
||||
);
|
||||
|
||||
$num_req = 6;
|
||||
|
||||
// Calling array_rand() with optional argument
|
||||
echo"\n-- with all default and optional arguments --\n";
|
||||
var_dump( array_rand($input,$num_req) );
|
||||
|
||||
// Calling array_rand() with default arguments
|
||||
echo"\n-- with default argument --\n";
|
||||
var_dump( array_rand($input) );
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_rand() : with associative array ***
|
||||
|
||||
-- with all default and optional arguments --
|
||||
array(6) {
|
||||
[0]=>
|
||||
string(%d) "%s"
|
||||
[1]=>
|
||||
string(%d) "%s"
|
||||
[2]=>
|
||||
string(%d) "%s"
|
||||
[3]=>
|
||||
string(%d) "%s"
|
||||
[4]=>
|
||||
string(%d) "%s"
|
||||
[5]=>
|
||||
string(%d) "%s"
|
||||
}
|
||||
|
||||
-- with default argument --
|
||||
string(%d) "%s"
|
||||
Done
|
37
ext/standard/tests/array/array_rand_error.phpt
Normal file
37
ext/standard/tests/array/array_rand_error.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--TEST--
|
||||
Test array_rand() function : error conditions
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_rand(array input [, int num_req])
|
||||
* Description: Return key/keys for random entry/entries in the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
echo "*** Testing array_rand() : error conditions ***\n";
|
||||
|
||||
// Zero arguments
|
||||
echo "\n-- Testing array_rand() function with Zero arguments --\n";
|
||||
var_dump( array_rand() );
|
||||
|
||||
//Test array_rand with one more than the expected number of arguments
|
||||
echo "\n-- Testing array_rand() function with more than expected no. of arguments --\n";
|
||||
$input = array(1, 2);
|
||||
$num_req = 10;
|
||||
$extra_arg = 10;
|
||||
var_dump( array_rand($input,$num_req, $extra_arg) );
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_rand() : error conditions ***
|
||||
|
||||
-- Testing array_rand() function with Zero arguments --
|
||||
|
||||
Warning: Wrong parameter count for array_rand() in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Testing array_rand() function with more than expected no. of arguments --
|
||||
|
||||
Warning: Wrong parameter count for array_rand() in %s on line %d
|
||||
NULL
|
||||
Done
|
216
ext/standard/tests/array/array_rand_variation1.phpt
Normal file
216
ext/standard/tests/array/array_rand_variation1.phpt
Normal file
|
@ -0,0 +1,216 @@
|
|||
--TEST--
|
||||
Test array_rand() function : usage variations - unexpected values for 'input' parameter
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_rand(array input [, int num_req])
|
||||
* Description: Return key/keys for random entry/entries in the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test array_rand() with different types of values other than arrays passed to the 'input' parameter
|
||||
* to see that function works with unexpeced data and generates warning message as required.
|
||||
*/
|
||||
|
||||
echo "*** Testing array_rand() : unexpected values for 'input' parameter ***\n";
|
||||
|
||||
// Initialise function arguments
|
||||
$num_req = 10;
|
||||
|
||||
//get an unset variable
|
||||
$unset_var = 10;
|
||||
unset ($unset_var);
|
||||
|
||||
//get a resource variable
|
||||
$fp = fopen(__FILE__, "r");
|
||||
|
||||
//define a class
|
||||
class test
|
||||
{
|
||||
var $t = 10;
|
||||
function __toString()
|
||||
{
|
||||
return "object";
|
||||
}
|
||||
}
|
||||
|
||||
//array of different values for 'input' parameter
|
||||
$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 test(),
|
||||
|
||||
// resource data
|
||||
/*21*/ $fp,
|
||||
|
||||
// undefined data
|
||||
/*22*/ @$undefined_var,
|
||||
|
||||
// unset data
|
||||
/*23*/ @$unset_var,
|
||||
);
|
||||
|
||||
/* loop through each element of the array to test array_rand() function
|
||||
* for different values for 'input' argument
|
||||
*/
|
||||
$count = 1;
|
||||
foreach($values as $value) {
|
||||
echo "\n-- Iteration $count --\n";
|
||||
var_dump( array_rand($value,$num_req) );
|
||||
$count++;
|
||||
};
|
||||
|
||||
// closing the resource
|
||||
fclose($fp);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_rand() : unexpected values for 'input' parameter ***
|
||||
|
||||
-- Iteration 1 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 2 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 3 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 4 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 5 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 6 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 7 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 8 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 9 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 10 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 11 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 12 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 13 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 14 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 15 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 16 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 17 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 18 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 19 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 20 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 21 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 22 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 23 --
|
||||
|
||||
Warning: array_rand(): First argument has to be an array in %s on line %d
|
||||
NULL
|
||||
Done
|
214
ext/standard/tests/array/array_rand_variation2.phpt
Normal file
214
ext/standard/tests/array/array_rand_variation2.phpt
Normal file
|
@ -0,0 +1,214 @@
|
|||
--TEST--
|
||||
Test array_rand() function : usage variations - unexpected values for 'num_req' parameter
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_rand(array input [, int num_req])
|
||||
* Description: Return key/keys for random entry/entries in the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test array_rand() with different types of values other than int passed to 'num_req' argument
|
||||
* to see that function works with unexpeced data and generates warning message as required.
|
||||
*/
|
||||
|
||||
echo "*** Testing array_rand() : unexpected values for 'num_req' parameter ***\n";
|
||||
|
||||
// Initialise function arguments
|
||||
$input = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
|
||||
|
||||
//get an unset variable
|
||||
$unset_var = 10;
|
||||
unset ($unset_var);
|
||||
|
||||
//define a class
|
||||
class test
|
||||
{
|
||||
var $t = 10;
|
||||
function __toString()
|
||||
{
|
||||
return "3object";
|
||||
}
|
||||
}
|
||||
|
||||
//array of values to iterate over
|
||||
$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 test(),
|
||||
|
||||
// undefined data
|
||||
/*21*/ @$undefined_var,
|
||||
|
||||
// unset data
|
||||
/*22*/ @$unset_var,
|
||||
);
|
||||
|
||||
|
||||
// loop through each element of the array for different values for 'num_req' argument
|
||||
$count = 1;
|
||||
foreach($values as $value) {
|
||||
echo "\n-- Iteration $count --\n";
|
||||
var_dump( array_rand($input,$value) );
|
||||
$count++;
|
||||
};
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_rand() : unexpected values for 'num_req' parameter ***
|
||||
|
||||
-- Iteration 1 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 2 --
|
||||
int(%d)
|
||||
|
||||
-- Iteration 3 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 4 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 5 --
|
||||
array(10) {
|
||||
[0]=>
|
||||
int(%d)
|
||||
[1]=>
|
||||
int(%d)
|
||||
[2]=>
|
||||
int(%d)
|
||||
[3]=>
|
||||
int(%d)
|
||||
[4]=>
|
||||
int(%d)
|
||||
[5]=>
|
||||
int(%d)
|
||||
[6]=>
|
||||
int(%d)
|
||||
[7]=>
|
||||
int(%d)
|
||||
[8]=>
|
||||
int(%d)
|
||||
[9]=>
|
||||
int(%d)
|
||||
}
|
||||
|
||||
-- Iteration 6 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 7 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 8 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 9 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 10 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 11 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 12 --
|
||||
int(%d)
|
||||
|
||||
-- Iteration 13 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 14 --
|
||||
int(%d)
|
||||
|
||||
-- Iteration 15 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 16 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 17 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 18 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 19 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 20 --
|
||||
|
||||
Notice: Object of class test could not be converted to int in %s on line %d
|
||||
int(%d)
|
||||
|
||||
-- Iteration 21 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- Iteration 22 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
Done
|
149
ext/standard/tests/array/array_rand_variation3.phpt
Normal file
149
ext/standard/tests/array/array_rand_variation3.phpt
Normal file
|
@ -0,0 +1,149 @@
|
|||
--TEST--
|
||||
Test array_rand() function : usage variation - with MultiDimensional array
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_rand(array $input [, int $num_req])
|
||||
* Description: Return key/keys for random entry/entries in the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test behaviour of array_rand() function when multi-dimensional array
|
||||
* is passed to 'input' argument
|
||||
*/
|
||||
|
||||
echo "*** Testing array_rand() : with multi-dimensional array ***\n";
|
||||
|
||||
// initialise the multi-dimensional array
|
||||
$input = array(
|
||||
// array with int values
|
||||
/*1*/ array(1, 2, 0, -0, -1, -2),
|
||||
|
||||
// array with float values
|
||||
array(1.23, -1.23, 0.34, -0.34, 0e2, 2e-3, -2e2, -40e-2),
|
||||
|
||||
// array with single quoted strings
|
||||
/*3*/ array('one', '123numbers', 'hello\tworld', 'hello world\0', '12.34floatnum'),
|
||||
|
||||
// array with double quoted strings
|
||||
array("one","123numbers", "hello\tworld", "hello world\0", "12.34floatnum"),
|
||||
|
||||
// array with bool values
|
||||
/*5*/ array(true, TRUE, FALSE, false, TrUe, FaLsE),
|
||||
|
||||
// array with hexa values
|
||||
array(0x123, -0x123, 0xabc, 0xABC, 0xab),
|
||||
|
||||
// array with null values
|
||||
/*7*/ array(null, NULL, "\0", Null, NuLl)
|
||||
|
||||
);
|
||||
|
||||
// initialise 'num_req' variable
|
||||
$num_req = 3;
|
||||
|
||||
// calling array_rand() function with multi-dimensional array
|
||||
var_dump( array_rand($input, $num_req) );
|
||||
|
||||
// looping to test array_rand() with each sub-array in the multi-dimensional array
|
||||
echo "\n*** Testing array_rand() with arrays having different types of values ***\n";
|
||||
$counter = 1;
|
||||
foreach($input as $arr) {
|
||||
echo "\n-- Iteration $counter --\n";
|
||||
var_dump( array_rand($arr) ); // with default arguments
|
||||
var_dump( array_rand($arr, 3) ); // with default as well as optional arguments
|
||||
$counter++;
|
||||
}
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_rand() : with multi-dimensional array ***
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(%d)
|
||||
[1]=>
|
||||
int(%d)
|
||||
[2]=>
|
||||
int(%d)
|
||||
}
|
||||
|
||||
*** Testing array_rand() with arrays having different types of values ***
|
||||
|
||||
-- Iteration 1 --
|
||||
int(%d)
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(%d)
|
||||
[1]=>
|
||||
int(%d)
|
||||
[2]=>
|
||||
int(%d)
|
||||
}
|
||||
|
||||
-- Iteration 2 --
|
||||
int(%d)
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(%d)
|
||||
[1]=>
|
||||
int(%d)
|
||||
[2]=>
|
||||
int(%d)
|
||||
}
|
||||
|
||||
-- Iteration 3 --
|
||||
int(%d)
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(%d)
|
||||
[1]=>
|
||||
int(%d)
|
||||
[2]=>
|
||||
int(%d)
|
||||
}
|
||||
|
||||
-- Iteration 4 --
|
||||
int(%d)
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(%d)
|
||||
[1]=>
|
||||
int(%d)
|
||||
[2]=>
|
||||
int(%d)
|
||||
}
|
||||
|
||||
-- Iteration 5 --
|
||||
int(%d)
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(%d)
|
||||
[1]=>
|
||||
int(%d)
|
||||
[2]=>
|
||||
int(%d)
|
||||
}
|
||||
|
||||
-- Iteration 6 --
|
||||
int(%d)
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(%d)
|
||||
[1]=>
|
||||
int(%d)
|
||||
[2]=>
|
||||
int(%d)
|
||||
}
|
||||
|
||||
-- Iteration 7 --
|
||||
int(%d)
|
||||
array(3) {
|
||||
[0]=>
|
||||
int(%d)
|
||||
[1]=>
|
||||
int(%d)
|
||||
[2]=>
|
||||
int(%d)
|
||||
}
|
||||
Done
|
167
ext/standard/tests/array/array_rand_variation4.phpt
Normal file
167
ext/standard/tests/array/array_rand_variation4.phpt
Normal file
|
@ -0,0 +1,167 @@
|
|||
--TEST--
|
||||
Test array_rand() function : usage variation - with associative arrays for 'input' parameter
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_rand(array $input [, int $num_req])
|
||||
* Description: Return key/keys for random entry/entries in the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test behaviour of array_rand() function when associative array is passed to
|
||||
* the 'input' parameter in the function call
|
||||
*/
|
||||
|
||||
echo "*** Testing array_rand() : with associative arrays ***\n";
|
||||
|
||||
// initialise associative arrays
|
||||
$asso_arrays = array(
|
||||
|
||||
// array with numeric keys
|
||||
/*1*/ array(1 => 'one', 2 => 2, 1234567890 => 'big', -1 => 'negative key',
|
||||
2.3 => 'float key', 0 => "zero key", 0.2 => 'decimal key',
|
||||
2e2 => 'exp key1', -2e3 => 'negative exp key'),
|
||||
|
||||
// array with string keys
|
||||
array('one' => 1, "two" => 2.0, "three" => 'three',
|
||||
'12twelve' => 12.00, "" => 'empty string', " " => "space key"),
|
||||
|
||||
// array with hexa values as keys
|
||||
/*3*/ array(0xabc => 2748, 0x12f => '303', 0xff => "255", -0xff => "-255"),
|
||||
|
||||
// array with octal values as keys
|
||||
array(0123 => 83, 0129 => 10, 010 => "8", -0348 => "-28", 0012 => '10'),
|
||||
|
||||
// array with bool values as keys
|
||||
array(TRUE => '1', true => true, TrUe => "TRUE",
|
||||
FALSE => '0', false => false, FaLsE => "FALSE"),
|
||||
|
||||
// array with special chars as keys
|
||||
/*6*/ array('##' => "key1", '&$r' => 'key2', '!' => "key3", '<>' =>'key4',
|
||||
"NULL" => 'key5', "\n" => 'newline as key',
|
||||
"\t" => "tab as key", "'" => 'single quote as key',
|
||||
'"' => 'double quote as key', "\0" => "null char as key")
|
||||
);
|
||||
|
||||
/* looping to test array_rand() function with different arrays having
|
||||
* different types of keys
|
||||
*/
|
||||
$counter = 1;
|
||||
foreach($asso_arrays as $input) {
|
||||
echo "\n-- Iteration $counter --\n";
|
||||
|
||||
// with default argument
|
||||
echo"\nWith default argument\n";
|
||||
var_dump( array_rand($input) );
|
||||
|
||||
// with default and optional arguments
|
||||
echo"\nWith num_req = 1\n";
|
||||
var_dump( array_rand($input, 1) ); // with $num_req=1
|
||||
echo"\nWith num_req = 2\n";
|
||||
var_dump( array_rand($input, 2) ); // with $num_req=2
|
||||
|
||||
$counter++;
|
||||
} // end of for loop
|
||||
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTREGEX--
|
||||
\*\*\* Testing array_rand\(\) : with associative arrays \*\*\*
|
||||
|
||||
-- Iteration 1 --
|
||||
|
||||
With default argument
|
||||
int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
|
||||
|
||||
With num_req = 1
|
||||
int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
|
||||
|
||||
With num_req = 2
|
||||
array\(2\) {
|
||||
\[0\]=>
|
||||
int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
|
||||
\[1\]=>
|
||||
int\([012-][12.e]*[23e]*[34]*[5]*[6]*[7]*[8]*[9]*[0]*\)
|
||||
}
|
||||
|
||||
-- Iteration 2 --
|
||||
|
||||
With default argument
|
||||
string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
|
||||
|
||||
With num_req = 1
|
||||
string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
|
||||
|
||||
With num_req = 2
|
||||
array\(2\) {
|
||||
\[0\]=>
|
||||
string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
|
||||
\[1\]=>
|
||||
string\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*"
|
||||
}
|
||||
|
||||
-- Iteration 3 --
|
||||
|
||||
With default argument
|
||||
int\([23-]*[2570]*[345]*[58]*\)
|
||||
|
||||
With num_req = 1
|
||||
int\([23-]*[2570]*[345]*[58]*\)
|
||||
|
||||
With num_req = 2
|
||||
array\(2\) {
|
||||
\[0\]=>
|
||||
int\([23-]*[2570]*[345]*[58]*\)
|
||||
\[1\]=>
|
||||
int\([23-]*[2570]*[345]*[58]*\)
|
||||
}
|
||||
|
||||
-- Iteration 4 --
|
||||
|
||||
With default argument
|
||||
int\([18-]*[023]*[8]*\)
|
||||
|
||||
With num_req = 1
|
||||
int\([18-]*[023]*[8]*\)
|
||||
|
||||
With num_req = 2
|
||||
array\(2\) {
|
||||
\[0\]=>
|
||||
int\([18-]*[023]*[8]*\)
|
||||
\[1\]=>
|
||||
int\([18-]*[023]*[8]*\)
|
||||
}
|
||||
|
||||
-- Iteration 5 --
|
||||
|
||||
With default argument
|
||||
int\([01]\)
|
||||
|
||||
With num_req = 1
|
||||
int\([01]\)
|
||||
|
||||
With num_req = 2
|
||||
array\(2\) {
|
||||
\[0\]=>
|
||||
int\([01]\)
|
||||
\[1\]=>
|
||||
int\([01]\)
|
||||
}
|
||||
|
||||
-- Iteration 6 --
|
||||
|
||||
With default argument
|
||||
string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
|
||||
|
||||
With num_req = 1
|
||||
string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
|
||||
|
||||
With num_req = 2
|
||||
array\(2\) {
|
||||
\[0\]=>
|
||||
string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
|
||||
\[1\]=>
|
||||
string\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*"
|
||||
}
|
||||
Done
|
74
ext/standard/tests/array/array_rand_variation5.phpt
Normal file
74
ext/standard/tests/array/array_rand_variation5.phpt
Normal file
|
@ -0,0 +1,74 @@
|
|||
--TEST--
|
||||
Test array_rand() function : usage variation - invalid values for 'req_num' parameter
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_rand(array $input [, int $num_req])
|
||||
* Description: Return key/keys for random entry/entries in the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test behaviour of array_rand() function when associative array and
|
||||
* various invalid values are passed to the 'input' and 'req_num'
|
||||
* parameters respectively
|
||||
*/
|
||||
|
||||
echo "*** Testing array_rand() : with invalid values for 'req_num' ***\n";
|
||||
|
||||
// initialise associative arrays
|
||||
$input = array(
|
||||
1 => 'one', 2.2 => 'float key', 0.9 => 'decimal key',
|
||||
2e2 => 'exp key1', 2000e-3 => 'negative exp key',
|
||||
0xabc => 2748, 0x12f => '303', 0xff => "255",
|
||||
0123 => 83, 0129 => 10, 010 => "8"
|
||||
);
|
||||
|
||||
// Testing array_rand() function with various invalid 'req_num' values
|
||||
// with valid num_req values
|
||||
echo"\n-- With default num_req value --\n";
|
||||
var_dump( array_rand($input) ); // with default $num_req value
|
||||
echo"\n-- With num_req = 1 --\n";
|
||||
var_dump( array_rand($input, 1) ); // with valid $num_req value
|
||||
|
||||
// with invalid num_req value
|
||||
echo"\n-- With num_req = 0 --\n";
|
||||
var_dump( array_rand($input, 0) ); // with $num_req=0
|
||||
echo"\n-- With num_req = -1 --\n";
|
||||
var_dump( array_rand($input, -1) ); // with $num_req=-1
|
||||
echo"\n-- With num_req = -2 --\n";
|
||||
var_dump( array_rand($input, -2) ); // with $num_req=-2
|
||||
echo"\n-- With num_req more than number of members in 'input' array --\n";
|
||||
var_dump( array_rand($input, 13) ); // with $num_req=13
|
||||
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_rand() : with invalid values for 'req_num' ***
|
||||
|
||||
-- With default num_req value --
|
||||
int(%d)
|
||||
|
||||
-- With num_req = 1 --
|
||||
int(%d)
|
||||
|
||||
-- With num_req = 0 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- With num_req = -1 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- With num_req = -2 --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
|
||||
-- With num_req more than number of members in 'input' array --
|
||||
|
||||
Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
|
||||
NULL
|
||||
Done
|
103
ext/standard/tests/array/array_rand_variation6.phpt
Normal file
103
ext/standard/tests/array/array_rand_variation6.phpt
Normal file
|
@ -0,0 +1,103 @@
|
|||
--TEST--
|
||||
Test array_rand() function : usage variation - with heredoc string as key in the 'input' array
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : mixed array_rand(array $input [, int $num_req])
|
||||
* Description: Return key/keys for random entry/entries in the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test behaviour of array_rand() when keys of the 'input' array is heredoc string
|
||||
*/
|
||||
|
||||
echo "*** Testing array_rand() : with keys of input array as heredoc strings ***\n";
|
||||
|
||||
// defining different heredoc strings
|
||||
$empty_heredoc = <<<EOT
|
||||
EOT;
|
||||
|
||||
$heredoc_with_newline = <<<EOT
|
||||
\n
|
||||
EOT;
|
||||
|
||||
$heredoc_with_characters = <<<EOT
|
||||
first line of heredoc string
|
||||
second line of heredoc string
|
||||
third line of heredocstring
|
||||
EOT;
|
||||
|
||||
$heredoc_with_newline_and_tabs = <<<EOT
|
||||
hello\tworld\nhello\nworld\n
|
||||
EOT;
|
||||
|
||||
$heredoc_with_alphanumerics = <<<EOT
|
||||
hello123world456
|
||||
1234hello\t1234
|
||||
EOT;
|
||||
|
||||
$heredoc_with_embedded_nulls = <<<EOT
|
||||
hello\0world\0hello
|
||||
\0hello\0
|
||||
EOT;
|
||||
|
||||
$input = array(
|
||||
$empty_heredoc => "heredoc1",
|
||||
$heredoc_with_newline => "heredoc2",
|
||||
$heredoc_with_characters => "heredoc3",
|
||||
$heredoc_with_newline_and_tabs => "heredoc3",
|
||||
$heredoc_with_alphanumerics => "heredoc4",
|
||||
$heredoc_with_embedded_nulls => "heredoc5"
|
||||
);
|
||||
|
||||
// Test array_rand() function with different valid 'req_num' values
|
||||
echo "\n-- with default parameters --\n";
|
||||
var_dump( array_rand($input) );
|
||||
|
||||
echo "\n-- with num_req = 1 --\n";
|
||||
var_dump( array_rand($input, 1) );
|
||||
|
||||
echo "\n-- with num_req = 3 --\n";
|
||||
var_dump( array_rand($input, 3) );
|
||||
|
||||
echo "\n-- with num_req = 6 --\n";
|
||||
var_dump( array_rand($input, 6) );
|
||||
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTREGEX--
|
||||
\*\*\* Testing array_rand\(\) : with keys of input array as heredoc strings \*\*\*
|
||||
|
||||
-- with default parameters --
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
|
||||
-- with num_req = 1 --
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
|
||||
-- with num_req = 3 --
|
||||
array\(3\) {
|
||||
\[0\]=>
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
\[1\]=>
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
\[2\]=>
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
}
|
||||
|
||||
-- with num_req = 6 --
|
||||
array\(6\) {
|
||||
\[0\]=>
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
\[1\]=>
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
\[2\]=>
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
\[3\]=>
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
\[4\]=>
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
\[5\]=>
|
||||
string\([0-9]*\) "[a-z \n \t \0 0-9 ]*"
|
||||
}
|
||||
Done
|
Loading…
Add table
Add a link
Reference in a new issue