Skip to content

Commit c6342cd

Browse files
committed
Add tests Generic Heap and add check null item
1 parent 90d20b3 commit c6342cd

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class GenericHeap<T extends Comparable<T>> {
99
HashMap<T, Integer> map = new HashMap<>();
1010

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

0 commit comments

Comments
 (0)