laminas / laminas-stdlib

SPL extensions, array utilities, error handlers, and more
https://docs.laminas.dev/laminas-stdlib/
BSD 3-Clause "New" or "Revised" License
190 stars 40 forks source link

Extract method in Stdlib\PriorityQueue does not remove top element correctly #12

Closed lougv1 closed 3 years ago

lougv1 commented 3 years ago

Bug Report

laminas/laminas-stdlib version 3.2.1.

Summary

Extract method in PriorityQueue does not remove top element correctly

Current behavior

Method extract removes the extracted element from the queue's internal binary heap, but not from its internal array of items. The count method is based on the array of items, so after calling extract a call to count returns an incorrect value.

How to reproduce

Here is a failing unit test:

$lmnsPriorityQueue = new Stdlib\PriorityQueue();

$lmnsPriorityQueue->insert("abcd");
$lmnsPriorityQueue->insert("efgh");

$extracted = $lmnsPriorityQueue->extract();
$count = $lmnsPriorityQueue->count();

$this->assertEquals(1, $count);

The result of the test is the following:

Failed asserting that 2 matches expected 1. Expected: 1. Actual: 2.

Expected behavior

After extract is called, the result returned from method count should decrease by 1.

weierophinney commented 3 years ago

Fixed with #13.