- New tests for reset() function

This commit is contained in:
Josie Messa 2008-02-22 09:19:56 +00:00
parent 92cd32246b
commit 116ae80376
5 changed files with 395 additions and 0 deletions

View File

@ -0,0 +1,45 @@
--TEST--
Test reset() function : basic functionality
--FILE--
<?php
/* Prototype : mixed reset(array $array_arg)
* Description: Set array argument's internal pointer to the first element and return it
* Source code: ext/standard/array.c
*/
/*
* Test basic functionality of reset()
*/
echo "*** Testing reset() : basic functionality ***\n";
$array = array('zero', 'one', 200 => 'two');
echo "\n-- Initial Position: --\n";
echo key($array) . " => " . current($array) . "\n";
echo "\n-- Call to next() --\n";
var_dump(next($array));
echo "\n-- Current Position: --\n";
echo key($array) . " => " . current($array) . "\n";
echo "\n-- Call to reset() --\n";
var_dump(reset($array));
?>
===DONE===
--EXPECTF--
*** Testing reset() : basic functionality ***
-- Initial Position: --
0 => zero
-- Call to next() --
string(3) "one"
-- Current Position: --
1 => one
-- Call to reset() --
string(4) "zero"
===DONE===

View File

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

View File

@ -0,0 +1,221 @@
--TEST--
Test reset() function : usage variations - Pass different data types as $array_arg arg.
--FILE--
<?php
/* Prototype : mixed reset(array $array_arg)
* Description: Set array argument's internal pointer to the first element and return it
* Source code: ext/standard/array.c
*/
/*
* Pass different data types as $array_arg argument to reset() to test behaviour
*/
echo "*** Testing reset() : 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 $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 reset()
$iterator = 1;
foreach($inputs as $input) {
echo "\n-- Iteration $iterator --\n";
var_dump( reset($input) );
$iterator++;
};
fclose($fp);
?>
===DONE===
--EXPECTF--
*** Testing reset() : usage variations ***
-- Iteration 1 --
Warning: reset() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 2 --
Warning: reset() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 3 --
Warning: reset() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 4 --
Warning: reset() expects parameter 1 to be array, integer given in %s on line %d
NULL
-- Iteration 5 --
Warning: reset() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 6 --
Warning: reset() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 7 --
Warning: reset() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 8 --
Warning: reset() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 9 --
Warning: reset() expects parameter 1 to be array, double given in %s on line %d
NULL
-- Iteration 10 --
Warning: reset() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 11 --
Warning: reset() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 12 --
Warning: reset() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 13 --
Warning: reset() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 14 --
Warning: reset() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 15 --
Warning: reset() expects parameter 1 to be array, boolean given in %s on line %d
NULL
-- Iteration 16 --
Warning: reset() expects parameter 1 to be array, string given in %s on line %d
NULL
-- Iteration 17 --
Warning: reset() expects parameter 1 to be array, string given in %s on line %d
NULL
-- Iteration 18 --
bool(false)
-- Iteration 19 --
Warning: reset() expects parameter 1 to be array, string given in %s on line %d
NULL
-- Iteration 20 --
Warning: reset() expects parameter 1 to be array, string given in %s on line %d
NULL
-- Iteration 21 --
Warning: reset() expects parameter 1 to be array, string given in %s on line %d
NULL
-- Iteration 22 --
Warning: reset() expects parameter 1 to be array, object given in %s on line %d
NULL
-- Iteration 23 --
Warning: reset() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 24 --
Warning: reset() expects parameter 1 to be array, null given in %s on line %d
NULL
-- Iteration 25 --
Warning: reset() expects parameter 1 to be array, resource given in %s on line %d
NULL
===DONE===

View File

@ -0,0 +1,34 @@
--TEST--
Test reset() function : usage variations - unset first element
--FILE--
<?php
/* Prototype : mixed reset(array $array_arg)
* Description: Set array argument's internal pointer to the first element and return it
* Source code: ext/standard/array.c
*/
/*
* Unset first element of an array and test behaviour of reset()
*/
echo "*** Testing reset() : usage variations ***\n";
$array = array('a', 'b', 'c');
echo "\n-- Initial Position: --\n";
echo current($array) . " => " . key($array) . "\n";
echo "\n-- Unset First element in array and check reset() --\n";
unset($array[0]);
var_dump(reset($array));
?>
===DONE===
--EXPECTF--
*** Testing reset() : usage variations ***
-- Initial Position: --
a => 0
-- Unset First element in array and check reset() --
string(1) "b"
===DONE===

View File

@ -0,0 +1,56 @@
--TEST--
Test reset() function : usage variations - Referenced variables
--FILE--
<?php
/* Prototype : mixed reset(array $array_arg)
* Description: Set array argument's internal pointer to the first element and return it
* Source code: ext/standard/array.c
*/
/*
* Reference two arrays to each other then call reset() to test position of
* internal pointer in both arrays
*/
echo "*** Testing reset() : usage variations ***\n";
$array1 = array ('zero', 'one', 'two');
echo "\n-- Initial position of internal pointer --\n";
var_dump(current($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(current($array1));
echo "\$array2: ";
var_dump(current($array2));
echo "\n-- Position after calling reset() --\n";
var_dump(reset($array1));
echo "\$array1: ";
var_dump(current($array1));
echo "\$array2: ";
var_dump(current($array2));
?>
===DONE===
--EXPECTF--
*** Testing reset() : usage variations ***
-- Initial position of internal pointer --
string(4) "zero"
-- Position after calling next() --
$array1: string(3) "one"
$array2: string(3) "one"
-- Position after calling reset() --
string(4) "zero"
$array1: string(4) "zero"
$array2: string(4) "zero"
===DONE===