~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__groupcompress.py

  • Committer: John Arbash Meinel
  • Date: 2009-03-27 22:29:55 UTC
  • mto: (3735.39.2 clean)
  • mto: This revision was merged to the branch mainline in revision 4280.
  • Revision ID: john@arbash-meinel.com-20090327222955-utifmfm888zerixt
Implement apply_delta_to_source which doesn't have to malloc another string.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
32
32
    scenarios = [
33
33
        ('python', {'_gc_module': _groupcompress_py}),
34
34
        ]
35
 
    if compiled_groupcompress_feature.available():
36
 
        gc_module = compiled_groupcompress_feature.module
 
35
    if CompiledGroupCompressFeature.available():
 
36
        from bzrlib import _groupcompress_pyx
37
37
        scenarios.append(('C',
38
 
            {'_gc_module': gc_module}))
 
38
            {'_gc_module': _groupcompress_pyx}))
39
39
        two_way_scenarios.extend([
40
 
            ('CC', {'make_delta': gc_module.make_delta,
41
 
                    'apply_delta': gc_module.apply_delta}),
 
40
            ('CC', {'make_delta': _groupcompress_pyx.make_delta,
 
41
                    'apply_delta': _groupcompress_pyx.apply_delta}),
42
42
            ('PC', {'make_delta': _groupcompress_py.make_delta,
43
 
                    'apply_delta': gc_module.apply_delta}),
44
 
            ('CP', {'make_delta': gc_module.make_delta,
 
43
                    'apply_delta': _groupcompress_pyx.apply_delta}),
 
44
            ('CP', {'make_delta': _groupcompress_pyx.make_delta,
45
45
                    'apply_delta': _groupcompress_py.apply_delta}),
46
46
            ])
47
47
    to_adapt, result = tests.split_suite_by_condition(
54
54
    return result
55
55
 
56
56
 
57
 
compiled_groupcompress_feature = tests.ModuleAvailableFeature(
58
 
                                    'bzrlib._groupcompress_pyx')
 
57
class _CompiledGroupCompressFeature(tests.Feature):
 
58
 
 
59
    def _probe(self):
 
60
        try:
 
61
            import bzrlib._groupcompress_pyx
 
62
        except ImportError:
 
63
            return False
 
64
        else:
 
65
            return True
 
66
 
 
67
    def feature_name(self):
 
68
        return 'bzrlib._groupcompress_pyx'
 
69
 
 
70
 
 
71
CompiledGroupCompressFeature = _CompiledGroupCompressFeature()
59
72
 
60
73
_text1 = """\
61
74
This is a bit
173
186
            'N\x90\x1d\x1ewhich is meant to differ from\n\x91:\x13',
174
187
            delta)
175
188
 
176
 
    def test_make_delta_with_large_copies(self):
177
 
        # We want to have a copy that is larger than 64kB, which forces us to
178
 
        # issue multiple copy instructions.
179
 
        big_text = _text3 * 1220
180
 
        delta = self.make_delta(big_text, big_text)
181
 
        self.assertDeltaIn(
182
 
            '\xdc\x86\x0a'      # Encoding the length of the uncompressed text
183
 
            '\x80'              # Copy 64kB, starting at byte 0
184
 
            '\x84\x01'          # and another 64kB starting at 64kB
185
 
            '\xb4\x02\x5c\x83', # And the bit of tail.
186
 
            None,   # Both implementations should be identical
187
 
            delta)
188
 
 
189
189
    def test_apply_delta_is_typesafe(self):
190
190
        self.apply_delta(_text1, 'M\x90M')
191
191
        self.assertRaises(TypeError, self.apply_delta, object(), 'M\x90M')
251
251
        # This test isn't multiplied, because we only have DeltaIndex for the
252
252
        # compiled form
253
253
        # We call this here, because _test_needs_features happens after setUp
254
 
        self.requireFeature(compiled_groupcompress_feature)
255
 
        self._gc_module = compiled_groupcompress_feature.module
 
254
        self.requireFeature(CompiledGroupCompressFeature)
 
255
        from bzrlib import _groupcompress_pyx
 
256
        self._gc_module = _groupcompress_pyx
256
257
 
257
258
    def test_repr(self):
258
259
        di = self._gc_module.DeltaIndex('test text\n')
259
260
        self.assertEqual('DeltaIndex(1, 10)', repr(di))
260
261
 
261
 
    def test_first_add_source_doesnt_index_until_make_delta(self):
262
 
        di = self._gc_module.DeltaIndex()
263
 
        self.assertFalse(di._has_index())
264
 
        di.add_source(_text1, 0)
265
 
        self.assertFalse(di._has_index())
266
 
        # However, asking to make a delta will trigger the index to be
267
 
        # generated, and will generate a proper delta
268
 
        delta = di.make_delta(_text2)
269
 
        self.assertTrue(di._has_index())
270
 
        self.assertEqual('N\x90/\x1fdiffer from\nagainst other text\n', delta)
271
 
 
272
 
    def test_second_add_source_triggers_make_index(self):
273
 
        di = self._gc_module.DeltaIndex()
274
 
        self.assertFalse(di._has_index())
275
 
        di.add_source(_text1, 0)
276
 
        self.assertFalse(di._has_index())
277
 
        di.add_source(_text2, 0)
278
 
        self.assertTrue(di._has_index())
279
 
 
280
262
    def test_make_delta(self):
281
263
        di = self._gc_module.DeltaIndex(_text1)
282
264
        delta = di.make_delta(_text2)
376
358
        self.assertEqual((exp_offset, exp_length, exp_newpos), out)
377
359
 
378
360
    def test_encode_no_length(self):
379
 
        self.assertEncode('\x80', 0, 64*1024)
380
 
        self.assertEncode('\x81\x01', 1, 64*1024)
381
 
        self.assertEncode('\x81\x0a', 10, 64*1024)
382
 
        self.assertEncode('\x81\xff', 255, 64*1024)
383
 
        self.assertEncode('\x82\x01', 256, 64*1024)
384
 
        self.assertEncode('\x83\x01\x01', 257, 64*1024)
385
 
        self.assertEncode('\x8F\xff\xff\xff\xff', 0xFFFFFFFF, 64*1024)
386
 
        self.assertEncode('\x8E\xff\xff\xff', 0xFFFFFF00, 64*1024)
387
 
        self.assertEncode('\x8D\xff\xff\xff', 0xFFFF00FF, 64*1024)
388
 
        self.assertEncode('\x8B\xff\xff\xff', 0xFF00FFFF, 64*1024)
389
 
        self.assertEncode('\x87\xff\xff\xff', 0x00FFFFFF, 64*1024)
390
 
        self.assertEncode('\x8F\x04\x03\x02\x01', 0x01020304, 64*1024)
 
361
        self.assertEncode('\x80', 0, None)
 
362
        self.assertEncode('\x81\x01', 1, None)
 
363
        self.assertEncode('\x81\x0a', 10, None)
 
364
        self.assertEncode('\x81\xff', 255, None)
 
365
        self.assertEncode('\x82\x01', 256, None)
 
366
        self.assertEncode('\x83\x01\x01', 257, None)
 
367
        self.assertEncode('\x8F\xff\xff\xff\xff', 0xFFFFFFFF, None)
 
368
        self.assertEncode('\x8E\xff\xff\xff', 0xFFFFFF00, None)
 
369
        self.assertEncode('\x8D\xff\xff\xff', 0xFFFF00FF, None)
 
370
        self.assertEncode('\x8B\xff\xff\xff', 0xFF00FFFF, None)
 
371
        self.assertEncode('\x87\xff\xff\xff', 0x00FFFFFF, None)
 
372
        self.assertEncode('\x8F\x04\x03\x02\x01', 0x01020304, None)
391
373
 
392
374
    def test_encode_no_offset(self):
393
375
        self.assertEncode('\x90\x01', 0, 1)