bf.put(Integer.toString(i)); assertApproximateElementCountGuess(bf, numInsertions); assertTrue(bf.mightContain(Integer.toString(i))); for (int i = 1; i < 900; i += 2) { if (!falsePositives.contains(i)) { assertFalse("BF should not contain " + i, bf.mightContain(Integer.toString(i))); assertEquals(knownNumberOfFalsePositives, numFpp); double expectedReportedFpp = (double) knownNumberOfFalsePositives / numInsertions; double actualReportedFpp = bf.expectedFpp(); assertEquals(expectedReportedFpp, actualReportedFpp, 0.00033);
public void testCopy() { BloomFilter<String> original = BloomFilter.create(Funnels.unencodedCharsFunnel(), 100); BloomFilter<String> copy = original.copy(); assertNotSame(original, copy); assertEquals(original, copy); }
/** Sanity checking with many combinations of false positive rates and expected insertions */ public void testBasic() { for (double fpr = 0.0000001; fpr < 0.1; fpr *= 10) { for (int expectedInsertions = 1; expectedInsertions <= 10000; expectedInsertions *= 10) { checkSanity(BloomFilter.create(HashTestUtils.BAD_FUNNEL, expectedInsertions, fpr)); } } }
private static void checkSanity(BloomFilter<Object> bf) { assertFalse(bf.mightContain(new Object())); assertFalse(bf.apply(new Object())); for (int i = 0; i < 100; i++) { Object o = new Object(); bf.put(o); assertTrue(bf.mightContain(o)); assertTrue(bf.apply(o)); } }
public void testExpectedFpp() { BloomFilter<Object> bf = BloomFilter.create(HashTestUtils.BAD_FUNNEL, 10, 0.03); double fpp = bf.expectedFpp(); assertEquals(0.0, fpp); // usually completed in less than 200 iterations while (fpp != 1.0) { boolean changed = bf.put(new Object()); double newFpp = bf.expectedFpp(); // if changed, the new fpp is strictly higher, otherwise it is the same assertTrue(changed ? newFpp > fpp : newFpp == fpp); fpp = newFpp; } }
/** Tests that we always get a non-negative optimal size. */ public void testOptimalSize() { for (int n = 1; n < 1000; n++) { for (double fpp = Double.MIN_VALUE; fpp < 1.0; fpp += 0.001) { assertTrue(BloomFilter.optimalNumOfBits(n, fpp) >= 0); } } // some random values Random random = new Random(0); for (int repeats = 0; repeats < 10000; repeats++) { assertTrue(BloomFilter.optimalNumOfBits(random.nextInt(1 << 16), random.nextDouble()) >= 0); } // and some crazy values (this used to be capped to Integer.MAX_VALUE, now it can go bigger assertEquals(3327428144502L, BloomFilter.optimalNumOfBits(Integer.MAX_VALUE, Double.MIN_VALUE)); try { BloomFilter<String> unused = BloomFilter.create(HashTestUtils.BAD_FUNNEL, Integer.MAX_VALUE, Double.MIN_VALUE); fail("we can't represent such a large BF!"); } catch (IllegalArgumentException expected) { assertThat(expected) .hasMessageThat() .isEqualTo("Could not create BloomFilter of 3327428144502 bits"); } }
public void testPutAllWithSelf() { BloomFilter<Integer> bf1 = BloomFilter.create(Funnels.integerFunnel(), 1); try { assertFalse(bf1.isCompatible(bf1)); bf1.putAll(bf1); fail(); } catch (IllegalArgumentException expected) { } }
public void testOptimalNumOfHashFunctionsRounding() { assertEquals(7, BloomFilter.optimalNumOfHashFunctions(319, 3072)); }
/** Tests that we never get an optimal hashes number of zero. */ public void testOptimalHashes() { for (int n = 1; n < 1000; n++) { for (int m = 0; m < 1000; m++) { assertTrue(BloomFilter.optimalNumOfHashFunctions(n, m) > 0); } } }
public void testFailureWhenMoreThan255HashFunctionsAreNeeded() { try { int n = 1000; double p = 0.00000000000000000000000000000000000000000000000000000000000000000000000000000001; BloomFilter.create(Funnels.unencodedCharsFunnel(), n, p); fail(); } catch (IllegalArgumentException expected) { } }
public void testApproximateElementCount() { int numInsertions = 1000; BloomFilter<Integer> bf = BloomFilter.create(Funnels.integerFunnel(), numInsertions); bf.put(-1); for (int i = 0; i < numInsertions; i++) { bf.put(i); } assertApproximateElementCountGuess(bf, numInsertions); }
/** * Tests that we always get a non-negative optimal size. */ public void testOptimalSize() { for (int n = 1; n < 1000; n++) { for (double fpp = Double.MIN_VALUE; fpp < 1.0; fpp += 0.001) { assertTrue(BloomFilter.optimalNumOfBits(n, fpp) >= 0); } } // some random values Random random = new Random(0); for (int repeats = 0; repeats < 10000; repeats++) { assertTrue(BloomFilter.optimalNumOfBits(random.nextInt(1 << 16), random.nextDouble()) >= 0); } // and some crazy values (this used to be capped to Integer.MAX_VALUE, now it can go bigger assertEquals(3327428144502L, BloomFilter.optimalNumOfBits( Integer.MAX_VALUE, Double.MIN_VALUE)); try { BloomFilter<String> unused = BloomFilter.create(HashTestUtils.BAD_FUNNEL, Integer.MAX_VALUE, Double.MIN_VALUE); fail("we can't represent such a large BF!"); } catch (IllegalArgumentException expected) { assertThat(expected).hasMessage("Could not create BloomFilter of 3327428144502 bits"); } }
public void testPutAll() { int element1 = 1; int element2 = 2; BloomFilter<Integer> bf1 = BloomFilter.create(Funnels.integerFunnel(), 100); bf1.put(element1); assertTrue(bf1.mightContain(element1)); assertFalse(bf1.mightContain(element2)); BloomFilter<Integer> bf2 = BloomFilter.create(Funnels.integerFunnel(), 100); bf2.put(element2); assertFalse(bf2.mightContain(element1)); assertTrue(bf2.mightContain(element2)); assertTrue(bf1.isCompatible(bf2)); bf1.putAll(bf2); assertTrue(bf1.mightContain(element1)); assertTrue(bf1.mightContain(element2)); assertFalse(bf2.mightContain(element1)); assertTrue(bf2.mightContain(element2)); }
public void testPutAllDifferentSizes() { BloomFilter<Integer> bf1 = BloomFilter.create(Funnels.integerFunnel(), 1); BloomFilter<Integer> bf2 = BloomFilter.create(Funnels.integerFunnel(), 10); try { assertFalse(bf1.isCompatible(bf2)); bf1.putAll(bf2); fail(); } catch (IllegalArgumentException expected) { } try { assertFalse(bf2.isCompatible(bf1)); bf2.putAll(bf1); fail(); } catch (IllegalArgumentException expected) { } }
public void testJavaSerialization() { BloomFilter<byte[]> bf = BloomFilter.create(Funnels.byteArrayFunnel(), 100); for (int i = 0; i < 10; i++) { bf.put(Ints.toByteArray(i)); } BloomFilter<byte[]> copy = SerializableTester.reserialize(bf); for (int i = 0; i < 10; i++) { assertTrue(copy.mightContain(Ints.toByteArray(i))); } assertEquals(bf.expectedFpp(), copy.expectedFpp()); SerializableTester.reserializeAndAssert(bf); }
/** * This test will fail whenever someone updates/reorders the BloomFilterStrategies constants. Only * appending a new constant is allowed. */ public void testBloomFilterStrategies() { assertThat(BloomFilterStrategies.values()).hasLength(2); assertEquals(BloomFilterStrategies.MURMUR128_MITZ_32, BloomFilterStrategies.values()[0]); assertEquals(BloomFilterStrategies.MURMUR128_MITZ_64, BloomFilterStrategies.values()[1]); }
public void testPutReturnValue() { for (int i = 0; i < 10; i++) { BloomFilter<String> bf = BloomFilter.create(Funnels.unencodedCharsFunnel(), 100); for (int j = 0; j < 10; j++) { String value = new Object().toString(); boolean mightContain = bf.mightContain(value); boolean put = bf.put(value); assertTrue(mightContain != put); } } }
public void testPreconditions() { try { BloomFilter.create(Funnels.unencodedCharsFunnel(), -1); fail(); } catch (IllegalArgumentException expected) { } try { BloomFilter.create(Funnels.unencodedCharsFunnel(), -1, 0.03); fail(); } catch (IllegalArgumentException expected) { } try { BloomFilter.create(Funnels.unencodedCharsFunnel(), 1, 0.0); fail(); } catch (IllegalArgumentException expected) { } try { BloomFilter.create(Funnels.unencodedCharsFunnel(), 1, 1.0); fail(); } catch (IllegalArgumentException expected) { } }
public void testApproximateElementCount() { int numInsertions = 1000; BloomFilter<Integer> bf = BloomFilter.create(Funnels.integerFunnel(), numInsertions); bf.put(-1); for (int i = 0; i < numInsertions; i++) { bf.put(i); } assertApproximateElementCountGuess(bf, numInsertions); }
bf.put(Integer.toString(i)); assertApproximateElementCountGuess(bf, numInsertions); assertTrue(bf.mightContain(Integer.toString(i))); for (int i = 1; i < 900; i += 2) { if (!falsePositives.contains(i)) { assertFalse("BF should not contain " + i, bf.mightContain(Integer.toString(i))); assertEquals(knownNumberOfFalsePositives, numFpp); double expectedReportedFpp = (double) knownNumberOfFalsePositives / numInsertions; double actualReportedFpp = bf.expectedFpp(); assertEquals(expectedReportedFpp, actualReportedFpp, 0.00015);