-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathGenericHeapTest.java
85 lines (68 loc) · 2.07 KB
/
GenericHeapTest.java
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
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
void setUp() {
heap = new GenericHeap<>();
}
@Test
void testAddAndGet() {
heap.add(10);
heap.add(20);
heap.add(5);
assertEquals(20, heap.get());
}
@Test
void testRemove() {
heap.add(10);
heap.add(20);
heap.add(5);
assertEquals(20, heap.remove());
assertEquals(10, heap.get());
}
@Test
void testIsEmpty() {
assertTrue(heap.isEmpty());
heap.add(1);
assertFalse(heap.isEmpty());
}
@Test
void testSize() {
assertEquals(0, heap.size());
heap.add(1);
heap.add(2);
assertEquals(2, heap.size());
}
@Test
void testUpdatePriority() {
heap.add(10);
heap.add(20);
heap.add(5);
heap.updatePriority(10);
assertEquals(20, heap.get());
heap.add(30);
heap.updatePriority(20); // 20 will be moved up
assertEquals(30, heap.get());
}
@Test
void testRemoveFromEmptyHeap() {
Exception exception = assertThrows(IllegalStateException.class, () -> heap.remove());
assertEquals("Heap is empty", exception.getMessage());
}
@Test
void testGetFromEmptyHeap() {
Exception exception = assertThrows(IllegalStateException.class, () -> heap.get());
assertEquals("Heap is empty", exception.getMessage());
}
@Test
void testUpdatePriorityForNonExistentItem() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> heap.updatePriority(100));
assertEquals("Item not found in the heap", exception.getMessage());
}
}