From: Josie Messa Date: Tue, 19 Feb 2008 17:00:58 +0000 (+0000) Subject: - New tests for each() function X-Git-Tag: php-5.2.6RC1~41 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5c394578de7c50c6f2b1528e280dd50e80dc321a;p=php - New tests for each() function --- diff --git a/ext/standard/tests/array/each_basic.phpt b/ext/standard/tests/array/each_basic.phpt new file mode 100644 index 0000000000..350b40f9a3 --- /dev/null +++ b/ext/standard/tests/array/each_basic.phpt @@ -0,0 +1,74 @@ +--TEST-- +Test each() function : basic functionality +--FILE-- + 1, 'zero', 'two' => 'deux', 20 => 'twenty'); +echo "\n-- Passed array: --\n"; +var_dump($arr); + +echo "\n-- Initial position: --\n"; +var_dump(each($arr)); + +echo "\n-- End position: --\n"; +end($arr); +var_dump(each($arr)); + +echo "\n-- Passed the end of array: --\n"; +var_dump(each($arr)); + +echo "Done"; +?> +--EXPECTF-- +*** Testing each() : basic functionality *** + +-- Passed array: -- +array(4) { + ["one"]=> + int(1) + [0]=> + string(4) "zero" + ["two"]=> + string(4) "deux" + [20]=> + string(6) "twenty" +} + +-- Initial position: -- +array(4) { + [1]=> + int(1) + ["value"]=> + int(1) + [0]=> + string(3) "one" + ["key"]=> + string(3) "one" +} + +-- End position: -- +array(4) { + [1]=> + string(6) "twenty" + ["value"]=> + string(6) "twenty" + [0]=> + int(20) + ["key"]=> + int(20) +} + +-- Passed the end of array: -- +bool(false) +Done \ No newline at end of file diff --git a/ext/standard/tests/array/each_error.phpt b/ext/standard/tests/array/each_error.phpt new file mode 100644 index 0000000000..02c8ef9c06 --- /dev/null +++ b/ext/standard/tests/array/each_error.phpt @@ -0,0 +1,42 @@ +--TEST-- +Test each() function : error conditions - pass incorrect number of args +--FILE-- + + +--EXPECTF-- +*** Testing each() : error conditions *** + +-- Testing each() function with Zero arguments -- + +Warning: Wrong parameter count for each() in %s on line %d +NULL + +-- Testing each() function with more than expected no. of arguments -- + +Warning: Wrong parameter count for each() in %s on line %d +NULL +Done \ No newline at end of file diff --git a/ext/standard/tests/array/each_variation1.phpt b/ext/standard/tests/array/each_variation1.phpt new file mode 100644 index 0000000000..0afef31432 --- /dev/null +++ b/ext/standard/tests/array/each_variation1.phpt @@ -0,0 +1,222 @@ +--TEST-- +Test each() function : usage variations - Pass different data types as $arr arg +--FILE-- + + +--EXPECTF-- +*** Testing each() : usage variations *** + +-- Iteration 1 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 2 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 3 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 4 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 5 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 6 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 7 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 8 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 9 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 10 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 11 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 12 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 13 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 14 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 15 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 16 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 17 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 18 -- +bool(false) + +-- Iteration 19 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 20 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 21 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 22 -- +bool(false) + +-- Iteration 23 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 24 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL + +-- Iteration 25 -- + +Warning: Variable passed to each() is not an array or object in %s on line %d +NULL +Done diff --git a/ext/standard/tests/array/each_variation2.phpt b/ext/standard/tests/array/each_variation2.phpt new file mode 100644 index 0000000000..3f7211c89c --- /dev/null +++ b/ext/standard/tests/array/each_variation2.phpt @@ -0,0 +1,248 @@ +--TEST-- +Test each() function : usage variations - arrays of different data types +--FILE-- + array( + 0, + 1, + 12345, + -2345, + ), + + // float data +/*2*/ 'float' => array( + 10.5, + -10.5, + 12.3456789000e10, + 12.3456789000E-10, + .5, + ), + + // null data +/*3*/ 'null' => array( + NULL, + null, + ), + + // boolean data +/*4*/ 'bool' => array( + true, + false, + TRUE, + FALSE, + ), + + // empty data +/*5*/ 'empty string' => array( + "", + '', + ), + +/*6*/ 'empty array' => array( + ), + + // string data +/*7*/ 'string' => array( + "string", + 'string', + $heredoc, + ), + + // object data +/*8*/ 'object' => array( + new classA(), + ), + + // undefined data +/*9*/ 'undefined' => array( + @$undefined_var, + ), + + // unset data +/*10*/ 'unset' => array( + @$unset_var, + ), + + // resource variable +/*11*/ 'resource' => array( + $fp + ), +); + +// loop through each element of $inputs to check the behavior of each() +$iterator = 1; +foreach($inputs as $key => $input) { + echo "\n-- Iteration $iterator: $key data --\n"; + var_dump( each($input) ); + $iterator++; +}; + +fclose($fp); + +echo "Done"; +?> + +--EXPECTF-- +*** Testing each() : usage variations *** + +-- Iteration 1: int data -- +array(4) { + [1]=> + int(0) + ["value"]=> + int(0) + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- Iteration 2: float data -- +array(4) { + [1]=> + float(10.5) + ["value"]=> + float(10.5) + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- Iteration 3: null data -- +array(4) { + [1]=> + NULL + ["value"]=> + NULL + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- Iteration 4: bool data -- +array(4) { + [1]=> + bool(true) + ["value"]=> + bool(true) + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- Iteration 5: empty string data -- +array(4) { + [1]=> + string(0) "" + ["value"]=> + string(0) "" + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- Iteration 6: empty array data -- +bool(false) + +-- Iteration 7: string data -- +array(4) { + [1]=> + string(6) "string" + ["value"]=> + string(6) "string" + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- Iteration 8: object data -- +array(4) { + [1]=> + object(classA)#%d (0) { + } + ["value"]=> + object(classA)#%d (0) { + } + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- Iteration 9: undefined data -- +array(4) { + [1]=> + NULL + ["value"]=> + NULL + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- Iteration 10: unset data -- +array(4) { + [1]=> + NULL + ["value"]=> + NULL + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- Iteration 11: resource data -- +array(4) { + [1]=> + resource(%d) of type (stream) + ["value"]=> + resource(%d) of type (stream) + [0]=> + int(0) + ["key"]=> + int(0) +} +Done \ No newline at end of file diff --git a/ext/standard/tests/array/each_variation3.phpt b/ext/standard/tests/array/each_variation3.phpt new file mode 100644 index 0000000000..b31ddc61b4 --- /dev/null +++ b/ext/standard/tests/array/each_variation3.phpt @@ -0,0 +1,253 @@ +--TEST-- +Test each() function : usage variations - keys of different data types +--FILE-- + array( + 0 => 'zero', + 1 => 'one', + 12345 => 'positive', + -2345 => 'negative', + ), + + // float data +/*2*/ 'float' => array( + 10.5 => 'positive', + -10.5 => 'negative', + .5 => 'half', + ), + +/*3*/ 'extreme floats' => array( + 12.3456789000e6 => 'large', + 12.3456789000E-10 => 'small', + ), + + // null data +/*4*/ 'null uppercase' => array( + NULL => 'null 1', + ), + +/*5*/ 'null lowercase' => array( + null => 'null 2', + ), + + // boolean data +/*6*/ 'bool lowercase' => array( + true => 'lowert', + false => 'lowerf', + ), + +/*7*/ 'bool uppercase' => array( + TRUE => 'uppert', + FALSE => 'upperf', + ), + + // empty data +/*8*/ 'empty double quotes' => array( + "" => 'emptyd', + ), + +/*9*/ 'empty single quotes' => array( + '' => 'emptys', + ), + + // string data +/*10*/ 'string' => array( + "stringd" => 'stringd', + 'strings' => 'strings', + $heredoc => 'stringh', + ), + + // undefined data +/*11*/ 'undefined' => array( + @$undefined_var => 'undefined', + ), + + // unset data +/*12*/ 'unset' => array( + @$unset_var => 'unset', + ), +); + +// loop through each element of $inputs to check the behavior of each() +$iterator = 1; +foreach($inputs as $key => $input) { + echo "\n-- Iteration $iterator: $key data --\n"; + var_dump( each($input) ); + $iterator++; +}; + +echo "Done"; +?> + +--EXPECTF-- +*** Testing each() : usage variations *** + +-- Iteration 1: int data -- +array(4) { + [1]=> + string(4) "zero" + ["value"]=> + string(4) "zero" + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- Iteration 2: float data -- +array(4) { + [1]=> + string(8) "positive" + ["value"]=> + string(8) "positive" + [0]=> + int(10) + ["key"]=> + int(10) +} + +-- Iteration 3: extreme floats data -- +array(4) { + [1]=> + string(5) "large" + ["value"]=> + string(5) "large" + [0]=> + int(12345678) + ["key"]=> + int(12345678) +} + +-- Iteration 4: null uppercase data -- +array(4) { + [1]=> + string(6) "null 1" + ["value"]=> + string(6) "null 1" + [0]=> + string(0) "" + ["key"]=> + string(0) "" +} + +-- Iteration 5: null lowercase data -- +array(4) { + [1]=> + string(6) "null 2" + ["value"]=> + string(6) "null 2" + [0]=> + string(0) "" + ["key"]=> + string(0) "" +} + +-- Iteration 6: bool lowercase data -- +array(4) { + [1]=> + string(6) "lowert" + ["value"]=> + string(6) "lowert" + [0]=> + int(1) + ["key"]=> + int(1) +} + +-- Iteration 7: bool uppercase data -- +array(4) { + [1]=> + string(6) "uppert" + ["value"]=> + string(6) "uppert" + [0]=> + int(1) + ["key"]=> + int(1) +} + +-- Iteration 8: empty double quotes data -- +array(4) { + [1]=> + string(6) "emptyd" + ["value"]=> + string(6) "emptyd" + [0]=> + string(0) "" + ["key"]=> + string(0) "" +} + +-- Iteration 9: empty single quotes data -- +array(4) { + [1]=> + string(6) "emptys" + ["value"]=> + string(6) "emptys" + [0]=> + string(0) "" + ["key"]=> + string(0) "" +} + +-- Iteration 10: string data -- +array(4) { + [1]=> + string(7) "stringd" + ["value"]=> + string(7) "stringd" + [0]=> + string(7) "stringd" + ["key"]=> + string(7) "stringd" +} + +-- Iteration 11: undefined data -- +array(4) { + [1]=> + string(9) "undefined" + ["value"]=> + string(9) "undefined" + [0]=> + string(0) "" + ["key"]=> + string(0) "" +} + +-- Iteration 12: unset data -- +array(4) { + [1]=> + string(5) "unset" + ["value"]=> + string(5) "unset" + [0]=> + string(0) "" + ["key"]=> + string(0) "" +} +Done \ No newline at end of file diff --git a/ext/standard/tests/array/each_variation4.phpt b/ext/standard/tests/array/each_variation4.phpt new file mode 100644 index 0000000000..16b6096396 --- /dev/null +++ b/ext/standard/tests/array/each_variation4.phpt @@ -0,0 +1,88 @@ +--TEST-- +Test each() function : usage variations - Referenced variables +--FILE-- + &$val1, &$val2); + +echo "-- Call each until at the end of the array: --\n"; +var_dump( each($arr1) ); +var_dump( each($arr1) ); +var_dump( each($arr1) ); + + +echo "\n-- Pass an array by reference to each(): --\n"; +$arr2 = array('zero', 'one', 'two'); + +var_dump( each(&$arr2) ); +echo "-- Check original array: --\n"; +var_dump($arr2); + +echo "Done"; +?> + +--EXPECTF-- +*** Testing each() : usage variations *** + +-- Array made up of referenced variables: -- +-- Call each until at the end of the array: -- +array(4) { + [1]=> + string(3) "foo" + ["value"]=> + string(3) "foo" + [0]=> + string(3) "one" + ["key"]=> + string(3) "one" +} +array(4) { + [1]=> + string(3) "bar" + ["value"]=> + string(3) "bar" + [0]=> + int(0) + ["key"]=> + int(0) +} +bool(false) + +-- Pass an array by reference to each(): -- +array(4) { + [1]=> + string(4) "zero" + ["value"]=> + string(4) "zero" + [0]=> + int(0) + ["key"]=> + int(0) +} +-- Check original array: -- +array(3) { + [0]=> + string(4) "zero" + [1]=> + string(3) "one" + [2]=> + string(3) "two" +} +Done \ No newline at end of file diff --git a/ext/standard/tests/array/each_variation5.phpt b/ext/standard/tests/array/each_variation5.phpt new file mode 100644 index 0000000000..941ad5e3ac --- /dev/null +++ b/ext/standard/tests/array/each_variation5.phpt @@ -0,0 +1,96 @@ +--TEST-- +Test each() function : usage variations - Multi-dimensional arrays +--FILE-- + 'un', + array('a', 'b', 'c') + ); + +echo "\n-- Pass each() a two-dimensional array --\n"; +for ($i = 1; $i < count($arr); $i++) { + var_dump( each($arr) ); +} + +echo "\n-- Pass each() a sub-array --\n"; +var_dump( each($arr[2])); + +echo "Done"; +?> + +--EXPECTF-- +*** Testing each() : usage variations *** + +-- Pass each() a two-dimensional array -- +array(4) { + [1]=> + string(4) "zero" + ["value"]=> + string(4) "zero" + [0]=> + int(0) + ["key"]=> + int(0) +} +array(4) { + [1]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + } + ["value"]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + } + [0]=> + int(1) + ["key"]=> + int(1) +} +array(4) { + [1]=> + string(2) "un" + ["value"]=> + string(2) "un" + [0]=> + string(3) "one" + ["key"]=> + string(3) "one" +} + +-- Pass each() a sub-array -- +array(4) { + [1]=> + string(1) "a" + ["value"]=> + string(1) "a" + [0]=> + int(0) + ["key"]=> + int(0) +} +Done \ No newline at end of file diff --git a/ext/standard/tests/array/each_variation6.phpt b/ext/standard/tests/array/each_variation6.phpt new file mode 100644 index 0000000000..445d63f318 --- /dev/null +++ b/ext/standard/tests/array/each_variation6.phpt @@ -0,0 +1,51 @@ +--TEST-- +Test each() function : usage variations - Internal array pointer +--FILE-- + " . current($arr) . "\n"; + +echo "\n-- Call to each(): --\n"; +var_dump( each($arr) ); + +echo "\n-- New position: --\n"; +echo key($arr) . " => " . current($arr) . "\n"; + +echo "Done"; +?> + +--EXPECTF-- +*** Testing each() : usage variations *** + +-- Current position: -- +0 => zero + +-- Call to each(): -- +array(4) { + [1]=> + string(4) "zero" + ["value"]=> + string(4) "zero" + [0]=> + int(0) + ["key"]=> + int(0) +} + +-- New position: -- +1 => one +Done \ No newline at end of file