]> arthur.barton.de Git - bup.git/blob - lib/bup/t/thashsplit.py
62d973e6410a51ca55e3d4ff6b86709388c3c511
[bup.git] / lib / bup / t / thashsplit.py
1 from bup import hashsplit, _helpers
2 from wvtest import *
3 from cStringIO import StringIO
4
5 @wvtest
6 def test_rolling_sums():
7     WVPASS(_helpers.selftest())
8
9 @wvtest
10 def test_fanout_behaviour():
11
12     # Drop in replacement for bupsplit, but splitting if the int value of a
13     # byte >= BUP_BLOBBITS
14     basebits = _helpers.blobbits()
15     def splitbuf(buf):
16         ofs = 0
17         for c in buf:
18             ofs += 1
19             if ord(c) >= basebits:
20                 return ofs, ord(c)
21         return 0, 0
22
23     old_splitbuf = _helpers.splitbuf
24     _helpers.splitbuf = splitbuf
25     old_BLOB_MAX = hashsplit.BLOB_MAX
26     hashsplit.BLOB_MAX = 4
27     old_BLOB_READ_SIZE = hashsplit.BLOB_READ_SIZE
28     hashsplit.BLOB_READ_SIZE = 10
29     old_fanout = hashsplit.fanout
30     hashsplit.fanout = 2
31
32     levels = lambda f: [(len(b), l) for b, l in
33         hashsplit.hashsplit_iter([f], True, None)]
34     # Return a string of n null bytes
35     z = lambda n: '\x00' * n
36     # Return a byte which will be split with a level of n
37     sb = lambda n: chr(basebits + n)
38
39     split_never = StringIO(z(16))
40     split_first = StringIO(z(1) + sb(3) + z(14))
41     split_end   = StringIO(z(13) + sb(1) + z(2))
42     split_many  = StringIO(sb(1) + z(3) + sb(2) + z(4) +
43                             sb(0) + z(4) + sb(5) + z(1))
44     WVPASSEQ(levels(split_never), [(4, 0), (4, 0), (4, 0), (4, 0)])
45     WVPASSEQ(levels(split_first), [(2, 3), (4, 0), (4, 0), (4, 0), (2, 0)])
46     WVPASSEQ(levels(split_end), [(4, 0), (4, 0), (4, 0), (2, 1), (2, 0)])
47     WVPASSEQ(levels(split_many),
48         [(1, 1), (4, 2), (4, 0), (1, 0), (4, 0), (1, 5), (1, 0)])
49
50     _helpers.splitbuf = old_splitbuf
51     hashsplit.BLOB_MAX = old_BLOB_MAX
52     hashsplit.BLOB_READ_SIZE = old_BLOB_READ_SIZE
53     hashsplit.fanout = old_fanout