Beispiel #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);
 }
Beispiel #2
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);
   }
 }
Beispiel #3
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);
 }