コード例 #1
0
  public void testGrowReplaceShrink() {
    Random r = new Random();
    int k = 10000;
    String s = "A";
    double t = 0;
    FibonacciHeap<String> h = new FibonacciHeap<>();
    for (int i = 0; i < (k * 3); ++i) {
      // during first two-thirds, insert
      if (i < (k * 2)) {
        double d = r.nextDouble();
        t += d;
        FibonacciHeapNode<String> n = new FibonacciHeapNode<>(s);
        h.insert(n, d);
      }

      // during last two-thirds, delete (so during middle
      // third, we'll do both insert and delete, interleaved)
      if (i >= k) {
        FibonacciHeapNode<String> n2 = h.removeMin();
        t -= n2.getKey();
      }
    }
    assertTrue(h.isEmpty());

    // tally should come back down to zero, or thereabouts (due to roundoff)
    assertEquals(0.0, t, 0.00001);
  }
コード例 #2
0
 // in honor of sf.net bug #1845376
 public void testAddRemoveOne() {
   String s = "A";
   FibonacciHeapNode<String> n = new FibonacciHeapNode<>(s);
   FibonacciHeap<String> h = new FibonacciHeap<>();
   assertTrue(h.isEmpty());
   h.insert(n, 1.0);
   assertFalse(h.isEmpty());
   FibonacciHeapNode<String> n2 = h.removeMin();
   assertEquals(s, n2.getData());
   assertTrue(h.isEmpty());
 }