Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- public class BinaryHeapTest extends TestCase {
- public void test_init() {
- BinaryHeap<Integer> heap = new BinaryHeap<Integer>(1);
- assertNotNull(heap);
- assertEquals(0, heap.size());
- assertEquals(1, heap.capacity());
- }
- public void test_one_element() {
- BinaryHeap<Integer> heap = new BinaryHeap<Integer>(8);
- assertEquals(0, heap.size());
- assertEquals(8, heap.capacity());
- heap.add(1);
- assertEquals(1, heap.size());
- assertEquals(new Integer(1), heap.peek());
- assertEquals(new Integer(1), heap.element());
- assertEquals(new Integer(1), heap.remove());
- assertEquals(0, heap.size());
- }
- public void test_elements() {
- BinaryHeap<Integer> heap = new BinaryHeap<Integer>(5);
- assertEquals(0, heap.size());
- assertEquals(5, heap.capacity());
- // add
- heap.add(20);
- heap.add(5);
- heap.add(19);
- heap.add(1);
- heap.add(27);
- assertEquals(5, heap.size());
- // peek
- assertEquals(new Integer(1), heap.peek());
- assertEquals(5, heap.size());
- // remove
- assertEquals(new Integer(1), heap.remove());
- assertEquals(4, heap.size());
- assertEquals(new Integer(5), heap.remove());
- assertEquals(3, heap.size());
- }
- public void test_reversed() {
- BinaryHeap<Integer> heap = new BinaryHeap<Integer>(5);
- assertEquals(0, heap.size());
- assertEquals(5, heap.capacity());
- // add
- heap.add(5);
- heap.add(4);
- heap.add(3);
- heap.add(2);
- heap.add(1);
- assertEquals(5, heap.size());
- // peek
- assertEquals(new Integer(1), heap.peek());
- assertEquals(5, heap.size());
- // remove
- assertEquals(new Integer(1), heap.remove());
- assertEquals(4, heap.size());
- assertEquals(new Integer(2), heap.remove());
- assertEquals(3, heap.size());
- assertEquals(new Integer(3), heap.remove());
- assertEquals(2, heap.size());
- assertEquals(new Integer(4), heap.remove());
- assertEquals(1, heap.size());
- assertEquals(new Integer(5), heap.remove());
- assertEquals(0, heap.size());
- }
- }
Add Comment
Please, Sign In to add comment