~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to tests/test_groupcompress.py

Bring in the 'rabin' experiment.
Change the names and disk-strings for the various repository formats.
Make the CHK format repositories all 'rich-root' we can introduce non-rich-root later.
Make a couple other small tweaks, like copyright statements, etc.
Remove patch-delta.c, at this point, it was only a reference implementation,
as we have fully integrated the patching into pyrex, to allow nicer exception
handling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import zlib
21
21
 
22
22
from bzrlib import tests
23
 
from bzrlib.osutils import sha_strings
 
23
from bzrlib.osutils import sha_string
24
24
from bzrlib.plugins.groupcompress import errors, groupcompress
25
25
from bzrlib.tests import (
26
26
    TestCaseWithTransport,
35
35
    vf_interface_tests = loader.loadTestsFromTestCase(TestVersionedFiles)
36
36
    cleanup_pack_group = groupcompress.cleanup_pack_group
37
37
    make_pack_factory = groupcompress.make_pack_factory
38
 
    group_scenario = ('groupcompress-nograph', {
 
38
    group_scenario = ('groupcompressrabin-nograph', {
39
39
            'cleanup':cleanup_pack_group,
40
40
            'factory':make_pack_factory(False, False, 1),
41
41
            'graph': False,
60
60
        # diff against NUKK
61
61
        compressor = groupcompress.GroupCompressor(True)
62
62
        sha1, end_point = compressor.compress(('label',),
63
 
            ['strange\n', 'common\n'], None)
64
 
        self.assertEqual(sha_strings(['strange\n', 'common\n']), sha1)
 
63
            'strange\ncommon\n', None)
 
64
        self.assertEqual(sha_string('strange\ncommon\n'), sha1)
65
65
        expected_lines = [
66
 
            'label: label\n',
67
 
            'sha1: %s\n' % sha1,
68
 
            'i,3\n',
69
 
            'strange\n',
70
 
            'common\n',
71
 
            '\n', # the last \n in a text is removed, which allows safe
72
 
            # serialisation of lines without trailing \n.
 
66
            'fulltext\n',
 
67
            'label:label\nsha1:%s\n' % sha1,
 
68
            'len:15\n',
 
69
            'strange\ncommon\n',
73
70
            ]
74
71
        self.assertEqual(expected_lines, compressor.lines)
75
72
        self.assertEqual(sum(map(len, expected_lines)), end_point)
76
73
 
 
74
    def _chunks_to_repr_lines(self, chunks):
 
75
        return '\n'.join(map(repr, ''.join(chunks).split('\n')))
 
76
 
 
77
    def assertEqualDiffEncoded(self, expected, actual):
 
78
        """Compare the actual content to the expected content.
 
79
 
 
80
        :param expected: A group of chunks that we expect to see
 
81
        :param actual: The measured 'chunks'
 
82
 
 
83
        We will transform the chunks back into lines, and then run 'repr()'
 
84
        over them to handle non-ascii characters.
 
85
        """
 
86
        self.assertEqualDiff(self._chunks_to_repr_lines(expected),
 
87
                             self._chunks_to_repr_lines(actual))
 
88
 
77
89
    def test_two_nosha_delta(self):
78
90
        compressor = groupcompress.GroupCompressor(True)
79
91
        sha1_1, _ = compressor.compress(('label',),
80
 
            ['strange\n', 'common long line\n'], None)
 
92
            'strange\ncommon long line\nthat needs a 16 byte match\n', None)
81
93
        expected_lines = list(compressor.lines)
82
94
        sha1_2, end_point = compressor.compress(('newlabel',),
83
 
            ['common long line\n', 'different\n'], None)
84
 
        self.assertEqual(sha_strings(['common long line\n', 'different\n']),
85
 
                         sha1_2)
 
95
            'common long line\nthat needs a 16 byte match\ndifferent\n', None)
 
96
        self.assertEqual(sha_string('common long line\n'
 
97
                                    'that needs a 16 byte match\n'
 
98
                                    'different\n'), sha1_2)
86
99
        expected_lines.extend([
87
 
            'label: newlabel\n',
88
 
            'sha1: %s\n' % sha1_2,
 
100
            'delta\n'
 
101
            'label:newlabel\n',
 
102
            'sha1:%s\n' % sha1_2,
 
103
            'len:16\n',
 
104
            # source and target length
 
105
            '\x7e\x36',
89
106
            # copy the line common
90
 
            'c,72,17\n',
 
107
            '\x91\x52\x2c', #copy, offset 0x52, len 0x2c
91
108
            # add the line different, and the trailing newline
92
 
            'i,2\n',
93
 
            'different\n',
94
 
            '\n'
 
109
            '\x0adifferent\n', # insert 10 bytes
95
110
            ])
96
 
        self.assertEqualDiff(''.join(expected_lines), ''.join(compressor.lines))
 
111
        self.assertEqualDiffEncoded(expected_lines, compressor.lines)
97
112
        self.assertEqual(sum(map(len, expected_lines)), end_point)
98
113
 
99
114
    def test_three_nosha_delta(self):
101
116
        # both parents.
102
117
        compressor = groupcompress.GroupCompressor(True)
103
118
        sha1_1, end_point = compressor.compress(('label',),
104
 
            ['strange\n', 'common long line\n'], None)
 
119
            'strange\ncommon very very long line\nwith some extra text\n', None)
105
120
        sha1_2, _ = compressor.compress(('newlabel',),
106
 
            ['common long line\n', 'different\n', 'moredifferent\n'], None)
 
121
            'different\nmoredifferent\nand then some more\n', None)
107
122
        expected_lines = list(compressor.lines)
108
123
        sha1_3, end_point = compressor.compress(('label3',),
109
 
            ['new\n', 'common long line\n', 'different\n', 'moredifferent\n'],
 
124
            'new\ncommon very very long line\nwith some extra text\n'
 
125
            'different\nmoredifferent\nand then some more\n',
110
126
            None)
111
127
        self.assertEqual(
112
 
            sha_strings(['new\n', 'common long line\n', 'different\n',
113
 
                         'moredifferent\n']),
 
128
            sha_string('new\ncommon very very long line\nwith some extra text\n'
 
129
                       'different\nmoredifferent\nand then some more\n'),
114
130
            sha1_3)
115
131
        expected_lines.extend([
116
 
            'label: label3\n',
117
 
            'sha1: %s\n' % sha1_3,
 
132
            'delta\n',
 
133
            'label:label3\n',
 
134
            'sha1:%s\n' % sha1_3,
 
135
            'len:13\n',
 
136
            '\xfa\x01\x5f' # source and target length
118
137
            # insert new
119
 
            'i,1\n',
120
 
            'new\n',
121
 
            # copy the line common
122
 
            'c,72,17\n',
123
 
            # copy the lines different, moredifferent and trailing newline
124
 
            'c,165,25\n',
 
138
            '\x03new',
 
139
            # Copy of first parent 'common' range
 
140
            '\x91\x51\x31' # copy, offset 0x51, 0x31 bytes
 
141
            # Copy of second parent 'different' range
 
142
            '\x91\xcf\x2b' # copy, offset 0xcf, 0x2b bytes
125
143
            ])
126
 
        self.assertEqualDiff(''.join(expected_lines),
127
 
                             ''.join(compressor.lines))
 
144
        self.assertEqualDiffEncoded(expected_lines, compressor.lines)
128
145
        self.assertEqual(sum(map(len, expected_lines)), end_point)
129
146
 
130
147
    def test_stats(self):
131
148
        compressor = groupcompress.GroupCompressor(True)
132
 
        compressor.compress(('label',),
133
 
            ['strange\n', 'common\n'], None)
 
149
        compressor.compress(('label',), 'strange\ncommon\n', None)
134
150
        compressor.compress(('newlabel',),
135
 
            ['common\n', 'different\n', 'moredifferent\n'], None)
 
151
                            'common\ndifferent\nmoredifferent\n', None)
136
152
        compressor.compress(('label3',),
137
 
            ['new\n', 'common\n', 'different\n', 'moredifferent\n'], None)
 
153
                            'new\ncommon\ndifferent\nmoredifferent\n', None)
138
154
        self.assertAlmostEqual(0.3, compressor.ratio(), 1)
139
155
 
140
156
    def test_extract_from_compressor(self):
141
157
        # Knit fetching will try to reconstruct texts locally which results in
142
158
        # reading something that is in the compressor stream already.
143
159
        compressor = groupcompress.GroupCompressor(True)
144
 
        sha_1,  _ = compressor.compress(('label',),
145
 
            ['strange\n', 'common\n'], None)
 
160
        sha_1,  _ = compressor.compress(('label',), 'strange\ncommon\n', None)
146
161
        sha_2, _ = compressor.compress(('newlabel',),
147
 
            ['common\n', 'different\n', 'moredifferent\n'], None)
 
162
            'common\ndifferent\nmoredifferent\n', None)
148
163
        # get the first out
149
 
        self.assertEqual((['strange\n', 'common\n'], sha_1),
 
164
        self.assertEqual((['strange\ncommon\n'], sha_1),
150
165
            compressor.extract(('label',)))
151
166
        # and the second
152
 
        self.assertEqual((['common\n', 'different\n', 'moredifferent\n'],
 
167
        self.assertEqual((['common\ndifferent\nmoredifferent\n'],
153
168
            sha_2), compressor.extract(('newlabel',)))