def test_simple_bloomfilter():
    bf = BloomFilter(100)
    bf.add('test')
    assert 'test' in bf

    bf.add('anothertest')
    assert 'anothertest' in bf
    assert 'notthere' not in bf
def test_bloomfilter_urls(n, r):
    bf = BloomFilter(n, error_rate=r)
    misses = []
    for key in xrange(n):
        url = generate_url(key)
        if url in bf:
            # false positive!
            misses.append(key)
        else:
            bf.add(url)

    observed_count = len(misses)
    observed_rate = observed_count / float(n)

    assert observed_rate <= r
def test_bloom_filter(capacity, max_keys):
    replays = randint(0, max_keys)

    bf = BloomFilter(capacity)

    keys = []
    errors = []
    for i in range(replays):
        key = 'bloom-filter-key-%s' % i
        keys.append(key)
        bf.add(key)

    for key in keys:
        assert key in bf
        if 'this-should-not-%s' % key in bf:
            errors.append(1)

    actual = len(errors) / float(capacity)
    assert actual <= 0.01
    def __init__(self, capacity, error_rate=0.001, redis_host='localhost',
                 redis_port=6379, redis_prefix='bloomfilter'):

        RedisHashing.__init__(self, redis_host, redis_port, redis_prefix)
        BloomFilter.__init__(self, capacity, error_rate)