Skip to content

Commit 2e27775

Browse files
committed
SPL Heap stuff
1 parent aa4e638 commit 2e27775

8 files changed

+51
-47
lines changed

ext/spl/spl_heap.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ static zend_object *spl_heap_object_new_ex(zend_class_entry *class_type, zend_ob
434434
}
435435

436436
if (!parent) { /* this must never happen */
437-
php_error_docref(NULL, E_COMPILE_ERROR, "Internal compiler error, Class is not child of SplHeap");
437+
zend_throw_error(NULL, "Internal compiler error, Class is not child of SplHeap");
438438
}
439439

440440
if (inherited) {
@@ -602,7 +602,7 @@ SPL_METHOD(SplHeap, insert)
602602
intern = Z_SPLHEAP_P(ZEND_THIS);
603603

604604
if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
605-
zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
605+
zend_throw_error(NULL, "Heap is corrupted, heap properties are no longer ensured");
606606
RETURN_THROWS();
607607
}
608608

@@ -626,11 +626,12 @@ SPL_METHOD(SplHeap, extract)
626626
intern = Z_SPLHEAP_P(ZEND_THIS);
627627

628628
if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
629-
zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
629+
zend_throw_error(NULL, "Heap is corrupted, heap properties are no longer ensured");
630630
RETURN_THROWS();
631631
}
632632

633633
if (spl_ptr_heap_delete_top(intern->heap, return_value, ZEND_THIS) == FAILURE) {
634+
/* TODO change normal Error? */
634635
zend_throw_exception(spl_ce_RuntimeException, "Can't extract from an empty heap", 0);
635636
RETURN_THROWS();
636637
}
@@ -652,7 +653,7 @@ SPL_METHOD(SplPriorityQueue, insert)
652653
intern = Z_SPLHEAP_P(ZEND_THIS);
653654

654655
if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
655-
zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
656+
zend_throw_error(NULL, "Heap is corrupted, heap properties are no longer ensured");
656657
RETURN_THROWS();
657658
}
658659

@@ -679,7 +680,7 @@ SPL_METHOD(SplPriorityQueue, extract)
679680
intern = Z_SPLHEAP_P(ZEND_THIS);
680681

681682
if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
682-
zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
683+
zend_throw_error(NULL, "Heap is corrupted, heap properties are no longer ensured");
683684
RETURN_THROWS();
684685
}
685686

@@ -707,7 +708,7 @@ SPL_METHOD(SplPriorityQueue, top)
707708
intern = Z_SPLHEAP_P(ZEND_THIS);
708709

709710
if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
710-
zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
711+
zend_throw_error(NULL, "Heap is corrupted, heap properties are no longer ensured");
711712
RETURN_THROWS();
712713
}
713714

@@ -736,7 +737,7 @@ SPL_METHOD(SplPriorityQueue, setExtractFlags)
736737

737738
value &= SPL_PQUEUE_EXTR_MASK;
738739
if (!value) {
739-
zend_throw_exception(spl_ce_RuntimeException, "Must specify at least one extract flag", 0);
740+
zend_argument_value_error(1, "must specify at least one extract flag", 0);
740741
RETURN_THROWS();
741742
}
742743

@@ -824,7 +825,7 @@ SPL_METHOD(SplHeap, top)
824825
intern = Z_SPLHEAP_P(ZEND_THIS);
825826

826827
if (intern->heap->flags & SPL_HEAP_CORRUPTED) {
827-
zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
828+
zend_throw_error(NULL, "Heap is corrupted, heap properties are no longer ensured");
828829
RETURN_THROWS();
829830
}
830831

@@ -893,7 +894,7 @@ static zval *spl_heap_it_get_current_data(zend_object_iterator *iter) /* {{{ */
893894
spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
894895

895896
if (object->heap->flags & SPL_HEAP_CORRUPTED) {
896-
zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
897+
zend_throw_error(NULL, "Heap is corrupted, heap properties are no longer ensured");
897898
return NULL;
898899
}
899900

@@ -911,7 +912,7 @@ static zval *spl_pqueue_it_get_current_data(zend_object_iterator *iter) /* {{{ *
911912
spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
912913

913914
if (object->heap->flags & SPL_HEAP_CORRUPTED) {
914-
zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
915+
zend_throw_error(NULL, "Heap is corrupted, heap properties are no longer ensured");
915916
return NULL;
916917
}
917918

@@ -940,7 +941,7 @@ static void spl_heap_it_move_forward(zend_object_iterator *iter) /* {{{ */
940941
spl_heap_object *object = Z_SPLHEAP_P(&iter->data);
941942

942943
if (object->heap->flags & SPL_HEAP_CORRUPTED) {
943-
zend_throw_exception(spl_ce_RuntimeException, "Heap is corrupted, heap properties are no longer ensured.", 0);
944+
zend_throw_error(NULL, "Heap is corrupted, heap properties are no longer ensured");
944945
return;
945946
}
946947

@@ -1087,7 +1088,7 @@ zend_object_iterator *spl_heap_get_iterator(zend_class_entry *ce, zval *object,
10871088
spl_heap_object *heap_object = Z_SPLHEAP_P(object);
10881089

10891090
if (by_ref) {
1090-
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
1091+
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
10911092
return NULL;
10921093
}
10931094

@@ -1112,7 +1113,7 @@ zend_object_iterator *spl_pqueue_get_iterator(zend_class_entry *ce, zval *object
11121113
spl_heap_object *heap_object = Z_SPLHEAP_P(object);
11131114

11141115
if (by_ref) {
1115-
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
1116+
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
11161117
return NULL;
11171118
}
11181119

ext/spl/tests/SplPriorityQueue_setExtractFlags_zero.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ Setting SplPriorityQueue extract flags to zero generates an exception
44
<?php
55

66
$queue = new SplPriorityQueue();
7-
$queue->setExtractFlags(0);
7+
try {
8+
$queue->setExtractFlags(0);
9+
} catch (\ValueError $e) {
10+
echo $e->getMessage() . \PHP_EOL;
11+
}
812

913
?>
10-
--EXPECTF--
11-
Fatal error: Uncaught RuntimeException: Must specify at least one extract flag in %s:%d
12-
Stack trace:
13-
#0 %s(%d): SplPriorityQueue->setExtractFlags(0)
14-
#1 {main}
15-
thrown in %s on line %d
14+
--EXPECT--
15+
SplPriorityQueue::setExtractFlags(): Argument #1 ($flags) must specify at least one extract flag

ext/spl/tests/heap_004.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ try {
2424
try {
2525
$h->insert(4);
2626
echo "inserted 4\n";
27-
} catch(Exception $e) {
27+
} catch(Error $e) {
2828
echo "Exception: ".$e->getMessage()."\n";
2929
}
3030

3131
try {
3232
var_dump($h->extract());
33-
} catch(Exception $e) {
33+
} catch(Error $e) {
3434
echo "Exception: ".$e->getMessage()."\n";
3535
}
3636
try {
3737
var_dump($h->extract());
38-
} catch(Exception $e) {
38+
} catch(Error $e) {
3939
echo "Exception: ".$e->getMessage()."\n";
4040
}
4141

@@ -56,9 +56,9 @@ try {
5656
--EXPECT--
5757
inserted 1
5858
Exception: foo
59-
Exception: Heap is corrupted, heap properties are no longer ensured.
60-
Exception: Heap is corrupted, heap properties are no longer ensured.
61-
Exception: Heap is corrupted, heap properties are no longer ensured.
59+
Exception: Heap is corrupted, heap properties are no longer ensured
60+
Exception: Heap is corrupted, heap properties are no longer ensured
61+
Exception: Heap is corrupted, heap properties are no longer ensured
6262
Recovering..
6363
int(1)
6464
int(2)

ext/spl/tests/heap_009.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,44 @@ Thomas Koch <thomas@koch.ro>
55
#Hackday Webtuesday 2008-05-24
66
--FILE--
77
<?php
8-
function testForException( $heap )
8+
function testForeachReferenceError( $heap )
99
{
1010
try
1111
{
1212
foreach( $heap as &$item );
1313
}
14-
catch( RuntimeException $e )
14+
catch( Error $e )
1515
{
1616
echo $e->getMessage(),"\n";
1717
}
1818
}
1919

2020
// 1. SplMinHeap empty
2121
$heap = new SplMinHeap;
22-
testForException( $heap );
22+
testForeachReferenceError( $heap );
2323

2424
// 2. SplMinHeap non-empty
2525
$heap = new SplMinHeap;
2626
$heap->insert( 1 );
27-
testForException( $heap );
27+
testForeachReferenceError( $heap );
2828

2929
// 3. SplMaxHeap empty
3030
$heap = new SplMaxHeap;
31-
testForException( $heap );
31+
testForeachReferenceError( $heap );
3232

3333
// 4. SplMaxHeap non-empty
3434
$heap = new SplMaxHeap;
3535
$heap->insert( 1 );
36-
testForException( $heap );
36+
testForeachReferenceError( $heap );
3737

3838
// 5. SplPriorityQueue empty
3939
$heap = new SplPriorityQueue;
40-
testForException( $heap );
40+
testForeachReferenceError( $heap );
4141

4242
// 6. SplPriorityQueue non-empty
4343
$heap = new SplPriorityQueue;
4444
$heap->insert( 1, 2 );
45-
testForException( $heap );
45+
testForeachReferenceError( $heap );
4646

4747
?>
4848
--EXPECT--

ext/spl/tests/heap_corruption.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ catch (Exception $e) {
5454
try {
5555
$heap->top();
5656
}
57-
catch (Exception $e) {
57+
catch (Error $e) {
5858
echo "Corruption Exception: " . $e->getMessage() . PHP_EOL;
5959
}
6060

@@ -65,6 +65,6 @@ var_dump($heap->isCorrupted());
6565
--EXPECT--
6666
bool(false)
6767
Compare Exception: Compare exception
68-
Corruption Exception: Heap is corrupted, heap properties are no longer ensured.
68+
Corruption Exception: Heap is corrupted, heap properties are no longer ensured
6969
bool(true)
7070
bool(false)

ext/spl/tests/heap_top_variation_002.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ $h = new SplMinHeap2();
1818
$h->insert(4);
1919
try {
2020
$h->insert(5);
21-
} catch (Exception $e) {}
21+
} catch (Exception $e) {
22+
echo $e->getMessage() . \PHP_EOL;
23+
}
2224

2325
// call top, should fail with corrupted heap
2426
try {
2527
$h->top();
26-
} catch (Exception $e) {
28+
} catch (Error $e) {
2729
echo $e->getMessage();
2830
}
2931
?>
3032
--EXPECT--
31-
Heap is corrupted, heap properties are no longer ensured.
33+
Corrupt heap
34+
Heap is corrupted, heap properties are no longer ensured

ext/spl/tests/pqueue_002.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ try {
2424
try {
2525
$h->insert(4, 1);
2626
echo "inserted 4\n";
27-
} catch(Exception $e) {
27+
} catch(Error $e) {
2828
echo "Exception: ".$e->getMessage()."\n";
2929
}
3030

3131
try {
3232
var_dump($h->extract());
33-
} catch(Exception $e) {
33+
} catch(Error $e) {
3434
echo "Exception: ".$e->getMessage()."\n";
3535
}
3636
try {
3737
var_dump($h->extract());
38-
} catch(Exception $e) {
38+
} catch(Error $e) {
3939
echo "Exception: ".$e->getMessage()."\n";
4040
}
4141

@@ -56,9 +56,9 @@ try {
5656
--EXPECT--
5757
inserted 1
5858
Exception: foo
59-
Exception: Heap is corrupted, heap properties are no longer ensured.
60-
Exception: Heap is corrupted, heap properties are no longer ensured.
61-
Exception: Heap is corrupted, heap properties are no longer ensured.
59+
Exception: Heap is corrupted, heap properties are no longer ensured
60+
Exception: Heap is corrupted, heap properties are no longer ensured
61+
Exception: Heap is corrupted, heap properties are no longer ensured
6262
Recovering..
6363
int(1)
6464
int(2)

ext/spl/tests/spl_pq_top_error_corrupt.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ try {
2929

3030
try {
3131
$priorityQueue->top();
32-
} catch (RuntimeException $e) {
32+
} catch (Error $e) {
3333
echo "Exception: ".$e->getMessage().PHP_EOL;
3434
}
3535

3636
?>
3737
--EXPECT--
38-
Exception: Heap is corrupted, heap properties are no longer ensured.
38+
Exception: Heap is corrupted, heap properties are no longer ensured

0 commit comments

Comments
 (0)