コード例 #1
0
  @Repeat(iterations = 10)
  @Test
  public void testPreallocatedSize() {
    final Random randomVK = RandomizedTest.getRandom();
    // Test that the container do not resize if less that the initial size

    // 1) Choose a random number of elements
    /*! #if ($TemplateOptions.isKType("GENERIC", "INT", "LONG", "FLOAT", "DOUBLE")) !*/
    final int PREALLOCATED_SIZE = randomVK.nextInt(10000);
    /*!
    #elseif ($TemplateOptions.isKType("SHORT", "CHAR"))
     int PREALLOCATED_SIZE = randomVK.nextInt(1500);
    #else
      int PREALLOCATED_SIZE = randomVK.nextInt(126);
    #end !*/

    final KTypeSet<KType> newSet =
        createNewSetInstance(PREALLOCATED_SIZE, HashContainers.DEFAULT_LOAD_FACTOR);

    // computed real capacity
    final int realCapacity = newSet.capacity();

    // 3) Add PREALLOCATED_SIZE different values. At the end, size() must be == realCapacity,
    // and internal buffer/allocated must not have changed of size
    final int contructorBufferSize = getKeys(newSet).length;

    Assert.assertEquals(contructorBufferSize, getKeys(newSet).length);

    for (int i = 0; i < 1.5 * realCapacity; i++) {

      newSet.add(cast(i));

      // internal size has not changed until realCapacity
      if (newSet.size() <= realCapacity) {

        Assert.assertEquals(contructorBufferSize, getKeys(newSet).length);
      }

      if (contructorBufferSize < getKeys(newSet).length) {
        // The container as just reallocated, its actual size must be not too far from the previous
        // capacity:
        Assert.assertTrue(
            "Container as reallocated at size = "
                + newSet.size()
                + " with previous capacity = "
                + realCapacity,
            (newSet.size() - realCapacity) <= 3);
        break;
      }
    }
  }
コード例 #2
0
  @Repeat(iterations = 25)
  @Seed("88DC7A1093FD66C5")
  @Test
  public void testNoOverallocation() {

    final Random randomVK = RandomizedTest.getRandom();
    // Test that the container do not resize if less that the initial size

    // 1) Choose a random number of elements
    /*! #if ($TemplateOptions.isKType("GENERIC", "INT", "LONG", "FLOAT", "DOUBLE")) !*/
    final int PREALLOCATED_SIZE = randomVK.nextInt(10000);
    /*!
    #elseif ($TemplateOptions.isKType("SHORT", "CHAR"))
     int PREALLOCATED_SIZE = randomVK.nextInt(126);
    #else
      int PREALLOCATED_SIZE = randomVK.nextInt(10000);
    #end !*/

    // 2) Preallocate to PREALLOCATED_SIZE, use default factor because copy-constructor use this.
    final KTypeSet<KType> refContainer =
        createNewSetInstance(PREALLOCATED_SIZE, HashContainers.DEFAULT_LOAD_FACTOR);

    final int refCapacity = refContainer.capacity();

    // 3) Fill with random values, random number of elements below preallocation
    final int nbElements = RandomizedTest.randomInt(PREALLOCATED_SIZE);

    for (int i = 0; i < nbElements; i++) {

      refContainer.add(cast(i));
    }

    // Capacity must have not changed, i.e no reallocation must have occured.
    Assert.assertEquals(refCapacity, refContainer.capacity());

    final int nbRefElements = refContainer.size();

    Assert.assertEquals(refCapacity, refContainer.capacity());

    // 4) Duplicate by copy-construction and/or clone
    KTypeSet<KType> clonedContainer = getClone(refContainer);
    KTypeSet<KType> copiedContainer = getCopyConstructor(refContainer);

    final int copiedCapacity = copiedContainer.capacity();
    final int clonedCapacity = copiedContainer.capacity();

    Assert.assertEquals(nbRefElements, clonedContainer.size());
    Assert.assertEquals(nbRefElements, copiedContainer.size());
    // Due to different pre-sizings, clones and copy constructed may or may not have the same
    // capacity as the refContainer.
    Assert.assertTrue(clonedContainer.equals(refContainer));
    Assert.assertTrue(copiedContainer.equals(refContainer));

    // Maybe we were lucky, iterate duplication over itself several times
    for (int j = 0; j < 10; j++) {

      clonedContainer = getClone(clonedContainer);
      copiedContainer = getFrom(copiedContainer);

      // when copied over itself, of course every characteristic must be constant, else something is
      // wrong.
      Assert.assertEquals("j = " + j, nbRefElements, clonedContainer.size());
      Assert.assertEquals("j = " + j, nbRefElements, copiedContainer.size());
      Assert.assertEquals("j = " + j, clonedCapacity, clonedContainer.capacity());
      Assert.assertEquals("j = " + j, copiedCapacity, copiedContainer.capacity());
      Assert.assertTrue("j = " + j, clonedContainer.equals(refContainer));
      Assert.assertTrue("j = " + j, copiedContainer.equals(refContainer));
    }
  }