diff options
Diffstat (limited to 'Zend/tests/list')
-rw-r--r-- | Zend/tests/list/list_reference_001.phpt | 88 | ||||
-rw-r--r-- | Zend/tests/list/list_reference_002.phpt | 20 | ||||
-rw-r--r-- | Zend/tests/list/list_reference_003.phpt | 73 | ||||
-rw-r--r-- | Zend/tests/list/list_reference_004.phpt | 28 | ||||
-rw-r--r-- | Zend/tests/list/list_reference_005.phpt | 73 | ||||
-rw-r--r-- | Zend/tests/list/list_reference_006.phpt | 58 | ||||
-rw-r--r-- | Zend/tests/list/list_reference_007.phpt | 75 | ||||
-rw-r--r-- | Zend/tests/list/list_reference_008.phpt | 68 | ||||
-rw-r--r-- | Zend/tests/list/list_reference_009.phpt | 47 | ||||
-rw-r--r-- | Zend/tests/list/list_reference_010.phpt | 8 | ||||
-rw-r--r-- | Zend/tests/list/list_reference_011.phpt | 9 |
11 files changed, 547 insertions, 0 deletions
diff --git a/Zend/tests/list/list_reference_001.phpt b/Zend/tests/list/list_reference_001.phpt new file mode 100644 index 0000000000..a173c7103e --- /dev/null +++ b/Zend/tests/list/list_reference_001.phpt @@ -0,0 +1,88 @@ +--TEST-- +"Reference Unpacking - General" list() +--FILE-- +<?php +$arr = array(1, array(2)); +list(&$a, list(&$b)) = $arr; +var_dump($a, $b); +var_dump($arr); + +$arr = array(1, array(2)); +list($a, &$b) = $arr; +var_dump($arr); + +$arr = array(1, array(2)); +[&$a, [&$b]] = $arr; +var_dump($a, $b); +var_dump($arr); + +$arr = array(1, array(2)); +[&$a, [&$b], &$c] = $arr; +var_dump($a, $b, $c); +var_dump($arr); + +$arr = array("one" => 1, "two" => array(2)); +["one" => &$a, "two" => [&$b], "three" => &$c] = $arr; +var_dump($a, $b, $c); +var_dump($arr); +?> +--EXPECTF-- +int(1) +int(2) +array(2) { + [0]=> + &int(1) + [1]=> + array(1) { + [0]=> + &int(2) + } +} +array(2) { + [0]=> + int(1) + [1]=> + &array(1) { + [0]=> + int(2) + } +} +int(1) +int(2) +array(2) { + [0]=> + &int(1) + [1]=> + array(1) { + [0]=> + &int(2) + } +} +int(1) +int(2) +NULL +array(3) { + [0]=> + &int(1) + [1]=> + array(1) { + [0]=> + &int(2) + } + [2]=> + &NULL +} +int(1) +int(2) +NULL +array(3) { + ["one"]=> + &int(1) + ["two"]=> + array(1) { + [0]=> + &int(2) + } + ["three"]=> + &NULL +} diff --git a/Zend/tests/list/list_reference_002.phpt b/Zend/tests/list/list_reference_002.phpt new file mode 100644 index 0000000000..32aad686d1 --- /dev/null +++ b/Zend/tests/list/list_reference_002.phpt @@ -0,0 +1,20 @@ +--TEST-- +"Reference Unpacking - New Reference" list() +--FILE-- +<?php +$arr = array(new stdclass); +list(&$a, &$b) = $arr; +var_dump($a, $b); +var_dump($arr); +?> +--EXPECTF-- +object(stdClass)#%d (0) { +} +NULL +array(2) { + [0]=> + &object(stdClass)#%d (0) { + } + [1]=> + &NULL +} diff --git a/Zend/tests/list/list_reference_003.phpt b/Zend/tests/list/list_reference_003.phpt new file mode 100644 index 0000000000..9c903407d5 --- /dev/null +++ b/Zend/tests/list/list_reference_003.phpt @@ -0,0 +1,73 @@ +--TEST-- +"Reference Unpacking - From Functions" list() +--FILE-- +<?php +$arr = [1, 2]; +function no_ref($a) { + return $a; +} + +function no_ref_by_ref(&$a) { + return $a; +} + +function &ref_return(&$a) { + return $a; +} + +function &ref_return_global() { + global $arr; + return $arr; +} + +$a = [1, 2]; +[&$var] = no_ref($a); +var_dump($var); +var_dump($a); + +$a = [1, 2]; +[&$var] = no_ref_by_ref($a); +var_dump($var); +var_dump($a); + +$a = [1, 2]; +[&$var] = ref_return($a); +var_dump($var); +var_dump($a); + +[,&$var] = ref_return_global(); +var_dump($var); +var_dump($arr); +?> +--EXPECTF-- +Notice: Attempting to set reference to non referenceable value in %s on line %d +int(1) +array(2) { + [0]=> + int(1) + [1]=> + int(2) +} + +Notice: Attempting to set reference to non referenceable value in %s on line %d +int(1) +array(2) { + [0]=> + int(1) + [1]=> + int(2) +} +int(1) +array(2) { + [0]=> + &int(1) + [1]=> + int(2) +} +int(2) +array(2) { + [0]=> + int(1) + [1]=> + &int(2) +} diff --git a/Zend/tests/list/list_reference_004.phpt b/Zend/tests/list/list_reference_004.phpt new file mode 100644 index 0000000000..fc9955e28a --- /dev/null +++ b/Zend/tests/list/list_reference_004.phpt @@ -0,0 +1,28 @@ +--TEST-- +"Reference Unpacking - Foreach" list() +--FILE-- +<?php +$coords = array(array(1, 2), array(3, 4)); +foreach ($coords as [&$x, $y]) { + $x++; + $y++; +} +var_dump($coords); +?> +--EXPECTF-- +array(2) { + [0]=> + array(2) { + [0]=> + int(2) + [1]=> + int(2) + } + [1]=> + array(2) { + [0]=> + &int(4) + [1]=> + int(4) + } +} diff --git a/Zend/tests/list/list_reference_005.phpt b/Zend/tests/list/list_reference_005.phpt new file mode 100644 index 0000000000..397f9013b5 --- /dev/null +++ b/Zend/tests/list/list_reference_005.phpt @@ -0,0 +1,73 @@ +--TEST-- +"Reference Unpacking - Class Property and Methods" list() +--FILE-- +<?php +class A { + public $a = [['hello']]; + public $b = ['world']; + + public function getVar() { + return $this->a; + } + + public function &getVarRef() { + return $this->a; + } +} + +class B { + static $a = [['world']]; +} + +$a = new A(); +[&$var] = $a->a; +[&$var_too] = $a->b; +var_dump($a->a); +var_dump($a->b); + +$a = new A(); +[&$var] = $a->getVar(); +var_dump($a->a); + +$a = new A(); +[&$var] = $a->getVarRef(); +var_dump($a->a); + +[&$var] = B::$a; +var_dump(B::$a); +?> +--EXPECTF-- +array(1) { + [0]=> + &array(1) { + [0]=> + string(5) "hello" + } +} +array(1) { + [0]=> + &string(5) "world" +} + +Notice: Attempting to set reference to non referenceable value in %s on line %d +array(1) { + [0]=> + array(1) { + [0]=> + string(5) "hello" + } +} +array(1) { + [0]=> + &array(1) { + [0]=> + string(5) "hello" + } +} +array(1) { + [0]=> + &array(1) { + [0]=> + string(5) "world" + } +} diff --git a/Zend/tests/list/list_reference_006.phpt b/Zend/tests/list/list_reference_006.phpt new file mode 100644 index 0000000000..f85edf04a4 --- /dev/null +++ b/Zend/tests/list/list_reference_006.phpt @@ -0,0 +1,58 @@ +--TEST-- +"Reference Unpacking - Class ArrayAccess No Reference" list() +--FILE-- +<?php +class StorageNoRef implements ArrayAccess { + private $s = []; + function __construct(array $a) { $this->s = $a; } + function offsetSet ($k, $v) { $this->s[$k] = $v; } + function offsetGet ($k) { return $this->s[$k]; } + function offsetExists ($k) { return isset($this->s[$k]); } + function offsetUnset ($k) { unset($this->s[$k]); } +} + +$a = new StorageNoRef([1, 2]); +list(&$one, $two) = $a; +var_dump($a); + +$a = new StorageNoRef([1, 2]); +list(,,list($var)) = $a; +var_dump($a); + +$a = new StorageNoRef(['one' => 1, 'two' => 2]); +['one' => &$one, 'two' => $two] = $a; +var_dump($a); +?> +--EXPECTF-- +Notice: Indirect modification of overloaded element of %s has no effect in %s on line %d +object(StorageNoRef)#1 (1) { + ["s":"StorageNoRef":private]=> + array(2) { + [0]=> + int(1) + [1]=> + int(2) + } +} + +Notice: Undefined offset: 2 in %s on line %d +object(StorageNoRef)#2 (1) { + ["s":"StorageNoRef":private]=> + array(2) { + [0]=> + int(1) + [1]=> + int(2) + } +} + +Notice: Indirect modification of overloaded element of %s has no effect in %s on line %d +object(StorageNoRef)#1 (1) { + ["s":"StorageNoRef":private]=> + array(2) { + ["one"]=> + int(1) + ["two"]=> + int(2) + } +} diff --git a/Zend/tests/list/list_reference_007.phpt b/Zend/tests/list/list_reference_007.phpt new file mode 100644 index 0000000000..51f1cac496 --- /dev/null +++ b/Zend/tests/list/list_reference_007.phpt @@ -0,0 +1,75 @@ +--TEST-- +"Reference Unpacking - Class ArrayAccess With Reference" list() +--FILE-- +<?php + +class StorageRef implements ArrayAccess { + private $s = []; + function __construct(array $a) { $this->s = $a; } + function offsetSet ($k, $v) { $this->s[$k] = $v; } + function &offsetGet ($k) { return $this->s[$k]; } + function offsetExists ($k) { return isset($this->s[$k]); } + function offsetUnset ($k) { unset($this->s[$k]); } +} + +$a = new StorageRef([1, 2]); +list(&$one, $two) = $a; +var_dump($a); + +$a = new StorageRef([1, 2]); +list(,,list($var)) = $a; +var_dump($a); + +$a = new StorageRef([1, 2]); +list(,,list(&$var)) = $a; +var_dump($a); + +$a = new StorageRef(['one' => 1, 'two' => 2]); +['one' => &$one, 'two' => $two] = $a; +var_dump($a); + +?> +--EXPECTF-- +object(StorageRef)#1 (1) { + ["s":"StorageRef":private]=> + array(2) { + [0]=> + &int(1) + [1]=> + int(2) + } +} +object(StorageRef)#2 (1) { + ["s":"StorageRef":private]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + NULL + } +} +object(StorageRef)#1 (1) { + ["s":"StorageRef":private]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + array(1) { + [0]=> + &NULL + } + } +} +object(StorageRef)#2 (1) { + ["s":"StorageRef":private]=> + array(2) { + ["one"]=> + &int(1) + ["two"]=> + int(2) + } +} diff --git a/Zend/tests/list/list_reference_008.phpt b/Zend/tests/list/list_reference_008.phpt new file mode 100644 index 0000000000..9c754a698d --- /dev/null +++ b/Zend/tests/list/list_reference_008.phpt @@ -0,0 +1,68 @@ +--TEST-- +"Reference Unpacking - Oddities" list() +--FILE-- +<?php +$a = 1; +$b =& $a; +$arr = [&$a, &$b]; +list(&$a, &$b) = $arr; +var_dump($a, $b, $arr); +$b++; +var_dump($a, $b, $arr); +unset($a, $b, $arr); + +/* + * $a is first set as a reference to the 0'th elem, '1' + * $a is then set to the value of the 1'st elem, '2' + * $arr would look like, [2,2] + * Increment $a, and it should be [3, 2] + */ +$arr = [1, 2]; +list(&$a, $a) = $arr; +var_dump($a); +$a++; +var_dump($arr); +unset($a, $arr); + +/* + * We do not allow references to the same variable of rhs. + */ +$a = [1, 2]; +$ref =& $a; +list(&$a, &$b) = $a; +var_dump($a, $b); +$a++; $b++; +var_dump($ref); +?> +--EXPECTF-- +int(1) +int(1) +array(2) { + [0]=> + &int(1) + [1]=> + &int(1) +} +int(2) +int(2) +array(2) { + [0]=> + &int(2) + [1]=> + &int(2) +} +int(2) +array(2) { + [0]=> + &int(3) + [1]=> + int(2) +} +int(1) +int(2) +array(2) { + [0]=> + &int(2) + [1]=> + &int(3) +} diff --git a/Zend/tests/list/list_reference_009.phpt b/Zend/tests/list/list_reference_009.phpt new file mode 100644 index 0000000000..f0adc1f088 --- /dev/null +++ b/Zend/tests/list/list_reference_009.phpt @@ -0,0 +1,47 @@ +--TEST-- +"Reference Unpacking - VM Safety" list() +--FILE-- +<?php +$ary = [[0, 1]]; +[[ + 0 => &$a, + ($ary["foo"] = 1) => &$b +]] = $ary; + +var_dump($ary, $a, $b); +unset($ary, $a, $b); + +$ary = [[0, 1]]; +[ + 0 => &$a, + ($ary["foo"] = 1) => &$b +] = $ary[0]; +var_dump($ary, $a, $b); +?> +--EXPECTF-- +array(2) { + [0]=> + array(2) { + [0]=> + &int(0) + [1]=> + &int(1) + } + ["foo"]=> + int(1) +} +int(0) +int(1) +array(2) { + [0]=> + array(2) { + [0]=> + &int(0) + [1]=> + &int(1) + } + ["foo"]=> + int(1) +} +int(0) +int(1) diff --git a/Zend/tests/list/list_reference_010.phpt b/Zend/tests/list/list_reference_010.phpt new file mode 100644 index 0000000000..8ceb344a33 --- /dev/null +++ b/Zend/tests/list/list_reference_010.phpt @@ -0,0 +1,8 @@ +--TEST-- +"Reference Unpacking - Compile Error (scalar)" list() +--FILE-- +<?php +list(&$foo) = [42]; +?> +--EXPECTF-- +Fatal error: Cannot assign reference to non referencable value in %s on line %d diff --git a/Zend/tests/list/list_reference_011.phpt b/Zend/tests/list/list_reference_011.phpt new file mode 100644 index 0000000000..405f34f227 --- /dev/null +++ b/Zend/tests/list/list_reference_011.phpt @@ -0,0 +1,9 @@ +--TEST-- +"Reference Unpacking - Compile Error (const)" list() +--FILE-- +<?php +const FOO = 10; +[&$f] = FOO; +?> +--EXPECTF-- +Fatal error: Cannot assign reference to non referencable value in %s on line %d |