Exemple #1
0
 /**
  * Verify that the sort() method correctly sorts every elements in the correct order in an array.
  * Moreover, sort() should throw NullPointerException when sorting a null object.
  */
 public void testSort() {
   String[] array = new String[10];
   array[0] = "C";
   array[1] = "E";
   array[2] = "D";
   array[3] = "H";
   array[4] = "A";
   array[5] = "H";
   array[6] = "C";
   array[7] = "Z";
   array[8] = "Y";
   array[9] = "B";
   Heap12.sort(array);
   assertTrue(array[0].equals("A"));
   assertTrue(array[1].equals("B"));
   assertTrue(array[2].equals("C"));
   assertTrue(array[3].equals("C"));
   assertTrue(array[4].equals("D"));
   assertTrue(array[5].equals("E"));
   assertTrue(array[6].equals("H"));
   assertTrue(array[7].equals("H"));
   assertTrue(array[8].equals("Y"));
   assertTrue(array[9].equals("Z"));
   String[] nullArray = null;
   try {
     Heap12.sort(nullArray);
     fail("should throw NullPointerException()");
   } catch (NullPointerException e) {
     assertTrue(true);
   }
 }
Exemple #2
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);
 }
Exemple #3
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);
   }
 }
Exemple #4
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));
 }
Exemple #5
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);
 }
Exemple #6
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());
 }
Exemple #7
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);
 }