Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "mozilla/Assertions.h" |
michael@0 | 7 | #include "mozilla/BloomFilter.h" |
michael@0 | 8 | |
michael@0 | 9 | #include <stddef.h> |
michael@0 | 10 | #include <stdio.h> |
michael@0 | 11 | |
michael@0 | 12 | using mozilla::BloomFilter; |
michael@0 | 13 | |
michael@0 | 14 | class FilterChecker |
michael@0 | 15 | { |
michael@0 | 16 | public: |
michael@0 | 17 | FilterChecker(uint32_t hash) : mHash(hash) { } |
michael@0 | 18 | |
michael@0 | 19 | uint32_t hash() const { return mHash; } |
michael@0 | 20 | |
michael@0 | 21 | private: |
michael@0 | 22 | uint32_t mHash; |
michael@0 | 23 | }; |
michael@0 | 24 | |
michael@0 | 25 | int |
michael@0 | 26 | main() |
michael@0 | 27 | { |
michael@0 | 28 | BloomFilter<12, FilterChecker> *filter = new BloomFilter<12, FilterChecker>(); |
michael@0 | 29 | MOZ_RELEASE_ASSERT(filter); |
michael@0 | 30 | |
michael@0 | 31 | FilterChecker one(1); |
michael@0 | 32 | FilterChecker two(0x20000); |
michael@0 | 33 | FilterChecker many(0x10000); |
michael@0 | 34 | FilterChecker multiple(0x20001); |
michael@0 | 35 | |
michael@0 | 36 | filter->add(&one); |
michael@0 | 37 | MOZ_RELEASE_ASSERT(filter->mightContain(&one), |
michael@0 | 38 | "Filter should contain 'one'"); |
michael@0 | 39 | |
michael@0 | 40 | MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple), |
michael@0 | 41 | "Filter claims to contain 'multiple' when it should not"); |
michael@0 | 42 | |
michael@0 | 43 | MOZ_RELEASE_ASSERT(filter->mightContain(&many), |
michael@0 | 44 | "Filter should contain 'many' (false positive)"); |
michael@0 | 45 | |
michael@0 | 46 | filter->add(&two); |
michael@0 | 47 | MOZ_RELEASE_ASSERT(filter->mightContain(&multiple), |
michael@0 | 48 | "Filter should contain 'multiple' (false positive)"); |
michael@0 | 49 | |
michael@0 | 50 | // Test basic removals |
michael@0 | 51 | filter->remove(&two); |
michael@0 | 52 | MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple), |
michael@0 | 53 | "Filter claims to contain 'multiple' when it should not after two " |
michael@0 | 54 | "was removed"); |
michael@0 | 55 | |
michael@0 | 56 | // Test multiple addition/removal |
michael@0 | 57 | const size_t FILTER_SIZE = 255; |
michael@0 | 58 | for (size_t i = 0; i < FILTER_SIZE - 1; ++i) |
michael@0 | 59 | filter->add(&two); |
michael@0 | 60 | |
michael@0 | 61 | MOZ_RELEASE_ASSERT(filter->mightContain(&multiple), |
michael@0 | 62 | "Filter should contain 'multiple' after 'two' added lots of times " |
michael@0 | 63 | "(false positive)"); |
michael@0 | 64 | |
michael@0 | 65 | for (size_t i = 0; i < FILTER_SIZE - 1; ++i) |
michael@0 | 66 | filter->remove(&two); |
michael@0 | 67 | |
michael@0 | 68 | MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple), |
michael@0 | 69 | "Filter claims to contain 'multiple' when it should not after two " |
michael@0 | 70 | "was removed lots of times"); |
michael@0 | 71 | |
michael@0 | 72 | // Test overflowing the filter buckets |
michael@0 | 73 | for (size_t i = 0; i < FILTER_SIZE + 1; ++i) |
michael@0 | 74 | filter->add(&two); |
michael@0 | 75 | |
michael@0 | 76 | MOZ_RELEASE_ASSERT(filter->mightContain(&multiple), |
michael@0 | 77 | "Filter should contain 'multiple' after 'two' added lots more " |
michael@0 | 78 | "times (false positive)"); |
michael@0 | 79 | |
michael@0 | 80 | for (size_t i = 0; i < FILTER_SIZE + 1; ++i) |
michael@0 | 81 | filter->remove(&two); |
michael@0 | 82 | |
michael@0 | 83 | MOZ_RELEASE_ASSERT(filter->mightContain(&multiple), |
michael@0 | 84 | "Filter claims to not contain 'multiple' even though we should " |
michael@0 | 85 | "have run out of space in the buckets (false positive)"); |
michael@0 | 86 | MOZ_RELEASE_ASSERT(filter->mightContain(&two), |
michael@0 | 87 | "Filter claims to not contain 'two' even though we should have " |
michael@0 | 88 | "run out of space in the buckets (false positive)"); |
michael@0 | 89 | |
michael@0 | 90 | filter->remove(&one); |
michael@0 | 91 | |
michael@0 | 92 | MOZ_RELEASE_ASSERT(!filter->mightContain(&one), |
michael@0 | 93 | "Filter should not contain 'one', because we didn't overflow its " |
michael@0 | 94 | "bucket"); |
michael@0 | 95 | |
michael@0 | 96 | filter->clear(); |
michael@0 | 97 | |
michael@0 | 98 | MOZ_RELEASE_ASSERT(!filter->mightContain(&multiple), |
michael@0 | 99 | "clear() failed to work"); |
michael@0 | 100 | |
michael@0 | 101 | return 0; |
michael@0 | 102 | } |