예제 #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 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();
  }