Search in sources :

Example 1 with OfInt

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

the class HasherBloomFilterTest method getBitsTest_LowestBitOnly.

/**
 * Test the edge case where the filter has only 1 bit in the lowest index and the getBits()
 * function returns an array of length 1.
 */
@Test
public void getBitsTest_LowestBitOnly() {
    final BloomFilter filter = createEmptyFilter(shape);
    // Set the lowest bit index only.
    filter.merge(new Hasher() {

        @Override
        public OfInt iterator(final Shape shape) {
            return Arrays.stream(new int[] { 0 }).iterator();
        }

        @Override
        public HashFunctionIdentity getHashFunctionIdentity() {
            return shape.getHashFunctionIdentity();
        }
    });
    assertArrayEquals(new long[] { 1L }, filter.getBits());
}
Also used : OfInt(java.util.PrimitiveIterator.OfInt) Hasher(org.apache.commons.collections4.bloomfilter.hasher.Hasher) DynamicHasher(org.apache.commons.collections4.bloomfilter.hasher.DynamicHasher) Shape(org.apache.commons.collections4.bloomfilter.hasher.Shape) HashFunctionIdentity(org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity) Test(org.junit.jupiter.api.Test)

Example 2 with OfInt

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

the class DynamicHasherBuilderTest method buildTest_UnencodedString.

/**
 * Tests that hashing a string works as expected.
 */
@Test
public void buildTest_UnencodedString() {
    final byte[] bytes = testString.getBytes(StandardCharsets.UTF_16LE);
    final DynamicHasher hasher = builder.withUnencoded(testString).build();
    final int expected = (int) Math.floorMod((long) hf.apply(bytes, 0), (long) shape.getNumberOfBits());
    final OfInt iter = hasher.iterator(shape);
    assertTrue(iter.hasNext());
    assertEquals(expected, iter.nextInt());
    assertFalse(iter.hasNext());
}
Also used : OfInt(java.util.PrimitiveIterator.OfInt) Test(org.junit.jupiter.api.Test)

Example 3 with OfInt

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

the class DynamicHasherBuilderTest method buildTest_String.

/**
 * Tests that hashing a string works as expected.
 */
@Test
public void buildTest_String() {
    final byte[] bytes = testString.getBytes(StandardCharsets.UTF_8);
    final DynamicHasher hasher = builder.with(testString, StandardCharsets.UTF_8).build();
    final int expected = (int) Math.floorMod((long) hf.apply(bytes, 0), (long) shape.getNumberOfBits());
    final OfInt iter = hasher.iterator(shape);
    assertTrue(iter.hasNext());
    assertEquals(expected, iter.nextInt());
    assertFalse(iter.hasNext());
}
Also used : OfInt(java.util.PrimitiveIterator.OfInt) Test(org.junit.jupiter.api.Test)

Example 4 with OfInt

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

the class DynamicHasherTest method testGetBits.

/**
 * Tests that the expected bits are returned from hashing.
 */
@Test
public void testGetBits() {
    final int[] expected = { 6, 69, 44, 19, 10, 57, 48, 23, 70, 61, 36, 11, 2, 49, 24, 15, 62 };
    final Hasher hasher = builder.with("Hello", 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());
}
Also used : OfInt(java.util.PrimitiveIterator.OfInt) Test(org.junit.jupiter.api.Test)

Example 5 with OfInt

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

the class StaticHasherTest method testGetBits_DuplicateValues.

/**
 * Tests that iterator does not return duplicates and orders the indices.
 */
@Test
public void testGetBits_DuplicateValues() {
    final int[] input = { 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 int[] expected = { 1, 2, 3, 6, 7, 10, 11, 13, 15, 17, 19, 23, 24, 25, 35, 36, 39, 43, 44, 45, 48, 49, 53, 55, 57, 59, 61, 62, 63, 65, 69, 70 };
    final StaticHasher hasher = new StaticHasher(Arrays.stream(input).iterator(), 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