mirror of
https://github.com/php/php-src.git
synced 2025-01-08 20:17:28 +08:00
- New tests for array_push() function
This commit is contained in:
parent
4546084866
commit
f32e65988c
63
ext/standard/tests/array/array_push_basic.phpt
Normal file
63
ext/standard/tests/array/array_push_basic.phpt
Normal file
@ -0,0 +1,63 @@
|
||||
--TEST--
|
||||
Test array_push() function : basic functionality
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : int array_push(array $stack, mixed $var [, mixed $...])
|
||||
* Description: Pushes elements onto the end of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test basic functionality of array_push with indexed and associative arrays
|
||||
*/
|
||||
|
||||
echo "*** Testing array_push() : basic functionality ***\n";
|
||||
|
||||
$array = array ('zero', 'one', 'two');
|
||||
$var1 = 'three';
|
||||
$var2 = 'four';
|
||||
|
||||
echo "\n-- Push values onto an indexed array --\n";
|
||||
var_dump(array_push($array, $var1, $var2));
|
||||
var_dump($array);
|
||||
|
||||
$array_assoc = array ('one' => 'un', 'two' => 'deux');
|
||||
|
||||
echo "\n-- Push values onto an associative array --\n";
|
||||
var_dump(array_push($array_assoc, $var1, $var2));
|
||||
var_dump($array_assoc);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
|
||||
--EXPECTF--
|
||||
*** Testing array_push() : basic functionality ***
|
||||
|
||||
-- Push values onto an indexed array --
|
||||
int(5)
|
||||
array(5) {
|
||||
[0]=>
|
||||
string(4) "zero"
|
||||
[1]=>
|
||||
string(3) "one"
|
||||
[2]=>
|
||||
string(3) "two"
|
||||
[3]=>
|
||||
string(5) "three"
|
||||
[4]=>
|
||||
string(4) "four"
|
||||
}
|
||||
|
||||
-- Push values onto an associative array --
|
||||
int(4)
|
||||
array(4) {
|
||||
["one"]=>
|
||||
string(2) "un"
|
||||
["two"]=>
|
||||
string(4) "deux"
|
||||
[0]=>
|
||||
string(5) "three"
|
||||
[1]=>
|
||||
string(4) "four"
|
||||
}
|
||||
Done
|
30
ext/standard/tests/array/array_push_error1.phpt
Normal file
30
ext/standard/tests/array/array_push_error1.phpt
Normal file
@ -0,0 +1,30 @@
|
||||
--TEST--
|
||||
Test array_push() function : error conditions - Pass incorrect number of args
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : int array_push(array $stack, mixed $var [, mixed $...])
|
||||
* Description: Pushes elements onto the end of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Pass incorrect number of arguments to array_push() to test behaviour
|
||||
*/
|
||||
|
||||
echo "*** Testing array_push() : error conditions ***\n";
|
||||
|
||||
// Testing array_push with one less than the expected number of arguments
|
||||
echo "\n-- Testing array_push() function with less than expected no. of arguments --\n";
|
||||
$stack = array(1, 2);
|
||||
var_dump( array_push($stack) );
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_push() : error conditions ***
|
||||
|
||||
-- Testing array_push() function with less than expected no. of arguments --
|
||||
|
||||
Warning: Wrong parameter count for array_push() in %s on line %d
|
||||
NULL
|
||||
Done
|
49
ext/standard/tests/array/array_push_error2.phpt
Normal file
49
ext/standard/tests/array/array_push_error2.phpt
Normal file
@ -0,0 +1,49 @@
|
||||
--TEST--
|
||||
Test array_push() function : error conditions - min and max int values as keys
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : int array_push(array $stack, mixed $var [, mixed $...])
|
||||
* Description: Pushes elements onto the end of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Use PHP's minimum and maximum integer values as array keys
|
||||
* then try and push new elements onto the array
|
||||
*/
|
||||
|
||||
echo "*** Testing array_push() : error conditions ***\n";
|
||||
|
||||
$array = array(-PHP_INT_MAX => 'min', PHP_INT_MAX => 'max');
|
||||
|
||||
var_dump(array_push($array, 'new'));
|
||||
var_dump($array);
|
||||
var_dump(array_push($array, 'var'));
|
||||
var_dump($array);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
|
||||
--EXPECTF--
|
||||
*** Testing array_push() : error conditions ***
|
||||
int(3)
|
||||
array(3) {
|
||||
[-2147483647]=>
|
||||
string(3) "min"
|
||||
[2147483647]=>
|
||||
string(3) "max"
|
||||
[-2147483648]=>
|
||||
string(3) "new"
|
||||
}
|
||||
|
||||
Warning: array_push(): Cannot add element to the array as the next element is already occupied in %s on line %d
|
||||
bool(false)
|
||||
array(3) {
|
||||
[-2147483647]=>
|
||||
string(3) "min"
|
||||
[2147483647]=>
|
||||
string(3) "max"
|
||||
[-2147483648]=>
|
||||
string(3) "new"
|
||||
}
|
||||
Done
|
225
ext/standard/tests/array/array_push_variation1.phpt
Normal file
225
ext/standard/tests/array/array_push_variation1.phpt
Normal file
@ -0,0 +1,225 @@
|
||||
--TEST--
|
||||
Test array_push() function : usage variations - Pass different data types as $stack arg
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : int array_push(array $stack, mixed $var [, mixed $...])
|
||||
* Description: Pushes elements onto the end of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Pass different data types as $stack argument to array_push() to test behaviour
|
||||
*/
|
||||
|
||||
echo "*** Testing array_push() : usage variations ***\n";
|
||||
|
||||
// Initialise function arguments not being substituted
|
||||
$var = 'value';
|
||||
|
||||
//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 $stack 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 array_push()
|
||||
$iterator = 1;
|
||||
foreach($inputs as $input) {
|
||||
echo "\n-- Iteration $iterator --\n";
|
||||
var_dump( array_push($input, $var) );
|
||||
$iterator++;
|
||||
};
|
||||
|
||||
fclose($fp);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_push() : usage variations ***
|
||||
|
||||
-- Iteration 1 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 2 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 3 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 4 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 5 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 6 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 7 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 8 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 9 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 10 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 11 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 12 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 13 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 14 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 15 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 16 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 17 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 18 --
|
||||
int(1)
|
||||
|
||||
-- Iteration 19 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 20 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 21 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 22 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 23 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 24 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
|
||||
-- Iteration 25 --
|
||||
|
||||
Warning: array_push(): First argument should be an array in %s on line %d
|
||||
bool(false)
|
||||
Done
|
178
ext/standard/tests/array/array_push_variation2.phpt
Normal file
178
ext/standard/tests/array/array_push_variation2.phpt
Normal file
@ -0,0 +1,178 @@
|
||||
--TEST--
|
||||
Test array_push() function : usage variations - Pass different data types as $var arg
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : int array_push(array $stack, mixed $var [, mixed $...])
|
||||
* Description: Pushes elements onto the end of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Pass different data types as $var argument to array_push to test behaviour
|
||||
*/
|
||||
|
||||
echo "*** Testing array_push() : usage variations ***\n";
|
||||
|
||||
// Initialise function arguments not being substituted
|
||||
$stack = array (1, 2);
|
||||
|
||||
//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 $var 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 array_push()
|
||||
$iterator = 1;
|
||||
foreach($inputs as $input) {
|
||||
echo "\n-- Iteration $iterator --\n";
|
||||
$temp_array = $stack;
|
||||
var_dump( array_push($temp_array, $input) );
|
||||
$iterator++;
|
||||
};
|
||||
|
||||
fclose($fp);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_push() : usage variations ***
|
||||
|
||||
-- Iteration 1 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 2 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 3 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 4 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 5 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 6 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 7 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 8 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 9 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 10 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 11 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 12 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 13 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 14 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 15 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 16 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 17 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 18 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 19 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 20 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 21 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 22 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 23 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 24 --
|
||||
int(3)
|
||||
|
||||
-- Iteration 25 --
|
||||
int(3)
|
||||
Done
|
70
ext/standard/tests/array/array_push_variation3.phpt
Normal file
70
ext/standard/tests/array/array_push_variation3.phpt
Normal file
@ -0,0 +1,70 @@
|
||||
--TEST--
|
||||
Test array_push() function : usage variations - multidimensional arrays
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : int array_push(array $stack, mixed $var [, mixed $...])
|
||||
* Description: Pushes elements onto the end of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test array_push when passed:
|
||||
* 1. an array as $var arg
|
||||
* 2. as sub-array as $stack arg
|
||||
*/
|
||||
|
||||
echo "*** Testing array_push() : usage variations ***\n";
|
||||
|
||||
echo "\n-- Pass array as \$var argument --\n";
|
||||
$array = array(1, 2, 3);
|
||||
$sub_array = array('one', 'two');
|
||||
var_dump(array_push($array, $sub_array));
|
||||
var_dump($array);
|
||||
|
||||
echo "\n-- Pass sub-array as \$stack argument --\n";
|
||||
var_dump(array_push($array[3], 'a'));
|
||||
var_dump($array);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_push() : usage variations ***
|
||||
|
||||
-- Pass array as $var argument --
|
||||
int(4)
|
||||
array(4) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
[3]=>
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
[1]=>
|
||||
string(3) "two"
|
||||
}
|
||||
}
|
||||
|
||||
-- Pass sub-array as $stack argument --
|
||||
int(3)
|
||||
array(4) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
[3]=>
|
||||
array(3) {
|
||||
[0]=>
|
||||
string(3) "one"
|
||||
[1]=>
|
||||
string(3) "two"
|
||||
[2]=>
|
||||
string(1) "a"
|
||||
}
|
||||
}
|
||||
Done
|
129
ext/standard/tests/array/array_push_variation4.phpt
Normal file
129
ext/standard/tests/array/array_push_variation4.phpt
Normal file
@ -0,0 +1,129 @@
|
||||
--TEST--
|
||||
Test array_push() function : usage variations - referenced variables
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : int array_push(array $stack, mixed $var [, mixed $...])
|
||||
* Description: Pushes elements onto the end of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Test array_push when:
|
||||
* 1. passed referenced variables as $var arguments
|
||||
* 2. $var argument is a reference to $stack argument
|
||||
*/
|
||||
|
||||
echo "*** Testing array_push() : usage variations ***\n";
|
||||
|
||||
$var1 = 'a';
|
||||
$var2 = 'b';
|
||||
$var3 = 'c';
|
||||
$var4 = 'x';
|
||||
$var5 = 'y';
|
||||
$var6 = 'z';
|
||||
|
||||
$array = array(1, 2, 3);
|
||||
|
||||
echo "\n-- Pass array_push referenced varialbes as \$var arguments --\n";
|
||||
var_dump(array_push($array, &$var1, &$var2, &$var3, &$var4, &$var5, &$var6));
|
||||
var_dump($array);
|
||||
|
||||
echo "\n-- Pass \$var argument which is a reference to \$stack argument --\n";
|
||||
var_dump(array_push($array, &$array));
|
||||
var_dump($array);
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
*** Testing array_push() : usage variations ***
|
||||
|
||||
-- Pass array_push referenced varialbes as $var arguments --
|
||||
int(9)
|
||||
array(9) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
[3]=>
|
||||
&string(1) "a"
|
||||
[4]=>
|
||||
&string(1) "b"
|
||||
[5]=>
|
||||
&string(1) "c"
|
||||
[6]=>
|
||||
&string(1) "x"
|
||||
[7]=>
|
||||
&string(1) "y"
|
||||
[8]=>
|
||||
&string(1) "z"
|
||||
}
|
||||
|
||||
-- Pass $var argument which is a reference to $stack argument --
|
||||
int(10)
|
||||
array(10) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
[3]=>
|
||||
&string(1) "a"
|
||||
[4]=>
|
||||
&string(1) "b"
|
||||
[5]=>
|
||||
&string(1) "c"
|
||||
[6]=>
|
||||
&string(1) "x"
|
||||
[7]=>
|
||||
&string(1) "y"
|
||||
[8]=>
|
||||
&string(1) "z"
|
||||
[9]=>
|
||||
&array(10) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
[3]=>
|
||||
&string(1) "a"
|
||||
[4]=>
|
||||
&string(1) "b"
|
||||
[5]=>
|
||||
&string(1) "c"
|
||||
[6]=>
|
||||
&string(1) "x"
|
||||
[7]=>
|
||||
&string(1) "y"
|
||||
[8]=>
|
||||
&string(1) "z"
|
||||
[9]=>
|
||||
&array(10) {
|
||||
[0]=>
|
||||
int(1)
|
||||
[1]=>
|
||||
int(2)
|
||||
[2]=>
|
||||
int(3)
|
||||
[3]=>
|
||||
&string(1) "a"
|
||||
[4]=>
|
||||
&string(1) "b"
|
||||
[5]=>
|
||||
&string(1) "c"
|
||||
[6]=>
|
||||
&string(1) "x"
|
||||
[7]=>
|
||||
&string(1) "y"
|
||||
[8]=>
|
||||
&string(1) "z"
|
||||
[9]=>
|
||||
*RECURSION*
|
||||
}
|
||||
}
|
||||
}
|
||||
Done
|
36
ext/standard/tests/array/array_push_variation5.phpt
Normal file
36
ext/standard/tests/array/array_push_variation5.phpt
Normal file
@ -0,0 +1,36 @@
|
||||
--TEST--
|
||||
Test array_push() function : usage variations - position of internal array pointer
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : int array_push(array $stack, mixed $var [, mixed $...])
|
||||
* Description: Pushes elements onto the end of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Check the position of the internal array pointer after calling array_push()
|
||||
*/
|
||||
|
||||
echo "*** Testing array_push() : usage variations ***\n";
|
||||
|
||||
$stack = array ('one' => 'un', 'two' => 'deux');
|
||||
$var0 = 'zero';
|
||||
|
||||
echo "\n-- Call array_push() --\n";
|
||||
var_dump($result = array_push($stack, $var0));
|
||||
|
||||
echo "\n-- Position of Internal Pointer in Original Array: --\n";
|
||||
echo key($stack) . " => " . current ($stack) . "\n";
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
*** Testing array_push() : usage variations ***
|
||||
|
||||
-- Call array_push() --
|
||||
int(3)
|
||||
|
||||
-- Position of Internal Pointer in Original Array: --
|
||||
one => un
|
||||
Done
|
159
ext/standard/tests/array/array_push_variation6.phpt
Normal file
159
ext/standard/tests/array/array_push_variation6.phpt
Normal file
@ -0,0 +1,159 @@
|
||||
--TEST--
|
||||
Test array_push() function : usage variations - array keys are different data types
|
||||
--FILE--
|
||||
<?php
|
||||
/* Prototype : int array_push(array $stack, mixed $var [, mixed $...])
|
||||
* Description: Pushes elements onto the end of the array
|
||||
* Source code: ext/standard/array.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Pass array_push arrays where the keys are different data types.
|
||||
*/
|
||||
|
||||
echo "*** Testing array_push() : usage variations ***\n";
|
||||
|
||||
// Initialise function arguments not being substituted
|
||||
$var = 'value';
|
||||
|
||||
//get an unset variable
|
||||
$unset_var = 10;
|
||||
unset ($unset_var);
|
||||
|
||||
// heredoc string
|
||||
$heredoc = <<<EOT
|
||||
hello world
|
||||
EOT;
|
||||
|
||||
// arrays of different data types as keys to be passed to $stack argument
|
||||
$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',
|
||||
),
|
||||
|
||||
'extreme floats' => array(
|
||||
12.3456789000e10 => 'large',
|
||||
12.3456789000E-10 => 'small',
|
||||
),
|
||||
|
||||
// null data
|
||||
/*3*/ 'null uppercase' => array(
|
||||
NULL => 'null 1',
|
||||
),
|
||||
'null lowercase' => array(
|
||||
null => 'null 2',
|
||||
),
|
||||
|
||||
// boolean data
|
||||
/*4*/ 'bool lowercase' => array(
|
||||
true => 'lowert',
|
||||
false => 'lowerf',
|
||||
),
|
||||
'bool uppercase' => array(
|
||||
TRUE => 'uppert',
|
||||
FALSE => 'upperf',
|
||||
),
|
||||
|
||||
// empty data
|
||||
/*5*/ 'empty double quotes' => array(
|
||||
"" => 'emptyd',
|
||||
),
|
||||
'empty single quotes' => array(
|
||||
'' => 'emptys',
|
||||
),
|
||||
|
||||
// string data
|
||||
/*6*/ 'string' => array(
|
||||
"stringd" => 'stringd',
|
||||
'strings' => 'strings',
|
||||
$heredoc => 'stringh',
|
||||
),
|
||||
|
||||
// undefined data
|
||||
/*8*/ 'undefined' => array(
|
||||
@$undefined_var => 'undefined',
|
||||
),
|
||||
|
||||
// unset data
|
||||
/*9*/ 'unset' => array(
|
||||
@$unset_var => 'unset',
|
||||
),
|
||||
);
|
||||
|
||||
// loop through each sub-array of $inputs to check the behavior of array_push()
|
||||
$iterator = 1;
|
||||
foreach($inputs as $key => $input) {
|
||||
echo "\n-- Iteration $iterator : $key data --\n";
|
||||
echo "Before : ";
|
||||
var_dump(count($input));
|
||||
echo "After : ";
|
||||
var_dump( array_push($input, $var) );
|
||||
$iterator++;
|
||||
};
|
||||
|
||||
echo "Done";
|
||||
?>
|
||||
|
||||
--EXPECTF--
|
||||
*** Testing array_push() : usage variations ***
|
||||
|
||||
-- Iteration 1 : int data --
|
||||
Before : int(4)
|
||||
After : int(5)
|
||||
|
||||
-- Iteration 2 : float data --
|
||||
Before : int(3)
|
||||
After : int(4)
|
||||
|
||||
-- Iteration 3 : extreme floats data --
|
||||
Before : int(2)
|
||||
After : int(3)
|
||||
|
||||
-- Iteration 4 : null uppercase data --
|
||||
Before : int(1)
|
||||
After : int(2)
|
||||
|
||||
-- Iteration 5 : null lowercase data --
|
||||
Before : int(1)
|
||||
After : int(2)
|
||||
|
||||
-- Iteration 6 : bool lowercase data --
|
||||
Before : int(2)
|
||||
After : int(3)
|
||||
|
||||
-- Iteration 7 : bool uppercase data --
|
||||
Before : int(2)
|
||||
After : int(3)
|
||||
|
||||
-- Iteration 8 : empty double quotes data --
|
||||
Before : int(1)
|
||||
After : int(2)
|
||||
|
||||
-- Iteration 9 : empty single quotes data --
|
||||
Before : int(1)
|
||||
After : int(2)
|
||||
|
||||
-- Iteration 10 : string data --
|
||||
Before : int(3)
|
||||
After : int(4)
|
||||
|
||||
-- Iteration 11 : undefined data --
|
||||
Before : int(1)
|
||||
After : int(2)
|
||||
|
||||
-- Iteration 12 : unset data --
|
||||
Before : int(1)
|
||||
After : int(2)
|
||||
Done
|
Loading…
Reference in New Issue
Block a user