Skip to content

Commit

Permalink
Add tests Generic Heap and add check null item (#5801)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorduke22 authored Oct 25, 2024
1 parent 3b2ba48 commit 131e538
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public class GenericHeap<T extends Comparable<T>> {
HashMap<T, Integer> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,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); });
}
}

0 comments on commit 131e538

Please sign in to comment.