php-src/tests/lang/foreachLoop.015.phpt
Dmitry Stogov 97fe15db43 Fix "forech" statemt behaviour according to https://wiki.php.net/rfc/php7_foreach
Squashed commit of the following:

commit 1e41295097
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Sat Jan 31 07:28:58 2015 +0300

    Generalize HashTableIterator API to allows its usage without involvement of HashTable.nInternalPonter

commit 5406f21b11
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 18:08:43 2015 +0300

    Reduced alghorithms complexity

commit b37f1d58d2
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 18:08:30 2015 +0300

    Fixed test name

commit fb2d079645
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 18:08:05 2015 +0300

    API cleanup

commit 08302c0d6d
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 14:20:46 2015 +0300

    Make array_splice() to preserve foreach hash position

commit cc4b7be41e
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 12:24:31 2015 +0300

    Make internal function, operation on array passed by reference, to preserve foreach hash position

commit 5aa9712b0a
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 09:49:35 2015 +0300

    Implement consistent behavior for foreach by value over plain object

commit 4c5b385ff5
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Fri Jan 30 07:56:37 2015 +0300

    More careful iterators update.

commit 721fc9e80d
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Thu Jan 29 21:43:28 2015 +0300

    Added new test

commit 15a23b1218
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Thu Jan 29 21:05:02 2015 +0300

    Reimplement iteration magic with HashTableIterators (see https://wiki.php.net/rfc/php7_foreach#implementation_details)

commit 10a3260b1f
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Thu Jan 29 21:04:44 2015 +0300

    New test

commit eef80c5837
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Jan 28 16:52:21 2015 +0300

    Fixed foreach by reference iteration over constant array

commit 61e7391873
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Jan 28 14:59:54 2015 +0300

    Fixed temporary variable re-allocation pass

commit 92e90c09f0
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Jan 28 12:44:57 2015 +0300

    Fixed operand destruction in case of exceptions in iterator

commit dd2a36a207
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Jan 28 10:02:34 2015 +0300

    Use GET_OP1_ZVAL_PTR_DEREF() (IS_TMP_VAR and IS_CONST can't be IS_REFERENCE)

commit 4638f7b914
Author: Dmitry Stogov <dmitry@zend.com>
Date:   Wed Jan 28 07:43:28 2015 +0300

    Change "foreach" statement behavior (this is just a PoC yet)

    - "foreach by value" don't relay on internal array/object pointer and doesnt perform array duplication. It just locks it incrementing reference counter. If the original array is modified by some code, the copy on write is performed and "foreach" still work with the old copy.

    - it makes no difference if array given to "foreach by value" is reference itself

    - "foreach by reference" still use internal array/object pointer and should work similar to PHP-5. (This id not completely implemented)
2015-02-12 13:57:12 +03:00

538 lines
10 KiB
PHP

--TEST--
Directly modifying a REFERENCED array when foreach'ing over it while using &$value syntax.
--FILE--
<?php
define('MAX_LOOPS',5);
function withRefValue($elements, $transform) {
echo "\n---( Array with $elements element(s): )---\n";
//Build array:
for ($i=0; $i<$elements; $i++) {
$a[] = "v.$i";
}
$counter=0;
$ref = &$a;
echo "--> State of referenced array before loop:\n";
var_dump($a);
echo "--> Do loop:\n";
foreach ($a as $k=>&$v) {
echo " iteration $counter: \$k=$k; \$v=$v\n";
eval($transform);
$counter++;
if ($counter>MAX_LOOPS) {
echo " ** Stuck in a loop! **\n";
break;
}
}
echo "--> State of array after loop:\n";
var_dump($a);
}
echo "\nPopping elements off end of a referenced array, using &\$value";
$transform = 'array_pop($a);';
withRefValue(1, $transform);
withRefValue(2, $transform);
withRefValue(3, $transform);
withRefValue(4, $transform);
echo "\n\n\nShift elements off start of a referenced array, using &\$value";
$transform = 'array_shift($a);';
withRefValue(1, $transform);
withRefValue(2, $transform);
withRefValue(3, $transform);
withRefValue(4, $transform);
echo "\n\n\nRemove current element of a referenced array, using &\$value";
$transform = 'unset($a[$k]);';
withRefValue(1, $transform);
withRefValue(2, $transform);
withRefValue(3, $transform);
withRefValue(4, $transform);
echo "\n\n\nAdding elements to the end of a referenced array, using &\$value";
$transform = 'array_push($a, "new.$counter");';
withRefValue(1, $transform);
withRefValue(2, $transform);
withRefValue(3, $transform);
withRefValue(4, $transform);
echo "\n\n\nAdding elements to the start of a referenced array, using &\$value";
$transform = 'array_unshift($a, "new.$counter");';
withRefValue(1, $transform);
withRefValue(2, $transform);
withRefValue(3, $transform);
withRefValue(4, $transform);
?>
--EXPECT--
Popping elements off end of a referenced array, using &$value
---( Array with 1 element(s): )---
--> State of referenced array before loop:
array(1) {
[0]=>
string(3) "v.0"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
--> State of array after loop:
array(0) {
}
---( Array with 2 element(s): )---
--> State of referenced array before loop:
array(2) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
--> State of array after loop:
array(1) {
[0]=>
&string(3) "v.0"
}
---( Array with 3 element(s): )---
--> State of referenced array before loop:
array(3) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=1; $v=v.1
--> State of array after loop:
array(1) {
[0]=>
string(3) "v.0"
}
---( Array with 4 element(s): )---
--> State of referenced array before loop:
array(4) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
[3]=>
string(3) "v.3"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=1; $v=v.1
--> State of array after loop:
array(2) {
[0]=>
string(3) "v.0"
[1]=>
&string(3) "v.1"
}
Shift elements off start of a referenced array, using &$value
---( Array with 1 element(s): )---
--> State of referenced array before loop:
array(1) {
[0]=>
string(3) "v.0"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
--> State of array after loop:
array(0) {
}
---( Array with 2 element(s): )---
--> State of referenced array before loop:
array(2) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=0; $v=v.1
--> State of array after loop:
array(0) {
}
---( Array with 3 element(s): )---
--> State of referenced array before loop:
array(3) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=0; $v=v.1
iteration 2: $k=0; $v=v.2
--> State of array after loop:
array(0) {
}
---( Array with 4 element(s): )---
--> State of referenced array before loop:
array(4) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
[3]=>
string(3) "v.3"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=0; $v=v.1
iteration 2: $k=0; $v=v.2
iteration 3: $k=0; $v=v.3
--> State of array after loop:
array(0) {
}
Remove current element of a referenced array, using &$value
---( Array with 1 element(s): )---
--> State of referenced array before loop:
array(1) {
[0]=>
string(3) "v.0"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
--> State of array after loop:
array(0) {
}
---( Array with 2 element(s): )---
--> State of referenced array before loop:
array(2) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=1; $v=v.1
--> State of array after loop:
array(0) {
}
---( Array with 3 element(s): )---
--> State of referenced array before loop:
array(3) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=1; $v=v.1
iteration 2: $k=2; $v=v.2
--> State of array after loop:
array(0) {
}
---( Array with 4 element(s): )---
--> State of referenced array before loop:
array(4) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
[3]=>
string(3) "v.3"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=1; $v=v.1
iteration 2: $k=2; $v=v.2
iteration 3: $k=3; $v=v.3
--> State of array after loop:
array(0) {
}
Adding elements to the end of a referenced array, using &$value
---( Array with 1 element(s): )---
--> State of referenced array before loop:
array(1) {
[0]=>
string(3) "v.0"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=1; $v=new.0
iteration 2: $k=2; $v=new.1
iteration 3: $k=3; $v=new.2
iteration 4: $k=4; $v=new.3
iteration 5: $k=5; $v=new.4
** Stuck in a loop! **
--> State of array after loop:
array(7) {
[0]=>
string(3) "v.0"
[1]=>
string(5) "new.0"
[2]=>
string(5) "new.1"
[3]=>
string(5) "new.2"
[4]=>
string(5) "new.3"
[5]=>
&string(5) "new.4"
[6]=>
string(5) "new.5"
}
---( Array with 2 element(s): )---
--> State of referenced array before loop:
array(2) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=1; $v=v.1
iteration 2: $k=2; $v=new.0
iteration 3: $k=3; $v=new.1
iteration 4: $k=4; $v=new.2
iteration 5: $k=5; $v=new.3
** Stuck in a loop! **
--> State of array after loop:
array(8) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(5) "new.0"
[3]=>
string(5) "new.1"
[4]=>
string(5) "new.2"
[5]=>
&string(5) "new.3"
[6]=>
string(5) "new.4"
[7]=>
string(5) "new.5"
}
---( Array with 3 element(s): )---
--> State of referenced array before loop:
array(3) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=1; $v=v.1
iteration 2: $k=2; $v=v.2
iteration 3: $k=3; $v=new.0
iteration 4: $k=4; $v=new.1
iteration 5: $k=5; $v=new.2
** Stuck in a loop! **
--> State of array after loop:
array(9) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
[3]=>
string(5) "new.0"
[4]=>
string(5) "new.1"
[5]=>
&string(5) "new.2"
[6]=>
string(5) "new.3"
[7]=>
string(5) "new.4"
[8]=>
string(5) "new.5"
}
---( Array with 4 element(s): )---
--> State of referenced array before loop:
array(4) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
[3]=>
string(3) "v.3"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=1; $v=v.1
iteration 2: $k=2; $v=v.2
iteration 3: $k=3; $v=v.3
iteration 4: $k=4; $v=new.0
iteration 5: $k=5; $v=new.1
** Stuck in a loop! **
--> State of array after loop:
array(10) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
[3]=>
string(3) "v.3"
[4]=>
string(5) "new.0"
[5]=>
&string(5) "new.1"
[6]=>
string(5) "new.2"
[7]=>
string(5) "new.3"
[8]=>
string(5) "new.4"
[9]=>
string(5) "new.5"
}
Adding elements to the start of a referenced array, using &$value
---( Array with 1 element(s): )---
--> State of referenced array before loop:
array(1) {
[0]=>
string(3) "v.0"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
--> State of array after loop:
array(2) {
[0]=>
string(5) "new.0"
[1]=>
&string(3) "v.0"
}
---( Array with 2 element(s): )---
--> State of referenced array before loop:
array(2) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=2; $v=v.1
--> State of array after loop:
array(4) {
[0]=>
string(5) "new.1"
[1]=>
string(5) "new.0"
[2]=>
string(3) "v.0"
[3]=>
&string(3) "v.1"
}
---( Array with 3 element(s): )---
--> State of referenced array before loop:
array(3) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=3; $v=v.2
--> State of array after loop:
array(5) {
[0]=>
string(5) "new.1"
[1]=>
string(5) "new.0"
[2]=>
string(3) "v.0"
[3]=>
string(3) "v.1"
[4]=>
&string(3) "v.2"
}
---( Array with 4 element(s): )---
--> State of referenced array before loop:
array(4) {
[0]=>
string(3) "v.0"
[1]=>
string(3) "v.1"
[2]=>
string(3) "v.2"
[3]=>
string(3) "v.3"
}
--> Do loop:
iteration 0: $k=0; $v=v.0
iteration 1: $k=4; $v=v.3
--> State of array after loop:
array(6) {
[0]=>
string(5) "new.1"
[1]=>
string(5) "new.0"
[2]=>
string(3) "v.0"
[3]=>
string(3) "v.1"
[4]=>
string(3) "v.2"
[5]=>
&string(3) "v.3"
}