Esempio n. 1
0
  public void testEqualKeys() {
    PriorityQueue q = new MinHeap(N);
    Item[] items = new Item[20];
    int j = 0;

    for (int i = 0; i < 5; i++) {
      items[j] = new Item(5);
      q.insert(items[j]);
      j++;
    }
    for (int i = 0; i < 5; i++) {
      items[j] = new Item(3);
      q.insert(items[j]);
      j++;
    }
    for (int i = 0; i < 5; i++) {
      items[j] = new Item(4);
      q.insert(items[j]);
      j++;
    }
    for (int i = 0; i < 5; i++) {
      items[j] = new Item(7);
      q.insert(items[j]);
      j++;
    }

    assertEquals(20, q.size());
    for (int i = 0; i < items.length; i++) {
      assertTrue(q.contains(items[i]));
    }

    for (int i = 0; i < 5; i++) {
      QueueElement e = q.extractMin();
      assertTrue(q.contains(q.min()));
      assertEquals(3.0, e.getPriority());
    }
    for (int i = 0; i < 5; i++) {
      QueueElement e = q.extractMin();
      assertTrue(q.contains(q.min()));
      assertEquals(4.0, e.getPriority());
    }
    for (int i = 0; i < 5; i++) {
      QueueElement e = q.extractMin();
      assertTrue(q.contains(q.min()));
      assertEquals(5.0, e.getPriority());
    }
    for (int i = 0; i < 5; i++) {
      QueueElement e = q.extractMin();
      if (q.size() > 0) assertTrue(q.contains(q.min()));
      assertEquals(7.0, e.getPriority());
    }
  }