Repository navigation
Expand file tree
/
Copy pathArrayObject_separate_on_write.phpt
More file actions
82 lines (80 loc) · 4.17 KB
/
Copy pathArrayObject_separate_on_write.phpt
File metadata and controls
82 lines (80 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
--TEST--
ArrayObject and ArrayIterator separate their storage from the arrays they hand out before writing to it
--FILE--
<?php
// __serialize() and __debugInfo() hand out the storage without copying it:
// every write path separates the storage first
function check(string $label, callable $write, array $classes = ['ArrayIterator', 'ArrayObject']) {
foreach ($classes as $class) {
$expected = ['a' => 1, 'b' => ['x' => 1], 'c' => 3];
$object = new $class($expected, ArrayObject::ARRAY_AS_PROPS);
$serialized = $object->__serialize()[1];
$debug = array_values($object->__debugInfo())[0];
$write($object);
if ($serialized !== $expected || $debug !== $expected) {
echo "$class $label: FAIL\n";
var_dump($serialized, $debug);
}
echo "$class $label: ", json_encode($object->getArrayCopy()), "\n";
}
}
check('offsetSet', function ($o) { $o['a'] = 2; });
check('offsetSet()', function ($o) { $o->offsetSet('a', 2); });
check('append', function ($o) { $o[] = 4; });
check('append()', function ($o) { $o->append(4); });
check('offsetUnset', function ($o) { unset($o['a']); });
check('offsetUnset() missing', function ($o) { $o->offsetUnset('z'); });
check('compound assign', function ($o) { $o['a'] += 10; });
check('increment', function ($o) { $o['a']++; });
check('nested write', function ($o) { $o['b']['y'] = 2; });
check('nested append', function ($o) { $o['b'][] = 2; });
check('nested unset', function ($o) { unset($o['b']['x']); });
check('reference', function ($o) { $r = &$o['a']; $r = 5; });
check('property', function ($o) { $o->a = 6; });
check('property compound assign', function ($o) { $o->a .= 'x'; });
check('property nested write', function ($o) { $o->b['z'] = 3; });
check('property reference', function ($o) { $r = &$o->c; $r = 7; });
check('property unset', function ($o) { unset($o->c); });
check('foreach by reference', function ($o) { foreach ($o as &$v) { $v = 8; } });
check('uasort', function ($o) { $o->uasort(fn ($a, $b) => is_array($a) <=> is_array($b)); }, ['ArrayObject']);
check('ksort', function ($o) { $o->ksort(); $o['d'] = 0; }, ['ArrayObject']);
?>
--EXPECT--
ArrayIterator offsetSet: {"a":2,"b":{"x":1},"c":3}
ArrayObject offsetSet: {"a":2,"b":{"x":1},"c":3}
ArrayIterator offsetSet(): {"a":2,"b":{"x":1},"c":3}
ArrayObject offsetSet(): {"a":2,"b":{"x":1},"c":3}
ArrayIterator append: {"a":1,"b":{"x":1},"c":3,"0":4}
ArrayObject append: {"a":1,"b":{"x":1},"c":3,"0":4}
ArrayIterator append(): {"a":1,"b":{"x":1},"c":3,"0":4}
ArrayObject append(): {"a":1,"b":{"x":1},"c":3,"0":4}
ArrayIterator offsetUnset: {"b":{"x":1},"c":3}
ArrayObject offsetUnset: {"b":{"x":1},"c":3}
ArrayIterator offsetUnset() missing: {"a":1,"b":{"x":1},"c":3}
ArrayObject offsetUnset() missing: {"a":1,"b":{"x":1},"c":3}
ArrayIterator compound assign: {"a":11,"b":{"x":1},"c":3}
ArrayObject compound assign: {"a":11,"b":{"x":1},"c":3}
ArrayIterator increment: {"a":2,"b":{"x":1},"c":3}
ArrayObject increment: {"a":2,"b":{"x":1},"c":3}
ArrayIterator nested write: {"a":1,"b":{"x":1,"y":2},"c":3}
ArrayObject nested write: {"a":1,"b":{"x":1,"y":2},"c":3}
ArrayIterator nested append: {"a":1,"b":{"x":1,"0":2},"c":3}
ArrayObject nested append: {"a":1,"b":{"x":1,"0":2},"c":3}
ArrayIterator nested unset: {"a":1,"b":[],"c":3}
ArrayObject nested unset: {"a":1,"b":[],"c":3}
ArrayIterator reference: {"a":5,"b":{"x":1},"c":3}
ArrayObject reference: {"a":5,"b":{"x":1},"c":3}
ArrayIterator property: {"a":6,"b":{"x":1},"c":3}
ArrayObject property: {"a":6,"b":{"x":1},"c":3}
ArrayIterator property compound assign: {"a":"1x","b":{"x":1},"c":3}
ArrayObject property compound assign: {"a":"1x","b":{"x":1},"c":3}
ArrayIterator property nested write: {"a":1,"b":{"x":1,"z":3},"c":3}
ArrayObject property nested write: {"a":1,"b":{"x":1,"z":3},"c":3}
ArrayIterator property reference: {"a":1,"b":{"x":1},"c":7}
ArrayObject property reference: {"a":1,"b":{"x":1},"c":7}
ArrayIterator property unset: {"a":1,"b":{"x":1}}
ArrayObject property unset: {"a":1,"b":{"x":1}}
ArrayIterator foreach by reference: {"a":8,"b":8,"c":8}
ArrayObject foreach by reference: {"a":8,"b":8,"c":8}
ArrayObject uasort: {"a":1,"c":3,"b":{"x":1}}
ArrayObject ksort: {"a":1,"b":{"x":1},"c":3,"d":0}