~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__groupcompress.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2010 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 CompiledGroupCompressFeature.available():
36
 
        from bzrlib import _groupcompress_pyx
 
35
    if compiled_groupcompress_feature.available():
 
36
        gc_module = compiled_groupcompress_feature.module
37
37
        scenarios.append(('C',
38
 
            {'_gc_module': _groupcompress_pyx}))
 
38
            {'_gc_module': gc_module}))
39
39
        two_way_scenarios.extend([
40
 
            ('CC', {'make_delta': _groupcompress_pyx.make_delta,
41
 
                    'apply_delta': _groupcompress_pyx.apply_delta}),
 
40
            ('CC', {'make_delta': gc_module.make_delta,
 
41
                    'apply_delta': gc_module.apply_delta}),
42
42
            ('PC', {'make_delta': _groupcompress_py.make_delta,
43
 
                    'apply_delta': _groupcompress_pyx.apply_delta}),
44
 
            ('CP', {'make_delta': _groupcompress_pyx.make_delta,
 
43
                    'apply_delta': gc_module.apply_delta}),
 
44
            ('CP', {'make_delta': gc_module.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
 
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()
 
57
compiled_groupcompress_feature = tests.ModuleAvailableFeature(
 
58
                                    'bzrlib._groupcompress_pyx')
72
59
 
73
60
_text1 = """\
74
61
This is a bit
186
173
            'N\x90\x1d\x1ewhich is meant to differ from\n\x91:\x13',
187
174
            delta)
188
175
 
 
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(CompiledGroupCompressFeature)
255
 
        from bzrlib import _groupcompress_pyx
256
 
        self._gc_module = _groupcompress_pyx
 
254
        self.requireFeature(compiled_groupcompress_feature)
 
255
        self._gc_module = compiled_groupcompress_feature.module
257
256
 
258
257
    def test_repr(self):
259
258
        di = self._gc_module.DeltaIndex('test text\n')
260
259
        self.assertEqual('DeltaIndex(1, 10)', repr(di))
261
260
 
 
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
 
262
280
    def test_make_delta(self):
263
281
        di = self._gc_module.DeltaIndex(_text1)
264
282
        delta = di.make_delta(_text2)
358
376
        self.assertEqual((exp_offset, exp_length, exp_newpos), out)
359
377
 
360
378
    def test_encode_no_length(self):
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)
 
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)
373
391
 
374
392
    def test_encode_no_offset(self):
375
393
        self.assertEncode('\x90\x01', 0, 1)