예제 #1
0
 /**
  * Verfiy that our remove method correctly removes the elements with the highest priority,
  * decrements the size, and correctly sorts the elements
  */
 public void testRemove() {
   A.add("A");
   A.add("C");
   A.add("E");
   A.add("A");
   A.add("U");
   A.add("B");
   A.add("Z");
   assertEquals(A.size(), 7);
   assertEquals(A.peek(), "Z");
   assertEquals(A.remove(), "Z");
   assertEquals(A.size(), 6);
   assertEquals(A.peek(), "U");
   assertEquals(A.remove(), "U");
   assertEquals(A.size(), 5);
   assertEquals(A.peek(), "E");
   assertEquals(A.remove(), "E");
   assertEquals(A.size(), 4);
   assertEquals(A.peek(), "C");
   assertEquals(A.remove(), "C");
   assertEquals(A.size(), 3);
   assertEquals(A.peek(), "B");
   assertEquals(A.remove(), "B");
   assertEquals(A.size(), 2);
   assertEquals(A.peek(), "A");
   assertEquals(A.remove(), "A");
   assertEquals(A.size(), 1);
   assertEquals(A.peek(), "A");
   assertEquals(A.remove(), "A");
   assertEquals(A.size(), 0);
   assertEquals(A.peek(), null);
   assertEquals(A.remove(), null);
 }
예제 #2
0
 /** Verify that the size() method correctly returns the number of elements in this PQueue */
 public void testSize() {
   assertTrue(A.size() == 0);
   A.add("A");
   assertTrue(A.size() == 1);
   A.add("B");
   A.remove();
   assertTrue(A.size() == 1);
   A.add("B");
   A.add("C");
   assertTrue(A.size() == 3);
   A.add("E");
   assertTrue(A.size() == 4);
   A.add("F");
   A.add("G");
   assertTrue(A.size() == 6);
   A.remove();
   A.remove();
   A.remove();
   assertTrue(A.size() == 3);
   A.remove();
   assertTrue(A.size() == 2);
   A.remove();
   assertTrue(A.size() == 1);
   A.remove();
   assertTrue(A.size() == 0);
   A.remove();
   assertTrue(A.size() == 0);
 }
예제 #3
0
 /** Verify that the peek method correctly returns the element with the highest priority */
 public void testPeek() {
   A.add("A");
   A.add("C");
   A.add("E");
   A.add("A");
   A.add("U");
   A.add("B");
   A.add("Z");
   assertEquals(A.peek(), "Z");
   assertEquals(A.remove(), "Z");
   assertEquals(A.peek(), "U");
   assertEquals(A.remove(), "U");
   assertEquals(A.peek(), "E");
   assertEquals(A.remove(), "E");
   assertEquals(A.peek(), "C");
   assertEquals(A.remove(), "C");
   assertEquals(A.peek(), "B");
   assertEquals(A.remove(), "B");
   assertEquals(A.peek(), "A");
   assertEquals(A.remove(), "A");
   assertEquals(A.peek(), "A");
   assertEquals(A.remove(), "A");
   assertEquals(A.peek(), null);
   assertEquals(A.remove(), null);
 }
예제 #4
0
 /**
  * Verify that the add method adds the specified elements and increase the size accordingly,
  * Moreover, make sure that the elements are sorted correctly according to their priority in this
  * PQueue. Furthermore, we check that the add method throw NullPointerException if we add a null
  * element
  */
 public void testAdd() {
   A.add("A");
   assertFalse(A.isEmpty());
   assertEquals(A.peek(), "A");
   assertTrue(A.size() == 1);
   A.add("D");
   assertTrue(A.size() == 2);
   assertEquals(A.peek(), "D");
   A.add("E");
   assertTrue(A.size() == 3);
   assertEquals(A.peek(), "E");
   A.add("C");
   assertTrue(A.size() == 4);
   assertEquals(A.peek(), "E");
   A.add("F");
   assertTrue(A.size() == 5);
   assertEquals(A.peek(), "F");
   A.add("F");
   assertTrue(A.size() == 6);
   assertEquals(A.peek(), "F");
   try {
     A.add(null);
     fail("should throw NullPointerException()");
   } catch (NullPointerException e) {
     assertTrue(true);
   }
 }
예제 #5
0
 /**
  * Verify that isEmpty() method returns </tt>true<tt> when this PQueue is empty, false when this
  * PQueue has at least one element
  */
 public void testIsEmpty() {
   assertTrue(A.isEmpty());
   A.add("A");
   assertFalse(A.isEmpty());
   assertEquals(A.remove(), "A");
   assertTrue(A.isEmpty());
   A.add("A");
   A.add("B");
   A.add("C");
   A.add("D");
   A.add("E");
   A.add("F");
   assertFalse(A.isEmpty());
   assertEquals(A.remove(), "F");
   assertEquals(A.remove(), "E");
   assertEquals(A.remove(), "D");
   assertEquals(A.remove(), "C");
   assertEquals(A.remove(), "B");
   assertEquals(A.remove(), "A");
   assertTrue(A.isEmpty());
 }
예제 #6
0
 /**
  * Verify that the equals() method returns true when two Heap12's have the same size, same
  * elements, and same type, and false otherwise.
  */
 public void testEquals() {
   assertTrue(A.equals(B));
   assertTrue(B.equals(A));
   A.add("A");
   A.add("B");
   B.add("A");
   B.add("B");
   assertTrue(A.equals(B));
   assertTrue(B.equals(A));
   B.add("C");
   A.add("D");
   assertFalse(A.equals(B));
   assertFalse(B.equals(A));
   A.add("E");
   assertFalse(A.equals(B));
   assertFalse(B.equals(A));
   assertEquals(A.remove(), "E");
   assertEquals(A.remove(), "D");
   assertEquals(B.remove(), "C");
   assertTrue(A.equals(B));
   assertTrue(B.equals(A));
 }