- New tests for key() function

This commit is contained in:
Josie Messa 2008-02-21 14:36:15 +00:00
parent eb7565451d
commit 7b5a0bc42b
6 changed files with 798 additions and 0 deletions

View File

@ -0,0 +1,62 @@
--TEST--
Test key() function : basic functionality
--FILE--
<?php
/* Prototype : mixed key(array $array_arg)
* Description: Return the key of the element currently pointed to by the internal array pointer
* Source code: ext/standard/array.c
*/
/*
* Test basic functionality of key()
*/
echo "*** Testing key() : basic functionality ***\n";
$array = array ('zero', 99 => 'one', 'two', 'three' => 3);
echo "\n-- Initial Position: --\n";
var_dump(key($array));
echo "\n-- Next Position: --\n";
next($array);
var_dump(key($array));
echo "\n-- End Position: --\n";
end($array);
var_dump(key($array));
echo "\n-- Past end of the array --\n";
next($array);
var_dump(key($array));
?>
===DONE===
--EXPECTF--
*** Testing key() : basic functionality ***
-- Initial Position: --
int(0)
-- Next Position: --
int(99)
-- End Position: --
string(5) "three"
-- Past end of the array --
NULL
===DONE===
--UEXPECTF--
*** Testing key() : basic functionality ***
-- Initial Position: --
int(0)
-- Next Position: --
int(99)
-- End Position: --
unicode(5) "three"
-- Past end of the array --
NULL
===DONE===

View File

@ -0,0 +1,52 @@
--TEST--
Test key() function : error conditions - Pass incorrect number of args
--FILE--
<?php
/* Prototype : mixed key(array $array_arg)
* Description: Return the key of the element currently pointed to by the internal array pointer
* Source code: ext/standard/array.c
*/
/*
* Pass incorrect number of arguments to key() to test behaviour
*/
echo "*** Testing key() : error conditions ***\n";
// Zero arguments
echo "\n-- Testing key() function with Zero arguments --\n";
var_dump( key() );
//Test current with one more than the expected number of arguments
echo "\n-- Testing key() function with more than expected no. of arguments --\n";
$array_arg = array(1, 2);
$extra_arg = 10;
var_dump( key($array_arg, $extra_arg) );
?>
===DONE===
--EXPECTF--
*** Testing key() : error conditions ***
-- Testing key() function with Zero arguments --
Warning: key() expects exactly 1 parameter, 0 given in %s on line %d
NULL
-- Testing key() function with more than expected no. of arguments --
Warning: key() expects exactly 1 parameter, 2 given in %s on line %d
NULL
===DONE===
--UEXPECTF--
*** Testing key() : error conditions ***
-- Testing key() function with Zero arguments --
Warning: key() expects exactly 1 parameter, 0 given in %s on line %d
NULL
-- Testing key() function with more than expected no. of arguments --
Warning: key() expects exactly 1 parameter, 2 given in %s on line %d
NULL
===DONE===

View File

@ -0,0 +1,348 @@
--TEST--
Test key() function : usage variations - Pass different data types as $array_arg arg.
--FILE--
<?php
/* Prototype : mixed key(array $array_arg)
* Description: Return the key of the element currently pointed to by the internal array pointer
* Source code: ext/standard/array.c
*/
/*
* Pass different data types as $array_arg argument to test behaviour of key()
*/
echo "*** Testing key() : usage variations ***\n";
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// get a class
class classA
{
var $var1;
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 $array_arg 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 key()
$iterator = 1;
foreach($inputs as $input) {
echo "\n-- Iteration $iterator --\n";
var_dump( key($input) );
$iterator++;
};
fclose($fp);
?>
===DONE===
--EXPECTF--
*** Testing key() : usage variations ***
-- Iteration 1 --
Warning: key() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 2 --
Warning: key() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 3 --
Warning: key() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 4 --
Warning: key() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 5 --
Warning: key() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 6 --
Warning: key() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 7 --
Warning: key() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 8 --
Warning: key() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 9 --
Warning: key() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 10 --
Warning: key() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 11 --
Warning: key() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 12 --
Warning: key() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 13 --
Warning: key() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 14 --
Warning: key() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 15 --
Warning: key() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 16 --
Warning: key() expects parameter 1 to be array, string given in %s on line %d
NULL
-- Iteration 17 --
Warning: key() expects parameter 1 to be array, string given in %s on line %d
NULL
-- Iteration 18 --
NULL
-- Iteration 19 --
Warning: key() expects parameter 1 to be array, string given in %s on line %d
NULL
-- Iteration 20 --
Warning: key() expects parameter 1 to be array, string given in %s on line %d
NULL
-- Iteration 21 --
Warning: key() expects parameter 1 to be array, string given in %s on line %d
NULL
-- Iteration 22 --
Warning: key() expects parameter 1 to be array, object given in %s on line %d
NULL
-- Iteration 23 --
Warning: key() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 24 --
Warning: key() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 25 --
Warning: key() expects parameter 1 to be array, resource given in %s on line %d
NULL
===DONE===
--UEXPECTF--
*** Testing key() : usage variations ***
-- Iteration 1 --
Warning: key() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 2 --
Warning: key() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 3 --
Warning: key() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 4 --
Warning: key() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 5 --
Warning: key() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 6 --
Warning: key() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 7 --
Warning: key() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 8 --
Warning: key() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 9 --
Warning: key() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 10 --
Warning: key() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 11 --
Warning: key() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 12 --
Warning: key() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 13 --
Warning: key() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 14 --
Warning: key() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 15 --
Warning: key() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 16 --
Warning: key() expects parameter 1 to be array, Unicode string given in %s on line %d
NULL
-- Iteration 17 --
Warning: key() expects parameter 1 to be array, Unicode string given in %s on line %d
NULL
-- Iteration 18 --
NULL
-- Iteration 19 --
Warning: key() expects parameter 1 to be array, Unicode string given in %s on line %d
NULL
-- Iteration 20 --
Warning: key() expects parameter 1 to be array, Unicode string given in %s on line %d
NULL
-- Iteration 21 --
Warning: key() expects parameter 1 to be array, Unicode string given in %s on line %d
NULL
-- Iteration 22 --
Warning: key() expects parameter 1 to be array, object given in %s on line %d
NULL
-- Iteration 23 --
Warning: key() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 24 --
Warning: key() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 25 --
Warning: key() expects parameter 1 to be array, resource given in %s on line %d
NULL
===DONE===

View File

@ -0,0 +1,204 @@
--TEST--
Test key() function : usage variations
--FILE--
<?php
/* Prototype : mixed key(array $array_arg)
* Description: Return the key of the element currently pointed to by the internal array pointer
* Source code: ext/standard/array.c
*/
/*
* Pass arrays where keys are different data types as $array_arg to key() to test behaviour
*/
echo "*** Testing key() : 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 $array_arg
$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 key()
$iterator = 1;
foreach($inputs as $key => $input) {
echo "\n-- Iteration $iterator : $key data --\n";
while (key($input) !== NULL) {
var_dump(key($input));
next($input);
}
$iterator++;
};
?>
===DONE===
--EXPECTF--
*** Testing key() : usage variations ***
-- Iteration 1 : int data --
int(0)
int(1)
int(12345)
int(-2345)
-- Iteration 2 : float data --
int(10)
int(-10)
int(0)
-- Iteration 3 : extreme floats data --
int(12345678)
int(0)
-- Iteration 4 : null uppercase data --
string(0) ""
-- Iteration 5 : null lowercase data --
string(0) ""
-- Iteration 6 : bool lowercase data --
int(1)
int(0)
-- Iteration 7 : bool uppercase data --
int(1)
int(0)
-- Iteration 8 : empty double quotes data --
string(0) ""
-- Iteration 9 : empty single quotes data --
string(0) ""
-- Iteration 10 : string data --
string(7) "stringd"
string(7) "strings"
string(11) "hello world"
-- Iteration 11 : undefined data --
string(0) ""
-- Iteration 12 : unset data --
string(0) ""
===DONE===
--UEXPECTF--
*** Testing key() : usage variations ***
-- Iteration 1 : int data --
int(0)
int(1)
int(12345)
int(-2345)
-- Iteration 2 : float data --
int(10)
int(-10)
int(0)
-- Iteration 3 : extreme floats data --
int(12345678)
int(0)
-- Iteration 4 : null uppercase data --
unicode(0) ""
-- Iteration 5 : null lowercase data --
unicode(0) ""
-- Iteration 6 : bool lowercase data --
int(1)
int(0)
-- Iteration 7 : bool uppercase data --
int(1)
int(0)
-- Iteration 8 : empty double quotes data --
unicode(0) ""
-- Iteration 9 : empty single quotes data --
unicode(0) ""
-- Iteration 10 : string data --
unicode(7) "stringd"
unicode(7) "strings"
unicode(11) "hello world"
-- Iteration 11 : undefined data --
unicode(0) ""
-- Iteration 12 : unset data --
unicode(0) ""
===DONE===

View File

@ -0,0 +1,53 @@
--TEST--
Test key() function : usage variations
--FILE--
<?php
/* Prototype : mixed key(array $array_arg)
* Description: Return the key of the element currently pointed to by the internal array pointer
* Source code: ext/standard/array.c
*/
/*
* Test how the internal pointer is affected when two variables are referenced to each other
*/
echo "*** Testing key() : usage variations ***\n";
$array1 = array ('zero', 'one', 'two');
echo "\n-- Initial position of internal pointer --\n";
var_dump(key($array1));
// Test that when two variables are referenced to one another
// the internal pointer is the same for both
$array2 = &$array1;
next($array1);
echo "\n-- Position after calling next() --\n";
echo "\$array1: ";
var_dump(key($array1));
echo "\$array2: ";
var_dump(key($array2));
?>
===DONE===
--EXPECTF--
*** Testing key() : usage variations ***
-- Initial position of internal pointer --
int(0)
-- Position after calling next() --
$array1: int(1)
$array2: int(1)
===DONE===
--UEXPECTF--
*** Testing key() : usage variations ***
-- Initial position of internal pointer --
int(0)
-- Position after calling next() --
$array1: int(1)
$array2: int(1)
===DONE===

View File

@ -0,0 +1,79 @@
--TEST--
Test key() function : usage variations
--FILE--
<?php
/* Prototype : mixed key(array $array_arg)
* Description: Return the key of the element currently pointed to by the internal array pointer
* Source code: ext/standard/array.c
*/
/*
* Test how key() behaves with muti-dimensional and recursive arrays
*/
echo "*** Testing key() : usage variations ***\n";
echo "\n-- Two Dimensional Array --\n";
$multi_array = array ('zero', array (1, 2, 3), 'two');
echo "Initial Position: ";
var_dump(key($multi_array));
echo "Next Position: ";
next($multi_array);
var_dump(key($multi_array));
echo "End Position: ";
end($multi_array);
var_dump(key($multi_array));
echo "\n-- Access an Array Within an Array --\n";
//accessing an array within an array
echo "Initial Position: ";
var_dump(key($multi_array[1]));
echo "\n-- Recursive, Multidimensional Array --\n";
//create a recursive array
$multi_array[] = &$multi_array;
//See where internal pointer is after adding more elements
echo "Current Position: ";
var_dump(key($multi_array));
//see if internal pointer is in same position as referenced array
var_dump(key($multi_array[3][3][3]));
// see if internal pointer is in the same position from when accessing this inner array
var_dump(key($multi_array[3][3][3][1]));
?>
===DONE===
--EXPECTF--
*** Testing key() : usage variations ***
-- Two Dimensional Array --
Initial Position: int(0)
Next Position: int(1)
End Position: int(2)
-- Access an Array Within an Array --
Initial Position: int(0)
-- Recursive, Multidimensional Array --
Current Position: int(2)
int(2)
int(0)
===DONE===
--UEXPECTF--
*** Testing key() : usage variations ***
-- Two Dimensional Array --
Initial Position: int(0)
Next Position: int(1)
End Position: int(2)
-- Access an Array Within an Array --
Initial Position: int(0)
-- Recursive, Multidimensional Array --
Current Position: int(2)
int(2)
int(0)
===DONE===