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)
71
'\n', # the last \n in a text is removed, which allows safe
72
# serialisation of lines without trailing \n.
67
'label:label\nsha1:%s\n' % sha1,
74
71
self.assertEqual(expected_lines, compressor.lines)
75
72
self.assertEqual(sum(map(len, expected_lines)), end_point)
74
def _chunks_to_repr_lines(self, chunks):
75
return '\n'.join(map(repr, ''.join(chunks).split('\n')))
77
def assertEqualDiffEncoded(self, expected, actual):
78
"""Compare the actual content to the expected content.
80
:param expected: A group of chunks that we expect to see
81
:param actual: The measured 'chunks'
83
We will transform the chunks back into lines, and then run 'repr()'
84
over them to handle non-ascii characters.
86
self.assertEqualDiff(self._chunks_to_repr_lines(expected),
87
self._chunks_to_repr_lines(actual))
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']),
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([
88
'sha1: %s\n' % sha1_2,
102
'sha1:%s\n' % sha1_2,
104
# source and target length
89
106
# copy the line common
107
'\x91\x52\x2c', #copy, offset 0x52, len 0x2c
91
108
# add the line different, and the trailing newline
109
'\x0adifferent\n', # insert 10 bytes
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)
99
114
def test_three_nosha_delta(self):
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',
111
127
self.assertEqual(
112
sha_strings(['new\n', 'common long line\n', 'different\n',
128
sha_string('new\ncommon very very long line\nwith some extra text\n'
129
'different\nmoredifferent\nand then some more\n'),
115
131
expected_lines.extend([
117
'sha1: %s\n' % sha1_3,
134
'sha1:%s\n' % sha1_3,
136
'\xfa\x01\x5f' # source and target length
121
# copy the line common
123
# copy the lines different, moredifferent and trailing newline
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
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)
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)
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',)))
152
self.assertEqual((['common\n', 'different\n', 'moredifferent\n'],
167
self.assertEqual((['common\ndifferent\nmoredifferent\n'],
153
168
sha_2), compressor.extract(('newlabel',)))