Enhance docs, add tests in LeftistHeap (#5982)

This commit is contained in:
Hardik Pawar
2024-10-26 20:58:55 +05:30
committed by GitHub
parent 709d9771e2
commit 20239f20f1
2 changed files with 133 additions and 37 deletions

View File

@@ -6,23 +6,80 @@ import org.junit.jupiter.api.Test;
public class LeftistHeapTest {
@Test
void testLeftistHeap() {
void testIsEmpty() {
LeftistHeap heap = new LeftistHeap();
Assertions.assertTrue(heap.isEmpty(), "Heap should be empty initially.");
heap.insert(10);
Assertions.assertFalse(heap.isEmpty(), "Heap should not be empty after insertion.");
heap.clear();
Assertions.assertTrue(heap.isEmpty(), "Heap should be empty after clearing.");
}
@Test
void testInsertAndExtractMin() {
LeftistHeap heap = new LeftistHeap();
Assertions.assertTrue(heap.isEmpty());
heap.insert(6);
Assertions.assertTrue(!heap.isEmpty());
heap.insert(2);
heap.insert(3);
heap.insert(1);
heap.inOrder();
Assertions.assertTrue(heap.inOrder().toString().equals("[6, 2, 3, 1]"));
Assertions.assertTrue(heap.extractMin() == 1);
Assertions.assertTrue(heap.inOrder().toString().equals("[6, 2, 3]"));
Assertions.assertEquals(1, heap.extractMin(), "Minimum should be 1.");
Assertions.assertEquals(2, heap.extractMin(), "Next minimum should be 2.");
Assertions.assertEquals(3, heap.extractMin(), "Next minimum should be 3.");
Assertions.assertEquals(6, heap.extractMin(), "Next minimum should be 6.");
Assertions.assertEquals(-1, heap.extractMin(), "Extracting from an empty heap should return -1.");
}
@Test
void testMerge() {
LeftistHeap heap1 = new LeftistHeap();
heap1.insert(1);
heap1.insert(3);
heap1.insert(5);
LeftistHeap heap2 = new LeftistHeap();
heap2.insert(2);
heap2.insert(4);
heap2.insert(6);
heap1.merge(heap2);
Assertions.assertEquals(1, heap1.extractMin(), "After merging, minimum should be 1.");
Assertions.assertEquals(2, heap1.extractMin(), "Next minimum should be 2.");
Assertions.assertEquals(3, heap1.extractMin(), "Next minimum should be 3.");
Assertions.assertEquals(4, heap1.extractMin(), "Next minimum should be 4.");
Assertions.assertEquals(5, heap1.extractMin(), "Next minimum should be 5.");
Assertions.assertEquals(6, heap1.extractMin(), "Next minimum should be 6.");
Assertions.assertEquals(-1, heap1.extractMin(), "Extracting from an empty heap should return -1.");
}
@Test
void testInOrderTraversal() {
LeftistHeap heap = new LeftistHeap();
heap.insert(10);
heap.insert(5);
heap.insert(20);
heap.insert(15);
heap.insert(30);
Assertions.assertEquals("[20, 15, 30, 5, 10]", heap.inOrder().toString(), "In-order traversal should match the expected output.");
}
@Test
void testMultipleExtractions() {
LeftistHeap heap = new LeftistHeap();
heap.insert(10);
heap.insert(5);
heap.insert(3);
heap.insert(8);
heap.insert(12);
heap.insert(4);
Assertions.assertTrue(heap.inOrder().toString().equals("[8, 3, 12, 2, 6, 4]"));
heap.clear();
Assertions.assertTrue(heap.isEmpty());
// Extract multiple elements
Assertions.assertEquals(3, heap.extractMin());
Assertions.assertEquals(5, heap.extractMin());
Assertions.assertEquals(8, heap.extractMin());
Assertions.assertEquals(10, heap.extractMin());
Assertions.assertEquals(-1, heap.extractMin(), "Extracting from an empty heap should return -1.");
}
}