From: Raghubansh Kumar Date: Fri, 19 Oct 2007 18:41:13 +0000 (+0000) Subject: New testcases for array_rand() function X-Git-Tag: RELEASE_1_3_1~824 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=36ca7b94e6844069a0479439d7e18d15c607d762;p=php New testcases for array_rand() function --- diff --git a/ext/standard/tests/array/array_rand_basic1.phpt b/ext/standard/tests/array/array_rand_basic1.phpt new file mode 100644 index 0000000000..0a8c2d88fe --- /dev/null +++ b/ext/standard/tests/array/array_rand_basic1.phpt @@ -0,0 +1,52 @@ +--TEST-- +Test array_rand() function : basic functionality - array with default keys +--FILE-- + +--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 diff --git a/ext/standard/tests/array/array_rand_basic2.phpt b/ext/standard/tests/array/array_rand_basic2.phpt new file mode 100644 index 0000000000..154fcb55a4 --- /dev/null +++ b/ext/standard/tests/array/array_rand_basic2.phpt @@ -0,0 +1,57 @@ +--TEST-- +Test array_rand() function : basic functionality - with associative array for 'input' argument +--FILE-- + 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 diff --git a/ext/standard/tests/array/array_rand_error.phpt b/ext/standard/tests/array/array_rand_error.phpt new file mode 100644 index 0000000000..bdbbfdb016 --- /dev/null +++ b/ext/standard/tests/array/array_rand_error.phpt @@ -0,0 +1,37 @@ +--TEST-- +Test array_rand() function : error conditions +--FILE-- + +--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 diff --git a/ext/standard/tests/array/array_rand_variation1.phpt b/ext/standard/tests/array/array_rand_variation1.phpt new file mode 100644 index 0000000000..3b15610760 --- /dev/null +++ b/ext/standard/tests/array/array_rand_variation1.phpt @@ -0,0 +1,216 @@ +--TEST-- +Test array_rand() function : usage variations - unexpected values for 'input' parameter +--FILE-- + +--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 diff --git a/ext/standard/tests/array/array_rand_variation2.phpt b/ext/standard/tests/array/array_rand_variation2.phpt new file mode 100644 index 0000000000..e97a7186fd --- /dev/null +++ b/ext/standard/tests/array/array_rand_variation2.phpt @@ -0,0 +1,214 @@ +--TEST-- +Test array_rand() function : usage variations - unexpected values for 'num_req' parameter +--FILE-- + +--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 diff --git a/ext/standard/tests/array/array_rand_variation3.phpt b/ext/standard/tests/array/array_rand_variation3.phpt new file mode 100644 index 0000000000..62ab6b8dbd --- /dev/null +++ b/ext/standard/tests/array/array_rand_variation3.phpt @@ -0,0 +1,149 @@ +--TEST-- +Test array_rand() function : usage variation - with MultiDimensional array +--FILE-- + +--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 diff --git a/ext/standard/tests/array/array_rand_variation4.phpt b/ext/standard/tests/array/array_rand_variation4.phpt new file mode 100644 index 0000000000..9eb7edcd59 --- /dev/null +++ b/ext/standard/tests/array/array_rand_variation4.phpt @@ -0,0 +1,167 @@ +--TEST-- +Test array_rand() function : usage variation - with associative arrays for 'input' parameter +--FILE-- + '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 diff --git a/ext/standard/tests/array/array_rand_variation5.phpt b/ext/standard/tests/array/array_rand_variation5.phpt new file mode 100644 index 0000000000..e578d81252 --- /dev/null +++ b/ext/standard/tests/array/array_rand_variation5.phpt @@ -0,0 +1,74 @@ +--TEST-- +Test array_rand() function : usage variation - invalid values for 'req_num' parameter +--FILE-- + '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 diff --git a/ext/standard/tests/array/array_rand_variation6.phpt b/ext/standard/tests/array/array_rand_variation6.phpt new file mode 100644 index 0000000000..887a18d4a4 --- /dev/null +++ b/ext/standard/tests/array/array_rand_variation6.phpt @@ -0,0 +1,103 @@ +--TEST-- +Test array_rand() function : usage variation - with heredoc string as key in the 'input' array +--FILE-- + "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