diff options
author | Raghubansh Kumar <kraghuba@php.net> | 2007-05-12 09:53:30 +0000 |
---|---|---|
committer | Raghubansh Kumar <kraghuba@php.net> | 2007-05-12 09:53:30 +0000 |
commit | 42ae6b508eb9e0d6f1b8ab7c429202833579ce3d (patch) | |
tree | 067393e8fdb90667ba9e4109bce97a71dab67163 | |
parent | 02dd014013466b9f964349ea8474146b41c29eb2 (diff) | |
download | php-git-42ae6b508eb9e0d6f1b8ab7c429202833579ce3d.tar.gz |
New version of 005.phpt, array_change_key_case.phpt, array_key_exists.phpt, array_keys.phpt, array_map.phpt, array_pop.phpt, array_search.phpt, array_values.phpt, each.phpt, end.phpt, extract.phpt
-rw-r--r-- | ext/standard/tests/array/005.phpt | 309 | ||||
-rw-r--r-- | ext/standard/tests/array/array_change_key_case.phpt | 202 | ||||
-rw-r--r-- | ext/standard/tests/array/array_key_exists.phpt | 280 | ||||
-rw-r--r-- | ext/standard/tests/array/array_keys.phpt | 462 | ||||
-rw-r--r-- | ext/standard/tests/array/array_map.phpt | 423 | ||||
-rw-r--r-- | ext/standard/tests/array/array_pop.phpt | 286 | ||||
-rw-r--r-- | ext/standard/tests/array/array_search.phpt | 868 | ||||
-rw-r--r-- | ext/standard/tests/array/array_values.phpt | bin | 713 -> 5137 bytes | |||
-rw-r--r-- | ext/standard/tests/array/each.phpt | bin | 0 -> 9548 bytes | |||
-rw-r--r-- | ext/standard/tests/array/end.phpt | 232 | ||||
-rw-r--r-- | ext/standard/tests/array/extract.phpt | 276 |
11 files changed, 3251 insertions, 87 deletions
diff --git a/ext/standard/tests/array/005.phpt b/ext/standard/tests/array/005.phpt index cdf731827d..626c961665 100644 --- a/ext/standard/tests/array/005.phpt +++ b/ext/standard/tests/array/005.phpt @@ -1,46 +1,291 @@ --TEST-- -Test array_shift behaviour +Test array_shift() function --FILE-- <?php +/* Prototype: mixed array_shift( array &array ); + * Description: Shifts the first value of the array off and returns it. + */ array_shift($GLOBALS); -$a = array("foo", "bar", "fubar"); -$b = array("3" => "foo", "4" => "bar", "5" => "fubar"); -$c = array("a" => "foo", "b" => "bar", "c" => "fubar"); +$empty_array = array(); +$number = 5; +$str = "abc"; -/* simple array */ -echo array_shift($a), "\n"; -var_dump($a); -/* numerical assoc indices */ -echo array_shift($b), "\n"; -var_dump($b); +/* Various combinations of arrays to be used for the test */ +$mixed_array = array( + array(), + array( 1,2,3,4,5,6,7,8,9 ), + array( "One", "_Two", "Three", "Four", "Five" ), + array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ), + array( "a" => "aaa", "A" => "AAA", "c" => "ccc", "d" => "ddd", "e" => "eee" ), + array( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five" ), + array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ), + array( "f" => "fff", "1" => "one", 4 => 6, "" => "blank", 2.4 => "float", "F" => "FFF", + "blank" => "", 3.7 => 3.7, 5.4 => 7, 6 => 8.6, '5' => "Five", "4name" => "jonny", "a" => NULL, NULL => 3 ), + array( 12, "name", 'age', '45' ), + array( array("oNe", "tWo", 4), array(10, 20, 30, 40, 50), array() ), + array( "one" => 1, "one" => 2, "three" => 3, 3, 4, 3 => 33, 4 => 44, 5, 6, + 5.4 => 54, 5.7 => 57, "5.4" => 554, "5.7" => 557 ) +); -/* assoc indices */ -echo array_shift($c), "\n"; -var_dump($c); +/* Testing Error Conditions */ +echo "\n*** Testing Error Conditions ***\n"; +/* Zero argument */ +var_dump( array_shift() ); + +/* Scalar argument */ +var_dump( array_shift($number) ); + +/* String argument */ +var_dump( array_shift($str) ); + +/* Invalid Number of arguments */ +var_dump( array_shift($mixed_array[1],$mixed_array[2]) ); + +/* Empty Array as argument */ +var_dump( array_shift($empty_array) ); + +/* Loop to test normal functionality with different arrays inputs */ +echo "\n*** Testing with various array inputs ***\n"; + +$counter = 1; +foreach( $mixed_array as $sub_array ) { + echo "\n-- Input Array for Iteration $counter is -- \n"; + print_r( $sub_array ); + echo "\nOutput after shift is :\n"; + var_dump( array_shift($sub_array) ); + $counter++; +} + +/*Checking for internal array pointer beint reset when shift is called */ + +echo"\n*** Checking for internal array pointer being reset when shift is called ***\n"; + +echo "\nCurrent Element is : "; +var_dump( current($mixed_array[1]) ); + +echo "\nNext Element is : "; +var_dump( next($mixed_array[1]) ); + +echo "\nNext Element is : "; +var_dump( next($mixed_array[1]) ); + +echo "\nshifted Element is : "; +var_dump( array_shift($mixed_array[1]) ); + +echo "\nCurrent Element after shift operation is: "; +var_dump( current($mixed_array[1]) ); + +echo"Done"; ?> ---EXPECT-- -foo -array(2) { - [0]=> - string(3) "bar" - [1]=> - string(5) "fubar" -} -foo -array(2) { +--EXPECTF-- +*** Testing Error Conditions *** + +Warning: Wrong parameter count for array_shift() in %s line %d +NULL + +Warning: array_shift(): The argument should be an array in %s on line %d +NULL + +Warning: array_shift(): The argument should be an array in %s on line %d +NULL + +Warning: Wrong parameter count for array_shift() in %s on line %d +NULL +NULL + +*** Testing with various array inputs *** + +-- Input Array for Iteration 1 is -- +Array +( +) + +Output after shift is : +NULL + +-- Input Array for Iteration 2 is -- +Array +( + [0] => 1 + [1] => 2 + [2] => 3 + [3] => 4 + [4] => 5 + [5] => 6 + [6] => 7 + [7] => 8 + [8] => 9 +) + +Output after shift is : +int(1) + +-- Input Array for Iteration 3 is -- +Array +( + [0] => One + [1] => _Two + [2] => Three + [3] => Four + [4] => Five +) + +Output after shift is : +string(3) "One" + +-- Input Array for Iteration 4 is -- +Array +( + [0] => 6 + [1] => six + [2] => 7 + [3] => seven + [4] => 8 + [5] => eight + [6] => 9 + [7] => nine +) + +Output after shift is : +int(6) + +-- Input Array for Iteration 5 is -- +Array +( + [a] => aaa + [A] => AAA + [c] => ccc + [d] => ddd + [e] => eee +) + +Output after shift is : +string(3) "aaa" + +-- Input Array for Iteration 6 is -- +Array +( + [1] => one + [2] => two + [3] => three + [4] => four + [5] => five +) + +Output after shift is : +string(3) "one" + +-- Input Array for Iteration 7 is -- +Array +( + [1] => one + [2] => two + [3] => 7 + [4] => four + [5] => five +) + +Output after shift is : +string(3) "one" + +-- Input Array for Iteration 8 is -- +Array +( + [f] => fff + [1] => one + [4] => 6 + [] => 3 + [2] => float + [F] => FFF + [blank] => + [3] => 3.7 + [5] => Five + [6] => 8.6 + [4name] => jonny + [a] => +) + +Output after shift is : +string(3) "fff" + +-- Input Array for Iteration 9 is -- +Array +( + [0] => 12 + [1] => name + [2] => age + [3] => 45 +) + +Output after shift is : +int(12) + +-- Input Array for Iteration 10 is -- +Array +( + [0] => Array + ( + [0] => oNe + [1] => tWo + [2] => 4 + ) + + [1] => Array + ( + [0] => 10 + [1] => 20 + [2] => 30 + [3] => 40 + [4] => 50 + ) + + [2] => Array + ( + ) + +) + +Output after shift is : +array(3) { [0]=> - string(3) "bar" + string(3) "oNe" [1]=> - string(5) "fubar" -} -foo -array(2) { - ["b"]=> - string(3) "bar" - ["c"]=> - string(5) "fubar" + string(3) "tWo" + [2]=> + int(4) } + +-- Input Array for Iteration 11 is -- +Array +( + [one] => 2 + [three] => 3 + [0] => 3 + [1] => 4 + [3] => 33 + [4] => 44 + [5] => 57 + [6] => 6 + [5.4] => 554 + [5.7] => 557 +) + +Output after shift is : +int(2) + +*** Checking for internal array pointer being reset when shift is called *** + +Current Element is : int(1) + +Next Element is : int(2) + +Next Element is : int(3) + +shifted Element is : int(1) + +Current Element after shift operation is: int(2) +Done diff --git a/ext/standard/tests/array/array_change_key_case.phpt b/ext/standard/tests/array/array_change_key_case.phpt index 0cd1d8203a..8e785a84d9 100644 --- a/ext/standard/tests/array/array_change_key_case.phpt +++ b/ext/standard/tests/array/array_change_key_case.phpt @@ -1,56 +1,92 @@ --TEST-- -array_change_key_case() +Test array_change_key_case() function --FILE-- <?php +/* Prototype: array array_change_key_case ( array $input [, int $case] ) + Description: Changes the keys in the input array to be all lowercase + or uppercase. The change depends on the last optional case parameter. + You can pass two constants there, CASE_UPPER and CASE_LOWER(default). + The function will leave number indices as is. +*/ $arrays = array ( - array (), - array (0), - array (1), - array (-1), - array (0, 2, 3, 4, 5), - array (1, 2, 3, 4, 5), - array ("" => 1), - array ("a" => 1), - array ("Z" => 1), - array ("one" => 1), - array ("ONE" => 1), - array ("OnE" => 1), - array ("oNe" => 1), - array ("one" => 1, "two" => 2), - array ("ONE" => 1, "two" => 2), - array ("OnE" => 1, "two" => 2), - array ("oNe" => 1, "two" => 2), - array ("one" => 1, "TWO" => 2), - array ("ONE" => 1, "TWO" => 2), - array ("OnE" => 1, "TWO" => 2), - array ("oNe" => 1, "TWO" => 2), - array ("one" => 1, "TwO" => 2), - array ("ONE" => 1, "TwO" => 2), - array ("OnE" => 1, "TwO" => 2), - array ("oNe" => 1, "TwO" => 2), - array ("one" => 1, "tWo" => 2), - array ("ONE" => 1, "tWo" => 2), - array ("OnE" => 1, "tWo" => 2), - array ("oNe" => 1, "tWo" => 2), - array ("one" => 1, 2), - array ("ONE" => 1, 2), - array ("OnE" => 1, 2), - array ("oNe" => 1, 2), - array ("ONE" => 1, "TWO" => 2, "THREE" => 3, "FOUR" => "four"), - array ("one" => 1, "two" => 2, "three" => 3, "four" => "FOUR"), - array ("ONE" => 1, "TWO" => 2, "three" => 3, "four" => "FOUR"), - array ("one" => 1, "two" => 2, "THREE" => 3, "FOUR" => "four") + array (), + array (0), + array (1), + array (-1), + array (0, 2, 3, 4, 5), + array (1, 2, 3, 4, 5), + array ("" => 1), + array ("a" => 1), + array ("Z" => 1), + array ("one" => 1), + array ("ONE" => 1), + array ("OnE" => 1), + array ("oNe" => 1), + array ("one" => 1, "two" => 2), + array ("ONE" => 1, "two" => 2), + array ("OnE" => 1, "two" => 2), + array ("oNe" => 1, "two" => 2), + array ("one" => 1, "TWO" => 2), + array ("ONE" => 1, "TWO" => 2), + array ("OnE" => 1, "TWO" => 2), + array ("oNe" => 1, "TWO" => 2), + array ("one" => 1, "TwO" => 2), + array ("ONE" => 1, "TwO" => 2), + array ("OnE" => 1, "TwO" => 2), + array ("oNe" => 1, "TwO" => 2), + array ("one" => 1, "tWo" => 2), + array ("ONE" => 1, "tWo" => 2), + array ("OnE" => 1, "tWo" => 2), + array ("oNe" => 1, "tWo" => 2), + array ("one" => 1, 2), + array ("ONE" => 1, 2), + array ("OnE" => 1, 2), + array ("oNe" => 1, 2), + array ("ONE" => 1, "TWO" => 2, "THREE" => 3, "FOUR" => "four"), + array ("one" => 1, "two" => 2, "three" => 3, "four" => "FOUR"), + array ("ONE" => 1, "TWO" => 2, "three" => 3, "four" => "FOUR"), + array ("one" => 1, "two" => 2, "THREE" => 3, "FOUR" => "four") ); +echo "*** Testing basic operations ***\n"; +$loop_counter = 1; foreach ($arrays as $item) { + echo "** Iteration $loop_counter **\n"; $loop_counter++; var_dump(array_change_key_case($item)); var_dump(array_change_key_case($item, CASE_UPPER)); var_dump(array_change_key_case($item, CASE_LOWER)); echo "\n"; } + +echo "\n*** Testing possible variations ***\n"; +$int_var = -19; +$item = array ("one" => 1, "two" => 2, "THREE" => 3, "FOUR" => "four"); + +/* use 'case' argument other than CASE_LOWER & CASE_UPPER */ +var_dump(array_change_key_case($item, "CASE_UPPER")); +var_dump(array_change_key_case($item, 5)); + +/* when keys are different in terms of only case */ +/* should return one value key pair with key being in lowercase */ +var_dump( array_change_key_case( array("ONE" => 1, "one" => 3, "One" => 4) ) ); +var_dump( array_change_key_case( array("ONE" => 1, "one" => 6, "One" => 5), "CASE_UPPER" ) ); + +/* should return one value key pair with key being in uppercase */ +var_dump( array_change_key_case( array("ONE" => 1, "one" => 2, "One" => 3), CASE_UPPER ) ); +var_dump( array_change_key_case( array("ONE" => 1, "one" => 1, "One" => 2), 5 ) ); + +echo "\n*** Testing error conditions ***\n"; +/* generate different failure conditions */ +var_dump( array_change_key_case($int_var) ); // args less than expected +var_dump( array_change_key_case($int_var, CASE_UPPER) ); // invalid first argument +var_dump( array_change_key_case() ); // Zero argument +var_dump( array_change_key_case($item, $item["one"], "CASE_UPPER") ); // more than expected numbers + echo "end\n"; ?> ---EXPECT-- +--EXPECTF-- +*** Testing basic operations *** +** Iteration 1 ** array(0) { } array(0) { @@ -58,6 +94,7 @@ array(0) { array(0) { } +** Iteration 2 ** array(1) { [0]=> int(0) @@ -71,6 +108,7 @@ array(1) { int(0) } +** Iteration 3 ** array(1) { [0]=> int(1) @@ -84,6 +122,7 @@ array(1) { int(1) } +** Iteration 4 ** array(1) { [0]=> int(-1) @@ -97,6 +136,7 @@ array(1) { int(-1) } +** Iteration 5 ** array(5) { [0]=> int(0) @@ -134,6 +174,7 @@ array(5) { int(5) } +** Iteration 6 ** array(5) { [0]=> int(1) @@ -171,6 +212,7 @@ array(5) { int(5) } +** Iteration 7 ** array(1) { [""]=> int(1) @@ -184,6 +226,7 @@ array(1) { int(1) } +** Iteration 8 ** array(1) { ["a"]=> int(1) @@ -197,6 +240,7 @@ array(1) { int(1) } +** Iteration 9 ** array(1) { ["z"]=> int(1) @@ -210,6 +254,7 @@ array(1) { int(1) } +** Iteration 10 ** array(1) { ["one"]=> int(1) @@ -223,6 +268,7 @@ array(1) { int(1) } +** Iteration 11 ** array(1) { ["one"]=> int(1) @@ -236,6 +282,7 @@ array(1) { int(1) } +** Iteration 12 ** array(1) { ["one"]=> int(1) @@ -249,6 +296,7 @@ array(1) { int(1) } +** Iteration 13 ** array(1) { ["one"]=> int(1) @@ -262,6 +310,7 @@ array(1) { int(1) } +** Iteration 14 ** array(2) { ["one"]=> int(1) @@ -281,6 +330,7 @@ array(2) { int(2) } +** Iteration 15 ** array(2) { ["one"]=> int(1) @@ -300,6 +350,7 @@ array(2) { int(2) } +** Iteration 16 ** array(2) { ["one"]=> int(1) @@ -319,6 +370,7 @@ array(2) { int(2) } +** Iteration 17 ** array(2) { ["one"]=> int(1) @@ -338,6 +390,7 @@ array(2) { int(2) } +** Iteration 18 ** array(2) { ["one"]=> int(1) @@ -357,6 +410,7 @@ array(2) { int(2) } +** Iteration 19 ** array(2) { ["one"]=> int(1) @@ -376,6 +430,7 @@ array(2) { int(2) } +** Iteration 20 ** array(2) { ["one"]=> int(1) @@ -395,6 +450,7 @@ array(2) { int(2) } +** Iteration 21 ** array(2) { ["one"]=> int(1) @@ -414,6 +470,7 @@ array(2) { int(2) } +** Iteration 22 ** array(2) { ["one"]=> int(1) @@ -433,6 +490,7 @@ array(2) { int(2) } +** Iteration 23 ** array(2) { ["one"]=> int(1) @@ -452,6 +510,7 @@ array(2) { int(2) } +** Iteration 24 ** array(2) { ["one"]=> int(1) @@ -471,6 +530,7 @@ array(2) { int(2) } +** Iteration 25 ** array(2) { ["one"]=> int(1) @@ -490,6 +550,7 @@ array(2) { int(2) } +** Iteration 26 ** array(2) { ["one"]=> int(1) @@ -509,6 +570,7 @@ array(2) { int(2) } +** Iteration 27 ** array(2) { ["one"]=> int(1) @@ -528,6 +590,7 @@ array(2) { int(2) } +** Iteration 28 ** array(2) { ["one"]=> int(1) @@ -547,6 +610,7 @@ array(2) { int(2) } +** Iteration 29 ** array(2) { ["one"]=> int(1) @@ -566,6 +630,7 @@ array(2) { int(2) } +** Iteration 30 ** array(2) { ["one"]=> int(1) @@ -585,6 +650,7 @@ array(2) { int(2) } +** Iteration 31 ** array(2) { ["one"]=> int(1) @@ -604,6 +670,7 @@ array(2) { int(2) } +** Iteration 32 ** array(2) { ["one"]=> int(1) @@ -623,6 +690,7 @@ array(2) { int(2) } +** Iteration 33 ** array(2) { ["one"]=> int(1) @@ -642,6 +710,7 @@ array(2) { int(2) } +** Iteration 34 ** array(4) { ["one"]=> int(1) @@ -673,6 +742,7 @@ array(4) { string(4) "four" } +** Iteration 35 ** array(4) { ["one"]=> int(1) @@ -704,6 +774,7 @@ array(4) { string(4) "FOUR" } +** Iteration 36 ** array(4) { ["one"]=> int(1) @@ -735,6 +806,7 @@ array(4) { string(4) "FOUR" } +** Iteration 37 ** array(4) { ["one"]=> int(1) @@ -766,4 +838,56 @@ array(4) { string(4) "four" } + +*** Testing possible variations *** +array(4) { + ["one"]=> + int(1) + ["two"]=> + int(2) + ["three"]=> + int(3) + ["four"]=> + string(4) "four" +} +array(4) { + ["ONE"]=> + int(1) + ["TWO"]=> + int(2) + ["THREE"]=> + int(3) + ["FOUR"]=> + string(4) "four" +} +array(1) { + ["one"]=> + int(4) +} +array(1) { + ["one"]=> + int(5) +} +array(1) { + ["ONE"]=> + int(3) +} +array(1) { + ["ONE"]=> + int(2) +} + +*** Testing error conditions *** + +Warning: array_change_key_case(): The argument should be an array in %s on line %d +bool(false) + +Warning: array_change_key_case(): The argument should be an array in %s on line %d +bool(false) + +Warning: Wrong parameter count for array_change_key_case() in %s on line %d +NULL + +Warning: Wrong parameter count for array_change_key_case() in %s on line %d +NULL end diff --git a/ext/standard/tests/array/array_key_exists.phpt b/ext/standard/tests/array/array_key_exists.phpt index e7b6c06773..0d0b663010 100644 --- a/ext/standard/tests/array/array_key_exists.phpt +++ b/ext/standard/tests/array/array_key_exists.phpt @@ -1,24 +1,254 @@ --TEST-- -array_key_exists() tests +Test array_key_exists() function --FILE-- <?php +/* Prototype: + * bool array_key_exists ( mixed $key, array $search ); + * Description: + * Returns TRUE if the given key is set in the array. + * key can be any value possible for an array index. + * Also also works on objects. + */ -var_dump(array_key_exists()); -var_dump(array_key_exists(array(), array())); -var_dump(array_key_exists("", "")); -var_dump(array_key_exists("", array())); -var_dump(array_key_exists(1, array())); +echo "*** Testing basic functionalities ***\n"; +/* Arrays with regular values */ +$search_arrays = array( + array(1,2,3,4), + array('a','b','c'), + array('abc', 'bcd', 'dcf'), + array("test", "rest", "enjoy"), + array("Name" => "Jack", "Loc" => "Mars", "Id" => "MS123"), + array('Red' => 'Rose', 'I' => 'You'), + array(0 => 'Zero', 1 => 'One', 2 => 'Two', 3 => "Three" ), + array(0.1 => 'Zero', 1.1 => 'One', 2.2 => 'Two', 3.3 => "Three" ) + ); +/* keys to search in $search_arrays. $keys[0] + is the key to be searched in $search_arrays[0] and so on */ +$keys = array( 1, 'a', 2, 4, "Name", "Red", 0, 3 ); -var_dump(array_key_exists(1, array(1,2,3))); -var_dump(array_key_exists("a", array(3,2,1,"a"=>1))); -var_dump(array_key_exists("a", array(3,2,1))); -var_dump(array_key_exists(NULL, array(5,6,7,""=>"value", 3,2,1))); -var_dump(array_key_exists(NULL, array(5,6,7,3,2,1))); -var_dump(array_key_exists(false, array(5,6,7,""=>"value", 3,2,1))); +$key_counter = 0; +foreach ($search_arrays as $search_array) { + $key = $keys[ $key_counter++ ]; + echo "-- Iteration $key_counter --\n"; + var_dump( array_key_exists($key, $search_array) ); +} +echo "\n*** Testing possible variations ***\n"; +// use different keys on each sub array of $search_arrays +$key_variations = array ("", NULL, null, " ", '', "test", 1); +$key_counter = 0; +$key_count = count ( $key_variations ); +echo "\n** Variation loop 1 **\n"; +$out_loop_count = 0; +foreach ($search_arrays as $search_array) { + $key_counter = 0; + $out_loop_count ++; echo "-- Iteration $out_loop_count --\n"; + while ( $key_counter < $key_count ) { + $key = $key_variations[ $key_counter++ ]; + var_dump( array_key_exists($key, $search_array) ); + } +} +// arrays with variation in elements +$search_arrays_v = array ( + array(), + array(NULL), + array(array(), 1, 2), + array(1,2,3, "" => "value", NULL => "value", true => "value" ), + array( array(2,4,5), array ("a","b","d") ) + ); +// search for $key_variations in each sub array of $search_arrays_v +echo "\n** Variation loop 2 **\n"; +$out_loop_count = 0; +foreach ($search_arrays_v as $search_array) { + $key_counter = 0; + $out_loop_count ++; echo "-- Iteration $out_loop_count --\n"; + while ( $key_counter < $key_count ) { + $key = $key_variations[ $key_counter++ ]; + var_dump( array_key_exists($key, $search_array) ); + } +} + +echo "\n*** Testing error conditions ***\n"; +//Zeor args +var_dump( array_key_exists() ); +// first args as array +var_dump( array_key_exists(array(), array()) ); +// second args as string +var_dump( array_key_exists("", "") ); +// second args a integer +var_dump( array_key_exists(1, 1) ); +// second args as NULL +var_dump( array_key_exists(1, NULL) ); +// second args as boolean +var_dump( array_key_exists(1, true) ); +// first args as boolean +var_dump( array_key_exists(false, true) ); +// second args as float +var_dump( array_key_exists(false, 17.5) ); +// args more than expected +var_dump( array_key_exists(1, array(), array()) ); +// first argument as floating point value +var_dump( array_key_exists(17.5, array(1,23) ) ) ; + +echo "\n*** Testing operation on objects ***\n"; +class key_check +{ + private $private_var = "Priviate var"; + protected $protected_var = "Protected var"; + public $public_var = "Public var"; + public $arr = array("var" => "value", "1" => "one", ""=>"value"); + public function print_member() + { + echo $this->$private_var."\n"; + echo $this->$protected_var."\n"; + echo $this->$public_var."\n"; + } +} + +$key_check_obj = new key_check; //new object +/* array_key_exists() on an object, it should work on only public member variables */ +var_dump(array_key_exists("private_var", $key_check_obj)); // not found, private member +var_dump(array_key_exists("protected_var", $key_check_obj)); // not found, private member +var_dump(array_key_exists("public_var", $key_check_obj)); // found, public member +var_dump(array_key_exists("print_member", $key_check_obj)); // not found, its a function +var_dump(array_key_exists("arr", $key_check_obj)); //found, public member +var_dump(array_key_exists("var", $key_check_obj->arr)); //found, key is in member array + +/* error condition, first arguemnt as object */ +var_dump( array_key_exists($key_check_obj, $key_check_obj) ); echo "Done\n"; ?> --EXPECTF-- +*** Testing basic functionalities *** +-- Iteration 1 -- +bool(true) +-- Iteration 2 -- +bool(false) +-- Iteration 3 -- +bool(true) +-- Iteration 4 -- +bool(false) +-- Iteration 5 -- +bool(true) +-- Iteration 6 -- +bool(true) +-- Iteration 7 -- +bool(true) +-- Iteration 8 -- +bool(true) + +*** Testing possible variations *** + +** Variation loop 1 ** +-- Iteration 1 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(true) +-- Iteration 2 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(true) +-- Iteration 3 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(true) +-- Iteration 4 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(true) +-- Iteration 5 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +-- Iteration 6 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +-- Iteration 7 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(true) +-- Iteration 8 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(true) + +** Variation loop 2 ** +-- Iteration 1 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +-- Iteration 2 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +-- Iteration 3 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(true) +-- Iteration 4 -- +bool(true) +bool(true) +bool(true) +bool(false) +bool(true) +bool(false) +bool(true) +-- Iteration 5 -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(true) + +*** Testing error conditions *** + Warning: Wrong parameter count for array_key_exists() in %s on line %d NULL @@ -27,13 +257,35 @@ bool(false) Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d bool(false) + +Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d bool(false) + +Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d +bool(false) + +Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d +bool(false) + +Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d +bool(false) + +Warning: array_key_exists(): The second argument should be either an array or an object in %s on line %d +bool(false) + +Warning: Wrong parameter count for array_key_exists() in %s on line %d +NULL + +Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d +bool(false) + +*** Testing operation on objects *** bool(false) -bool(true) -bool(true) bool(false) bool(true) bool(false) +bool(true) +bool(true) Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d bool(false) diff --git a/ext/standard/tests/array/array_keys.phpt b/ext/standard/tests/array/array_keys.phpt new file mode 100644 index 0000000000..0d1e05b20c --- /dev/null +++ b/ext/standard/tests/array/array_keys.phpt @@ -0,0 +1,462 @@ +--TEST-- +Test array_keys() function +--FILE-- +<?php +/* +Prototype: array array_keys( array $input [, mixed $search_value [, + bool $strict]]); +Description: Return all the keys of an array +*/ + +echo "*** Testing array_keys() on basic array operation ***\n"; +$basic_arr = array("a" => 1, "b" => 2, 2.0 => 2.0, -23.45 => "asdasd", + array(1,2,3)); +var_dump(array_keys($basic_arr)); + +echo "\n*** Testing array_keys() on various arrays ***"; +$arrays = array( + array(), + array(0), + array( array() ), + array("Hello" => "World"), + array("" => ""), + array(1,2,3, "d" => array(4,6, "d")), + array("a" => 1, "b" => 2, "c" =>3, "d" => array()), + array(0 => 0, 1 => 1, 2 => 2, 3 => 3), + array(0.001=>3.000, 1.002=>2, 1.999=>3, "a"=>3, 3=>5, "5"=>3.000), + array(TRUE => TRUE, FALSE => FALSE, NULL => NULL, "\x000", "\000"), + array("a" => "abcd", "a" => "", "ab" => -6, "cd" => -0.5 ), + array(0 => array(), 1=> array(0), 2 => array(1), ""=> array(),""=>"" ) +); + +$i = 0; +/* loop through to test array_keys() with different arrays */ +foreach ($arrays as $array) { + echo "\n-- Iteration $i --\n"; + var_dump(array_keys($array)); + $i++; +} + +echo "\n*** Testing array_keys() on all the types other than arrays ***"; +$types_arr = array( + TRUE => TRUE, + FALSE => FALSE, + 1 => 1, + 0 => 0, + -1 => -1, + "1" => "1", + "0" => "0", + "-1" => "-1", + NULL, + array(), + "php" => "php", + "" => "" +); +$values = array(TRUE, FALSE, 1, 0, -1, "1", "0", "-1", NULL, array(), "php", ""); +foreach ($values as $value){ + echo "\n-- Loose type checking --\n"; + var_dump(array_keys($types_arr, $value)); + echo "\n-- strict type checking --\n"; + var_dump(array_keys($types_arr, $value, TRUE)); +} + +echo "\n*** Testing array_keys() with resource type ***\n"; +$resource1 = fopen( __FILE__, "r"); +$resource2 = opendir( "." ); + +/* creating an array with resource types as elements */ +$arr_resource = array($resource1, $resource2); + +var_dump(array_keys($arr_resource, $resource1)); // loose type checking +var_dump(array_keys($arr_resource, $resource1, TRUE)); // strict type checking +var_dump(array_keys($arr_resource, $resource2)); // loose type checking +var_dump(array_keys($arr_resource, $resource2, TRUE)); // strict type checking + +echo "\n*** Testing array_keys() on range of values ***\n"; +$arr_range = array( + 2147483647 => 1, + 2147483648 => 2, + -2147483647 => 3, + -2147483648 => 4, + -2147483649 => 5, + -0 => 6, + 0 => 7 +); +var_dump(array_keys($arr_range)); + +echo "\n*** Testing array_keys() on an array created on the fly ***\n"; +var_dump(array_keys(array("a" => 1, "b" => 2, "c" => 3))); +var_dump(array_keys(array())); // null array + +echo "\n*** Testing error conditions ***"; +var_dump(array_keys(100)); +var_dump(array_keys("string")); +var_dump(array_keys(new stdclass)); // object +var_dump(array_keys()); // Zero arguments +var_dump(array_keys(array(), "", TRUE, 100)); // args > expected +var_dump(array_keys(array(1,2,3, array() => array()))); // (W)illegal offset + +echo "Done\n"; + +--CLEAN-- +/* Closing the resource handles */ +fclose( $resource1 ); +closedir( $resource2 ); +?> +--EXPECTF-- +*** Testing array_keys() on basic array operation *** +array(5) { + [0]=> + string(1) "a" + [1]=> + string(1) "b" + [2]=> + int(2) + [3]=> + int(-23) + [4]=> + int(3) +} + +*** Testing array_keys() on various arrays *** +-- Iteration 0 -- +array(0) { +} + +-- Iteration 1 -- +array(1) { + [0]=> + int(0) +} + +-- Iteration 2 -- +array(1) { + [0]=> + int(0) +} + +-- Iteration 3 -- +array(1) { + [0]=> + string(5) "Hello" +} + +-- Iteration 4 -- +array(1) { + [0]=> + string(0) "" +} + +-- Iteration 5 -- +array(4) { + [0]=> + int(0) + [1]=> + int(1) + [2]=> + int(2) + [3]=> + string(1) "d" +} + +-- Iteration 6 -- +array(4) { + [0]=> + string(1) "a" + [1]=> + string(1) "b" + [2]=> + string(1) "c" + [3]=> + string(1) "d" +} + +-- Iteration 7 -- +array(4) { + [0]=> + int(0) + [1]=> + int(1) + [2]=> + int(2) + [3]=> + int(3) +} + +-- Iteration 8 -- +array(5) { + [0]=> + int(0) + [1]=> + int(1) + [2]=> + string(1) "a" + [3]=> + int(3) + [4]=> + int(5) +} + +-- Iteration 9 -- +array(5) { + [0]=> + int(1) + [1]=> + int(0) + [2]=> + string(0) "" + [3]=> + int(2) + [4]=> + int(3) +} + +-- Iteration 10 -- +array(3) { + [0]=> + string(1) "a" + [1]=> + string(2) "ab" + [2]=> + string(2) "cd" +} + +-- Iteration 11 -- +array(4) { + [0]=> + int(0) + [1]=> + int(1) + [2]=> + int(2) + [3]=> + string(0) "" +} + +*** Testing array_keys() on all the types other than arrays *** +-- Loose type checking -- +array(3) { + [0]=> + int(1) + [1]=> + int(-1) + [2]=> + string(3) "php" +} + +-- strict type checking -- +array(0) { +} + +-- Loose type checking -- +array(4) { + [0]=> + int(0) + [1]=> + int(2) + [2]=> + int(3) + [3]=> + string(0) "" +} + +-- strict type checking -- +array(0) { +} + +-- Loose type checking -- +array(1) { + [0]=> + int(1) +} + +-- strict type checking -- +array(0) { +} + +-- Loose type checking -- +array(4) { + [0]=> + int(0) + [1]=> + int(2) + [2]=> + string(3) "php" + [3]=> + string(0) "" +} + +-- strict type checking -- +array(0) { +} + +-- Loose type checking -- +array(1) { + [0]=> + int(-1) +} + +-- strict type checking -- +array(0) { +} + +-- Loose type checking -- +array(1) { + [0]=> + int(1) +} + +-- strict type checking -- +array(1) { + [0]=> + int(1) +} + +-- Loose type checking -- +array(1) { + [0]=> + int(0) +} + +-- strict type checking -- +array(1) { + [0]=> + int(0) +} + +-- Loose type checking -- +array(1) { + [0]=> + int(-1) +} + +-- strict type checking -- +array(1) { + [0]=> + int(-1) +} + +-- Loose type checking -- +array(3) { + [0]=> + int(2) + [1]=> + int(3) + [2]=> + string(0) "" +} + +-- strict type checking -- +array(1) { + [0]=> + int(2) +} + +-- Loose type checking -- +array(2) { + [0]=> + int(2) + [1]=> + int(3) +} + +-- strict type checking -- +array(1) { + [0]=> + int(3) +} + +-- Loose type checking -- +array(1) { + [0]=> + string(3) "php" +} + +-- strict type checking -- +array(1) { + [0]=> + string(3) "php" +} + +-- Loose type checking -- +array(2) { + [0]=> + int(2) + [1]=> + string(0) "" +} + +-- strict type checking -- +array(1) { + [0]=> + string(0) "" +} + +*** Testing array_keys() with resource type *** +array(1) { + [0]=> + int(0) +} +array(1) { + [0]=> + int(0) +} +array(1) { + [0]=> + int(1) +} +array(1) { + [0]=> + int(1) +} + +*** Testing array_keys() on range of values *** +array(4) { + [0]=> + int(2147483647) + [1]=> + int(-2147483648) + [2]=> + int(-2147483647) + [3]=> + int(0) +} + +*** Testing array_keys() on an array created on the fly *** +array(3) { + [0]=> + string(1) "a" + [1]=> + string(1) "b" + [2]=> + string(1) "c" +} +array(0) { +} + +*** Testing error conditions *** +Warning: array_keys(): The first argument should be an array in %s on line %d +NULL + +Warning: array_keys(): The first argument should be an array in %s on line %d +NULL + +Warning: array_keys(): The first argument should be an array in %s on line %d +NULL + +Warning: Wrong parameter count for array_keys() in %s on line %d +NULL + +Warning: Wrong parameter count for array_keys() in %s on line %d +NULL + +Warning: Illegal offset type in %s on line %d +array(3) { + [0]=> + int(0) + [1]=> + int(1) + [2]=> + int(2) +} +Done diff --git a/ext/standard/tests/array/array_map.phpt b/ext/standard/tests/array/array_map.phpt new file mode 100644 index 0000000000..78815e0833 --- /dev/null +++ b/ext/standard/tests/array/array_map.phpt @@ -0,0 +1,423 @@ +--TEST-- +Test array_map() function +--FILE-- +<?php +/* Prototype: array array_map ( callback $callback, array $arr1 [, array $...] ); + Description: array_map() returns an array containing all the elements of arr1 + after applying the callback function to each one. The number of + parameters that the callback function accepts should match the + number of arrays passed to the array_map() +*/ + +echo "*** Testing basic operations ***\n"; +/* array_map with null as function and different arrays */ +var_dump( array_map(NULL, array()) ); +var_dump( array_map(NULL, array(), array()) ); +var_dump( array_map(NULL, array(1,2,3), array(1,2,3)) ); +var_dump( array_map(NULL, array(1,2), array(1,2,3,4)) ); +var_dump( array_map(NULL, array("Jan", "Feb", "March"), array("31","28","31")) ); +var_dump( array_map(NULL, array("Text", "Words", "Lineup"), array(4, 5, 6)) ); +var_dump( array_map(NULL, array("a", "ab", "abc", "abcd"), array()) ); +var_dump( array_map(NULL, + array("Jan"=>"01", "Feb"=>"02", "March"=>"03"), + array("31"=>"Jan", "28"=>"Feb", "031"=>"March") + ) + ); + +/* using key as "string" where no.of arguments passed to array_map() is 2 */ +var_dump( array_map( create_function('$n', 'return $n*$n;'), + array("key1"=>1, "key2"=>2, "key3"=>3) + ) + ); + +echo "\n*** Testing possible variations ***\n"; +/* anonymous callback function */ +var_dump( array_map( create_function('$a,$b', 'return $a+$b;'), + array(1,2,3), + array(5,6,7,8,9) + ) + ); + +/* anonymous callback function with reference */ +var_dump( array_map( create_function('&$a, $b', 'return array($a,$b);'), + array("Hello","Good"), + array("World","Day") + ) + ); + +/* callback function with reference */ +$a = array(1,2,3); +function square(&$var) { + return( $var * $var ); +} +print_r( array_map('square', $a) ); + +/* array_map in recursion */ +function square_recur($var) { + if (is_array($var)) + return array_map('square_recur', $var); + return $var * $var; +} +$rec_array = array(1, 2, array(3, 4, array(5, 2), array() ) ); +var_dump( array_map('square_recur', $rec_array) ); + +/* callback function as string variable containing the function name */ +$string_var = "square"; +var_dump( array_map("square", $a) ); +var_dump( array_map($string_var, $a) ); + +echo "\n*** Testing error conditions ***\n"; +/* arguments of non array type */ +$int_var=10; +$float_var = 10.5; +var_dump( array_map('square', $int_var) ); +var_dump( array_map('square', $float_var) ); +var_dump( array_map('square', $string_var) ); + +/* Zero argument */ +var_dump( array_map() ); + +/* use array(), echo(), empty(), eval(), exit(), isset(), list(), print() + and unset() as callback, failure expected */ +var_dump( array_map( 'echo', array(1) ) ); +var_dump( array_map( 'array', array(1) ) ); +var_dump( array_map( 'empty', array(1) ) ); +var_dump( array_map( 'eval', array(1) ) ); +var_dump( array_map( 'exit', array(1) ) ); +var_dump( array_map( 'isset', array(1) ) ); +var_dump( array_map( 'list', array(1) ) ); +var_dump( array_map( 'print', array(1) ) ); + + +echo "\n*** Testing operation on objects ***\n"; +/* array_map with class object */ +class check_array_map { + public static function helloWorld() { + return "Static_Function_helloWorld(): Hello World"; + } + public function Message($v) { + return $v; + } + + public static function Square( $n ) { + return $n * $n; + } +} +/* call static member function */ +var_dump( array_map( array('check_array_map', 'Square'), array(1,2,3)) ); + +/* call non static member function - notice should be issues*/ +var_dump( array_map( array('check_array_map', 'Message'), array(1)) ); + +/* call function using object */ +$obj = new check_array_map(); +var_dump( array_map( array($obj, 'helloWorld' ) ) ); // not enough args warning +var_dump( array_map( array($obj, 'helloWorld'), array(1) ) ); + +echo "Done\n"; +?> +--EXPECTF-- +*** Testing basic operations *** +array(0) { +} +array(0) { +} +array(3) { + [0]=> + array(2) { + [0]=> + int(1) + [1]=> + int(1) + } + [1]=> + array(2) { + [0]=> + int(2) + [1]=> + int(2) + } + [2]=> + array(2) { + [0]=> + int(3) + [1]=> + int(3) + } +} +array(4) { + [0]=> + array(2) { + [0]=> + int(1) + [1]=> + int(1) + } + [1]=> + array(2) { + [0]=> + int(2) + [1]=> + int(2) + } + [2]=> + array(2) { + [0]=> + NULL + [1]=> + int(3) + } + [3]=> + array(2) { + [0]=> + NULL + [1]=> + int(4) + } +} +array(3) { + [0]=> + array(2) { + [0]=> + string(3) "Jan" + [1]=> + string(2) "31" + } + [1]=> + array(2) { + [0]=> + string(3) "Feb" + [1]=> + string(2) "28" + } + [2]=> + array(2) { + [0]=> + string(5) "March" + [1]=> + string(2) "31" + } +} +array(3) { + [0]=> + array(2) { + [0]=> + string(4) "Text" + [1]=> + int(4) + } + [1]=> + array(2) { + [0]=> + string(5) "Words" + [1]=> + int(5) + } + [2]=> + array(2) { + [0]=> + string(6) "Lineup" + [1]=> + int(6) + } +} +array(4) { + [0]=> + array(2) { + [0]=> + string(1) "a" + [1]=> + NULL + } + [1]=> + array(2) { + [0]=> + string(2) "ab" + [1]=> + NULL + } + [2]=> + array(2) { + [0]=> + string(3) "abc" + [1]=> + NULL + } + [3]=> + array(2) { + [0]=> + string(4) "abcd" + [1]=> + NULL + } +} +array(3) { + [0]=> + array(2) { + [0]=> + string(2) "01" + [1]=> + string(3) "Jan" + } + [1]=> + array(2) { + [0]=> + string(2) "02" + [1]=> + string(3) "Feb" + } + [2]=> + array(2) { + [0]=> + string(2) "03" + [1]=> + string(5) "March" + } +} +array(3) { + ["key1"]=> + int(1) + ["key2"]=> + int(4) + ["key3"]=> + int(9) +} + +*** Testing possible variations *** +array(5) { + [0]=> + int(6) + [1]=> + int(8) + [2]=> + int(10) + [3]=> + int(8) + [4]=> + int(9) +} +array(2) { + [0]=> + array(2) { + [0]=> + string(5) "Hello" + [1]=> + string(5) "World" + } + [1]=> + array(2) { + [0]=> + string(4) "Good" + [1]=> + string(3) "Day" + } +} +Array +( + [0] => 1 + [1] => 4 + [2] => 9 +) +array(3) { + [0]=> + int(1) + [1]=> + int(4) + [2]=> + array(4) { + [0]=> + int(9) + [1]=> + int(16) + [2]=> + array(2) { + [0]=> + int(25) + [1]=> + int(4) + } + [3]=> + array(0) { + } + } +} +array(3) { + [0]=> + int(1) + [1]=> + int(4) + [2]=> + int(9) +} +array(3) { + [0]=> + int(1) + [1]=> + int(4) + [2]=> + int(9) +} + +*** Testing error conditions *** + +Warning: array_map(): Argument #2 should be an array in %s on line %d +NULL + +Warning: array_map(): Argument #2 should be an array in %s on line %d +NULL + +Warning: array_map(): Argument #2 should be an array in %s on line %d +NULL + +Warning: Wrong parameter count for array_map() %s on line %d +NULL + +Warning: array_map(): The first argument, 'echo', should be either NULL or a valid callback in %s on line %d +NULL + +Warning: array_map(): The first argument, 'array', should be either NULL or a valid callback in %s on line %d +NULL + +Warning: array_map(): The first argument, 'empty', should be either NULL or a valid callback in %s on line %d +NULL + +Warning: array_map(): The first argument, 'eval', should be either NULL or a valid callback in %s on line %d +NULL + +Warning: array_map(): The first argument, 'exit', should be either NULL or a valid callback in %s on line %d +NULL + +Warning: array_map(): The first argument, 'isset', should be either NULL or a valid callback in %s on line %d +NULL + +Warning: array_map(): The first argument, 'list', should be either NULL or a valid callback in %s on line %d +NULL + +Warning: array_map(): The first argument, 'print', should be either NULL or a valid callback in %s on line %d +NULL + +*** Testing operation on objects *** +array(3) { + [0]=> + int(1) + [1]=> + int(4) + [2]=> + int(9) +} + +Strict Standards: Non-static method check_array_map::Message() cannot be called statically in %s on line %d + +Strict Standards: Non-static method check_array_map::Message() cannot be called statically in %s on line %d +array(1) { + [0]=> + int(1) +} + +Warning: Wrong parameter count for array_map() in %s on line %d +NULL +array(1) { + [0]=> + string(41) "Static_Function_helloWorld(): Hello World" +} +Done diff --git a/ext/standard/tests/array/array_pop.phpt b/ext/standard/tests/array/array_pop.phpt new file mode 100644 index 0000000000..544be22118 --- /dev/null +++ b/ext/standard/tests/array/array_pop.phpt @@ -0,0 +1,286 @@ +--TEST-- +Test array_pop() function +--FILE-- +<?php + +/* Prototype: mixed array_pop( array &array ); + * Description: Pops and returns the last value of the array. + */ + +array_pop($GLOBALS); + +$empty_array = array(); +$number = 5; +$str = "abc"; + + +/* Various combinations of arrays to be used for the test */ +$mixed_array = array( + array(), + array( 1,2,3,4,5,6,7,8,9 ), + array( "One", "_Two", "Three", "Four", "Five" ), + array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ), + array( "a" => "aaa", "A" => "AAA", "c" => "ccc", "d" => "ddd", "e" => "eee" ), + array( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five" ), + array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ), + array( "f" => "fff", "1" => "one", 4 => 6, "" => "blank", 2.4 => "float", "F" => "FFF", + "blank" => "", 3.7 => 3.7, 5.4 => 7, 6 => 8.6, '5' => "Five", "4name" => "jonny", "a" => NULL, NULL => 3 ), + array( 12, "name", 'age', '45' ), + array( array("oNe", "tWo", 4), array(10, 20, 30, 40, 50), array() ), + array( "one" => 1, "one" => 2, "three" => 3, 3, 4, 3 => 33, 4 => 44, 5, 6, + 5.4 => 54, 5.7 => 57, "5.4" => 554, "5.7" => 557 ) +); + +/* Testing Error Conditions */ +echo "\n*** Testing Error Conditions ***\n"; + +/* Zero argument */ +var_dump( array_pop() ); + +/* Scalar argument */ +var_dump( array_pop($number) ); + +/* String argument */ +var_dump( array_pop($str) ); + +/* Invalid Number of arguments */ +var_dump( array_pop($mixed_array[1],$mixed_array[2]) ); + +/* Empty Array as argument */ +var_dump( array_pop($empty_array) ); + +/* Loop to test normal functionality with different arrays inputs */ +echo "\n*** Normal testing with various array inputs ***\n"; + +$counter = 1; +foreach( $mixed_array as $sub_array ) +{ + echo "\n-- Input Array for Iteration $counter is --\n"; + print_r( $sub_array ); + echo "\nOutput after Pop is :\n"; + var_dump( array_pop($sub_array) ); + $counter++; +} + +echo"\n*** Checking for internal array pointer being reset when pop is called ***\n"; + +echo "\nCurrent Element is : "; +var_dump( current($mixed_array[1]) ); + +echo "\nNext Element is : "; +var_dump( next($mixed_array[1]) ); + +echo "\nNext Element is : "; +var_dump( next($mixed_array[1]) ); + +echo "\nPOPed Element is : "; +var_dump( array_pop($mixed_array[1]) ); + +echo "\nCurrent Element after POP operation is: "; +var_dump( current($mixed_array[1]) ); + +echo"\nDone"; +?> +--EXPECTF-- +*** Testing Error Conditions *** + +Warning: Wrong parameter count for array_pop() in %s on line %d +NULL + +Warning: array_pop(): The argument should be an array in %s on line %d +NULL + +Warning: array_pop(): The argument should be an array in %s on line %d +NULL + +Warning: Wrong parameter count for array_pop() in %s on line %d +NULL +NULL + +*** Normal testing with various array inputs *** + +-- Input Array for Iteration 1 is -- +Array +( +) + +Output after Pop is : +NULL + +-- Input Array for Iteration 2 is -- +Array +( + [0] => 1 + [1] => 2 + [2] => 3 + [3] => 4 + [4] => 5 + [5] => 6 + [6] => 7 + [7] => 8 + [8] => 9 +) + +Output after Pop is : +int(9) + +-- Input Array for Iteration 3 is -- +Array +( + [0] => One + [1] => _Two + [2] => Three + [3] => Four + [4] => Five +) + +Output after Pop is : +string(4) "Five" + +-- Input Array for Iteration 4 is -- +Array +( + [0] => 6 + [1] => six + [2] => 7 + [3] => seven + [4] => 8 + [5] => eight + [6] => 9 + [7] => nine +) + +Output after Pop is : +string(4) "nine" + +-- Input Array for Iteration 5 is -- +Array +( + [a] => aaa + [A] => AAA + [c] => ccc + [d] => ddd + [e] => eee +) + +Output after Pop is : +string(3) "eee" + +-- Input Array for Iteration 6 is -- +Array +( + [1] => one + [2] => two + [3] => three + [4] => four + [5] => five +) + +Output after Pop is : +string(4) "five" + +-- Input Array for Iteration 7 is -- +Array +( + [1] => one + [2] => two + [3] => 7 + [4] => four + [5] => five +) + +Output after Pop is : +string(4) "five" + +-- Input Array for Iteration 8 is -- +Array +( + [f] => fff + [1] => one + [4] => 6 + [] => 3 + [2] => float + [F] => FFF + [blank] => + [3] => 3.7 + [5] => Five + [6] => 8.6 + [4name] => jonny + [a] => +) + +Output after Pop is : +NULL + +-- Input Array for Iteration 9 is -- +Array +( + [0] => 12 + [1] => name + [2] => age + [3] => 45 +) + +Output after Pop is : +string(2) "45" + +-- Input Array for Iteration 10 is -- +Array +( + [0] => Array + ( + [0] => oNe + [1] => tWo + [2] => 4 + ) + + [1] => Array + ( + [0] => 10 + [1] => 20 + [2] => 30 + [3] => 40 + [4] => 50 + ) + + [2] => Array + ( + ) + +) + +Output after Pop is : +array(0) { +} + +-- Input Array for Iteration 11 is -- +Array +( + [one] => 2 + [three] => 3 + [0] => 3 + [1] => 4 + [3] => 33 + [4] => 44 + [5] => 57 + [6] => 6 + [5.4] => 554 + [5.7] => 557 +) + +Output after Pop is : +int(557) + +*** Checking for internal array pointer being reset when pop is called *** + +Current Element is : int(1) + +Next Element is : int(2) + +Next Element is : int(3) + +POPed Element is : int(9) + +Current Element after POP operation is: int(1) + +Done diff --git a/ext/standard/tests/array/array_search.phpt b/ext/standard/tests/array/array_search.phpt index e95fda1286..f6712eee9c 100644 --- a/ext/standard/tests/array/array_search.phpt +++ b/ext/standard/tests/array/array_search.phpt @@ -1,7 +1,13 @@ --TEST-- -search_array and in_array (including bug 13567) +Test array_search() and in_array() functions (including bug 13567) --FILE-- <?php +/* Prototype: bool in_array ( mixed $needle, array $haystack [, bool $strict] ); + Description: Checks if a value exists in an array + Searches haystack for needle and returns TRUE if found in array, FALSE otherwise. + If the third parameter strict is set to TRUE then the in_array() function + will also check the types of the needle in the haystack. +*/ $arr1 = array('a','b','c'); $arr2 = array(); @@ -9,6 +15,7 @@ $arr3 = array('c','key'=>'d'); $arr4 = array("a\0b"=>'e','key'=>'d', 'f'); $tests = <<<TESTS +FALSE === in_array(123, \$arr2) FALSE === in_array(123, \$arr1) FALSE === array_search(123, \$arr1) TRUE === in_array('a', \$arr1) @@ -19,5 +26,862 @@ TESTS; include(dirname(__FILE__) . '/../../../../tests/quicktester.inc'); ---EXPECT-- +/* checking for STRICT option in in_array() */ +echo "\n*** Testing STRICT option of in_array() on arrays ***\n"; +$arrays = array ( + array(0), + array("a" => "A", 2 => "B", "C" => 3, 4 => 4, "one" => 1, "" => NULL, "b", "ab", "abcd"), + array(4, array(1, 2 => 3), "one" => 1, "5" => 5 ), + array(-1, -2, -3, -4, -2.989888, "-0.005" => "neg0.005", 2.0 => "float2", "-.9" => -.9), + array(TRUE, FALSE), + array("", array()), + array("abcd\x00abcd\x00abcd"), + array("abcd\tabcd\nabcd\rabcd\0abcdefghij") +); + +$array_compare = array ( + 4, + "4", + 4.00, + "b", + "5", + -2, + -2.0, + -2.98989, + "-.9", + "True", + "", + array(), + NULL, + "ab", + "abcd", + 0.0, + -0, + "abcd\x00abcd\x00abcd" +); +/* loop to check if elements in $array_compare exist in $arrays + using in_array() */ +$counter = 1; +foreach($arrays as $array) { + foreach($array_compare as $compare) { + echo "-- Iteration $counter --\n"; + //strict option OFF + var_dump(in_array($compare,$array)); + //strict option ON + var_dump(in_array($compare,$array,TRUE)); + //strict option OFF + var_dump(in_array($compare,$array,FALSE)); + $counter++; + } +} + +/* checking loose and strict TYPE comparisons in in_array() */ +echo "\n*** Testing loose and strict TYPE comparison of in_array() ***\n"; +$misc_array = array ( + 'a', + 'key' =>'d', + 3, + ".001" =>-67, + "-.051" =>"k", + 0.091 =>"-.08", + "e" =>"5", + "y" =>NULL, + NULL =>"", + 0, + TRUE, + FALSE, + -27.39999999999, + " ", + "abcd\x00abcd\x00\abcd\x00abcdefghij", + "abcd\nabcd\tabcd\rabcd\0abcd" +); +$array_type = array(TRUE, FALSE, 1, 0, -1, "1", "0", "-1", NULL, array(), "PHP", ""); +/* loop to do loose and strict type check of elements in + $array_type on elements in $misc_array using in_array(); + checking PHP type comparison tables +*/ +$counter = 1; +foreach($array_type as $type) { + echo "-- Iteration $counter --\n"; + //loose type checking + var_dump( in_array($type,$misc_array ) ); + //strict type checking + var_dump( in_array($type,$misc_array,true) ); + //loose type checking + var_dump( in_array($type,$misc_array,false) ); + $counter++; +} + +/* checking for sub-arrays with in_array() */ +echo "\n*** Testing sub-arrays with in_array() ***\n"; +$sub_array = array ( + "one", + array(1, 2 => "two", "three" => 3), + 4 => "four", + "five" => 5, + array('', 'i') +); +var_dump( in_array("four", $sub_array) ); +//checking for element in a sub-array +var_dump( in_array(3, $sub_array[1]) ); +var_dump( in_array(array('','i'), $sub_array) ); + +/* checking for objects in in_array() */ +echo "\n*** Testing objects with in_array() ***\n"; +class in_array_check { + public $array_var = array(1=>"one", "two"=>2, 3=>3); + public function foo() { + echo "Public function\n"; + } +} + +$in_array_obj = new in_array_check(); //creating new object +//error: as wrong datatype for second argument +var_dump( in_array("array_var", $in_array_obj) ); +//error: as wrong datatype for second argument +var_dump( in_array("foo", $in_array_obj) ); +//element found as "one" exists in array $array_var +var_dump( in_array("one", $in_array_obj->array_var) ); + +/* checking for Resources */ +echo "\n*** Testing resource type with in_array() ***\n"; +//file type resource +$file_handle = fopen(__FILE__, "r"); + +//directory type resource +$dir_handle = opendir( dirname(__FILE__) ); + +//store resources in array for comparision. +$resources = array($file_handle, $dir_handle); + +// search for resouce type in the resource array +var_dump( in_array($file_handle, $resources, true) ); +//checking for (int) type resource +var_dump( in_array((int)$dir_handle, $resources, true) ); + +/* Miscellenous input check */ +echo "\n*** Testing miscelleneos inputs with in_array() ***\n"; +//matching "Good" in array(0,"hello"), result:true in loose type check +var_dump( in_array("Good", array(0,"hello")) ); +//false in strict mode +var_dump( in_array("Good", array(0,"hello"), TRUE) ); + +//matching integer 0 in array("this"), result:true in loose type check +var_dump( in_array(0, array("this")) ); +// false in strict mode +var_dump( in_array(0, array("this")),TRUE ); + +//matching string "this" in array(0), result:true in loose type check +var_dump( in_array("this", array(0)) ); +// false in stric mode +var_dump( in_array("this", array(0), TRUE) ); + +//checking for type FALSE in multidimensional array with loose checking, result:false in loose type check +var_dump( in_array(FALSE, + array("a"=> TRUE, "b"=> TRUE, + array("c"=> TRUE, "d"=>TRUE) + ) + ) + ); + +//matching string having integer in beginning, result:true in loose type check +var_dump( in_array('123abc', array(123)) ); +var_dump( in_array('123abc', array(123), TRUE) ); // false in strict mode + +echo "\n*** Testing error conditions of in_array() ***\n"; +/* zero argument */ +var_dump( in_array() ); + +/* unexpected no.of arguments in in_array() */ +$var = array("mon", "tues", "wed", "thurs"); +var_dump( in_array(1, $var, 0, "test") ); +var_dump( in_array("test") ); + +/* unexpected second argument in in_array() */ +$var="test"; +var_dump( in_array("test", $var) ); +var_dump( in_array(1, 123) ); + +/* closing resource handles */ +fclose($file_handle); +closedir($dir_handle); + +echo "Done\n"; +?> +--EXPECTF-- OK +*** Testing STRICT option of in_array() on arrays *** +-- Iteration 1 -- +bool(false) +bool(false) +bool(false) +-- Iteration 2 -- +bool(false) +bool(false) +bool(false) +-- Iteration 3 -- +bool(false) +bool(false) +bool(false) +-- Iteration 4 -- +bool(true) +bool(false) +bool(true) +-- Iteration 5 -- +bool(false) +bool(false) +bool(false) +-- Iteration 6 -- +bool(false) +bool(false) +bool(false) +-- Iteration 7 -- +bool(false) +bool(false) +bool(false) +-- Iteration 8 -- +bool(false) +bool(false) +bool(false) +-- Iteration 9 -- +bool(false) +bool(false) +bool(false) +-- Iteration 10 -- +bool(true) +bool(false) +bool(true) +-- Iteration 11 -- +bool(true) +bool(false) +bool(true) +-- Iteration 12 -- +bool(false) +bool(false) +bool(false) +-- Iteration 13 -- +bool(true) +bool(false) +bool(true) +-- Iteration 14 -- +bool(true) +bool(false) +bool(true) +-- Iteration 15 -- +bool(true) +bool(false) +bool(true) +-- Iteration 16 -- +bool(true) +bool(false) +bool(true) +-- Iteration 17 -- +bool(true) +bool(true) +bool(true) +-- Iteration 18 -- +bool(true) +bool(false) +bool(true) +-- Iteration 19 -- +bool(true) +bool(true) +bool(true) +-- Iteration 20 -- +bool(true) +bool(false) +bool(true) +-- Iteration 21 -- +bool(true) +bool(false) +bool(true) +-- Iteration 22 -- +bool(true) +bool(true) +bool(true) +-- Iteration 23 -- +bool(false) +bool(false) +bool(false) +-- Iteration 24 -- +bool(false) +bool(false) +bool(false) +-- Iteration 25 -- +bool(false) +bool(false) +bool(false) +-- Iteration 26 -- +bool(false) +bool(false) +bool(false) +-- Iteration 27 -- +bool(false) +bool(false) +bool(false) +-- Iteration 28 -- +bool(false) +bool(false) +bool(false) +-- Iteration 29 -- +bool(true) +bool(false) +bool(true) +-- Iteration 30 -- +bool(true) +bool(false) +bool(true) +-- Iteration 31 -- +bool(true) +bool(true) +bool(true) +-- Iteration 32 -- +bool(true) +bool(true) +bool(true) +-- Iteration 33 -- +bool(true) +bool(true) +bool(true) +-- Iteration 34 -- +bool(true) +bool(false) +bool(true) +-- Iteration 35 -- +bool(true) +bool(false) +bool(true) +-- Iteration 36 -- +bool(false) +bool(false) +bool(false) +-- Iteration 37 -- +bool(true) +bool(true) +bool(true) +-- Iteration 38 -- +bool(true) +bool(false) +bool(true) +-- Iteration 39 -- +bool(true) +bool(false) +bool(true) +-- Iteration 40 -- +bool(false) +bool(false) +bool(false) +-- Iteration 41 -- +bool(true) +bool(false) +bool(true) +-- Iteration 42 -- +bool(false) +bool(false) +bool(false) +-- Iteration 43 -- +bool(false) +bool(false) +bool(false) +-- Iteration 44 -- +bool(false) +bool(false) +bool(false) +-- Iteration 45 -- +bool(false) +bool(false) +bool(false) +-- Iteration 46 -- +bool(false) +bool(false) +bool(false) +-- Iteration 47 -- +bool(false) +bool(false) +bool(false) +-- Iteration 48 -- +bool(false) +bool(false) +bool(false) +-- Iteration 49 -- +bool(false) +bool(false) +bool(false) +-- Iteration 50 -- +bool(false) +bool(false) +bool(false) +-- Iteration 51 -- +bool(false) +bool(false) +bool(false) +-- Iteration 52 -- +bool(false) +bool(false) +bool(false) +-- Iteration 53 -- +bool(false) +bool(false) +bool(false) +-- Iteration 54 -- +bool(false) +bool(false) +bool(false) +-- Iteration 55 -- +bool(false) +bool(false) +bool(false) +-- Iteration 56 -- +bool(false) +bool(false) +bool(false) +-- Iteration 57 -- +bool(false) +bool(false) +bool(false) +-- Iteration 58 -- +bool(false) +bool(false) +bool(false) +-- Iteration 59 -- +bool(false) +bool(false) +bool(false) +-- Iteration 60 -- +bool(true) +bool(true) +bool(true) +-- Iteration 61 -- +bool(true) +bool(false) +bool(true) +-- Iteration 62 -- +bool(false) +bool(false) +bool(false) +-- Iteration 63 -- +bool(true) +bool(false) +bool(true) +-- Iteration 64 -- +bool(false) +bool(false) +bool(false) +-- Iteration 65 -- +bool(false) +bool(false) +bool(false) +-- Iteration 66 -- +bool(false) +bool(false) +bool(false) +-- Iteration 67 -- +bool(false) +bool(false) +bool(false) +-- Iteration 68 -- +bool(false) +bool(false) +bool(false) +-- Iteration 69 -- +bool(false) +bool(false) +bool(false) +-- Iteration 70 -- +bool(true) +bool(false) +bool(true) +-- Iteration 71 -- +bool(true) +bool(false) +bool(true) +-- Iteration 72 -- +bool(false) +bool(false) +bool(false) +-- Iteration 73 -- +bool(true) +bool(false) +bool(true) +-- Iteration 74 -- +bool(true) +bool(false) +bool(true) +-- Iteration 75 -- +bool(true) +bool(false) +bool(true) +-- Iteration 76 -- +bool(true) +bool(false) +bool(true) +-- Iteration 77 -- +bool(true) +bool(false) +bool(true) +-- Iteration 78 -- +bool(true) +bool(false) +bool(true) +-- Iteration 79 -- +bool(true) +bool(false) +bool(true) +-- Iteration 80 -- +bool(true) +bool(false) +bool(true) +-- Iteration 81 -- +bool(true) +bool(false) +bool(true) +-- Iteration 82 -- +bool(true) +bool(false) +bool(true) +-- Iteration 83 -- +bool(true) +bool(false) +bool(true) +-- Iteration 84 -- +bool(true) +bool(false) +bool(true) +-- Iteration 85 -- +bool(true) +bool(false) +bool(true) +-- Iteration 86 -- +bool(true) +bool(false) +bool(true) +-- Iteration 87 -- +bool(true) +bool(false) +bool(true) +-- Iteration 88 -- +bool(true) +bool(false) +bool(true) +-- Iteration 89 -- +bool(true) +bool(false) +bool(true) +-- Iteration 90 -- +bool(true) +bool(false) +bool(true) +-- Iteration 91 -- +bool(false) +bool(false) +bool(false) +-- Iteration 92 -- +bool(false) +bool(false) +bool(false) +-- Iteration 93 -- +bool(false) +bool(false) +bool(false) +-- Iteration 94 -- +bool(false) +bool(false) +bool(false) +-- Iteration 95 -- +bool(false) +bool(false) +bool(false) +-- Iteration 96 -- +bool(false) +bool(false) +bool(false) +-- Iteration 97 -- +bool(false) +bool(false) +bool(false) +-- Iteration 98 -- +bool(false) +bool(false) +bool(false) +-- Iteration 99 -- +bool(false) +bool(false) +bool(false) +-- Iteration 100 -- +bool(false) +bool(false) +bool(false) +-- Iteration 101 -- +bool(true) +bool(true) +bool(true) +-- Iteration 102 -- +bool(true) +bool(true) +bool(true) +-- Iteration 103 -- +bool(true) +bool(false) +bool(true) +-- Iteration 104 -- +bool(false) +bool(false) +bool(false) +-- Iteration 105 -- +bool(false) +bool(false) +bool(false) +-- Iteration 106 -- +bool(true) +bool(false) +bool(true) +-- Iteration 107 -- +bool(true) +bool(false) +bool(true) +-- Iteration 108 -- +bool(false) +bool(false) +bool(false) +-- Iteration 109 -- +bool(false) +bool(false) +bool(false) +-- Iteration 110 -- +bool(false) +bool(false) +bool(false) +-- Iteration 111 -- +bool(false) +bool(false) +bool(false) +-- Iteration 112 -- +bool(false) +bool(false) +bool(false) +-- Iteration 113 -- +bool(false) +bool(false) +bool(false) +-- Iteration 114 -- +bool(false) +bool(false) +bool(false) +-- Iteration 115 -- +bool(false) +bool(false) +bool(false) +-- Iteration 116 -- +bool(false) +bool(false) +bool(false) +-- Iteration 117 -- +bool(false) +bool(false) +bool(false) +-- Iteration 118 -- +bool(false) +bool(false) +bool(false) +-- Iteration 119 -- +bool(false) +bool(false) +bool(false) +-- Iteration 120 -- +bool(false) +bool(false) +bool(false) +-- Iteration 121 -- +bool(false) +bool(false) +bool(false) +-- Iteration 122 -- +bool(false) +bool(false) +bool(false) +-- Iteration 123 -- +bool(false) +bool(false) +bool(false) +-- Iteration 124 -- +bool(true) +bool(false) +bool(true) +-- Iteration 125 -- +bool(true) +bool(false) +bool(true) +-- Iteration 126 -- +bool(true) +bool(true) +bool(true) +-- Iteration 127 -- +bool(false) +bool(false) +bool(false) +-- Iteration 128 -- +bool(false) +bool(false) +bool(false) +-- Iteration 129 -- +bool(false) +bool(false) +bool(false) +-- Iteration 130 -- +bool(false) +bool(false) +bool(false) +-- Iteration 131 -- +bool(false) +bool(false) +bool(false) +-- Iteration 132 -- +bool(false) +bool(false) +bool(false) +-- Iteration 133 -- +bool(false) +bool(false) +bool(false) +-- Iteration 134 -- +bool(false) +bool(false) +bool(false) +-- Iteration 135 -- +bool(false) +bool(false) +bool(false) +-- Iteration 136 -- +bool(false) +bool(false) +bool(false) +-- Iteration 137 -- +bool(false) +bool(false) +bool(false) +-- Iteration 138 -- +bool(false) +bool(false) +bool(false) +-- Iteration 139 -- +bool(false) +bool(false) +bool(false) +-- Iteration 140 -- +bool(false) +bool(false) +bool(false) +-- Iteration 141 -- +bool(false) +bool(false) +bool(false) +-- Iteration 142 -- +bool(true) +bool(false) +bool(true) +-- Iteration 143 -- +bool(true) +bool(false) +bool(true) +-- Iteration 144 -- +bool(false) +bool(false) +bool(false) + +*** Testing loose and strict TYPE comparison of in_array() *** +-- Iteration 1 -- +bool(true) +bool(true) +bool(true) +-- Iteration 2 -- +bool(true) +bool(true) +bool(true) +-- Iteration 3 -- +bool(true) +bool(false) +bool(true) +-- Iteration 4 -- +bool(true) +bool(true) +bool(true) +-- Iteration 5 -- +bool(true) +bool(false) +bool(true) +-- Iteration 6 -- +bool(true) +bool(false) +bool(true) +-- Iteration 7 -- +bool(true) +bool(false) +bool(true) +-- Iteration 8 -- +bool(true) +bool(false) +bool(true) +-- Iteration 9 -- +bool(true) +bool(true) +bool(true) +-- Iteration 10 -- +bool(true) +bool(false) +bool(true) +-- Iteration 11 -- +bool(true) +bool(false) +bool(true) +-- Iteration 12 -- +bool(true) +bool(true) +bool(true) + +*** Testing sub-arrays with in_array() *** +bool(true) +bool(true) +bool(true) + +*** Testing objects with in_array() *** + +Warning: in_array(): Wrong datatype for second argument in %s on line %d +bool(false) + +Warning: in_array(): Wrong datatype for second argument in %s on line %d +bool(false) +bool(true) + +*** Testing resource type with in_array() *** +bool(true) +bool(false) + +*** Testing miscelleneos inputs with in_array() *** +bool(true) +bool(false) +bool(true) +bool(true) +bool(true) +bool(true) +bool(false) +bool(false) +bool(true) +bool(false) + +*** Testing error conditions of in_array() *** + +Warning: Wrong parameter count for in_array() in %s on line %d +NULL + +Warning: Wrong parameter count for in_array() in %s on line %d +NULL + +Warning: Wrong parameter count for in_array() in %s on line %d +NULL + +Warning: in_array(): Wrong datatype for second argument in %s on line %d +bool(false) + +Warning: in_array(): Wrong datatype for second argument in %s on line %d +bool(false) +Done diff --git a/ext/standard/tests/array/array_values.phpt b/ext/standard/tests/array/array_values.phpt Binary files differindex 42e252ac81..c4c59b01d0 100644 --- a/ext/standard/tests/array/array_values.phpt +++ b/ext/standard/tests/array/array_values.phpt diff --git a/ext/standard/tests/array/each.phpt b/ext/standard/tests/array/each.phpt Binary files differnew file mode 100644 index 0000000000..1953a04397 --- /dev/null +++ b/ext/standard/tests/array/each.phpt diff --git a/ext/standard/tests/array/end.phpt b/ext/standard/tests/array/end.phpt new file mode 100644 index 0000000000..d2eaa28451 --- /dev/null +++ b/ext/standard/tests/array/end.phpt @@ -0,0 +1,232 @@ +--TEST-- +Test end() function +--FILE-- +<?php +/* Prototype: mixed end ( array &$array ); + Description: Advances internal pointer of array to last element, and returns its value. +*/ + +$arrays = array ( + array( 0 ), + range(1, 100 ), + range('a', 'z', 2 ), + array("a" => "A", 2 => "B", "C" => 3, 4 => 4, "one" => 1, "" => NULL ), + array(1, array(1, 2 => 3 ), "one" => 1, "5" => 5 ), + array(-1, -2, -3, -4, "-0.005" => "neg0.005", 2.0 => "float2", "neg.9" => -.9 ), + array(1.0005, 2.000000, -3.000000, -4.9999999 ), + array(true, false), + array("PHP", "Web2.0", "SOA"), + array(1, array() ), + array(1, 2, "" ), + array(" "), + array(2147483647, 2147483648, -2147483647, -2147483648 ), + array(0x7FFFFFFF, -0x80000000, 017777777777, -020000000000 ), + array(-.6700000E+3, -4.10003E+3, 1e-5, -1E+5, 000002.00 ) +); +/* loop through $arrays to print the last element of each sub-array */ +echo "*** Testing end() on different arrays ***\n"; +$counter = 1; +foreach ($arrays as $sub_array){ + echo "-- Iteration $counter --\n"; + var_dump( end($sub_array) ); + /* ensure that internal pointer is moved to last element */ + var_dump( current($sub_array) ); + $counter++; +} + +/* checking for end() on sub-arrays */ +echo "\n*** Testing end() with sub-arrays ***\n"; +$test_array = array(1, array(1 => "one", "two" => 2, "" => "f") ); +var_dump( end($test_array) ); +var_dump( end($test_array[1]) ); + +/* checking working of end() when array elements are deleted */ +echo "\n*** Testing end() when array elements are deleted ***\n"; +$array_test = array("a", "b", "d", 7, "u" => "U", -4, "-.008" => "neg.008"); + +// remove first element from array +echo "\n-- Remove first element from array --\n"; +unset($array_test[0]); +var_dump( end($array_test) ); + +// remove last element from array, rewind and check end() +echo "\n-- Remove last element from array --\n"; +unset($array_test['-.008']); +var_dump( end($array_test) ); +reset( $array_test ); +var_dump( end($array_test) ); + +// remove any element !first, !last, rewind and check end() +echo "\n-- Remove any element from array apart from first and last element --\n"; +unset($array_test[7]); +var_dump( end($array_test) ); +var_dump( reset($array_test) ); +var_dump( end($array_test) ); + +/* Checking on OBJECTS type */ +echo "\n*** Testing end() on objects ***\n"; +class foo +{ + function __toString() { + return "Object"; + } +} +class foo1 +{ + function __toString() { + return "Object1"; + } +} + +$object1 = new foo(); //new object created +$object2 = new foo1(); + +$array_object = array(); +$array_object[0] = &$object1; +$array_object[1] = &$object2; +var_dump( end($array_object) ); +var_dump($array_object); + +/* Checking on RESOURCE type */ +echo "\n*** Testing end() on resource type ***\n"; +//file type resource +$file_handle = fopen(__FILE__, "r"); + +//directory type resource +$dir_handle = opendir( dirname(__FILE__) ); + +//store resources in array +$resources = array($file_handle, $dir_handle); +var_dump( end($resources) ); +var_dump( current($resources) ); + +echo "\n*** Testing error conditions ***\n"; +/* checking for unexpected number of arguments */ +var_dump( end() ); +var_dump( end($array[0], $array[0]) ); + +/* checking for unexpected type of arguments */ +$var=1; +$var1="string"; +var_dump( end($var) ); +var_dump( end($var1) ); + +/* checking null array */ +$null_array = array(); +var_dump( end($null_array) ); + +echo "Done\n"; + +?> + +--CLEAN-- +/* cleaning resource handles */ +fclose( $file_handle ); //file resource handle deleted +closedir( $dir_handle ); //dir resource handle deleted + +--EXPECTF-- +*** Testing end() on different arrays *** +-- Iteration 1 -- +int(0) +int(0) +-- Iteration 2 -- +int(100) +int(100) +-- Iteration 3 -- +string(1) "y" +string(1) "y" +-- Iteration 4 -- +NULL +NULL +-- Iteration 5 -- +int(5) +int(5) +-- Iteration 6 -- +float(-0.9) +float(-0.9) +-- Iteration 7 -- +float(-4.9999999) +float(-4.9999999) +-- Iteration 8 -- +bool(false) +bool(false) +-- Iteration 9 -- +string(3) "SOA" +string(3) "SOA" +-- Iteration 10 -- +array(0) { +} +array(0) { +} +-- Iteration 11 -- +string(0) "" +string(0) "" +-- Iteration 12 -- +string(1) " " +string(1) " " +-- Iteration 13 -- +float(-2147483648) +float(-2147483648) +-- Iteration 14 -- +float(-2147483648) +float(-2147483648) +-- Iteration 15 -- +float(2) +float(2) + +*** Testing end() with sub-arrays *** +array(3) { + [1]=> + string(3) "one" + ["two"]=> + int(2) + [""]=> + string(1) "f" +} +string(1) "f" + +*** Testing end() when array elements are deleted *** + +-- Remove first element from array -- +string(7) "neg.008" + +-- Remove last element from array -- +int(-4) +int(-4) + +-- Remove any element from array apart from first and last element -- +int(-4) +string(1) "b" +int(-4) + +*** Testing end() on objects *** +object(foo1)#%d (0) { +} +array(2) { + [0]=> + &object(foo)#%d (0) { + } + [1]=> + &object(foo1)#%d (0) { + } +} + +*** Testing end() on resource type *** +resource(%d) of type (stream) +resource(%d) of type (stream) + +*** Testing error conditions *** + +Warning: Wrong parameter count for end() in %s on line %d +NULL + +Warning: Wrong parameter count for end() in %s on line %d +NULL + +Warning: end(): Passed variable is not an array or object in %s on line %d +bool(false) + +Warning: end(): Passed variable is not an array or object in %s on line %d +bool(false) +bool(false) +Done diff --git a/ext/standard/tests/array/extract.phpt b/ext/standard/tests/array/extract.phpt new file mode 100644 index 0000000000..d9b37c8594 --- /dev/null +++ b/ext/standard/tests/array/extract.phpt @@ -0,0 +1,276 @@ +--TEST-- +Test extract() function +--FILE-- +<?php +/* Prototype: int extract( array var_array[, int extract_type[, string prefix]] ) + * Description: Import variables into the current symbol table from an array + */ + +/* various combinations of arrays to be used for the test */ +$mixed_array = array( + array(), + array( 1,2,3,4,5,6,7,8,9 ), + array( "One", "Two", "Three", "Four", "Five" ), + array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ), + array( "a" => "aaa", "A" => "AAA", "c" => "ccc", "d" => "ddd", "e" => "eee" ), + array( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five" ), + array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ), + array( "f" => "fff", "1" => "one", 4 => 6, "" => "blank", 2.4 => "float", "F" => "FFF", + "blank" => "", 3.7 => 3.7, 5.4 => 7, 6 => 8.6, '5' => "Five", "4name" => "jonny", "a" => NULL, NULL => 3 ), + array( 12, "name", 'age', '45' ), + array( array("oNe", "tWo", 4), array(10, 20, 30, 40, 50), array() ), + array( "one" => 1, "one" => 2, "three" => 3, 3, 4, 3 => 33, 4 => 44, 5, 6, + 5.4 => 54, 5.7 => 57, "5.4" => 554, "5.7" => 557 ) +); + +$val = 4; +$str = "John"; + +/* Extracting Global Variables */ +extract($GLOBALS, EXTR_REFS); + +/* Testing Error Conditions */ +echo "*** Testing Error Conditions ***\n"; + +/* Zero Arguments */ +var_dump( extract() ); + +/* Invalid second argument ( only 0-6 is valid) */ +var_dump( extract($mixed_array[7], -1 . "wddr") ); +var_dump( extract($mixed_array[7], 7 , "wddr") ); + +/* scalar argument */ +var_dump( extract($val) ); + +/* string argument */ +var_dump( extract($str) ); + +/* More than valid number of arguments i.e. 3 args */ +var_dump( extract($mixed_array[7], EXTR_SKIP, "aa", "ee") ); + +/* Two Arguments, second as prefix but without prefix string as third argument */ +var_dump( extract($mixed_array[7],EXTR_PREFIX_IF_EXISTS) ); + +$counter = 0; + +foreach ( $mixed_array as $sub_array ) { + echo "\n-- Iteration $counter --\n"; + $counter++; + + var_dump ( extract($sub_array)); /* Single Argument */ + + /* variations of two arguments */ + var_dump ( extract($sub_array, EXTR_OVERWRITE)); + var_dump ( extract($sub_array, EXTR_SKIP)); + var_dump ( extract($sub_array, EXTR_IF_EXISTS)); + + /* variations of three arguments with use of various extract types*/ + var_dump ( extract($sub_array, EXTR_PREFIX_INVALID, "ssd")); + var_dump ( extract($sub_array, EXTR_PREFIX_SAME, "sss")); + var_dump ( extract($sub_array, EXTR_PREFIX_ALL, "bb")); + var_dump ( extract($sub_array, EXTR_PREFIX_ALL, "")); // "_" taken as default prefix + var_dump ( extract($sub_array, EXTR_PREFIX_IF_EXISTS, "bb")); +} + + +/* EXTR_REFS as second Argument */ +$a = array ('foo' => 'aaa'); +var_dump ( extract($a, EXTR_REFS)); +$b = $a; +$b['foo'] = 'bbb'; +var_dump ( extract($a, EXTR_REFS)); + +/* EXTR_PREFIX_ALL called twice with same prefix string */ +echo "\n*** Testing for EXTR_PREFIX_ALL called twice with same prefix string ***\n"; +var_dump ( extract($mixed_array[5], EXTR_PREFIX_ALL, "same")); +var_dump ( extract($mixed_array[7], EXTR_PREFIX_ALL, "same")); +var_dump ( extract($mixed_array[7], EXTR_PREFIX_ALL, "diff")); + +/* To show variables with numerical prefixes cannot be extracted */ +$var["i"] = 1; +$var["j"] = 2; +$var["k"] = 3; +echo "\n*** Testing for Numerical prefixes ***\n"; +var_dump(extract($var)); + + +$var1["m"] = 1; +$var1[2] = 2; +$var1[] = 3; +var_dump ( extract($var1)); + + +/* Using Class and objects */ + +echo "\n*** Testing for object ***\n"; +class classA +{ + public $v; +} + +$A = new classA(); +var_dump ( extract(get_object_vars($A),EXTR_REFS)); + +echo "\nDone"; +?> + +--EXPECTF-- +*** Testing Error Conditions *** + +Warning: Wrong parameter count for extract() in %s on line %d +NULL + +Warning: extract(): Unknown extract type in %s on line %d +NULL + +Warning: extract(): Unknown extract type in %s on line %d +NULL + +Warning: extract(): First argument should be an array in %s on line %d +NULL + +Warning: extract(): First argument should be an array in %s on line %d +NULL + +Warning: Wrong parameter count for extract() in %s on line %d +NULL + +Warning: extract(): Prefix expected to be specified in %s on line %d +NULL + +-- Iteration 0 -- +int(0) +int(0) +int(0) +int(0) +int(0) +int(0) +int(0) +int(0) +int(0) + +-- Iteration 1 -- +int(0) +int(0) +int(0) +int(0) +int(9) +int(0) +int(9) +int(9) +int(0) + +-- Iteration 2 -- +int(0) +int(0) +int(0) +int(0) +int(5) +int(0) +int(5) +int(5) +int(0) + +-- Iteration 3 -- +int(0) +int(0) +int(0) +int(0) +int(8) +int(0) +int(8) +int(8) +int(0) + +-- Iteration 4 -- +int(5) +int(5) +int(0) +int(5) +int(5) +int(5) +int(5) +int(5) +int(5) + +-- Iteration 5 -- +int(0) +int(0) +int(0) +int(0) +int(5) +int(0) +int(5) +int(5) +int(0) + +-- Iteration 6 -- +int(0) +int(0) +int(0) +int(0) +int(5) +int(0) +int(5) +int(5) +int(0) + +-- Iteration 7 -- +int(4) +int(4) +int(0) +int(4) +int(12) +int(4) +int(11) +int(11) +int(4) + +-- Iteration 8 -- +int(0) +int(0) +int(0) +int(0) +int(4) +int(0) +int(4) +int(4) +int(0) + +-- Iteration 9 -- +int(0) +int(0) +int(0) +int(0) +int(3) +int(0) +int(3) +int(3) +int(0) + +-- Iteration 10 -- +int(2) +int(2) +int(0) +int(2) +int(8) +int(2) +int(8) +int(8) +int(2) +int(1) +int(1) + +*** Testing for EXTR_PREFIX_ALL called twice with same prefix string *** +int(5) +int(11) +int(11) + +*** Testing for Numerical prefixes *** +int(3) +int(1) + +*** Testing for object *** +int(1) + +Done |