xref: /PHP-5.4/ext/spl/tests/pqueue_001.phpt (revision 9205c4fd)
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===DONE===
63<?php exit(0); ?>
64--EXPECTF--
65Exception: Can't extract from an empty heap
662=>b
671=>a
680=>c
69EXTR_BOTH
702=>Array
71(
72    [data] => b
73    [priority] => 2
74)
75
761=>Array
77(
78    [data] => a
79    [priority] => 1
80)
81
820=>Array
83(
84    [data] => c
85    [priority] => 0
86)
87
88EXTR_DATA
892=>b
901=>a
910=>c
92EXTR_PRIORITY
932=>2
941=>1
950=>0
96===DONE===
97