示例#1
0
  @Test(expectedExceptions = NoSuchElementException.class)
  public void emptySetTest() {
    WeakHashSet<MyReclaimable> hs = new WeakHashSet<MyReclaimable>();
    assertEquals(hs.size(), 0);

    Iterator<MyReclaimable> it = hs.iterator();
    it.next();
  }
示例#2
0
  @Test
  public void gcTest() throws Exception {
    WeakHashSet<MyReclaimable> hs;

    Set<MyReclaimable> s = new HashSet<MyReclaimable>();
    s.add(new MyReclaimable("one"));
    s.add(new MyReclaimable("two"));
    s.add(new MyReclaimable("three"));

    hs = new WeakHashSet<MyReclaimable>(s);
    assertEquals(hs.size(), 3);
    assertFalse(hs.isEmpty());

    s.clear();
    Thread.sleep(2000);
    gc();

    Thread.sleep(2000);
    assertEquals(hs.size(), 0);
  }
示例#3
0
  @Test
  public void fromExistingSetTest() {
    WeakHashSet<MyReclaimable> hs;

    Set<MyReclaimable> s = new HashSet<MyReclaimable>();
    hs = new WeakHashSet<MyReclaimable>(s);
    assertEquals(hs.size(), 0);
    assertTrue(hs.isEmpty());

    s.add(new MyReclaimable("hello"));
    hs = new WeakHashSet<MyReclaimable>(s);
    assertEquals(hs.size(), 1);
    assertTrue(!hs.isEmpty());
    assertTrue(hs.contains(new MyReclaimable("hello")));

    Iterator<MyReclaimable> it = hs.iterator();
    boolean found = false;
    while (it.hasNext()) {
      MyReclaimable value = it.next();
      if (value.equals(new MyReclaimable("hello"))) {
        found = true;
      }
    }
    assertTrue(found);

    assertTrue(!hs.add(new MyReclaimable("hello")));
    assertEquals(hs.size(), 1);

    assertTrue(hs.add(new MyReclaimable("goodbye")));
    assertEquals(hs.size(), 2);

    assertTrue(!hs.add(new MyReclaimable("goodbye")));
    assertEquals(hs.size(), 2);

    assertTrue(hs.remove(new MyReclaimable("goodbye")));
    assertEquals(hs.size(), 1);

    assertFalse(hs.remove(new MyReclaimable("goodbye")));
    assertEquals(hs.size(), 1);

    hs.clear();
    assertEquals(hs.size(), 0);

    s.clear();
  }