@@ -13,114 +13,73 @@ public class GenericHeapTest {
13
13
private GenericHeap <Integer > heap ;
14
14
15
15
@ BeforeEach
16
- public void setUp () {
16
+ void setUp () {
17
17
heap = new GenericHeap <>();
18
18
}
19
19
20
20
@ Test
21
- public void testGenericHeapAddAndGet () {
22
- heap .add (19 );
23
- heap .add (36 );
24
- heap .add (100 );
25
- heap .add (-17 );
26
- heap .add (3 );
27
-
28
- // Check that the largest element (100) is at the top of the heap
29
- assertEquals (100 , heap .get ());
30
- }
31
-
32
- @ Test
33
- public void testGenericHeapRemove () {
34
- heap .add (19 );
35
- heap .add (36 );
36
- heap .add (100 );
37
- heap .add (-17 );
38
- heap .add (3 );
39
-
40
- // Verify that the largest element is removed correctly
41
- assertEquals (100 , heap .remove ());
42
-
43
- // The new element at the top should be 36
44
- assertEquals (36 , heap .get ());
21
+ void testAddAndGet () {
22
+ heap .add (10 );
23
+ heap .add (20 );
24
+ heap .add (5 );
45
25
46
- // Check that the size is correct after removal
47
- assertEquals (4 , heap .size ());
26
+ assertEquals (20 , heap .get ());
48
27
}
49
28
50
29
@ Test
51
- public void testGenericHeapSize () {
52
- assertTrue (heap .isEmpty ());
53
-
30
+ void testRemove () {
54
31
heap .add (10 );
55
32
heap .add (20 );
33
+ heap .add (5 );
56
34
57
- // Check that the size is correct
58
- assertEquals (2 , heap .size ());
59
-
60
- heap .remove ();
61
-
62
- // After removal, the size should be 1
63
- assertEquals (1 , heap .size ());
35
+ assertEquals (20 , heap .remove ());
36
+ assertEquals (10 , heap .get ());
64
37
}
65
38
66
39
@ Test
67
- public void testGenericHeapIsEmpty () {
68
- // Verify that the heap is initially empty
40
+ void testIsEmpty () {
69
41
assertTrue (heap .isEmpty ());
70
-
71
- heap .add (15 );
72
-
73
- // Now the heap should not be empty
42
+ heap .add (1 );
74
43
assertFalse (heap .isEmpty ());
75
-
76
- heap .remove ();
77
-
78
- // After removing the one element, it should be empty again
79
- assertTrue (heap .isEmpty ());
80
44
}
81
45
82
46
@ Test
83
- public void testGenericHeapUpdatePriority () {
84
- heap .add (19 );
85
- heap .add (36 );
86
- heap .add (100 );
87
- heap .add (-17 );
88
- heap .add (3 );
89
-
90
- // Verify that the largest element initially is 100
91
- assertEquals (100 , heap .get ());
47
+ void testSize () {
48
+ assertEquals (0 , heap .size ());
49
+ heap .add (1 );
50
+ heap .add (2 );
51
+ assertEquals (2 , heap .size ());
52
+ }
92
53
93
- heap .remove ();
54
+ @ Test
55
+ void testUpdatePriority () {
56
+ heap .add (10 );
57
+ heap .add (20 );
58
+ heap .add (5 );
94
59
95
- // Simulates a change in priority by increasing the value of 100 to 44
96
- heap .add ( 44 );
60
+ heap . updatePriority ( 10 );
61
+ assertEquals ( 20 , heap .get () );
97
62
98
- // Now, the new high should be 25
99
- assertEquals (44 , heap .get ());
63
+ heap .add (30 );
64
+ heap .updatePriority (20 ); // 20 will be moved up
65
+ assertEquals (30 , heap .get ());
100
66
}
101
67
102
68
@ Test
103
- public void testGenericHeapRemoveUntilEmpty () {
104
- heap .add (5 );
105
- heap .add (3 );
106
- heap .add (4 );
107
- heap .add (1 );
108
- heap .add (2 );
109
-
110
- // Remove all items and check that they are removed in descending order
111
- assertEquals (5 , heap .remove ());
112
- assertEquals (4 , heap .remove ());
113
- assertEquals (3 , heap .remove ());
114
- assertEquals (2 , heap .remove ());
115
- assertEquals (1 , heap .remove ());
69
+ void testRemoveFromEmptyHeap () {
70
+ Exception exception = assertThrows (IllegalStateException .class , () -> heap .remove ());
71
+ assertEquals ("Heap is empty" , exception .getMessage ());
72
+ }
116
73
117
- // Empty heap
118
- assertTrue (heap .isEmpty ());
74
+ @ Test
75
+ void testGetFromEmptyHeap () {
76
+ Exception exception = assertThrows (IllegalStateException .class , () -> heap .get ());
77
+ assertEquals ("Heap is empty" , exception .getMessage ());
119
78
}
120
79
121
80
@ Test
122
- public void testGenericHeapAddNullItem () {
123
- // Check null item
124
- assertThrows ( IllegalArgumentException . class , () -> { heap . add ( null ); } );
81
+ void testUpdatePriorityForNonExistentItem () {
82
+ Exception exception = assertThrows ( IllegalArgumentException . class , () -> heap . updatePriority ( 100 ));
83
+ assertEquals ( "Item not found in the heap" , exception . getMessage () );
125
84
}
126
85
}
0 commit comments