xref: /PHP-8.0/ext/spl/tests/fixedarray_021.phpt (revision 063fdd94)
1--TEST--
2SPL: FixedArray: misc small tests
3--FILE--
4<?php
5
6/* empty count */
7$a = new SplFixedArray();
8
9var_dump(count($a));
10var_dump($a->count());
11
12/* negative init value */
13try {
14    $b = new SplFixedArray(-10);
15} catch (\ValueError $e) {
16    echo $e->getMessage() . \PHP_EOL;
17}
18
19/* resize and negative value */
20$b = new SplFixedArray();
21try {
22    $b->setSize(-5);
23} catch (\ValueError $e) {
24    echo $e->getMessage() . \PHP_EOL;
25}
26
27/* calling __construct() twice */
28$c = new SplFixedArray(0);
29var_dump($c->__construct());
30
31/* fromArray() from empty array */
32$d = new SplFixedArray();
33$d->fromArray(array());
34
35var_dump(count($a));
36var_dump($a->count());
37var_dump($a);
38
39/* foreach by ref */
40$e = new SplFixedArray(10);
41$e[0] = 1;
42$e[1] = 5;
43$e[2] = 10;
44
45try {
46    foreach ($e as $k=>&$v) {
47        var_dump($v);
48    }
49} catch (\Error $e) {
50    var_dump($e->getMessage());
51}
52
53//non-long indexes
54$a = new SplFixedArray(4);
55$a["2"] = "foo";
56$a["1"] = "foo";
57$a["3"] = "0";
58
59var_dump(isset($a["0"], $a[-1]), $a["1"]);
60var_dump(empty($a["3"]));
61
62?>
63--EXPECTF--
64int(0)
65int(0)
66SplFixedArray::__construct(): Argument #1 ($size) must be greater than or equal to 0
67SplFixedArray::setSize(): Argument #1 ($size) must be greater than or equal to 0
68NULL
69int(0)
70int(0)
71object(SplFixedArray)#%d (0) {
72}
73string(52) "An iterator cannot be used with foreach by reference"
74bool(false)
75string(3) "foo"
76bool(true)
77