forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericHeapTest.java
More file actions
126 lines (96 loc) · 3.08 KB
/
GenericHeapTest.java
File metadata and controls
126 lines (96 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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); });
}
}