kraghuba Sat Oct 20 05:34:35 2007 UTC Modified files: /php-src/ext/standard/tests/array array_rand_variation2.phpt array_rand_variation3.phpt array_rand_error.phpt array_rand_variation4.phpt array_rand_variation5.phpt array_rand_variation6.phpt array_rand_basic1.phpt array_rand_basic2.phpt array_rand_variation1.phpt Log: New testcases for array_rand() function
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_rand_variation2.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_rand_variation2.phpt diff -u /dev/null php-src/ext/standard/tests/array/array_rand_variation2.phpt:1.2 --- /dev/null Sat Oct 20 05:34:35 2007 +++ php-src/ext/standard/tests/array/array_rand_variation2.phpt Sat Oct 20 05:34:35 2007 @@ -0,0 +1,341 @@ +--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() expects parameter 2 to be long, string given in %s on line %d +NULL + +-- Iteration 17 -- + +Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d +NULL + +-- Iteration 18 -- + +Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d +NULL + +-- Iteration 19 -- + +Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d +NULL + +-- Iteration 20 -- + +Warning: array_rand() expects parameter 2 to be long, object given in %s on line %d +NULL + +-- 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 + +--UEXPECTF-- +*** 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() expects parameter 2 to be long, Unicode string given in %s on line %d +NULL + +-- Iteration 17 -- + +Warning: array_rand() expects parameter 2 to be long, Unicode string given in %s on line %d +NULL + +-- Iteration 18 -- + +Warning: array_rand() expects parameter 2 to be long, Unicode string given in %s on line %d +NULL + +-- Iteration 19 -- + +Warning: array_rand() expects parameter 2 to be long, Unicode string given in %s on line %d +NULL + +-- Iteration 20 -- + +Warning: array_rand() expects parameter 2 to be long, object given in %s on line %d +NULL + +-- 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 http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_rand_variation3.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_rand_variation3.phpt diff -u /dev/null php-src/ext/standard/tests/array/array_rand_variation3.phpt:1.2 --- /dev/null Sat Oct 20 05:34:35 2007 +++ php-src/ext/standard/tests/array/array_rand_variation3.phpt Sat Oct 20 05:34:35 2007 @@ -0,0 +1,240 @@ +--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 + +--UEXPECTF-- +*** 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 http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_rand_error.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_rand_error.phpt diff -u /dev/null php-src/ext/standard/tests/array/array_rand_error.phpt:1.2 --- /dev/null Sat Oct 20 05:34:35 2007 +++ php-src/ext/standard/tests/array/array_rand_error.phpt Sat Oct 20 05:34:35 2007 @@ -0,0 +1,51 @@ +--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: array_rand() expects at least 1 parameter, 0 given in %s on line %d +NULL + +-- Testing array_rand() function with more than expected no. of arguments -- + +Warning: array_rand() expects at most 2 parameters, 3 given in %s on line %d +NULL +Done + +--UEXPECTF-- +*** Testing array_rand() : error conditions *** + +-- Testing array_rand() function with Zero arguments -- + +Warning: array_rand() expects at least 1 parameter, 0 given in %s on line %d +NULL + +-- Testing array_rand() function with more than expected no. of arguments -- + +Warning: array_rand() expects at most 2 parameters, 3 given in %s on line %d +NULL +Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_rand_variation4.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_rand_variation4.phpt diff -u /dev/null php-src/ext/standard/tests/array/array_rand_variation4.phpt:1.2 --- /dev/null Sat Oct 20 05:34:35 2007 +++ php-src/ext/standard/tests/array/array_rand_variation4.phpt Sat Oct 20 05:34:35 2007 @@ -0,0 +1,267 @@ +--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 + +--UEXPECTREGEX-- +\*\*\* 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 +unicode\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*" + +With num_req = 1 +unicode\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*" + +With num_req = 2 +array\(2\) { + \[0\]=> + unicode\([0-9]*\) "[ot1 ]*[hnw2]*[eort]*[ew]*[e]*[l]*[v]*[e]*" + \[1\]=> + unicode\([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 +unicode\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*" + +With num_req = 1 +unicode\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*" + +With num_req = 2 +array\(2\) { + \[0\]=> + unicode\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*" + \[1\]=> + unicode\([0-9]*\) "[#&!N <\n\t'"\0]*[U#$>]*[rL]*[L]*" +} +Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_rand_variation5.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_rand_variation5.phpt diff -u /dev/null php-src/ext/standard/tests/array/array_rand_variation5.phpt:1.2 --- /dev/null Sat Oct 20 05:34:35 2007 +++ php-src/ext/standard/tests/array/array_rand_variation5.phpt Sat Oct 20 05:34:35 2007 @@ -0,0 +1,104 @@ +--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 + +--UEXPECTF-- +*** 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 http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_rand_variation6.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_rand_variation6.phpt diff -u /dev/null php-src/ext/standard/tests/array/array_rand_variation6.phpt:1.2 --- /dev/null Sat Oct 20 05:34:35 2007 +++ php-src/ext/standard/tests/array/array_rand_variation6.phpt Sat Oct 20 05:34:35 2007 @@ -0,0 +1,139 @@ +--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 + +--UEXPECTREGEX-- +\*\*\* Testing array_rand\(\) : with keys of input array as heredoc strings \*\*\* + +-- with default parameters -- +unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" + +-- with num_req = 1 -- +unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" + +-- with num_req = 3 -- +array\(3\) { + \[0\]=> + unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" + \[1\]=> + unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" + \[2\]=> + unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" +} + +-- with num_req = 6 -- +array\(6\) { + \[0\]=> + unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" + \[1\]=> + unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" + \[2\]=> + unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" + \[3\]=> + unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" + \[4\]=> + unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" + \[5\]=> + unicode\([0-9]*\) "[a-z \n \t \0 0-9 ]*" +} +Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_rand_basic1.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_rand_basic1.phpt diff -u /dev/null php-src/ext/standard/tests/array/array_rand_basic1.phpt:1.2 --- /dev/null Sat Oct 20 05:34:35 2007 +++ php-src/ext/standard/tests/array/array_rand_basic1.phpt Sat Oct 20 05:34:35 2007 @@ -0,0 +1,75 @@ +--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 + +--UEXPECTF-- +*** 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 http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_rand_basic2.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_rand_basic2.phpt diff -u /dev/null php-src/ext/standard/tests/array/array_rand_basic2.phpt:1.2 --- /dev/null Sat Oct 20 05:34:35 2007 +++ php-src/ext/standard/tests/array/array_rand_basic2.phpt Sat Oct 20 05:34:35 2007 @@ -0,0 +1,80 @@ +--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 + +--UEXPECTF-- +*** Testing array_rand() : with associative array *** + +-- with all default and optional arguments -- +array(6) { + [0]=> + unicode(%d) "%s" + [1]=> + unicode(%d) "%s" + [2]=> + unicode(%d) "%s" + [3]=> + unicode(%d) "%s" + [4]=> + unicode(%d) "%s" + [5]=> + unicode(%d) "%s" +} + +-- with default argument -- +unicode(%d) "%s" +Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/array/array_rand_variation1.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/array/array_rand_variation1.phpt diff -u /dev/null php-src/ext/standard/tests/array/array_rand_variation1.phpt:1.2 --- /dev/null Sat Oct 20 05:34:35 2007 +++ php-src/ext/standard/tests/array/array_rand_variation1.phpt Sat Oct 20 05:34:35 2007 @@ -0,0 +1,335 @@ +--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() expects parameter 1 to be array, integer given in %s on line %d +NULL + +-- Iteration 2 -- + +Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d +NULL + +-- Iteration 3 -- + +Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d +NULL + +-- Iteration 4 -- + +Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d +NULL + +-- Iteration 5 -- + +Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d +NULL + +-- Iteration 6 -- + +Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d +NULL + +-- Iteration 7 -- + +Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d +NULL + +-- Iteration 8 -- + +Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d +NULL + +-- Iteration 9 -- + +Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d +NULL + +-- Iteration 10 -- + +Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d +NULL + +-- Iteration 11 -- + +Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d +NULL + +-- Iteration 12 -- + +Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d +NULL + +-- Iteration 13 -- + +Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d +NULL + +-- Iteration 14 -- + +Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d +NULL + +-- Iteration 15 -- + +Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d +NULL + +-- Iteration 16 -- + +Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d +NULL + +-- Iteration 17 -- + +Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d +NULL + +-- Iteration 18 -- + +Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d +NULL + +-- Iteration 19 -- + +Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d +NULL + +-- Iteration 20 -- + +Warning: array_rand() expects parameter 1 to be array, object given in %s on line %d +NULL + +-- Iteration 21 -- + +Warning: array_rand() expects parameter 1 to be array, resource given in %s on line %d +NULL + +-- Iteration 22 -- + +Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d +NULL + +-- Iteration 23 -- + +Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d +NULL +Done + +--UEXPECTF-- +*** Testing array_rand() : unexpected values for 'input' parameter *** + +-- Iteration 1 -- + +Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d +NULL + +-- Iteration 2 -- + +Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d +NULL + +-- Iteration 3 -- + +Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d +NULL + +-- Iteration 4 -- + +Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d +NULL + +-- Iteration 5 -- + +Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d +NULL + +-- Iteration 6 -- + +Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d +NULL + +-- Iteration 7 -- + +Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d +NULL + +-- Iteration 8 -- + +Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d +NULL + +-- Iteration 9 -- + +Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d +NULL + +-- Iteration 10 -- + +Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d +NULL + +-- Iteration 11 -- + +Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d +NULL + +-- Iteration 12 -- + +Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d +NULL + +-- Iteration 13 -- + +Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d +NULL + +-- Iteration 14 -- + +Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d +NULL + +-- Iteration 15 -- + +Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d +NULL + +-- Iteration 16 -- + +Warning: array_rand() expects parameter 1 to be array, Unicode string given in %s on line %d +NULL + +-- Iteration 17 -- + +Warning: array_rand() expects parameter 1 to be array, Unicode string given in %s on line %d +NULL + +-- Iteration 18 -- + +Warning: array_rand() expects parameter 1 to be array, Unicode string given in %s on line %d +NULL + +-- Iteration 19 -- + +Warning: array_rand() expects parameter 1 to be array, Unicode string given in %s on line %d +NULL + +-- Iteration 20 -- + +Warning: array_rand() expects parameter 1 to be array, object given in %s on line %d +NULL + +-- Iteration 21 -- + +Warning: array_rand() expects parameter 1 to be array, resource given in %s on line %d +NULL + +-- Iteration 22 -- + +Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d +NULL + +-- Iteration 23 -- + +Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d +NULL +Done
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php