Search in sources :

Example 16 with OfInt

use of java.util.PrimitiveIterator.OfInt in project commons-collections by apache.

the class DynamicHasherBuilderTest method buildResetTest.

/**
 * Tests that build resets the builder.
 */
@Test
public void buildResetTest() {
    builder.with(new byte[] { 123 });
    final OfInt iter = builder.build().iterator(shape);
    assertTrue(iter.hasNext());
    iter.next();
    assertFalse(iter.hasNext());
    // Nothing added since last build so it should be an empty hasher
    final OfInt iter2 = builder.build().iterator(shape);
    assertFalse(iter2.hasNext());
}
Also used : OfInt(java.util.PrimitiveIterator.OfInt) Test(org.junit.jupiter.api.Test)

Example 17 with OfInt

use of java.util.PrimitiveIterator.OfInt in project commons-collections by apache.

the class DynamicHasherBuilderTest method buildTest_Empty.

/**
 * Tests that an empty hasher works as expected.
 */
@Test
public void buildTest_Empty() {
    final DynamicHasher hasher = builder.build();
    final OfInt iter = hasher.iterator(shape);
    assertFalse(iter.hasNext());
    assertThrows(NoSuchElementException.class, () -> iter.nextInt(), "Should have thrown NoSuchElementException");
}
Also used : OfInt(java.util.PrimitiveIterator.OfInt) Test(org.junit.jupiter.api.Test)

Example 18 with OfInt

use of java.util.PrimitiveIterator.OfInt in project commons-collections by apache.

the class DynamicHasherTest method testGetBits_MultipleHashes.

/**
 * Tests that bits from multiple hashes are returned correctly.
 */
@Test
public void testGetBits_MultipleHashes() {
    final int[] expected = { 6, 69, 44, 19, 10, 57, 48, 23, 70, 61, 36, 11, 2, 49, 24, 15, 62, 1, 63, 53, 43, 17, 7, 69, 59, 49, 39, 13, 3, 65, 55, 45, 35, 25 };
    final Hasher hasher = builder.with("Hello", StandardCharsets.UTF_8).with("World", StandardCharsets.UTF_8).build();
    final OfInt iter = hasher.iterator(shape);
    for (final int element : expected) {
        assertTrue(iter.hasNext());
        assertEquals(element, iter.nextInt());
    }
    assertFalse(iter.hasNext());
    assertThrows(NoSuchElementException.class, () -> iter.next(), "Should have thrown NoSuchElementException");
}
Also used : OfInt(java.util.PrimitiveIterator.OfInt) Test(org.junit.jupiter.api.Test)

Example 19 with OfInt

use of java.util.PrimitiveIterator.OfInt in project commons-collections by apache.

the class StaticHasherTest method assertSameBits.

/**
 * Compare 2 static hashers to verify they have the same bits enabled.
 *
 * @param hasher1 the first static hasher.
 * @param hasher2 the second static hasher.
 */
private void assertSameBits(final StaticHasher hasher1, final StaticHasher hasher2) {
    final OfInt iter1 = hasher1.iterator(shape);
    final OfInt iter2 = hasher2.iterator(shape);
    while (iter1.hasNext()) {
        assertTrue(iter2.hasNext(), "Not enough data in second hasher");
        assertEquals(iter1.nextInt(), iter2.nextInt());
    }
    assertFalse(iter2.hasNext(), "Too much data in second hasher");
}
Also used : OfInt(java.util.PrimitiveIterator.OfInt)

Example 20 with OfInt

use of java.util.PrimitiveIterator.OfInt in project commons-collections by apache.

the class StaticHasherTest method testConstructor_Hasher.

/**
 * Tests that passing a hasher other than a StaticHasher to the constructor works as
 * expected.
 */
@Test
public void testConstructor_Hasher() {
    final int[] expected = { 1, 3, 5, 7, 9 };
    final Hasher testHasher = new Hasher() {

        @Override
        public OfInt iterator(final Shape shape) {
            final int[] values = { 1, 3, 5, 7, 9, 3, 5, 1 };
            return Arrays.stream(values).iterator();
        }

        @Override
        public HashFunctionIdentity getHashFunctionIdentity() {
            return testFunction;
        }
    };
    final StaticHasher hasher = new StaticHasher(testHasher, shape);
    final OfInt iter = hasher.iterator(shape);
    for (final int element : expected) {
        assertTrue(iter.hasNext());
        assertEquals(element, iter.nextInt());
    }
    assertFalse(iter.hasNext());
}
Also used : OfInt(java.util.PrimitiveIterator.OfInt) Test(org.junit.jupiter.api.Test)

Aggregations

OfInt (java.util.PrimitiveIterator.OfInt)22 Test (org.junit.jupiter.api.Test)12 Random (java.util.Random)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 TreeSet (java.util.TreeSet)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Hasher (org.apache.commons.collections4.bloomfilter.hasher.Hasher)2 Shape (org.apache.commons.collections4.bloomfilter.hasher.Shape)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 IntBuffer (java.nio.IntBuffer)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1