//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //Min-heap Tests //Description: Test suite for the MinHeap class, using JUnit. //CS 284 //Programmed by Jonathan Voris //4/12/06 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import junit.framework.TestCase; import junit.framework.TestSuite; import junit.framework.Test; import java.util.Arrays; public class MinHeapTest extends TestCase { private MinHeap testHeap; //Set up all of the test arrays we'll need for our various tests. Integer[] noInsert = {6, 18, 29, 20, 28, 39, 66, 37, 26, 76, 32, 74, 89}; Integer[] firstInsert = {6, 18, 8, 20, 28, 39, 29, 37, 26, 76, 32, 74, 89, 66}; Integer[] secondInsert = {5, 18, 6, 20, 28, 39, 8, 37, 26, 76, 32, 74, 89, 66, 29}; Integer[] firstRemoval = {8, 18, 29, 20, 28, 39, 66, 37, 26, 76, 32, 74, 89}; Integer[] secondRemoval = {18, 20, 29, 26, 28, 39, 66, 37, 89, 76, 32, 74}; Integer[] sorted = {6, 18, 20, 26, 28, 29, 32, 37, 39, 66, 74, 76, 89}; protected void setUp() { testHeap = new MinHeap(noInsert); } public static Test suite() { return new TestSuite(MinHeapTest.class); } public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } //test that our constructor properly created the heap public void testInitial() { assertTrue(Arrays.equals(testHeap.getHeapArray().toArray(), noInsert)); } //test adding new values to the heap public void testInsertion() { testHeap.insert(8); assertTrue(Arrays.equals(testHeap.getHeapArray().toArray(), firstInsert)); testHeap.insert(5); assertTrue(Arrays.equals(testHeap.getHeapArray().toArray(), secondInsert)); } //test taking values out of the heap public void testRemoval() { testHeap.insert(8); assertTrue(Arrays.equals(testHeap.getHeapArray().toArray(), firstInsert)); testHeap.removeMin(); assertTrue(Arrays.equals(testHeap.getHeapArray().toArray(), firstRemoval)); testHeap.removeMin(); assertTrue(Arrays.equals(testHeap.getHeapArray().toArray(), secondRemoval)); } //test the getMin "peek" function public void testGetMin() { assertTrue(((Comparable)testHeap.getMin()).compareTo(6) == 0); testHeap.insert(5); assertTrue(((Comparable)testHeap.getMin()).compareTo(5) == 0); } //test that heapSort works correctly public void testSort() { assertTrue(Arrays.equals(testHeap.Heapsort().toArray(), sorted)); } }