--- /dev/null
+--TEST--
+Test each() function : basic functionality
+--FILE--
+<?php
+/* Prototype : array each(array $arr)
+ * Description: Return the currently pointed key..value pair in the passed array,
+ * and advance the pointer to the next element
+ * Source code: Zend/zend_builtin_functions.c
+ */
+
+/*
+ * Test basic functionality of each()
+ */
+
+echo "*** Testing each() : basic functionality ***\n";
+
+$arr = array ('one' => 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
--- /dev/null
+--TEST--
+Test each() function : error conditions - pass incorrect number of args
+--FILE--
+<?php
+/* Prototype : array each(array $arr)
+ * Description: Return the currently pointed key..value pair in the passed array,
+ * and advance the pointer to the next element
+ * Source code: Zend/zend_builtin_functions.c
+ */
+
+/*
+ * Pass an incorrect number of arguments to each() to test behaviour
+ */
+
+echo "*** Testing each() : error conditions ***\n";
+
+// Zero arguments
+echo "\n-- Testing each() function with Zero arguments --\n";
+var_dump( each() );
+
+//Test each with one more than the expected number of arguments
+echo "\n-- Testing each() function with more than expected no. of arguments --\n";
+$arr = array(1, 2);
+$extra_arg = 10;
+var_dump( each($arr, $extra_arg) );
+
+echo "Done";
+?>
+
+--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
--- /dev/null
+--TEST--
+Test each() function : usage variations - Pass different data types as $arr arg
+--FILE--
+<?php
+/* Prototype : array each(array $arr)
+ * Description: Return the currently pointed key..value pair in the passed array,
+ * and advance the pointer to the next element
+ * Source code: Zend/zend_builtin_functions.c
+ */
+
+/*
+ * Pass different data types as $arr arg to each() to test behaviour
+ */
+
+echo "*** Testing each() : usage variations ***\n";
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+// get a class
+class classA
+{
+ public function __toString() {
+ return "Class A object";
+ }
+}
+
+// heredoc string
+$heredoc = <<<EOT
+hello world
+EOT;
+
+// get a resource variable
+$fp = fopen(__FILE__, "r");
+
+// unexpected values to be passed to $arr argument
+$inputs = 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*/ "",
+ '',
+ array(),
+
+ // string data
+/*19*/ "string",
+ 'string',
+ $heredoc,
+
+ // object data
+/*22*/ new classA(),
+
+ // undefined data
+/*23*/ @$undefined_var,
+
+ // unset data
+/*24*/ @$unset_var,
+
+ // resource variable
+/*25*/ $fp
+);
+
+// loop through each element of $inputs to check the behavior of each()
+$iterator = 1;
+foreach($inputs as $input) {
+ echo "\n-- Iteration $iterator --\n";
+ var_dump( each($input) );
+ $iterator++;
+};
+
+fclose($fp);
+
+echo "Done";
+?>
+
+--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
--- /dev/null
+--TEST--
+Test each() function : usage variations - arrays of different data types
+--FILE--
+<?php
+/* Prototype : array each(array $arr)
+ * Description: Return the currently pointed key..value pair in the passed array,
+ * and advance the pointer to the next element
+ * Source code: Zend/zend_builtin_functions.c
+ */
+
+/*
+ * Pass arrays of different data types as $arr argument to each() to test behaviour
+ */
+
+echo "*** Testing each() : usage variations ***\n";
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+// get a class
+class classA
+{
+ public function __toString() {
+ return "Class A object";
+ }
+}
+
+// heredoc string
+$heredoc = <<<EOT
+hello world
+EOT;
+
+// get a resource variable
+$fp = fopen(__FILE__, "r");
+
+// arrays of different data types to be passed as $arr
+$inputs = array(
+
+ // int data
+/*1*/ 'int' => 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
--- /dev/null
+--TEST--
+Test each() function : usage variations - keys of different data types
+--FILE--
+<?php
+/* Prototype : array each(array $arr)
+ * Description: Return the currently pointed key..value pair in the passed array,
+ * and advance the pointer to the next element
+ * Source code: Zend/zend_builtin_functions.c
+ */
+
+/*
+ * Pass each() arrays where the keys are different data types to test behaviour
+ */
+
+echo "*** Testing each() : usage variations ***\n";
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+// heredoc string
+$heredoc = <<<EOT
+hello world
+EOT;
+
+// unexpected values to be passed as $arr
+$inputs = array(
+
+ // int data
+/*1*/ 'int' => 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
--- /dev/null
+--TEST--
+Test each() function : usage variations - Referenced variables
+--FILE--
+<?php
+/* Prototype : array each(array $arr)
+ * Description: Return the currently pointed key..value pair in the passed array,
+ * and advance the pointer to the next element
+ * Source code: Zend/zend_builtin_functions.c
+ */
+
+/*
+ * Test behaviour of each() when:
+ * 1. Passed an array made up of referenced variables
+ * 2. Passed an array as $arr argument by reference
+ */
+
+echo "*** Testing each() : usage variations ***\n";
+
+echo "\n-- Array made up of referenced variables: --\n";
+$val1 = 'foo';
+$val2 = 'bar';
+
+$arr1 = array('one' => &$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
--- /dev/null
+--TEST--
+Test each() function : usage variations - Multi-dimensional arrays
+--FILE--
+<?php
+/* Prototype : array each(array $arr)
+ * Description: Return the currently pointed key..value pair in the passed array,
+ * and advance the pointer to the next element
+ * Source code: Zend/zend_builtin_functions.c
+ */
+
+/*
+ * Test behaviour of each() when passed:
+ * 1. a two-dimensional array
+ * 2. a sub-array
+ */
+
+echo "*** Testing each() : usage variations ***\n";
+
+$arr = array ('zero',
+ array(1, 2, 3),
+ 'one' => '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
--- /dev/null
+--TEST--
+Test each() function : usage variations - Internal array pointer
+--FILE--
+<?php
+/* Prototype : array each(array $arr)
+ * Description: Return the currently pointed key..value pair in the passed array,
+ * and advance the pointer to the next element
+ * Source code: Zend/zend_builtin_functions.c
+ */
+
+/*
+ * Test the position of the internal array pointer after a call to each()
+ */
+
+echo "*** Testing each() : usage variations ***\n";
+
+$arr = array('zero', 'one', 'two', 'abc', 'xyz');
+
+echo "\n-- Current position: --\n";
+echo key($arr) . " => " . 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