xref: /PHP-8.1/ext/spl/tests/pqueue_001.phpt (revision a555cc0b)
1--TEST--
2SPL: SplPriorityQueue: std operations and extract flags
3--FILE--
4<?php
5$pq = new SplPriorityQueue();
6
7// errors
8try {
9    $pq->extract();
10} catch (RuntimeException $e) {
11    echo "Exception: ".$e->getMessage()."\n";
12}
13
14$pq->insert("a", 1);
15$pq->insert("b", 2);
16$pq->insert("c", 0);
17
18foreach ($pq as $k=>$v) {
19    echo "$k=>".print_r($v, 1)."\n";
20}
21
22echo "EXTR_BOTH\n";
23
24$pq1 = new SplPriorityQueue();
25$pq1->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
26
27$pq1->insert("a", 1);
28$pq1->insert("b", 2);
29$pq1->insert("c", 0);
30
31foreach ($pq1 as $k=>$v) {
32    echo "$k=>".print_r($v, 1)."\n";
33}
34
35echo "EXTR_DATA\n";
36
37$pq2 = new SplPriorityQueue();
38$pq2->setExtractFlags(SplPriorityQueue::EXTR_DATA);
39
40$pq2->insert("a", 1);
41$pq2->insert("b", 2);
42$pq2->insert("c", 0);
43
44foreach ($pq2 as $k=>$v) {
45    echo "$k=>".print_r($v, 1)."\n";
46}
47
48echo "EXTR_PRIORITY\n";
49
50$pq3 = new SplPriorityQueue();
51$pq3->setExtractFlags(SplPriorityQueue::EXTR_PRIORITY);
52
53$pq3->insert("a", 1);
54$pq3->insert("b", 2);
55$pq3->insert("c", 0);
56
57foreach ($pq3 as $k=>$v) {
58    echo "$k=>".print_r($v, 1)."\n";
59}
60
61?>
62--EXPECT--
63Exception: Can't extract from an empty heap
642=>b
651=>a
660=>c
67EXTR_BOTH
682=>Array
69(
70    [data] => b
71    [priority] => 2
72)
73
741=>Array
75(
76    [data] => a
77    [priority] => 1
78)
79
800=>Array
81(
82    [data] => c
83    [priority] => 0
84)
85
86EXTR_DATA
872=>b
881=>a
890=>c
90EXTR_PRIORITY
912=>2
921=>1
930=>0
94