コード例 #1
0
  @Test
  public void iterator_remove() {
    int size = MORE_COLLISIONS.size();
    for (int i = 0; i < size; i++) {
      MutableSet<Integer> actual =
          UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, SIZE)
              .withAll(MORE_COLLISIONS);
      Iterator<Integer> iterator = actual.iterator();
      for (int j = 0; j <= i; j++) {
        Assert.assertTrue(iterator.hasNext());
        iterator.next();
      }
      iterator.remove();

      MutableSet<Integer> expected =
          UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, MORE_COLLISIONS);
      expected.remove(MORE_COLLISIONS.get(i));
      Assert.assertEquals(expected, actual);
    }

    // remove the last element from within a 2-level long chain that is fully populated
    MutableSet<Integer> set =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY,
            COLLISION_1,
            COLLISION_2,
            COLLISION_3,
            COLLISION_4,
            COLLISION_5,
            COLLISION_6,
            COLLISION_7);
    Iterator<Integer> iterator1 = set.iterator();
    for (int i = 0; i < 7; i++) {
      iterator1.next();
    }
    iterator1.remove();
    Assert.assertEquals(
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY,
            COLLISION_1,
            COLLISION_2,
            COLLISION_3,
            COLLISION_4,
            COLLISION_5,
            COLLISION_6),
        set);

    // remove the second-to-last element from a 2-level long chain that that has one empty slot
    Iterator<Integer> iterator2 = set.iterator();
    for (int i = 0; i < 6; i++) {
      iterator2.next();
    }
    iterator2.remove();
    Assert.assertEquals(
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY,
            COLLISION_1,
            COLLISION_2,
            COLLISION_3,
            COLLISION_4,
            COLLISION_5),
        set);

    // Testing removing the last element in a fully populated chained bucket
    UnifiedSetWithHashingStrategy<Integer> set2 =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY, COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
    Iterator<Integer> iterator3 = set2.iterator();
    for (int i = 0; i < 3; ++i) {
      iterator3.next();
    }
    iterator3.next();
    iterator3.remove();
    Verify.assertSetsEqual(UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_3), set2);
  }