|
| 1 | +package com.thealgorithms.datastructures.heaps; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +public class GenericHeapTest { |
| 12 | + |
| 13 | + private GenericHeap<Integer> heap; |
| 14 | + |
| 15 | + @BeforeEach |
| 16 | + public void setUp() { |
| 17 | + heap = new GenericHeap<>(); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testGenericHeapAddAndGet() { |
| 22 | + heap.add(19); |
| 23 | + heap.add(36); |
| 24 | + heap.add(100); |
| 25 | + heap.add(-17); |
| 26 | + heap.add(3); |
| 27 | + |
| 28 | + // Check that the largest element (100) is at the top of the heap |
| 29 | + assertEquals(100, heap.get()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testGenericHeapRemove() { |
| 34 | + heap.add(19); |
| 35 | + heap.add(36); |
| 36 | + heap.add(100); |
| 37 | + heap.add(-17); |
| 38 | + heap.add(3); |
| 39 | + |
| 40 | + // Verify that the largest element is removed correctly |
| 41 | + assertEquals(100, heap.remove()); |
| 42 | + |
| 43 | + // The new element at the top should be 36 |
| 44 | + assertEquals(36, heap.get()); |
| 45 | + |
| 46 | + // Check that the size is correct after removal |
| 47 | + assertEquals(4, heap.size()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testGenericHeapSize() { |
| 52 | + assertTrue(heap.isEmpty()); |
| 53 | + |
| 54 | + heap.add(10); |
| 55 | + heap.add(20); |
| 56 | + |
| 57 | + // Check that the size is correct |
| 58 | + assertEquals(2, heap.size()); |
| 59 | + |
| 60 | + heap.remove(); |
| 61 | + |
| 62 | + // After removal, the size should be 1 |
| 63 | + assertEquals(1, heap.size()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testGenericHeapIsEmpty() { |
| 68 | + // Verify that the heap is initially empty |
| 69 | + assertTrue(heap.isEmpty()); |
| 70 | + |
| 71 | + heap.add(15); |
| 72 | + |
| 73 | + // Now the heap should not be empty |
| 74 | + assertFalse(heap.isEmpty()); |
| 75 | + |
| 76 | + heap.remove(); |
| 77 | + |
| 78 | + // After removing the one element, it should be empty again |
| 79 | + assertTrue(heap.isEmpty()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testGenericHeapUpdatePriority() { |
| 84 | + heap.add(19); |
| 85 | + heap.add(36); |
| 86 | + heap.add(100); |
| 87 | + heap.add(-17); |
| 88 | + heap.add(3); |
| 89 | + |
| 90 | + // Verify that the largest element initially is 100 |
| 91 | + assertEquals(100, heap.get()); |
| 92 | + |
| 93 | + heap.remove(); |
| 94 | + |
| 95 | + // Simulates a change in priority by increasing the value of 100 to 44 |
| 96 | + heap.add(44); |
| 97 | + |
| 98 | + // Now, the new high should be 25 |
| 99 | + assertEquals(44, heap.get()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testGenericHeapRemoveUntilEmpty() { |
| 104 | + heap.add(5); |
| 105 | + heap.add(3); |
| 106 | + heap.add(4); |
| 107 | + heap.add(1); |
| 108 | + heap.add(2); |
| 109 | + |
| 110 | + // Remove all items and check that they are removed in descending order |
| 111 | + assertEquals(5, heap.remove()); |
| 112 | + assertEquals(4, heap.remove()); |
| 113 | + assertEquals(3, heap.remove()); |
| 114 | + assertEquals(2, heap.remove()); |
| 115 | + assertEquals(1, heap.remove()); |
| 116 | + |
| 117 | + // Empty heap |
| 118 | + assertTrue(heap.isEmpty()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testGenericHeapAddNullItem() { |
| 123 | + // Check null item |
| 124 | + assertThrows(IllegalArgumentException.class, () -> { heap.add(null); }); |
| 125 | + } |
| 126 | +} |
0 commit comments