Skip to content

Commit

Permalink
add intermixed add and remove test and fix sink bug
Browse files Browse the repository at this point in the history
  • Loading branch information
huiwang committed Oct 26, 2015
1 parent 0791414 commit c4a262c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PriorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function sink(data, compareFunction, index) {
&& compareFunction(data[targetIndex + 1], data[targetIndex]) < 0) {
targetIndex++;
}
if (compareFunction(data[index], data[targetIndex]) <= 0) {
if (compareFunction(value, data[targetIndex]) <= 0) {
break;
}
data[index] = data[targetIndex];
Expand Down
29 changes: 29 additions & 0 deletions test/PriorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ suite("PriorityQueue", () => {
equal(q.remove(), 4);
equal(q.size, 0);
});

test("min pq with intermixed add and remove", () => {
const q = PriorityQueue.newNaturalMin();
q.add(56);
q.add(73);
q.add(37);
q.add(53);
equal(q.remove(), 37);

q.add(57);
q.add(53);
equal(q.remove(), 53);
equal(q.remove(), 53);

q.add(60);
q.add(58);
q.add(72);
q.add(73);
equal(q.remove(), 56);
equal(q.remove(), 57);
equal(q.remove(), 58);
equal(q.remove(), 60);
equal(q.remove(), 72);
equal(q.remove(), 73);
equal(q.remove(), 73);
equal(q.size, 0);
});

test("natural max", () => {
const q = PriorityQueue.newNaturalMax();
q.add(3);
Expand Down Expand Up @@ -88,6 +116,7 @@ suite("PriorityQueue", () => {
equal(q.remove(), 1);
equal(q.remove(), 2);
});

test("chained add", () => {
const q = PriorityQueue.newNaturalMin();
q.add(3).add(1);
Expand Down

0 comments on commit c4a262c

Please sign in to comment.