Beispiel #1
0
int
main()
{
  BloomFilter<12, FilterChecker> *filter = new BloomFilter<12, FilterChecker>();
  MOZ_RELEASE_ASSERT(filter);

  FilterChecker one(1);
  FilterChecker two(0x20000);
  FilterChecker many(0x10000);
  FilterChecker multiple(0x20001);

  filter->add(&one);
  MOZ_RELEASE_ASSERT(filter->mightContain(&one),
             "Filter should contain 'one'");

  MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple),
             "Filter claims to contain 'multiple' when it should not");

  MOZ_RELEASE_ASSERT(filter->mightContain(&many),
             "Filter should contain 'many' (false positive)");

  filter->add(&two);
  MOZ_RELEASE_ASSERT(filter->mightContain(&multiple),
             "Filter should contain 'multiple' (false positive)");

  // Test basic removals
  filter->remove(&two);
  MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple),
             "Filter claims to contain 'multiple' when it should not after two "
             "was removed");

  // Test multiple addition/removal
  const size_t FILTER_SIZE = 255;
  for (size_t i = 0; i < FILTER_SIZE - 1; ++i)
    filter->add(&two);

  MOZ_RELEASE_ASSERT(filter->mightContain(&multiple),
             "Filter should contain 'multiple' after 'two' added lots of times "
             "(false positive)");

  for (size_t i = 0; i < FILTER_SIZE - 1; ++i)
    filter->remove(&two);

  MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple),
             "Filter claims to contain 'multiple' when it should not after two "
             "was removed lots of times");

  // Test overflowing the filter buckets
  for (size_t i = 0; i < FILTER_SIZE + 1; ++i)
    filter->add(&two);

  MOZ_RELEASE_ASSERT(filter->mightContain(&multiple),
             "Filter should contain 'multiple' after 'two' added lots more "
             "times (false positive)");

  for (size_t i = 0; i < FILTER_SIZE + 1; ++i)
    filter->remove(&two);

  MOZ_RELEASE_ASSERT(filter->mightContain(&multiple),
             "Filter claims to not contain 'multiple' even though we should "
             "have run out of space in the buckets (false positive)");
  MOZ_RELEASE_ASSERT(filter->mightContain(&two),
             "Filter claims to not contain 'two' even though we should have "
             "run out of space in the buckets (false positive)");

  filter->remove(&one);

  MOZ_RELEASE_ASSERT(!filter->mightContain(&one),
             "Filter should not contain 'one', because we didn't overflow its "
             "bucket");

  filter->clear();

  MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple),
             "clear() failed to work");

  return 0;
}
int main()
{
  BloomFilter<12, FilterChecker> *filter = new BloomFilter<12, FilterChecker>();

  FilterChecker one(1);
  FilterChecker two(0x20000);
  FilterChecker many(0x10000);
  FilterChecker multiple(0x20001);

  filter->add(&one);
  if (!filter->mayContain(&one)) {
    fail("Filter should contain 'one'");
    return -1;
  }

  if (filter->mayContain(&multiple)) {
    fail("Filter claims to contain 'multiple' when it should not");
    return -1;
  }

  if (!filter->mayContain(&many)) {
    fail("Filter should contain 'many' (false positive)");
    return -1;
  }

  filter->add(&two);
  if (!filter->mayContain(&multiple)) {
    fail("Filter should contain 'multiple' (false positive)");
    return -1;
  }

  // Test basic removals
  filter->remove(&two);
  if (filter->mayContain(&multiple)) {
    fail("Filter claims to contain 'multiple' when it should not after two was "
         "removed");
    return -1;
  }

  // Test multiple addition/removal
  const unsigned FILTER_SIZE = 255;
  for (unsigned i = 0; i < FILTER_SIZE - 1; ++i) {
    filter->add(&two);
  }
  if (!filter->mayContain(&multiple)) {
    fail("Filter should contain 'multiple' after 'two' added lots of times "
         "(false positive)");
    return -1;
  }
  for (unsigned i = 0; i < FILTER_SIZE - 1; ++i) {
    filter->remove(&two);
  }
  if (filter->mayContain(&multiple)) {
    fail("Filter claims to contain 'multiple' when it should not after two was "
         "removed lots of times");
    return -1;
  }

  // Test overflowing the filter buckets
  for (unsigned i = 0; i < FILTER_SIZE + 1; ++i) {
    filter->add(&two);
  }
  if (!filter->mayContain(&multiple)) {
    fail("Filter should contain 'multiple' after 'two' added lots more times "
         "(false positive)");
    return -1;
  }
  for (unsigned i = 0; i < FILTER_SIZE + 1; ++i) {
    filter->remove(&two);
  }
  if (!filter->mayContain(&multiple)) {
    fail("Filter claims to not contain 'multiple' even though we should have "
         "run out of space in the buckets (false positive)");
    return -1;
  }
  if (!filter->mayContain(&two)) {
    fail("Filter claims to not contain 'two' even though we should have run "
         "out of space in the buckets (false positive)");
    return -1;
  }

  filter->remove(&one);
  if (filter->mayContain(&one)) {
    fail("Filter should not contain 'one', because we didn't overflow its "
         "bucket");
    return -1;
  }
  
  filter->clear();
  if (filter->mayContain(&multiple)) {
    fail("clear() failed to work");
    return -1;
  }

  return 0;
}