Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 🧪 improved coverage for PQ and BinaryHeap #24

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/tests/binary-heap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,83 @@ Deno.test('BinaryHeap - Additional Coverage Tests', async (t) => {
assertEquals(heap.peek().getValue(), 5);
});
});

Deno.test('BinaryHeap - Coverage Enhancement Tests', async (t) => {
// Test for invalid comparison type
await t.step('should throw error for unsupported type comparison', () => {
const heap = new MinHeap<{ x: number }>();

// First insert will succeed because no comparison is needed yet
heap.insert({ x: 1 });

// Second insert will trigger comparison and throw error
assertThrows(
() => heap.insert({ x: 2 }),
Error,
'No valid comparison method found for type',
);
});

// Test for right child being smallest in MinHeap siftDown
await t.step(
'should handle right child being smallest in MinHeap siftDown',
() => {
const heap = new MinHeap<number>();

// Insert elements in a specific order to create the desired heap structure
heap.insert(5); // root
heap.insert(4); // left child
heap.insert(2); // right child

// At this point, the heap should look like:
// 2
// / \
// 4 5

// Verify the structure after sifting
assertEquals(heap.peek(), 2);

// Remove the root to trigger siftDown
assertEquals(heap.remove(), 2);

// After removal and siftDown, verify the new structure
assertEquals(heap.peek(), 4);
assertEquals(heap.remove(), 4);
assertEquals(heap.peek(), 5);

// Verify final size
assertEquals(heap.size, 1);
},
);

// Additional test to ensure complete coverage of right child comparison
await t.step('should handle complex right child siftDown scenarios', () => {
const heap = new MinHeap<number>();

// Create a more complex heap structure
heap.insert(10); // Will become root
heap.insert(8); // Will become left child
heap.insert(3); // Will become right child
heap.insert(9); // Additional nodes
heap.insert(7);
heap.insert(1);

// The heap will rebalance with 1 at the root
assertEquals(heap.peek(), 1);

// Remove root to trigger complex siftDown
heap.remove();

// Verify the structure maintains heap property
assertEquals(heap.peek(), 3);

// Remove elements to verify complete structure
const elements = [];
while (!heap.isEmpty()) {
elements.push(heap.remove());
}

// Verify elements come out in ascending order
assertEquals(elements, [3, 7, 8, 9, 10]);
});
});
3 changes: 3 additions & 0 deletions src/tests/priority-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ Deno.test('PriorityQueue operations', async (t) => {
queue.enqueue('hi');
queue.enqueue('greetings');
queue.enqueue('hey');
assertEquals(queue.toArray(), ['hi', 'hey', 'greetings', 'hello']);
const values = [...queue];
assertEquals(values, ['hi', 'hey', 'greetings', 'hello']);

assertEquals(queue.dequeue(), 'hi');
assertEquals(queue.dequeue(), 'hey');
Expand Down