/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.18.15 by John Arbash Meinel
Start writing tests directly for the compiled class
1
# Copyright (C) 2008 Canonical Limited.
2
# 
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License version 2 as published
5
# by the Free Software Foundation.
6
# 
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
# GNU General Public License for more details.
11
# 
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
15
# 
16
17
"""Tests for the pyrex extension of groupcompress"""
18
19
from bzrlib import tests
20
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
21
from bzrlib.plugins.groupcompress_rabin import groupcompress
0.18.26 by John Arbash Meinel
Start with a copy implementation of the _get_longest_match function.
22
0.18.15 by John Arbash Meinel
Start writing tests directly for the compiled class
23
24
class _CompiledGroupCompress(tests.Feature):
25
26
    def _probe(self):
27
        try:
0.23.21 by John Arbash Meinel
Rename the extension to _pyx, since Robert prefers that form
28
            import bzrlib.plugins.groupcompress_rabin._groupcompress_pyx
0.18.15 by John Arbash Meinel
Start writing tests directly for the compiled class
29
        except ImportError:
30
            return False
31
        else:
32
            return True
33
34
    def feature_name(self):
0.23.21 by John Arbash Meinel
Rename the extension to _pyx, since Robert prefers that form
35
        return 'bzrlib.plugins.groupcompress_rabin._groupcompress_pyx'
0.18.15 by John Arbash Meinel
Start writing tests directly for the compiled class
36
37
CompiledGroupCompress = _CompiledGroupCompress()
38
0.23.8 by John Arbash Meinel
Add another test text.
39
_text1 = """\
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
40
This is a bit
41
of source text
42
which is meant to be matched
43
against other text
44
"""
45
0.23.8 by John Arbash Meinel
Add another test text.
46
_text2 = """\
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
47
This is a bit
48
of source text
49
which is meant to differ from
50
against other text
51
"""
52
0.23.8 by John Arbash Meinel
Add another test text.
53
_text3 = """\
54
This is a bit
55
of source text
56
which is meant to be matched
57
against other text
58
except it also
59
has a lot more data
60
at the end of the file
61
"""
62
0.23.26 by John Arbash Meinel
We now start to make use of the ability to extend the delta index
63
_first_text = """\
64
a bit of text, that
65
does not have much in
66
common with the next text
67
"""
68
69
_second_text = """\
70
some more bits of text
71
which does have a little bit in
72
common with the previous text
73
"""
74
75
76
_third_text = """\
77
a bit of text, that
78
has some in common with the previous text
79
and not much in
80
common with the next text
81
"""
82
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
83
84
class Test_GroupCompress(tests.TestCase):
85
    """Direct tests for the compiled extension."""
0.18.31 by John Arbash Meinel
We had a small bug when we had to rebuild the hash, as we would forget about the non-indexed entries.
86
0.18.15 by John Arbash Meinel
Start writing tests directly for the compiled class
87
    def setUp(self):
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
88
        super(Test_GroupCompress, self).setUp()
89
        self.requireFeature(CompiledGroupCompress)
0.23.21 by John Arbash Meinel
Rename the extension to _pyx, since Robert prefers that form
90
        from bzrlib.plugins.groupcompress_rabin import _groupcompress_pyx
91
        self._gc_module = _groupcompress_pyx
0.23.14 by John Arbash Meinel
Implement a DeltaIndex wrapper.
92
93
94
class TestMakeAndApplyDelta(Test_GroupCompress):
95
96
    def setUp(self):
97
        super(TestMakeAndApplyDelta, self).setUp()
98
        self.make_delta = self._gc_module.make_delta
99
        self.apply_delta = self._gc_module.apply_delta
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
100
101
    def test_make_delta_is_typesafe(self):
102
        self.make_delta('a string', 'another string')
103
        self.assertRaises(TypeError,
104
            self.make_delta, 'a string', object())
105
        self.assertRaises(TypeError,
106
            self.make_delta, 'a string', u'not a string')
107
        self.assertRaises(TypeError,
108
            self.make_delta, object(), 'a string')
109
        self.assertRaises(TypeError,
110
            self.make_delta, u'not a string', 'a string')
111
112
    def test_make_noop_delta(self):
0.23.8 by John Arbash Meinel
Add another test text.
113
        ident_delta = self.make_delta(_text1, _text1)
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
114
        self.assertEqual('MM\x90M', ident_delta)
0.23.8 by John Arbash Meinel
Add another test text.
115
        ident_delta = self.make_delta(_text2, _text2)
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
116
        self.assertEqual('NN\x90N', ident_delta)
0.23.8 by John Arbash Meinel
Add another test text.
117
        ident_delta = self.make_delta(_text3, _text3)
118
        self.assertEqual('\x87\x01\x87\x01\x90\x87', ident_delta)
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
119
120
    def test_make_delta(self):
0.23.8 by John Arbash Meinel
Add another test text.
121
        delta = self.make_delta(_text1, _text2)
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
122
        self.assertEqual('MN\x90/\x1fdiffer from\nagainst other text\n', delta)
0.23.8 by John Arbash Meinel
Add another test text.
123
        delta = self.make_delta(_text2, _text1)
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
124
        self.assertEqual('NM\x90/\x1ebe matched\nagainst other text\n', delta)
0.23.8 by John Arbash Meinel
Add another test text.
125
        delta = self.make_delta(_text3, _text1)
126
        self.assertEqual('\x87\x01M\x90M', delta)
127
        delta = self.make_delta(_text3, _text2)
128
        self.assertEqual('\x87\x01N\x90/\x1fdiffer from\nagainst other text\n',
129
                         delta)
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
130
131
    def test_apply_delta_is_typesafe(self):
0.23.8 by John Arbash Meinel
Add another test text.
132
        self.apply_delta(_text1, 'MM\x90M')
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
133
        self.assertRaises(TypeError,
134
            self.apply_delta, object(), 'MM\x90M')
135
        self.assertRaises(TypeError,
0.23.8 by John Arbash Meinel
Add another test text.
136
            self.apply_delta, unicode(_text1), 'MM\x90M')
137
        self.assertRaises(TypeError,
138
            self.apply_delta, _text1, u'MM\x90M')
139
        self.assertRaises(TypeError,
140
            self.apply_delta, _text1, object())
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
141
142
    def test_apply_delta(self):
0.23.8 by John Arbash Meinel
Add another test text.
143
        target = self.apply_delta(_text1,
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
144
                    'MN\x90/\x1fdiffer from\nagainst other text\n')
0.23.8 by John Arbash Meinel
Add another test text.
145
        self.assertEqual(_text2, target)
146
        target = self.apply_delta(_text2,
0.23.6 by John Arbash Meinel
Start stripping out the actual GroupCompressor
147
                    'NM\x90/\x1ebe matched\nagainst other text\n')
0.23.8 by John Arbash Meinel
Add another test text.
148
        self.assertEqual(_text1, target)
0.23.14 by John Arbash Meinel
Implement a DeltaIndex wrapper.
149
150
151
class TestDeltaIndex(Test_GroupCompress):
152
153
    def test_repr(self):
154
        di = self._gc_module.DeltaIndex('test text\n')
0.23.43 by John Arbash Meinel
Change the internals to allow delta indexes to be expanded with new source data.
155
        self.assertEqual('DeltaIndex(1, 10)', repr(di))
0.23.14 by John Arbash Meinel
Implement a DeltaIndex wrapper.
156
157
    def test_make_delta(self):
158
        di = self._gc_module.DeltaIndex(_text1)
159
        delta = di.make_delta(_text2)
160
        self.assertEqual('MN\x90/\x1fdiffer from\nagainst other text\n', delta)
0.23.25 by John Arbash Meinel
We are now able to add multiple sources to the delta generator.
161
162
    def test_delta_against_multiple_sources(self):
163
        di = self._gc_module.DeltaIndex()
0.23.26 by John Arbash Meinel
We now start to make use of the ability to extend the delta index
164
        di.add_source(_first_text, 0)
165
        self.assertEqual(len(_first_text), di._source_offset)
166
        di.add_source(_second_text, 0)
167
        self.assertEqual(len(_first_text) + len(_second_text), di._source_offset)
168
        delta = di.make_delta(_third_text)
169
        result = self._gc_module.apply_delta(_first_text + _second_text, delta)
170
        self.assertEqualDiff(_third_text, result)
0.23.25 by John Arbash Meinel
We are now able to add multiple sources to the delta generator.
171
        self.assertEqual('\x99\x01h\x90\x14\x0chas some in '
172
                         '\x91{\x1e\x07and not\x91!#', delta)
173
0.23.26 by John Arbash Meinel
We now start to make use of the ability to extend the delta index
174
    def test_delta_with_offsets(self):
175
        di = self._gc_module.DeltaIndex()
176
        di.add_source(_first_text, 5)
177
        self.assertEqual(len(_first_text) + 5, di._source_offset)
178
        di.add_source(_second_text, 10)
179
        self.assertEqual(len(_first_text) + len(_second_text) + 15,
180
                         di._source_offset)
181
        delta = di.make_delta(_third_text)
182
        self.assertIsNot(None, delta)
183
        result = self._gc_module.apply_delta(
184
            '12345' + _first_text + '1234567890' + _second_text, delta)
185
        self.assertIsNot(None, result)
186
        self.assertEqualDiff(_third_text, result)
187
        self.assertEqual('\xa8\x01h\x91\x05\x14\x0chas some in '
188
                         '\x91\x8a\x1e\x07and not\x91&#', delta)