mirror of
https://github.com/php/php-src.git
synced 2024-09-23 10:57:26 +00:00
- New tests for reset() function
This commit is contained in:
parent
92cd32246b
commit
116ae80376
45
ext/standard/tests/array/reset_basic.phpt
Normal file
45
ext/standard/tests/array/reset_basic.phpt
Normal 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===
|
39
ext/standard/tests/array/reset_error.phpt
Normal file
39
ext/standard/tests/array/reset_error.phpt
Normal 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===
|
221
ext/standard/tests/array/reset_variation1.phpt
Normal file
221
ext/standard/tests/array/reset_variation1.phpt
Normal 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===
|
34
ext/standard/tests/array/reset_variation2.phpt
Normal file
34
ext/standard/tests/array/reset_variation2.phpt
Normal 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===
|
56
ext/standard/tests/array/reset_variation3.phpt
Normal file
56
ext/standard/tests/array/reset_variation3.phpt
Normal 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===
|
Loading…
Reference in New Issue
Block a user