michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/BloomFilter.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: using mozilla::BloomFilter; michael@0: michael@0: class FilterChecker michael@0: { michael@0: public: michael@0: FilterChecker(uint32_t hash) : mHash(hash) { } michael@0: michael@0: uint32_t hash() const { return mHash; } michael@0: michael@0: private: michael@0: uint32_t mHash; michael@0: }; michael@0: michael@0: int michael@0: main() michael@0: { michael@0: BloomFilter<12, FilterChecker> *filter = new BloomFilter<12, FilterChecker>(); michael@0: MOZ_RELEASE_ASSERT(filter); michael@0: michael@0: FilterChecker one(1); michael@0: FilterChecker two(0x20000); michael@0: FilterChecker many(0x10000); michael@0: FilterChecker multiple(0x20001); michael@0: michael@0: filter->add(&one); michael@0: MOZ_RELEASE_ASSERT(filter->mightContain(&one), michael@0: "Filter should contain 'one'"); michael@0: michael@0: MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple), michael@0: "Filter claims to contain 'multiple' when it should not"); michael@0: michael@0: MOZ_RELEASE_ASSERT(filter->mightContain(&many), michael@0: "Filter should contain 'many' (false positive)"); michael@0: michael@0: filter->add(&two); michael@0: MOZ_RELEASE_ASSERT(filter->mightContain(&multiple), michael@0: "Filter should contain 'multiple' (false positive)"); michael@0: michael@0: // Test basic removals michael@0: filter->remove(&two); michael@0: MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple), michael@0: "Filter claims to contain 'multiple' when it should not after two " michael@0: "was removed"); michael@0: michael@0: // Test multiple addition/removal michael@0: const size_t FILTER_SIZE = 255; michael@0: for (size_t i = 0; i < FILTER_SIZE - 1; ++i) michael@0: filter->add(&two); michael@0: michael@0: MOZ_RELEASE_ASSERT(filter->mightContain(&multiple), michael@0: "Filter should contain 'multiple' after 'two' added lots of times " michael@0: "(false positive)"); michael@0: michael@0: for (size_t i = 0; i < FILTER_SIZE - 1; ++i) michael@0: filter->remove(&two); michael@0: michael@0: MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple), michael@0: "Filter claims to contain 'multiple' when it should not after two " michael@0: "was removed lots of times"); michael@0: michael@0: // Test overflowing the filter buckets michael@0: for (size_t i = 0; i < FILTER_SIZE + 1; ++i) michael@0: filter->add(&two); michael@0: michael@0: MOZ_RELEASE_ASSERT(filter->mightContain(&multiple), michael@0: "Filter should contain 'multiple' after 'two' added lots more " michael@0: "times (false positive)"); michael@0: michael@0: for (size_t i = 0; i < FILTER_SIZE + 1; ++i) michael@0: filter->remove(&two); michael@0: michael@0: MOZ_RELEASE_ASSERT(filter->mightContain(&multiple), michael@0: "Filter claims to not contain 'multiple' even though we should " michael@0: "have run out of space in the buckets (false positive)"); michael@0: MOZ_RELEASE_ASSERT(filter->mightContain(&two), michael@0: "Filter claims to not contain 'two' even though we should have " michael@0: "run out of space in the buckets (false positive)"); michael@0: michael@0: filter->remove(&one); michael@0: michael@0: MOZ_RELEASE_ASSERT(!filter->mightContain(&one), michael@0: "Filter should not contain 'one', because we didn't overflow its " michael@0: "bucket"); michael@0: michael@0: filter->clear(); michael@0: michael@0: MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple), michael@0: "clear() failed to work"); michael@0: michael@0: return 0; michael@0: }