summaryrefslogtreecommitdiff
path: root/Zend/tests
diff options
context:
space:
mode:
Diffstat (limited to 'Zend/tests')
-rw-r--r--Zend/tests/bug40509.phpt2
-rw-r--r--Zend/tests/bug40705.phpt2
-rw-r--r--Zend/tests/foreach_003.phpt71
-rw-r--r--Zend/tests/foreach_004.phpt65
-rw-r--r--Zend/tests/foreach_005.phpt22
-rw-r--r--Zend/tests/foreach_006.phpt20
-rw-r--r--Zend/tests/foreach_007.phpt13
-rw-r--r--Zend/tests/foreach_008.phpt21
-rw-r--r--Zend/tests/foreach_009.phpt40
-rw-r--r--Zend/tests/foreach_010.phpt40
-rw-r--r--Zend/tests/foreach_011.phpt19
-rw-r--r--Zend/tests/foreach_012.phpt18
-rw-r--r--Zend/tests/foreach_013.phpt17
-rw-r--r--Zend/tests/foreach_014.phpt15
-rw-r--r--Zend/tests/foreach_015.phpt18
-rw-r--r--Zend/tests/foreach_016.phpt18
-rw-r--r--Zend/tests/foreach_017.phpt111
17 files changed, 510 insertions, 2 deletions
diff --git a/Zend/tests/bug40509.phpt b/Zend/tests/bug40509.phpt
index 21eaae9444..65e32533ef 100644
--- a/Zend/tests/bug40509.phpt
+++ b/Zend/tests/bug40509.phpt
@@ -23,4 +23,4 @@ var_dump(key($arr["v"]));
int(0)
int(0)
int(0)
-NULL
+int(0)
diff --git a/Zend/tests/bug40705.phpt b/Zend/tests/bug40705.phpt
index 374f73b75e..8a679654d5 100644
--- a/Zend/tests/bug40705.phpt
+++ b/Zend/tests/bug40705.phpt
@@ -23,4 +23,4 @@ int(0)
int(0)
int(1)
int(2)
-NULL
+int(0)
diff --git a/Zend/tests/foreach_003.phpt b/Zend/tests/foreach_003.phpt
new file mode 100644
index 0000000000..71b0f2a5a3
--- /dev/null
+++ b/Zend/tests/foreach_003.phpt
@@ -0,0 +1,71 @@
+--TEST--
+Iterator exceptions in foreach by value
+--FILE--
+<?php
+class IT implements Iterator {
+ private $n = 0;
+ private $count = 0;
+ private $trap = null;
+
+ function __construct($count, $trap = null) {
+ $this->count = $count;
+ $this->trap = $trap;
+ }
+
+ function trap($trap) {
+ if ($trap === $this->trap) {
+ throw new Exception($trap);
+ }
+ }
+
+ function rewind() {$this->trap(__FUNCTION__); $this->n = 0;}
+ function valid() {$this->trap(__FUNCTION__); return $this->n < $this->count;}
+ function key() {$this->trap(__FUNCTION__); return $this->n;}
+ function current() {$this->trap(__FUNCTION__); return $this->n;}
+ function next() {$this->trap(__FUNCTION__); $this->n++;}
+}
+
+foreach(['rewind', 'valid', 'key', 'current', 'next'] as $trap) {
+ $obj = new IT(3, $trap);
+ try {
+ // IS_CV
+ foreach ($obj as $key => $val) echo "$val\n";
+ } catch (Exception $e) {
+ echo $e->getMessage() . "\n";
+ }
+ unset($obj);
+
+ try {
+ // IS_VAR
+ foreach (new IT(3, $trap) as $key => $val) echo "$val\n";
+ } catch (Exception $e) {
+ echo $e->getMessage() . "\n";
+ }
+
+ try {
+ // IS_TMP_VAR
+ foreach ((object)new IT(2, $trap) as $key => $val) echo "$val\n";
+ } catch (Exception $e) {
+ echo $e->getMessage() . "\n";
+ }
+}
+?>
+--EXPECT--
+rewind
+rewind
+rewind
+valid
+valid
+valid
+key
+key
+key
+current
+current
+current
+0
+next
+0
+next
+0
+next
diff --git a/Zend/tests/foreach_004.phpt b/Zend/tests/foreach_004.phpt
new file mode 100644
index 0000000000..1f754a77ed
--- /dev/null
+++ b/Zend/tests/foreach_004.phpt
@@ -0,0 +1,65 @@
+--TEST--
+Iterator exceptions in foreach by reference
+--FILE--
+<?php
+class IT extends ArrayIterator {
+ private $n = 0;
+
+ function __construct($trap = null) {
+ parent::__construct([0, 1]);
+ $this->trap = $trap;
+ }
+
+ function trap($trap) {
+ if ($trap === $this->trap) {
+ throw new Exception($trap);
+ }
+ }
+
+ function rewind() {$this->trap(__FUNCTION__); return parent::rewind();}
+ function valid() {$this->trap(__FUNCTION__); return parent::valid();}
+ function key() {$this->trap(__FUNCTION__); return parent::key();}
+ function next() {$this->trap(__FUNCTION__); return parent::next();}
+}
+
+foreach(['rewind', 'valid', 'key', 'next'] as $trap) {
+ $obj = new IT($trap);
+ try {
+ // IS_CV
+ foreach ($obj as $key => &$val) echo "$val\n";
+ } catch (Exception $e) {
+ echo $e->getMessage() . "\n";
+ }
+ unset($obj);
+
+ try {
+ // IS_VAR
+ foreach (new IT($trap) as $key => &$val) echo "$val\n";
+ } catch (Exception $e) {
+ echo $e->getMessage() . "\n";
+ }
+
+ try {
+ // IS_TMP_VAR
+ foreach ((object)new IT($trap) as $key => &$val) echo "$val\n";
+ } catch (Exception $e) {
+ echo $e->getMessage() . "\n";
+ }
+}
+?>
+--EXPECT--
+rewind
+rewind
+rewind
+valid
+valid
+valid
+key
+key
+key
+0
+next
+0
+next
+0
+next
diff --git a/Zend/tests/foreach_005.phpt b/Zend/tests/foreach_005.phpt
new file mode 100644
index 0000000000..6ed9fe940b
--- /dev/null
+++ b/Zend/tests/foreach_005.phpt
@@ -0,0 +1,22 @@
+--TEST--
+Nested foreach by reference on the same array
+--FILE--
+<?php
+$a = [1,2,3];
+foreach($a as &$x) {
+ foreach($a as &$y) {
+ echo "$x-$y\n";
+ $y++;
+ }
+}
+?>
+--EXPECT--
+1-1
+2-2
+2-3
+3-2
+3-3
+4-4
+5-3
+5-4
+5-5
diff --git a/Zend/tests/foreach_006.phpt b/Zend/tests/foreach_006.phpt
new file mode 100644
index 0000000000..65d6fdc52c
--- /dev/null
+++ b/Zend/tests/foreach_006.phpt
@@ -0,0 +1,20 @@
+--TEST--
+Foreach by reference on constant
+--FILE--
+<?php
+for ($i = 0; $i < 3; $i++) {
+ foreach ([1,2,3] as &$val) {
+ echo "$val\n";
+ }
+}
+?>
+--EXPECT--
+1
+2
+3
+1
+2
+3
+1
+2
+3
diff --git a/Zend/tests/foreach_007.phpt b/Zend/tests/foreach_007.phpt
new file mode 100644
index 0000000000..b99bc73ebe
--- /dev/null
+++ b/Zend/tests/foreach_007.phpt
@@ -0,0 +1,13 @@
+--TEST--
+Foreach by reference and inserting new element when we are already at the end
+--FILE--
+<?php
+$a = [1];
+foreach($a as &$v) {
+ echo "$v\n";
+ $a[1]=2;
+}
+?>
+--EXPECT--
+1
+2
diff --git a/Zend/tests/foreach_008.phpt b/Zend/tests/foreach_008.phpt
new file mode 100644
index 0000000000..c68bcd89da
--- /dev/null
+++ b/Zend/tests/foreach_008.phpt
@@ -0,0 +1,21 @@
+--TEST--
+Nested foreach by reference and array modification
+--FILE--
+<?php
+$a = [0, 1, 2, 3];
+foreach ($a as &$x) {
+ foreach ($a as &$y) {
+ echo "$x - $y\n";
+ if ($x == 0 && $y == 1) {
+ unset($a[2]);
+ unset($a[1]);
+ }
+ }
+}
+?>
+--EXPECT--
+0 - 0
+0 - 1
+0 - 3
+3 - 0
+3 - 3
diff --git a/Zend/tests/foreach_009.phpt b/Zend/tests/foreach_009.phpt
new file mode 100644
index 0000000000..6ce8384642
--- /dev/null
+++ b/Zend/tests/foreach_009.phpt
@@ -0,0 +1,40 @@
+--TEST--
+Nested foreach by reference and array modification with resize
+--FILE--
+<?php
+$a = [0, 1, 2, 3, 4, 5, 6, 7];
+unset($a[0], $a[1], $a[2], $a[3]);
+foreach ($a as &$ref) {
+ foreach ($a as &$ref2) {
+ echo "$ref-$ref2\n";
+ if ($ref == 5 && $ref2 == 6) {
+ $a[42] = 8;
+ }
+ }
+}
+?>
+--EXPECT--
+4-4
+4-5
+4-6
+4-7
+5-4
+5-5
+5-6
+5-7
+5-8
+6-4
+6-5
+6-6
+6-7
+6-8
+7-4
+7-5
+7-6
+7-7
+7-8
+8-4
+8-5
+8-6
+8-7
+8-8
diff --git a/Zend/tests/foreach_010.phpt b/Zend/tests/foreach_010.phpt
new file mode 100644
index 0000000000..6ba7e7e9fd
--- /dev/null
+++ b/Zend/tests/foreach_010.phpt
@@ -0,0 +1,40 @@
+--TEST--
+Nested foreach by value over object and object modification with resize
+--FILE--
+<?php
+$o = (object)['a'=>0, 'b'=>1, 'c'=>2, 'd'=>3, 'e'=>4, 'f'=>5, 'g'=>6, 'h'=>7];
+unset($o->a, $o->b, $o->c, $o->d);
+foreach ($o as $v1) {
+ foreach ($o as $v2) {
+ echo "$v1-$v2\n";
+ if ($v1 == 5 && $v2 == 6) {
+ $o->i = 8;
+ }
+ }
+}
+?>
+--EXPECT--
+4-4
+4-5
+4-6
+4-7
+5-4
+5-5
+5-6
+5-7
+5-8
+6-4
+6-5
+6-6
+6-7
+6-8
+7-4
+7-5
+7-6
+7-7
+7-8
+8-4
+8-5
+8-6
+8-7
+8-8
diff --git a/Zend/tests/foreach_011.phpt b/Zend/tests/foreach_011.phpt
new file mode 100644
index 0000000000..e91426fb27
--- /dev/null
+++ b/Zend/tests/foreach_011.phpt
@@ -0,0 +1,19 @@
+--TEST--
+sort() functions precerve foreach by reference iterator pointer
+--FILE--
+<?php
+$a = [1,2,3,4,5,0];
+foreach($a as &$v) {
+ echo "$v\n";
+ if ($v == 3) {
+ rsort($a);
+ }
+}
+?>
+--EXPECT--
+1
+2
+3
+2
+1
+0
diff --git a/Zend/tests/foreach_012.phpt b/Zend/tests/foreach_012.phpt
new file mode 100644
index 0000000000..5e5538cd4d
--- /dev/null
+++ b/Zend/tests/foreach_012.phpt
@@ -0,0 +1,18 @@
+--TEST--
+array_walk() function precerve foreach by reference iterator pointer
+--FILE--
+<?php
+$a = [1,2,3,4,5];
+foreach($a as &$v) {
+ echo "$v\n";
+ if ($v == 3) {
+ array_walk($a, function (&$x) {$x+=10;});
+ }
+}
+?>
+--EXPECT--
+1
+2
+3
+14
+15 \ No newline at end of file
diff --git a/Zend/tests/foreach_013.phpt b/Zend/tests/foreach_013.phpt
new file mode 100644
index 0000000000..cfbb3d7f79
--- /dev/null
+++ b/Zend/tests/foreach_013.phpt
@@ -0,0 +1,17 @@
+--TEST--
+array_push() function precerve foreach by reference iterator pointer
+--FILE--
+<?php
+$a = [1,2,3];
+foreach($a as &$v) {
+ echo "$v\n";
+ if ($v == 3) {
+ array_push($a, 4);
+ }
+}
+?>
+--EXPECT--
+1
+2
+3
+4
diff --git a/Zend/tests/foreach_014.phpt b/Zend/tests/foreach_014.phpt
new file mode 100644
index 0000000000..8d0ac582a9
--- /dev/null
+++ b/Zend/tests/foreach_014.phpt
@@ -0,0 +1,15 @@
+--TEST--
+array_pop() function precerve foreach by reference iterator pointer
+--FILE--
+<?php
+$a = [1,2,3];
+foreach($a as &$v) {
+ echo "$v\n";
+ if ($v == 2) {
+ array_pop($a);
+ }
+}
+?>
+--EXPECT--
+1
+2
diff --git a/Zend/tests/foreach_015.phpt b/Zend/tests/foreach_015.phpt
new file mode 100644
index 0000000000..adc8085f34
--- /dev/null
+++ b/Zend/tests/foreach_015.phpt
@@ -0,0 +1,18 @@
+--TEST--
+array_shift() function precerve foreach by reference iterator pointer
+--FILE--
+<?php
+$a = [1,2,3,4];
+foreach($a as &$v) {
+ echo "$v\n";
+ array_shift($a);
+}
+var_dump($a);
+?>
+--EXPECT--
+1
+2
+3
+4
+array(0) {
+} \ No newline at end of file
diff --git a/Zend/tests/foreach_016.phpt b/Zend/tests/foreach_016.phpt
new file mode 100644
index 0000000000..423c8dd0a6
--- /dev/null
+++ b/Zend/tests/foreach_016.phpt
@@ -0,0 +1,18 @@
+--TEST--
+array_unshift() function precerve foreach by reference iterator pointer
+--FILE--
+<?php
+$a = [1,2,3];
+foreach($a as &$v) {
+ echo "$v\n";
+ if ($v == 2) {
+ array_unshift($a, 0, 0, 0, 0, 0, 0, 0, 0);
+ }
+}
+var_dump(count($a));
+?>
+--EXPECT--
+1
+2
+3
+int(11)
diff --git a/Zend/tests/foreach_017.phpt b/Zend/tests/foreach_017.phpt
new file mode 100644
index 0000000000..55eeeb0891
--- /dev/null
+++ b/Zend/tests/foreach_017.phpt
@@ -0,0 +1,111 @@
+--TEST--
+array_splice() function precerve foreach by reference iterator pointer
+--FILE--
+<?php
+/* remove before */
+$done = 0;
+$a = [0,1,2,3,4];
+foreach($a as &$v) {
+ echo "$v\n";
+ if (!$done && $v == 3) {
+ $done = 1;
+ array_splice($a, 1, 2);
+ }
+}
+echo "\n";
+
+/* remove after */
+$done = 0;
+$a = [0,1,2,3,4];
+foreach($a as &$v) {
+ echo "$v\n";
+ if (!$done && $v == 0) {
+ $done = 1;
+ array_splice($a, 2, 2);
+ }
+}
+echo "\n";
+
+/* remove current */
+$done = 0;
+$a = [0,1,2,3,4];
+foreach($a as &$v) {
+ echo "$v\n";
+ if (!$done && $v == 2) {
+ $done = 1;
+ array_splice($a, 1, 3);
+ }
+}
+echo "\n";
+
+$replacement = ['x', 'y', 'z'];
+
+/* replace before */
+$done = 0;
+$a = [0,1,2,3,4];
+foreach($a as &$v) {
+ echo "$v\n";
+ if ($done && $v == 3) {
+ $done = 1;
+ array_splice($a, 1, 2, $replacement);
+ }
+}
+echo "\n";
+
+/* replace after */
+$done = 0;
+$a = [0,1,2,3,4];
+foreach($a as &$v) {
+ echo "$v\n";
+ if (!$done && $v == 0) {
+ $done = 1;
+ array_splice($a, 2, 2, $replacement);
+ }
+}
+echo "\n";
+
+/* replace current */
+$done = 0;
+$a = [0,1,2,3,4];
+foreach($a as &$v) {
+ echo "$v\n";
+ if (!$done && $v == 2) {
+ $done = 1;
+ array_splice($a, 1, 3, $replacement);
+ }
+}
+echo "\n";
+?>
+--EXPECT--
+0
+1
+2
+3
+4
+
+0
+1
+4
+
+0
+1
+2
+4
+
+0
+1
+2
+3
+4
+
+0
+1
+x
+y
+z
+4
+
+0
+1
+2
+4