@Test public void testIterable2() { addFromArray(this.set, this.keyE, this.key1, this.key2, this.key2, this.key3, this.key4); this.set.remove(this.k2); Assert.assertEquals(4, this.set.size()); int counted = 0; for (final KTypeCursor<KType> cursor : this.set) { if (cursor.index == getKeys(this.set).length) { Assert.assertTrue(this.isAllocatedDefaultKey(this.set)); TestUtils.assertEquals2(this.keyE, cursor.value); counted++; continue; } Assert.assertTrue(this.set.contains(cursor.value)); TestUtils.assertEquals2(cursor.value, this.getKeys(this.set)[cursor.index]); counted++; } Assert.assertEquals(counted, this.set.size()); this.set.clear(); Assert.assertFalse(this.set.iterator().hasNext()); }
/** * Check that the set is consistent, i.e all allocated slots are reachable by get(), and all * not-allocated contains nulls if Generic * * @param set */ @After public void checkConsistency() { if (this.set != null) { int occupied = 0; final int mask = getKeys(this.set).length - 1; for (int i = 0; i < getKeys(this.set).length; i++) { if (!is_allocated(i, Intrinsics.<KType[]>cast(getKeys(this.set)))) { // if not allocated, generic version if patched to null for GC sake /*! #if ($TemplateOptions.KTypeGeneric) !*/ TestUtils.assertEquals2(this.keyE, getKeys(this.set)[i]); /*! #end !*/ } else { // try to reach the key by contains() Assert.assertTrue(this.set.contains(Intrinsics.<KType>cast(getKeys(this.set)[i]))); occupied++; } } if (isAllocatedDefaultKey(this.set)) { // try to reach the key by contains() Assert.assertTrue(this.set.contains(this.keyE)); occupied++; } Assert.assertEquals(occupied, this.set.size()); } }
/** * Run some random insertions/ deletions and compare the results against <code>java.util.HashSet * </code>. */ @Test public void testAgainstHashMap() { final java.util.Random rnd = new Random(0xBADCAFE); final java.util.HashSet<KType> other = new java.util.HashSet<KType>(); for (int size = 1000; size < 20000; size += 4000) { other.clear(); this.set.clear(); for (int round = 0; round < size * 20; round++) { final KType key = cast(rnd.nextInt(size)); if (rnd.nextBoolean()) { other.add(key); this.set.add(key); Assert.assertTrue(this.set.contains(key)); } else { Assert.assertTrue( "size= " + size + ", round = " + round, other.remove(key) == this.set.remove(key)); } Assert.assertEquals(other.size(), this.set.size()); } } }
@Test public void testNullKey() { this.set.add((KType) null); Assert.assertEquals(1, this.set.size()); Assert.assertTrue(this.set.contains(null)); Assert.assertTrue(this.set.remove(null)); Assert.assertEquals(0, this.set.size()); Assert.assertFalse(this.set.contains(null)); }
@Test public void testAdd() { Assert.assertTrue(this.set.add(this.key1)); Assert.assertFalse(this.set.add(this.key1)); Assert.assertEquals(1, this.set.size()); Assert.assertTrue(this.set.add(this.keyE)); Assert.assertFalse(this.set.add(this.keyE)); Assert.assertEquals(2, this.set.size()); Assert.assertTrue(this.set.add(this.key2)); Assert.assertFalse(this.set.add(this.key2)); Assert.assertEquals(3, this.set.size()); }
@Test public void testPooledIteratorBrokenForEach() { // A) for-each loop interrupted // must accommodate even the smallest primitive type // so that the iteration do not break before it should... final int TEST_SIZE = 126; final long TEST_ROUNDS = 5000; final KTypeSet<KType> testContainer = createSetWithOrderedData(TEST_SIZE); int count = 0; for (int round = 0; round < TEST_ROUNDS; round++) { // for-each in test : final long initialPoolSize = getEntryPoolSize(testContainer); count = 0; int guard = 0; for (final KTypeCursor<KType> cursor : testContainer) { guard += castType(cursor.value); // we consume 1 iterator for this loop, but reallocs can happen, // so we can only say its != initialPoolSize Assert.assertTrue(initialPoolSize != getEntryPoolSize(testContainer)); // brutally interrupt in the middle if (count > TEST_SIZE / 2) { break; } count++; } // end for-each // iterator is NOT returned to its pool, due to the break. // reallocation could happen, so that the only testable thing // is that the size is != full pool Assert.assertTrue(initialPoolSize != getEntryPoolSize(testContainer)); } // end for rounds // Due to policy of the Iterator pool, the intended pool never get bigger that some limit // despite the Iterator leak. Assert.assertTrue(getEntryPoolCapacity(testContainer) < IteratorPool.getMaxPoolSize() + 1); }
@Test public void testRemove() { Assert.assertFalse(this.set.remove(this.k2)); Assert.assertFalse(this.set.remove(this.keyE)); addFromArray(this.set, this.key0, this.key1, this.key2, this.key3, this.key4); Assert.assertTrue(this.set.remove(this.k2)); Assert.assertFalse(this.set.remove(this.k2)); Assert.assertEquals(4, this.set.size()); TestUtils.assertSortedListEquals(this.set.toArray(), 0, 1, 3, 4); }
@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; } } }
@Test public void testRemoveAllWithPredicateInterrupted() { addFromArray( this.set, newArray(this.k0, this.k1, this.k2, this.k3, this.k4, this.k5, this.k6, this.k7, this.k8)); final RuntimeException t = new RuntimeException(); try { // the assert below should never be triggered because of the exception // so give it an invalid value in case the thing terminates = initial size + 1 Assert.assertEquals( 10, this.set.removeAll( new KTypePredicate<KType>() { @Override public boolean apply(final KType v) { if (v == AbstractKTypeHashSetTest.this.key7) { throw t; } return v == AbstractKTypeHashSetTest.this.key2 || v == AbstractKTypeHashSetTest.this.key9 || v == AbstractKTypeHashSetTest.this.key5; }; })); Assert.fail(); } catch (final RuntimeException e) { // Make sure it's really our exception... if (e != t) { throw e; } } // And check if the set is in consistent state. We cannot predict the pattern, // but we know that since key7 throws an exception, key7 is still present in the set. Assert.assertTrue(this.set.contains(this.key7)); checkConsistency(); }
@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)); } }