コード例 #1
0
  @Test
  public void removeFromPool() {
    Pool<Integer> unifiedSet =
        UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, 8).withAll(COLLISIONS);
    COLLISIONS.reverseForEach(
        each -> {
          Assert.assertNull(unifiedSet.removeFromPool(null));
          Assert.assertSame(each, unifiedSet.removeFromPool(each));
          Assert.assertNull(unifiedSet.removeFromPool(each));
          Assert.assertNull(unifiedSet.removeFromPool(null));
          Assert.assertNull(unifiedSet.removeFromPool(COLLISION_10));
        });

    Assert.assertEquals(UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY), unifiedSet);

    COLLISIONS.forEach(
        Procedures.cast(
            each -> {
              Pool<Integer> unifiedSet2 =
                  UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, 8)
                      .withAll(COLLISIONS);

              Assert.assertNull(unifiedSet2.removeFromPool(null));
              Assert.assertSame(each, unifiedSet2.removeFromPool(each));
              Assert.assertNull(unifiedSet2.removeFromPool(each));
              Assert.assertNull(unifiedSet2.removeFromPool(null));
              Assert.assertNull(unifiedSet2.removeFromPool(COLLISION_10));
            }));

    // search a chain for a non-existent element
    Pool<Integer> chain =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY, COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
    Assert.assertNull(chain.removeFromPool(COLLISION_5));

    // search a deep chain for a non-existent element
    Pool<Integer> deepChain =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY,
            COLLISION_1,
            COLLISION_2,
            COLLISION_3,
            COLLISION_4,
            COLLISION_5,
            COLLISION_6,
            COLLISION_7);
    Assert.assertNull(deepChain.removeFromPool(COLLISION_8));

    // search for a non-existent element
    Pool<Integer> empty =
        UnifiedSetWithHashingStrategy.newSetWith(INTEGER_HASHING_STRATEGY, COLLISION_1);
    Assert.assertNull(empty.removeFromPool(COLLISION_2));
  }
コード例 #2
0
  @Test
  public void put() {
    int size = MORE_COLLISIONS.size();
    for (int i = 1; i <= size; i++) {
      Pool<Integer> unifiedSet =
          UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, 1)
              .withAll(MORE_COLLISIONS.subList(0, i - 1));
      Integer newValue = MORE_COLLISIONS.get(i - 1);

      Assert.assertSame(newValue, unifiedSet.put(newValue));
      //noinspection UnnecessaryBoxing,CachedNumberConstructorCall,BoxingBoxedValue
      Assert.assertSame(newValue, unifiedSet.put(new Integer(newValue)));
    }

    // assert that all redundant puts into a each position of chain bucket return the original
    // element added
    Pool<Integer> set =
        UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, 4)
            .with(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
    for (int i = 0; i < set.size(); i++) {
      Integer value = COLLISIONS.get(i);
      Assert.assertSame(value, set.put(value));
    }

    // force rehashing at each step of putting a new colliding entry
    for (int i = 0; i < COLLISIONS.size(); i++) {
      Pool<Integer> pool =
          UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, i)
              .withAll(COLLISIONS.subList(0, i));
      if (i == 2) {
        pool.put(Integer.valueOf(1));
      }
      if (i == 4) {
        pool.put(Integer.valueOf(1));
        pool.put(Integer.valueOf(2));
      }
      Integer value = COLLISIONS.get(i);
      Assert.assertSame(value, pool.put(value));
    }

    // cover one case not covered in the above: a bucket with only one entry and a low capacity
    // forcing a rehash
    // set up a chained bucket
    Pool<Integer> pool =
        UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, 2)
            .with(COLLISION_1, COLLISION_2);
    // clear the bucket to one element
    pool.removeFromPool(COLLISION_2);
    // increase the occupied count to the threshold
    pool.put(Integer.valueOf(1));
    pool.put(Integer.valueOf(2));

    // put the colliding value back and force the rehash
    Assert.assertSame(COLLISION_2, pool.put(COLLISION_2));

    // put chained items into a pool without causing a rehash
    Pool<Integer> olympicPool = UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY);
    Assert.assertSame(COLLISION_1, olympicPool.put(COLLISION_1));
    Assert.assertSame(COLLISION_2, olympicPool.put(COLLISION_2));
  }