diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java index d546b7cc88d4..f1772b5b3112 100644 --- a/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java +++ b/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java @@ -9,6 +9,10 @@ public class GenericHeap> { HashMap map = new HashMap<>(); public void add(T item) { + if (item == null) { + throw new IllegalArgumentException("Cannot insert null into the heap."); + } + this.data.add(item); map.put(item, this.data.size() - 1); // upHeapify(this.data.size() - 1); diff --git a/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedArrayList.java b/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedArrayList.java index 99ab09f81c1c..1ed799a006d4 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedArrayList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedArrayList.java @@ -39,6 +39,10 @@ public static void main(String[] args) { * @param listC the result list after merging */ public static void merge(List listA, List listB, List listC) { + if (listA == null || listB == null || listC == null) { + throw new NullPointerException("Lists cannot be null."); + } + int pa = 0; /* the index of listA */ int pb = 0; diff --git a/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java b/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java new file mode 100644 index 000000000000..8915a6d8aef2 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java @@ -0,0 +1,126 @@ +package com.thealgorithms.datastructures.heaps; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class GenericHeapTest { + + private GenericHeap heap; + + @BeforeEach + public void setUp() { + heap = new GenericHeap<>(); + } + + @Test + public void testGenericHeapAddAndGet() { + heap.add(19); + heap.add(36); + heap.add(100); + heap.add(-17); + heap.add(3); + + // Check that the largest element (100) is at the top of the heap + assertEquals(100, heap.get()); + } + + @Test + public void testGenericHeapRemove() { + heap.add(19); + heap.add(36); + heap.add(100); + heap.add(-17); + heap.add(3); + + // Verify that the largest element is removed correctly + assertEquals(100, heap.remove()); + + // The new element at the top should be 36 + assertEquals(36, heap.get()); + + // Check that the size is correct after removal + assertEquals(4, heap.size()); + } + + @Test + public void testGenericHeapSize() { + assertTrue(heap.isEmpty()); + + heap.add(10); + heap.add(20); + + // Check that the size is correct + assertEquals(2, heap.size()); + + heap.remove(); + + // After removal, the size should be 1 + assertEquals(1, heap.size()); + } + + @Test + public void testGenericHeapIsEmpty() { + // Verify that the heap is initially empty + assertTrue(heap.isEmpty()); + + heap.add(15); + + // Now the heap should not be empty + assertFalse(heap.isEmpty()); + + heap.remove(); + + // After removing the one element, it should be empty again + assertTrue(heap.isEmpty()); + } + + @Test + public void testGenericHeapUpdatePriority() { + heap.add(19); + heap.add(36); + heap.add(100); + heap.add(-17); + heap.add(3); + + // Verify that the largest element initially is 100 + assertEquals(100, heap.get()); + + heap.remove(); + + // Simulates a change in priority by increasing the value of 100 to 44 + heap.add(44); + + // Now, the new high should be 25 + assertEquals(44, heap.get()); + } + + @Test + public void testGenericHeapRemoveUntilEmpty() { + heap.add(5); + heap.add(3); + heap.add(4); + heap.add(1); + heap.add(2); + + // Remove all items and check that they are removed in descending order + assertEquals(5, heap.remove()); + assertEquals(4, heap.remove()); + assertEquals(3, heap.remove()); + assertEquals(2, heap.remove()); + assertEquals(1, heap.remove()); + + // Empty heap + assertTrue(heap.isEmpty()); + } + + @Test + public void testGenericHeapAddNullItem() { + // Check null item + assertThrows(IllegalArgumentException.class, () -> { heap.add(null); }); + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/lists/MergeSortedArrayListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/MergeSortedArrayListTest.java new file mode 100644 index 000000000000..4619a8715a68 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/lists/MergeSortedArrayListTest.java @@ -0,0 +1,128 @@ +package com.thealgorithms.datastructures.lists; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + +public class MergeSortedArrayListTest { + + @Test + public void testMergeSortedWithSameSizeLists() { + List listA = Arrays.asList(1, 3, 5, 7, 9); + List listB = Arrays.asList(2, 4, 6, 8, 10); + List listC = new ArrayList<>(); + + MergeSortedArrayList.merge(listA, listB, listC); + + // Expected merged result + List expected = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + + // Assert the merged list is as expected + assertEquals(expected, listC); + } + + @Test + public void testMergeSortedWithEmptyListA() { + List listA = new ArrayList<>(); // Empty list + List listB = Arrays.asList(2, 4, 6, 8, 10); + List listC = new ArrayList<>(); + + MergeSortedArrayList.merge(listA, listB, listC); + + // Expected result is listB since listA is empty + List expected = Arrays.asList(2, 4, 6, 8, 10); + assertEquals(expected, listC); + } + + @Test + public void testMergeSortedWithEmptyListB() { + List listA = Arrays.asList(1, 3, 5, 7, 9); + List listB = new ArrayList<>(); // Empty list + List listC = new ArrayList<>(); + + MergeSortedArrayList.merge(listA, listB, listC); + + // Expected result is listA since listB is empty + List expected = Arrays.asList(1, 3, 5, 7, 9); + assertEquals(expected, listC); + } + + @Test + public void testMergeSortedWithBothEmptyLists() { + List listA = new ArrayList<>(); // Empty list + List listB = new ArrayList<>(); // Empty list + List listC = new ArrayList<>(); + + MergeSortedArrayList.merge(listA, listB, listC); + + // Expected result is an empty list since both inputs are empty + List expected = new ArrayList<>(); + assertEquals(expected, listC); + } + + @Test + public void testMergeSortedWithDifferentSizeLists() { + List listA = Arrays.asList(1, 3); + List listB = Arrays.asList(2, 4, 6, 8, 10); + List listC = new ArrayList<>(); + + MergeSortedArrayList.merge(listA, listB, listC); + + // Expected merged result + List expected = Arrays.asList(1, 2, 3, 4, 6, 8, 10); + assertEquals(expected, listC); + } + + @Test + public void testMergeSortedWithDuplicateElements() { + List listA = Arrays.asList(1, 3, 5); + List listB = Arrays.asList(1, 3, 5); + List listC = new ArrayList<>(); + + MergeSortedArrayList.merge(listA, listB, listC); + + // Expected merged result with duplicates + List expected = Arrays.asList(1, 1, 3, 3, 5, 5); + assertEquals(expected, listC); + } + + @Test + public void testMergeSortedWithNullListA() { + List listA = null; // Null list + List listB = Arrays.asList(2, 4, 6, 8, 10); + List listC = new ArrayList<>(); + + assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(listA, listB, listC)); + } + + @Test + public void testMergeSortedWithNullListB() { + List listA = Arrays.asList(1, 3, 5, 7, 9); + List listB = null; // Null list + List listC = new ArrayList<>(); + + assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(listA, listB, listC)); + } + + @Test + public void testMergeSortedWithBothNullLists() { + List listA = null; // Null list + List listB = null; // Null list + List listC = new ArrayList<>(); + + assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(listA, listB, listC)); + } + + @Test + public void testMergeSortedNullLists() { + List listA = null; // Null list + List listB = null; // Null list + List listC = null; // Null list + + assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(listA, listB, listC)); + } +}