Repository: TheAlgorithms/Java
Pull request #5801 (2024-10-14 08:28:06)

Add tests Generic Heap and add check null item
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5801?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 64.99%. Comparing base [(`90d20b3`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/90d20b3a43642548ecc55e425517583080a81e2a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`c6342cd`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/c6342cd5d979c7b1ce21adff477acfbd92e245a9?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 63 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5801      +/-   ##
============================================
+ Coverage     59.71%   64.99%   +5.27%     
- Complexity     3855     4312     +457     
============================================
  Files           561      599      +38     
  Lines         16017    16765     +748     
  Branches       3073     3219     +146     
============================================
+ Hits           9565    10896    +1331     
+ Misses         6047     5424     -623     
- Partials        405      445      +40     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5801?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java
index d546b7cc..f1772b5b 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<T extends Comparable<T>> {
     HashMap<T, Integer> 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);


Test patch:
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 00000000..8915a6d8
--- /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<Integer> 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); });
+    }
+}