1 from cStringIO import StringIO
5 from bup import hashsplit, _helpers, helpers
8 def nr_regions(x, max_count=None):
9 return list(hashsplit._nonresident_page_regions(bytearray(x), max_count))
12 def test_nonresident_page_regions():
13 WVPASSEQ(nr_regions([]), [])
14 WVPASSEQ(nr_regions([1]), [])
15 WVPASSEQ(nr_regions([0]), [(0, 1)])
16 WVPASSEQ(nr_regions([1, 0]), [(1, 1)])
17 WVPASSEQ(nr_regions([0, 0]), [(0, 2)])
18 WVPASSEQ(nr_regions([1, 0, 1]), [(1, 1)])
19 WVPASSEQ(nr_regions([1, 0, 0]), [(1, 2)])
20 WVPASSEQ(nr_regions([0, 1, 0]), [(0, 1), (2, 1)])
21 WVPASSEQ(nr_regions([0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0]),
22 [(0, 2), (5, 3), (9, 2)])
23 WVPASSEQ(nr_regions([2, 42, 3, 101]), [(0, 2)])
25 WVPASSEQ(nr_regions([0, 0, 0], None), [(0, 3)])
26 WVPASSEQ(nr_regions([0, 0, 0], 1), [(0, 1), (1, 1), (2, 1)])
27 WVPASSEQ(nr_regions([0, 0, 0], 2), [(0, 2), (2, 1)])
28 WVPASSEQ(nr_regions([0, 0, 0], 3), [(0, 3)])
29 WVPASSEQ(nr_regions([0, 0, 0], 4), [(0, 3)])
30 WVPASSEQ(nr_regions([0, 0, 1], None), [(0, 2)])
31 WVPASSEQ(nr_regions([0, 0, 1], 1), [(0, 1), (1, 1)])
32 WVPASSEQ(nr_regions([0, 0, 1], 2), [(0, 2)])
33 WVPASSEQ(nr_regions([0, 0, 1], 3), [(0, 2)])
34 WVPASSEQ(nr_regions([1, 0, 0], None), [(1, 2)])
35 WVPASSEQ(nr_regions([1, 0, 0], 1), [(1, 1), (2, 1)])
36 WVPASSEQ(nr_regions([1, 0, 0], 2), [(1, 2)])
37 WVPASSEQ(nr_regions([1, 0, 0], 3), [(1, 2)])
38 WVPASSEQ(nr_regions([1, 0, 0, 0, 1], None), [(1, 3)])
39 WVPASSEQ(nr_regions([1, 0, 0, 0, 1], 1), [(1, 1), (2, 1), (3, 1)])
40 WVPASSEQ(nr_regions([1, 0, 0, 0, 1], 2), [(1, 2), (3, 1)])
41 WVPASSEQ(nr_regions([1, 0, 0, 0, 1], 3), [(1, 3)])
42 WVPASSEQ(nr_regions([1, 0, 0, 0, 1], 4), [(1, 3)])
46 def test_uncache_ours_upto():
48 def mock_fadvise_pages_done(f, ofs, len):
49 history.append((f, ofs, len))
51 uncache_upto = hashsplit._uncache_ours_upto
52 page_size = helpers.sc_page_size
53 orig_pages_done = hashsplit._fadvise_pages_done
55 hashsplit._fadvise_pages_done = mock_fadvise_pages_done
57 uncache_upto(42, 0, (0, 1), iter([]))
59 uncache_upto(42, page_size, (0, 1), iter([]))
60 WVPASSEQ([(42, 0, 1)], history)
62 uncache_upto(42, page_size, (0, 3), iter([(5, 2)]))
64 uncache_upto(42, 2 * page_size, (0, 3), iter([(5, 2)]))
66 uncache_upto(42, 3 * page_size, (0, 3), iter([(5, 2)]))
67 WVPASSEQ([(42, 0, 3)], history)
69 uncache_upto(42, 5 * page_size, (0, 3), iter([(5, 2)]))
70 WVPASSEQ([(42, 0, 3)], history)
72 uncache_upto(42, 6 * page_size, (0, 3), iter([(5, 2)]))
73 WVPASSEQ([(42, 0, 3)], history)
75 uncache_upto(42, 7 * page_size, (0, 3), iter([(5, 2)]))
76 WVPASSEQ([(42, 0, 3), (42, 5, 2)], history)
78 hashsplit._fadvise_pages_done = orig_pages_done
82 def test_rolling_sums():
83 WVPASS(_helpers.selftest())
86 def test_fanout_behaviour():
88 # Drop in replacement for bupsplit, but splitting if the int value of a
89 # byte >= BUP_BLOBBITS
90 basebits = _helpers.blobbits()
95 if ord(c) >= basebits:
99 old_splitbuf = _helpers.splitbuf
100 _helpers.splitbuf = splitbuf
101 old_BLOB_MAX = hashsplit.BLOB_MAX
102 hashsplit.BLOB_MAX = 4
103 old_BLOB_READ_SIZE = hashsplit.BLOB_READ_SIZE
104 hashsplit.BLOB_READ_SIZE = 10
105 old_fanout = hashsplit.fanout
108 levels = lambda f: [(len(b), l) for b, l in
109 hashsplit.hashsplit_iter([f], True, None)]
110 # Return a string of n null bytes
111 z = lambda n: '\x00' * n
112 # Return a byte which will be split with a level of n
113 sb = lambda n: chr(basebits + n)
115 split_never = StringIO(z(16))
116 split_first = StringIO(z(1) + sb(3) + z(14))
117 split_end = StringIO(z(13) + sb(1) + z(2))
118 split_many = StringIO(sb(1) + z(3) + sb(2) + z(4) +
119 sb(0) + z(4) + sb(5) + z(1))
120 WVPASSEQ(levels(split_never), [(4, 0), (4, 0), (4, 0), (4, 0)])
121 WVPASSEQ(levels(split_first), [(2, 3), (4, 0), (4, 0), (4, 0), (2, 0)])
122 WVPASSEQ(levels(split_end), [(4, 0), (4, 0), (4, 0), (2, 1), (2, 0)])
123 WVPASSEQ(levels(split_many),
124 [(1, 1), (4, 2), (4, 0), (1, 0), (4, 0), (1, 5), (1, 0)])
126 _helpers.splitbuf = old_splitbuf
127 hashsplit.BLOB_MAX = old_BLOB_MAX
128 hashsplit.BLOB_READ_SIZE = old_BLOB_READ_SIZE
129 hashsplit.fanout = old_fanout