File: SplObjectStorage_seek.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (139 lines) | stat: -rw-r--r-- 2,263 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
--TEST--
SplObjectStorage::seek() basic functionality
--FILE--
<?php

class Test {
    public function __construct(public string $marker) {}
}

$a = new Test("a");
$b = new Test("b");
$c = new Test("c");
$d = new Test("d");
$e = new Test("e");

$storage = new SplObjectStorage();
$storage[$a] = 1;
$storage[$b] = 2;
$storage[$c] = 3;
$storage[$d] = 4;
$storage[$e] = 5;

echo "--- Error cases ---\n";

try {
    $storage->seek(-1);
} catch (OutOfBoundsException $e) {
    echo $e->getMessage(), "\n";
}
try {
    $storage->seek(5);
} catch (OutOfBoundsException $e) {
    echo $e->getMessage(), "\n";
}

var_dump($storage->key());
var_dump($storage->current());

echo "--- Normal cases ---\n";

$storage->seek(2);
var_dump($storage->key());
var_dump($storage->current());

$storage->seek(1);
var_dump($storage->key());
var_dump($storage->current());

$storage->seek(4);
var_dump($storage->key());
var_dump($storage->current());

$storage->seek(0);
var_dump($storage->key());
var_dump($storage->current());

$storage->seek(3);
var_dump($storage->key());
var_dump($storage->current());

$storage->seek(3);
var_dump($storage->key());
var_dump($storage->current());

echo "--- With holes cases ---\n";

$storage->detach($b);
$storage->detach($d);

foreach (range(0, 2) as $index) {
    $storage->seek($index);
    var_dump($storage->key());
    var_dump($storage->current());
}

try {
    $storage->seek(3);
} catch (OutOfBoundsException $e) {
    echo $e->getMessage(), "\n";
}

?>
--EXPECT--
--- Error cases ---
Seek position -1 is out of range
Seek position 5 is out of range
int(0)
object(Test)#1 (1) {
  ["marker"]=>
  string(1) "a"
}
--- Normal cases ---
int(2)
object(Test)#3 (1) {
  ["marker"]=>
  string(1) "c"
}
int(1)
object(Test)#2 (1) {
  ["marker"]=>
  string(1) "b"
}
int(4)
object(Test)#5 (1) {
  ["marker"]=>
  string(1) "e"
}
int(0)
object(Test)#1 (1) {
  ["marker"]=>
  string(1) "a"
}
int(3)
object(Test)#4 (1) {
  ["marker"]=>
  string(1) "d"
}
int(3)
object(Test)#4 (1) {
  ["marker"]=>
  string(1) "d"
}
--- With holes cases ---
int(0)
object(Test)#1 (1) {
  ["marker"]=>
  string(1) "a"
}
int(1)
object(Test)#3 (1) {
  ["marker"]=>
  string(1) "c"
}
int(2)
object(Test)#5 (1) {
  ["marker"]=>
  string(1) "e"
}
Seek position 3 is out of range