Exemplo n.º 1
0
  @Test
  public void testForEachProcedure() {
    // Test that the container do not resize if less that the initial size

    // 1) Choose a map to build
    /*! #if ($TemplateOptions.isKType("GENERIC", "int", "long", "float", "double")) !*/
    final int NB_ELEMENTS = 2000;
    /*!
    #elseif ($TemplateOptions.isKType("short", "char"))
     int NB_ELEMENTS = 1000;
    #else
      int NB_ELEMENTS = 126;
    #end !*/

    final KTypeSet<KType> newSet = createNewSetInstance();

    newSet.add(this.keyE);

    // add a increasing number of key
    for (int i = 0; i < NB_ELEMENTS; i++) {

      final int KVpair = i;

      newSet.add(cast(KVpair));
    }

    // List the keys in the reverse-order of the internal buffer, since forEach() is iterating in
    // reverse also:
    final KTypeArrayList<KType> keyList = new KTypeArrayList<KType>();

    keyList.add(this.keyE);

    for (int i = getKeys(newSet).length - 1; i >= 0; i--) {

      if (is_allocated(i, Intrinsics.<KType[]>cast(getKeys(newSet)))) {

        keyList.add(Intrinsics.<KType>cast(getKeys(newSet)[i]));
      }
    }

    // Test forEach predicate and stop at each key in turn.
    final KTypeArrayList<KType> keyListTest = new KTypeArrayList<KType>();

    keyListTest.clear();

    // A) Run forEach(KType)

    newSet.forEach(
        new KTypeProcedure<KType>() {

          @Override
          public void apply(final KType key) {
            keyListTest.add(key);
          }
        });

    // check that keyList/keyListTest and valueList/valueListTest are identical.
    Assert.assertEquals(keyList, keyListTest);
  }
Exemplo n.º 2
0
  /*! #if ($TemplateOptions.KTypeGeneric) !*/
  @SuppressWarnings("unchecked")
  @Test
  public void testHashCodeWithNulls() {
    final KTypeSet<KType> l1 = getFrom(this.k1, null, this.k3);
    final KTypeSet<KType> l2 = getFrom(this.k1, null);
    l2.add(this.k3);

    Assert.assertEquals(l1.hashCode(), l2.hashCode());
    Assert.assertEquals(l1, l2);
  }
Exemplo n.º 3
0
  private KTypeSet<KType> createSetWithOrderedData(final int size) {
    final KTypeSet<KType> newSet = createNewSetInstance();

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

      newSet.add(cast(i));
    }

    return newSet;
  }
Exemplo n.º 4
0
  @Test
  public void testInitialCapacityAndGrowth() {
    for (int i = 0; i < 256; i++) {
      final KTypeSet<KType> set = createNewSetInstance(i, HashContainers.MAX_LOAD_FACTOR);

      for (int j = 0; j < i; j++) {
        set.add(cast(j));
      }

      Assert.assertEquals(i, set.size());
    }
  }
Exemplo n.º 5
0
  @Test
  /*! #if ($TemplateOptions.KTypeGeneric) !*/
  @SuppressWarnings("unchecked")
  /*! #end !*/
  public void testHashCodeEquals() {
    final KTypeSet<KType> l0 = getFrom();
    Assert.assertEquals(0, l0.hashCode());
    Assert.assertEquals(l0, createNewSetInstance());

    final KTypeSet<KType> l1 = getFrom(this.k1, this.k2, this.k3);
    final KTypeSet<KType> l2 = getFrom(this.k1, this.k2);
    l2.add(this.k3);

    Assert.assertEquals(l1.hashCode(), l2.hashCode());
    Assert.assertEquals(l1, l2);

    l1.add(this.keyE);
    l2.add(this.keyE);

    Assert.assertEquals(l1.hashCode(), l2.hashCode());
    Assert.assertEquals(l1, l2);
  }
Exemplo n.º 6
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;
      }
    }
  }
Exemplo n.º 7
0
  @Test
  public void testForEachProcedureWithException() {

    // Test that the container do not resize if less that the initial size

    // 1) Choose a map to build
    /*! #if ($TemplateOptions.isKType("GENERIC", "int", "long", "float", "double")) !*/
    final int NB_ELEMENTS = 2000;
    /*!
    #elseif ($TemplateOptions.isKType("short", "char"))
     int NB_ELEMENTS = 1000;
    #else
      int NB_ELEMENTS = 126;
    #end !*/

    final KTypeSet<KType> newSet = createNewSetInstance();

    newSet.add(this.keyE);

    // add a increasing number of key
    for (int i = 0; i < NB_ELEMENTS; i++) {

      final int KVpair = i;

      newSet.add(cast(KVpair));
    }

    // List the keys in the reverse-order of the internal buffer, since forEach() is iterating in
    // reverse also:
    final KTypeArrayList<KType> keyList = new KTypeArrayList<KType>();

    keyList.add(this.keyE);

    // Test forEach predicate and stop at each key in turn.
    final KTypeArrayList<KType> keyListTest = new KTypeArrayList<KType>();

    for (int k = getKeys(newSet).length - 1; k >= 0; k--) {

      if (is_allocated(k, Intrinsics.<KType[]>cast(getKeys(newSet)))) {

        keyList.add(Intrinsics.<KType>cast(getKeys(newSet)[k]));
      }
    }

    final int size = keyList.size();

    for (int i = 0; i < size; i++) {
      final int currentPairIndexSizeToIterate = i + 1;

      keyListTest.clear();

      keyList.clear();

      keyList.add(this.keyE);

      for (int k = getKeys(newSet).length - 1; k >= 0; k--) {

        if (is_allocated(k, Intrinsics.<KType[]>cast(getKeys(newSet)))) {

          keyList.add(Intrinsics.<KType>cast(getKeys(newSet)[k]));
        }
      }

      // A) Run forEach(KType)
      try {
        newSet.forEach(
            new KTypeProcedure<KType>() {

              @Override
              public void apply(final KType key) {
                keyListTest.add(key);

                // when the stopping key/value pair is encountered, add to list and stop iteration
                if (key == keyList.get(currentPairIndexSizeToIterate - 1)) {
                  // interrupt iteration by an exception
                  throw new RuntimeException("Interrupted treatment by test");
                }
              }
            });
      } catch (final RuntimeException e) {
        if (!e.getMessage().equals("Interrupted treatment by test")) {
          throw e;
        }
      } finally {
        // despite the exception, the procedure terminates cleanly

        // check that keyList/keyListTest and valueList/valueListTest are identical for the first
        // currentPairIndexToIterate + 1 elements
        Assert.assertEquals("i = " + i, currentPairIndexSizeToIterate, keyListTest.size());

        for (int j = 0; j < currentPairIndexSizeToIterate; j++) {
          TestUtils.assertEquals2("j = " + j, keyList.get(j), keyListTest.get(j));
        }
      } // end finally
    } // end for each index
  }
Exemplo n.º 8
0
  @Test
  /*! #if ($TemplateOptions.KTypeGeneric) !*/
  @SuppressWarnings("unchecked")
  /*! #end !*/
  public void testEquals() {
    final KTypeSet<KType> l0 = getFrom();
    Assert.assertEquals(l0, createNewSetInstance());

    KTypeSet<KType> l1 = getFrom(this.k1, this.k2, this.k3, this.k4, this.k5);
    KTypeSet<KType> l2 = getFrom(this.k2, this.k1);

    Assert.assertNotEquals(l1, l2);
    Assert.assertNotEquals(l2, l1);

    Assert.assertFalse(l1.equals(null));
    Assert.assertFalse(l2.equals(null));

    l2.add(this.k5);
    Assert.assertNotEquals(l1, l2);
    Assert.assertNotEquals(l2, l1);

    l2.add(this.k4);
    Assert.assertNotEquals(l1, l2);
    Assert.assertNotEquals(l2, l1);

    l2.add(this.k3);
    Assert.assertEquals(l1, l2);
    Assert.assertEquals(l2, l1);
    // Check consistency with hashCode:
    Assert.assertEquals(l1.hashCode(), l2.hashCode());

    l1.add(this.keyE);
    Assert.assertNotEquals(l1, l2);
    Assert.assertNotEquals(l2, l1);

    l2.add(this.keyE);

    Assert.assertEquals(l1, l2);
    Assert.assertEquals(l2, l1);
    // Check consistency with hashCode:
    Assert.assertEquals(l1.hashCode(), l2.hashCode());

    l2.remove(this.keyE);

    Assert.assertNotEquals(l1, l2);
    Assert.assertNotEquals(l2, l1);

    l2.add(this.keyE);

    Assert.assertEquals(l1, l2);
    Assert.assertEquals(l2, l1);
    // Check consistency with hashCode:
    Assert.assertEquals(l1.hashCode(), l2.hashCode());

    l2.remove(this.k7); // not present, sets are still OK
    Assert.assertEquals(l1, l2);
    Assert.assertEquals(l2, l1);
    // Check consistency with hashCode:
    Assert.assertEquals(l1.hashCode(), l2.hashCode());

    l2.remove(this.k2);

    Assert.assertNotEquals(l1, l2);
    Assert.assertNotEquals(l2, l1);

    l1.remove(this.k2);
    Assert.assertEquals(l1, l2);
    Assert.assertEquals(l2, l1);
    // Check consistency with hashCode:
    Assert.assertEquals(l1.hashCode(), l2.hashCode());

    l1.add(this.k7);
    Assert.assertNotEquals(l1, l2);
    Assert.assertNotEquals(l2, l1);

    l2.clear();
    Assert.assertNotEquals(l1, l2);
    Assert.assertNotEquals(l2, l1);

    l1.clear();
    Assert.assertEquals(l1, l2);
    Assert.assertEquals(l2, l1);
    // Check consistency with hashCode:
    Assert.assertEquals(l1.hashCode(), l2.hashCode());

    // Same size, different contents
    l1 = getFrom(this.k1, this.k2, this.k3, this.k4, this.k5);
    l2 = getFrom(this.k2, this.k1, this.key5, this.key6, this.key7);

    Assert.assertNotEquals(l1, l2);
    Assert.assertNotEquals(l2, l1);
  }
Exemplo n.º 9
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));
    }
  }