From c6342cd5d979c7b1ce21adff477acfbd92e245a9 Mon Sep 17 00:00:00 2001 From: lorduke22 Date: Mon, 14 Oct 2024 10:25:06 +0200 Subject: [PATCH 1/2] Add tests Generic Heap and add check null item --- .../datastructures/heaps/GenericHeap.java | 2 + .../datastructures/heaps/GenericHeapTest.java | 123 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java index d546b7cc88d4..e41b5355b87d 100644 --- a/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java +++ b/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java @@ -9,6 +9,8 @@ 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/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java b/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java new file mode 100644 index 000000000000..c8c2fe481340 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java @@ -0,0 +1,123 @@ +package com.thealgorithms.datastructures.heaps; + +import static org.junit.jupiter.api.Assertions.*; + +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); }); + } +} From c423ff409f34cf47fcc263adcd1bb869f388e95d Mon Sep 17 00:00:00 2001 From: lorduke22 Date: Mon, 14 Oct 2024 10:37:30 +0200 Subject: [PATCH 2/2] Fix --- .../com/thealgorithms/datastructures/heaps/GenericHeap.java | 4 +++- .../thealgorithms/datastructures/heaps/GenericHeapTest.java | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java index e41b5355b87d..f1772b5b3112 100644 --- a/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java +++ b/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java @@ -9,7 +9,9 @@ public class GenericHeap> { HashMap map = new HashMap<>(); public void add(T item) { - if (item == null) throw new IllegalArgumentException("Cannot insert null into the heap."); + if (item == null) { + throw new IllegalArgumentException("Cannot insert null into the heap."); + } this.data.add(item); map.put(item, this.data.size() - 1); // diff --git a/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java b/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java index c8c2fe481340..8915a6d8aef2 100644 --- a/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java +++ b/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java @@ -1,6 +1,9 @@ package com.thealgorithms.datastructures.heaps; -import static org.junit.jupiter.api.Assertions.*; +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;