xref: /PHP-8.3/ext/spl/tests/heap_009.phpt (revision 9affbef0)
1--TEST--
2SPL: SplHeap and friends, throw: An iterator cannot be used with foreach by reference
3--CREDITS--
4Thomas Koch <thomas@koch.ro>
5#Hackday Webtuesday 2008-05-24
6--FILE--
7<?php
8function testForException( $heap )
9{
10    try
11    {
12        foreach( $heap as &$item );
13    }
14    catch( \Error $e )
15    {
16        echo $e->getMessage(),"\n";
17    }
18}
19
20// 1. SplMinHeap empty
21$heap = new SplMinHeap;
22testForException( $heap );
23
24// 2. SplMinHeap non-empty
25$heap = new SplMinHeap;
26$heap->insert( 1 );
27testForException( $heap );
28
29// 3. SplMaxHeap empty
30$heap = new SplMaxHeap;
31testForException( $heap );
32
33// 4. SplMaxHeap non-empty
34$heap = new SplMaxHeap;
35$heap->insert( 1 );
36testForException( $heap );
37
38// 5. SplPriorityQueue empty
39$heap = new SplPriorityQueue;
40testForException( $heap );
41
42// 6. SplPriorityQueue non-empty
43$heap = new SplPriorityQueue;
44$heap->insert( 1, 2 );
45testForException( $heap );
46
47?>
48--EXPECT--
49An iterator cannot be used with foreach by reference
50An iterator cannot be used with foreach by reference
51An iterator cannot be used with foreach by reference
52An iterator cannot be used with foreach by reference
53An iterator cannot be used with foreach by reference
54An iterator cannot be used with foreach by reference
55