Skip to content

Instantly share code, notes, and snippets.

@adamnew123456
Created March 21, 2016 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamnew123456/756862cbb5dacdc1c3e4 to your computer and use it in GitHub Desktop.
Save adamnew123456/756862cbb5dacdc1c3e4 to your computer and use it in GitHub Desktop.
UNC COMP 410 - Assn 5
import static org.junit.Assert.*;
import org.junit.Test;
public class BinaryHeapTest {
/**
* If you are expecting null, then don't use this function - it is not null
* safe.
*/
private void entryPairAssertEqual(EntryPair expect, EntryPair actual) {
if (expect.getPriority() != actual.getPriority() ||
!expect.getValue().equals(actual.getValue())) {
String message = String.format("Expected %s, got %s", expect, actual);
fail(message);
}
}
@Test
public void testGetMinEmpty() {
HeapInterface heap = new MinBinHeap();
assertEquals(null, heap.getMin());
}
@Test
public void delMinEmpty() {
HeapInterface heap = new MinBinHeap();
heap.delMin();
assertEquals(null, heap.getMin());
assertEquals(0, heap.size());
}
@Test
public void testSizeEmpty() {
HeapInterface heap = new MinBinHeap();
assertEquals(0, heap.size());
}
/**
* Insert the pairs in random order, to ensure that the heap is
* maintaining the heap order property
*
* Putting this as a static member of the class makes it easier to test with
* differently sized inputs to the heap, without having to change the size
* constants found in a few of the tests
*
* Just make sure that the minimum two elements are ("1", 1) and ("2", 2)
* (or otherwise change the test cases) and everything should work
*/
private static EntryPair[] CONSTRUCTION_PAIRS = new EntryPair[] {
new EntryPair("6", 6),
new EntryPair("3", 3),
new EntryPair("5", 5),
new EntryPair("1", 1),
new EntryPair("4", 4),
new EntryPair("2", 2),
new EntryPair("7", 7),
new EntryPair("5", 5),
new EntryPair("3", 3),
};
private HeapInterface makeNonEmptyHeapInsert() {
HeapInterface heap = new MinBinHeap();
for (EntryPair pair: CONSTRUCTION_PAIRS) {
heap.insert(pair);
}
return heap;
}
private HeapInterface makeNonEmptyHeapBuild() {
HeapInterface heap = new MinBinHeap();
heap.build(CONSTRUCTION_PAIRS);
return heap;
}
private void testGetMinNonEmpty(HeapInterface heap) {
EntryPair min_pair = new EntryPair("1", 1);
entryPairAssertEqual(min_pair, heap.getMin());
}
@Test
public void testGetMinBuild() {
testGetMinNonEmpty(makeNonEmptyHeapBuild());
}
@Test
public void testGetMinInsert() {
testGetMinNonEmpty(makeNonEmptyHeapInsert());
}
private void testDelMinNonEmpty(HeapInterface heap) {
heap.delMin();
EntryPair min_pair = new EntryPair("2", 2);
entryPairAssertEqual(min_pair, heap.getMin());
assertEquals(CONSTRUCTION_PAIRS.length - 1, heap.size());
}
@Test
public void testDelMinBuild() {
testDelMinNonEmpty(makeNonEmptyHeapBuild());
}
@Test
public void testDelMinInsert() {
testDelMinNonEmpty(makeNonEmptyHeapInsert());
}
public void testSizeNonEmpty(HeapInterface heap) {
assertEquals(CONSTRUCTION_PAIRS.length, heap.size());
}
@Test
public void testSizeBuild() {
testSizeNonEmpty(makeNonEmptyHeapBuild());
}
@Test
public void testSizeInsert() {
testSizeNonEmpty(makeNonEmptyHeapInsert());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment