]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tbloom.py
Move bloom-related stuff from git.py to a new bloom.py.
[bup.git] / lib / bup / t / tbloom.py
1 import tempfile
2 from bup import bloom
3 from bup.helpers import *
4 from wvtest import *
5
6 @wvtest
7 def test_bloom():
8     hashes = [os.urandom(20) for i in range(100)]
9     class Idx:
10         pass
11     ix = Idx()
12     ix.name='dummy.idx'
13     ix.shatable = ''.join(hashes)
14     for k in (4, 5):
15         b = bloom.ShaBloom.create('pybuptest.bloom', expected=100, k=k)
16         b.add_idx(ix)
17         WVPASSLT(b.pfalse_positive(), .1)
18         b.close()
19         b = bloom.ShaBloom('pybuptest.bloom')
20         all_present = True
21         for h in hashes:
22             all_present &= b.exists(h)
23         WVPASS(all_present)
24         false_positives = 0
25         for h in [os.urandom(20) for i in range(1000)]:
26             if b.exists(h):
27                 false_positives += 1
28         WVPASSLT(false_positives, 5)
29         os.unlink('pybuptest.bloom')
30
31     tf = tempfile.TemporaryFile()
32     b = bloom.ShaBloom.create('bup.bloom', f=tf, expected=100)
33     WVPASSEQ(b.rwfile, tf)
34     WVPASSEQ(b.k, 5)
35     tf = tempfile.TemporaryFile()
36     b = bloom.ShaBloom.create('bup.bloom', f=tf, expected=2**28,
37                               delaywrite=False)
38     WVPASSEQ(b.k, 4)