Ejemplo n.º 1
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);
  }
Ejemplo n.º 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));
    }
  }