Created
February 27, 2013 21:59
-
-
Save gtke/5052176 to your computer and use it in GitHub Desktop.
Binary Heap JUnit Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import static org.junit.Assert.assertEquals; | |
| import org.junit.Test; | |
| public class BinaryHeapTest { | |
| @Test (timeout=1000) | |
| public void testSimpleAdd1() { | |
| BinaryHeap<String> heap = new BinaryHeap<String>(); | |
| heap.add("6"); | |
| assertEquals("6", heap.peek()); | |
| } | |
| @Test (timeout=1000) | |
| public void testSimpleAdd2(){ | |
| BinaryHeap<String> heap = new BinaryHeap<String>(); | |
| heap.add("6"); | |
| heap.add("5"); | |
| assertEquals("5", heap.peek()); | |
| assertEquals("[null, 5, 6, null, null, null, null, null, null, null, null]", heap.toString()); | |
| } | |
| @Test (timeout=1000) | |
| public void testAdd1(){ | |
| BinaryHeap<String> heap = new BinaryHeap<String>(); | |
| heap.add("6"); | |
| heap.add("5"); | |
| heap.add("3"); | |
| heap.add("1"); | |
| assertEquals("1", heap.peek()); | |
| assertEquals("[null, 1, 3, 5, 6, null, null, null, null, null, null]", heap.toString()); | |
| } | |
| @Test (timeout=1000) | |
| public void testAdd2(){ | |
| BinaryHeap<String> heap = new BinaryHeap<String>(); | |
| heap.add("6"); | |
| heap.add("5"); | |
| heap.add("3"); | |
| heap.add("1"); | |
| heap.add("8"); | |
| heap.add("7"); | |
| heap.add("2"); | |
| heap.add("4"); | |
| assertEquals("1", heap.peek()); | |
| assertEquals("[null, 1, 3, 2, 4, 8, 7, 5, 6, null, null]", heap.toString()); | |
| } | |
| @Test (timeout=1000) | |
| public void testSimpleRemove1(){ | |
| BinaryHeap<String> heap = new BinaryHeap<String>(); | |
| heap.add("6"); | |
| heap.remove(); | |
| assertEquals(null, heap.peek()); | |
| assertEquals("[null, null, null, null, null, null, null, null, null, null, null]", heap.toString()); | |
| } | |
| @Test (timeout=1000) | |
| public void testSimpleRemove2(){ | |
| BinaryHeap<String> heap = new BinaryHeap<String>(); | |
| heap.add("6"); | |
| heap.add("5"); | |
| heap.remove(); | |
| assertEquals("6", heap.peek()); | |
| assertEquals("[null, 6, null, null, null, null, null, null, null, null, null]", heap.toString()); | |
| } | |
| @Test (timeout=1000) | |
| public void testRemove1(){ | |
| BinaryHeap<String> heap = new BinaryHeap<String>(); | |
| heap.add("6"); | |
| heap.add("5"); | |
| heap.add("3"); | |
| heap.add("1"); | |
| heap.add("8"); | |
| heap.add("7"); | |
| heap.add("2"); | |
| heap.add("4"); | |
| heap.remove(); | |
| assertEquals("2", heap.peek()); | |
| assertEquals("[null, 2, 3, 5, 4, 8, 7, 6, null, null, null]", heap.toString()); | |
| } | |
| @Test (timeout=1000) | |
| public void testIsEmpty(){ | |
| BinaryHeap<String> heap = new BinaryHeap<String>(); | |
| assertEquals(true, heap.isEmpty()); | |
| heap.add("6"); | |
| assertEquals(false, heap.isEmpty()); | |
| } | |
| @Test (timeout=1000) | |
| public void testPeek(){ | |
| BinaryHeap<String> heap = new BinaryHeap<String>(); | |
| assertEquals(null, heap.peek()); | |
| heap.add("6"); | |
| assertEquals("6", heap.peek()); | |
| } | |
| @Test (timeout=1000) | |
| public void testSize(){ | |
| BinaryHeap<String> heap = new BinaryHeap<String>(); | |
| assertEquals(0, heap.size()); | |
| heap.add("6"); | |
| assertEquals(1, heap.size()); | |
| heap.add("5"); | |
| assertEquals(2, heap.size()); | |
| heap.add("4"); | |
| heap.add("10"); | |
| assertEquals(4, heap.size()); | |
| heap.remove(); | |
| assertEquals(3, heap.size()); | |
| } | |
| // toString method to use the tests | |
| /* public String toString(){ | |
| String s ="["; | |
| for(int i=0; i<data.length; i++){ | |
| s+= data[i] +", "; | |
| } | |
| s = s.substring(0, s.length()-2); | |
| return s + "]"; | |
| } | |
| */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment