LCOV - code coverage report
Current view: directory - xpcom/tests - TestBloomFilter.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 61 37 60.7 %
Date: 2012-06-02 Functions: 3 3 100.0 %

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

Generated by: LCOV version 1.7