예제 #1
0
 @SuppressWarnings("CheckReturnValue")
 @AndroidIncompatible // OutOfMemoryError
 public void testLargeNumberOfInsertions() {
   // We use horrible FPPs here to keep Java from OOM'ing
   BloomFilter.create(Funnels.unencodedCharsFunnel(), 42L + Integer.MAX_VALUE, 0.28);
   BloomFilter.create(Funnels.unencodedCharsFunnel(), 50L * Integer.MAX_VALUE, 0.99);
 }
예제 #2
0
  public void testEquals() {
    BloomFilter<String> bf1 = BloomFilter.create(Funnels.unencodedCharsFunnel(), 100);
    bf1.put("1");
    bf1.put("2");

    BloomFilter<String> bf2 = BloomFilter.create(Funnels.unencodedCharsFunnel(), 100);
    bf2.put("1");
    bf2.put("2");

    new EqualsTester().addEqualityGroup(bf1, bf2).testEquals();

    bf2.put("3");

    new EqualsTester().addEqualityGroup(bf1).addEqualityGroup(bf2).testEquals();
  }
예제 #3
0
 public void testPutAllWithSelf() {
   BloomFilter<Integer> bf1 = BloomFilter.create(Funnels.integerFunnel(), 1);
   try {
     assertFalse(bf1.isCompatible(bf1));
     bf1.putAll(bf1);
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
예제 #4
0
  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) {
    }
  }
예제 #5
0
 public void testEquals_empty() {
   new EqualsTester()
       .addEqualityGroup(BloomFilter.create(Funnels.byteArrayFunnel(), 100, 0.01))
       .addEqualityGroup(BloomFilter.create(Funnels.byteArrayFunnel(), 100, 0.02))
       .addEqualityGroup(BloomFilter.create(Funnels.byteArrayFunnel(), 200, 0.01))
       .addEqualityGroup(BloomFilter.create(Funnels.byteArrayFunnel(), 200, 0.02))
       .addEqualityGroup(BloomFilter.create(Funnels.unencodedCharsFunnel(), 100, 0.01))
       .addEqualityGroup(BloomFilter.create(Funnels.unencodedCharsFunnel(), 100, 0.02))
       .addEqualityGroup(BloomFilter.create(Funnels.unencodedCharsFunnel(), 200, 0.01))
       .addEqualityGroup(BloomFilter.create(Funnels.unencodedCharsFunnel(), 200, 0.02))
       .testEquals();
 }
예제 #6
0
 public void testBitSize() {
   double fpp = 0.03;
   for (int i = 1; i < 10000; i++) {
     long numBits = BloomFilter.optimalNumOfBits(i, fpp);
     int arraySize = Ints.checkedCast(LongMath.divide(numBits, 64, RoundingMode.CEILING));
     assertEquals(
         arraySize * Long.SIZE,
         BloomFilter.create(Funnels.unencodedCharsFunnel(), i, fpp).bitSize());
   }
 }
예제 #7
0
 @SuppressWarnings("CheckReturnValue")
 public void testFailureWhenMoreThan255HashFunctionsAreNeeded() {
   try {
     int n = 1000;
     double p = 0.00000000000000000000000000000000000000000000000000000000000000000000000000000001;
     BloomFilter.create(Funnels.unencodedCharsFunnel(), n, p);
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
예제 #8
0
 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);
     }
   }
 }
예제 #9
0
  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));
  }
예제 #10
0
  public void testCustomSerialization() throws Exception {
    Funnel<byte[]> funnel = Funnels.byteArrayFunnel();
    BloomFilter<byte[]> bf = BloomFilter.create(funnel, 100);
    for (int i = 0; i < 100; i++) {
      bf.put(Ints.toByteArray(i));
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bf.writeTo(out);

    assertEquals(bf, BloomFilter.readFrom(new ByteArrayInputStream(out.toByteArray()), funnel));
  }
예제 #11
0
  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);
  }
예제 #12
0
 @SuppressWarnings("CheckReturnValue")
 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) {
   }
 }
예제 #13
0
  public void testBloom() throws Exception {
    int numInsertions = 1000000;
    double fpp = 0.03D;
    Random random = new Random(1L);

    BloomFilter<Long> filter = BloomFilter.create(Funnels.longFunnel(), numInsertions, fpp);
    for (int l = 0; l < numInsertions; l++) {
      filter.put(random.nextLong());
    }

    random = new Random(1L);
    for (int l = 0; l < numInsertions; l++) {
      assertTrue(filter.mightContain(random.nextLong()));
    }
  }
예제 #14
0
  public void testCreateAndCheckMitz32BloomFilterWithKnownFalsePositives() {
    int numInsertions = 1000000;
    BloomFilter<String> bf =
        BloomFilter.create(
            Funnels.unencodedCharsFunnel(),
            numInsertions,
            0.03,
            BloomFilterStrategies.MURMUR128_MITZ_32);

    // Insert "numInsertions" even numbers into the BF.
    for (int i = 0; i < numInsertions * 2; i += 2) {
      bf.put(Integer.toString(i));
    }

    // Assert that the BF "might" have all of the even numbers.
    for (int i = 0; i < numInsertions * 2; i += 2) {
      assertTrue(bf.mightContain(Integer.toString(i)));
    }

    // Now we check for known false positives using a set of known false positives.
    // (These are all of the false positives under 900.)
    ImmutableSet<Integer> falsePositives =
        ImmutableSet.of(
            49, 51, 59, 163, 199, 321, 325, 363, 367, 469, 545, 561, 727, 769, 773, 781);
    for (int i = 1; i < 900; i += 2) {
      if (!falsePositives.contains(i)) {
        assertFalse("BF should not contain " + i, bf.mightContain(Integer.toString(i)));
      }
    }

    // Check that there are exactly 29824 false positives for this BF.
    int knownNumberOfFalsePositives = 29824;
    int numFpp = 0;
    for (int i = 1; i < numInsertions * 2; i += 2) {
      if (bf.mightContain(Integer.toString(i))) {
        numFpp++;
      }
    }
    assertEquals(knownNumberOfFalsePositives, numFpp);
    double actualFpp = (double) knownNumberOfFalsePositives / numInsertions;
    double expectedFpp = bf.expectedFpp();
    // The normal order of (expected, actual) is reversed here on purpose.
    assertEquals(actualFpp, expectedFpp, 0.00015);
  }
예제 #15
0
  public void testCuckoo() throws Exception {
    int numInsertions = 1000000;
    double fpp = 0.03D;
    Random random = new Random(1L);

    CuckooFilter<Long> filter = CuckooFilter.create(Funnels.longFunnel(), numInsertions, fpp);

    for (int l = 0; l < numInsertions; l++) {
      final long nextLong = random.nextLong();
      assertTrue("Filter should put " + nextLong, filter.put(nextLong));
      assertTrue(
          "Filter should mightContain " + nextLong + " just after adding",
          filter.mightContain(nextLong));
      assertEquals(
          "Filter size should be the same as the number of insertions", l + 1, filter.size());
    }

    random = new Random(1L);
    for (int l = 0; l < numInsertions; l++) {
      final long nextLong = random.nextLong();
      assertTrue(
          "Filter should mightContain " + nextLong + " after adding a while ago",
          filter.mightContain(nextLong));
    }

    random = new Random(1L);
    for (int l = 0; l < numInsertions; l++) {
      final long nextLong = random.nextLong();
      assertTrue(
          "Filter should delete " + nextLong + " after adding a while ago",
          filter.delete(nextLong));
      assertEquals(
          "Filter size should be the same as the number of insertions less the "
              + "number of deletions",
          numInsertions - l - 1,
          filter.size());
    }

    final long nextLong = random.nextLong();
    assertFalse(
        "Filter should NOT delete " + nextLong + " since it should be empty!!",
        filter.delete(nextLong));
  }
예제 #16
0
  public void testCreateAndCheckBloomFilterWithKnownUtf8FalsePositives64() {
    int numInsertions = 1000000;
    BloomFilter<String> bf =
        BloomFilter.create(
            Funnels.stringFunnel(UTF_8),
            numInsertions,
            0.03,
            BloomFilterStrategies.MURMUR128_MITZ_64);

    // Insert "numInsertions" even numbers into the BF.
    for (int i = 0; i < numInsertions * 2; i += 2) {
      bf.put(Integer.toString(i));
    }

    // Assert that the BF "might" have all of the even numbers.
    for (int i = 0; i < numInsertions * 2; i += 2) {
      assertTrue(bf.mightContain(Integer.toString(i)));
    }

    // Now we check for known false positives using a set of known false positives.
    // (These are all of the false positives under 900.)
    ImmutableSet<Integer> falsePositives = ImmutableSet.of(129, 471, 723, 89, 751, 835, 871);
    for (int i = 1; i < 900; i += 2) {
      if (!falsePositives.contains(i)) {
        assertFalse("BF should not contain " + i, bf.mightContain(Integer.toString(i)));
      }
    }

    // Check that there are exactly 29763 false positives for this BF.
    int knownNumberOfFalsePositives = 29763;
    int numFpp = 0;
    for (int i = 1; i < numInsertions * 2; i += 2) {
      if (bf.mightContain(Integer.toString(i))) {
        numFpp++;
      }
    }
    assertEquals(knownNumberOfFalsePositives, numFpp);
    double actualFpp = (double) knownNumberOfFalsePositives / numInsertions;
    double expectedFpp = bf.expectedFpp();
    // The normal order of (expected, actual) is reversed here on purpose.
    assertEquals(actualFpp, expectedFpp, 0.00033);
  }
예제 #17
0
 public void testNullPointers() {
   NullPointerTester tester = new NullPointerTester();
   tester.testAllPublicInstanceMethods(BloomFilter.create(Funnels.unencodedCharsFunnel(), 100));
   tester.testAllPublicStaticMethods(BloomFilter.class);
 }
예제 #18
0
 public void testCopy() {
   BloomFilter<String> original = BloomFilter.create(Funnels.unencodedCharsFunnel(), 100);
   BloomFilter<String> copy = original.copy();
   assertNotSame(original, copy);
   assertEquals(original, copy);
 }