/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bisect_multi.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009, 2011 Canonical Ltd
 
1
# Copyright (C) 2007 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
16
16
 
17
17
"""Tests for bisect_multi."""
18
18
 
19
 
from ..bisect_multi import bisect_multi_bytes
20
 
from . import TestCase
 
19
from bzrlib import errors
 
20
from bzrlib.bisect_multi import bisect_multi_bytes
 
21
from bzrlib.tests import TestCase
21
22
 
22
23
 
23
24
class TestBisectMultiBytes(TestCase):
24
25
 
25
26
    def test_lookup_no_keys_no_calls(self):
26
27
        calls = []
27
 
 
28
28
        def missing_content(location_keys):
29
29
            calls.append(location_keys)
30
30
            return ((location_key, False) for location_key in location_keys)
31
31
        self.assertEqual([],
32
 
                         list(bisect_multi_bytes(missing_content, 100, [])))
 
32
            list(bisect_multi_bytes(missing_content, 100, [])))
33
33
        self.assertEqual([], calls)
34
34
 
35
35
    def test_lookup_missing_key_no_content(self):
40
40
        for a given location, key pair.
41
41
        """
42
42
        calls = []
43
 
 
44
43
        def missing_content(location_keys):
45
44
            calls.append(location_keys)
46
45
            return ((location_key, False) for location_key in location_keys)
47
46
        self.assertEqual([],
48
 
                         list(bisect_multi_bytes(missing_content, 0, ['foo', 'bar'])))
 
47
            list(bisect_multi_bytes(missing_content, 0, ['foo', 'bar'])))
49
48
        self.assertEqual([[(0, 'foo'), (0, 'bar')]], calls)
50
49
 
51
50
    def test_lookup_missing_key_before_all_others(self):
52
51
        calls = []
53
 
 
54
52
        def missing_first_content(location_keys):
55
53
            # returns -1 for all keys unless the byte offset is 0 when it
56
54
            # returns False
64
62
            return result
65
63
        # given a 0 length file, this should terminate with one call.
66
64
        self.assertEqual([],
67
 
                         list(bisect_multi_bytes(missing_first_content, 0, ['foo', 'bar'])))
 
65
            list(bisect_multi_bytes(missing_first_content, 0, ['foo', 'bar'])))
68
66
        self.assertEqual([[(0, 'foo'), (0, 'bar')]], calls)
69
67
        del calls[:]
70
68
        # given a 2 length file, this should make two calls - 1, 0.
71
69
        self.assertEqual([],
72
 
                         list(bisect_multi_bytes(missing_first_content, 2, ['foo', 'bar'])))
 
70
            list(bisect_multi_bytes(missing_first_content, 2, ['foo', 'bar'])))
73
71
        self.assertEqual([
74
72
            [(1, 'foo'), (1, 'bar')],
75
73
            [(0, 'foo'), (0, 'bar')],
86
84
        # 800 thousand keys, and log2 of 800000 is 19 - so we're doing log2
87
85
        # steps in the worst case there.
88
86
        self.assertEqual([],
89
 
                         list(bisect_multi_bytes(
90
 
                             missing_first_content, 268435456 - 1, ['foo', 'bar'])))
 
87
            list(bisect_multi_bytes(
 
88
                missing_first_content,268435456 - 1 , ['foo', 'bar'])))
91
89
        self.assertEqual([
92
90
            [(134217727, 'foo'), (134217727, 'bar')],
93
91
            [(67108864, 'foo'), (67108864, 'bar')],
148
146
    def test_lookup_missing_key_after_all_others(self):
149
147
        calls = []
150
148
        end = None
151
 
 
152
149
        def missing_last_content(location_keys):
153
150
            # returns +1 for all keys unless the byte offset is 'end' when it
154
151
            # returns False
163
160
        # given a 0 length file, this should terminate with one call.
164
161
        end = 0
165
162
        self.assertEqual([],
166
 
                         list(bisect_multi_bytes(missing_last_content, 0, ['foo', 'bar'])))
 
163
            list(bisect_multi_bytes(missing_last_content, 0, ['foo', 'bar'])))
167
164
        self.assertEqual([[(0, 'foo'), (0, 'bar')]], calls)
168
165
        del calls[:]
169
166
        end = 2
170
167
        # given a 3 length file, this should make two calls - 1, 2.
171
168
        self.assertEqual([],
172
 
                         list(bisect_multi_bytes(missing_last_content, 3, ['foo', 'bar'])))
 
169
            list(bisect_multi_bytes(missing_last_content, 3, ['foo', 'bar'])))
173
170
        self.assertEqual([
174
171
            [(1, 'foo'), (1, 'bar')],
175
172
            [(2, 'foo'), (2, 'bar')],
180
177
        # test_lookup_missing_key_before_all_others for details about this
181
178
        # assertion.
182
179
        self.assertEqual([],
183
 
                         list(bisect_multi_bytes(
184
 
                             missing_last_content, 268435456 - 1, ['foo', 'bar'])))
 
180
            list(bisect_multi_bytes(
 
181
                missing_last_content,268435456 - 1 , ['foo', 'bar'])))
185
182
        self.assertEqual([
186
183
            [(134217727, 'foo'), (134217727, 'bar')],
187
184
            [(201326590, 'foo'), (201326590, 'bar')],
241
238
 
242
239
    def test_lookup_when_a_key_is_missing_continues(self):
243
240
        calls = []
244
 
 
245
241
        def missing_foo_otherwise_missing_first_content(location_keys):
246
242
            # returns -1 for all keys unless the byte offset is 0 when it
247
243
            # returns False
256
252
        # given a 2 length file, this should terminate with two calls, one for
257
253
        # both keys, and one for bar only.
258
254
        self.assertEqual([],
259
 
                         list(bisect_multi_bytes(
260
 
                             missing_foo_otherwise_missing_first_content, 2,
261
 
                             ['foo', 'bar'])))
 
255
            list(bisect_multi_bytes(
 
256
                missing_foo_otherwise_missing_first_content, 2,
 
257
                ['foo', 'bar'])))
262
258
        self.assertEqual([
263
259
            [(1, 'foo'), (1, 'bar')],
264
260
            [(0, 'bar')],
266
262
 
267
263
    def test_found_keys_returned_other_searches_continue(self):
268
264
        calls = []
269
 
 
270
265
        def find_bar_at_1_foo_missing_at_0(location_keys):
271
266
            calls.append(location_keys)
272
267
            result = []
281
276
        # given a 4 length file, this should terminate with three calls, two for
282
277
        # both keys, and one for foo only.
283
278
        self.assertEqual([('bar', 'bar-result')],
284
 
                         list(bisect_multi_bytes(
285
 
                             find_bar_at_1_foo_missing_at_0, 4,
286
 
                             ['foo', 'bar'])))
 
279
            list(bisect_multi_bytes(
 
280
                find_bar_at_1_foo_missing_at_0, 4,
 
281
                ['foo', 'bar'])))
287
282
        self.assertEqual([
288
283
            [(2, 'foo'), (2, 'bar')],
289
284
            [(1, 'foo'), (1, 'bar')],
292
287
 
293
288
    def test_searches_different_keys_in_different_directions(self):
294
289
        calls = []
295
 
 
296
290
        def missing_bar_at_1_foo_at_3(location_keys):
297
291
            calls.append(location_keys)
298
292
            result = []
312
306
            return result
313
307
        # given a 4 length file, this should terminate with two calls.
314
308
        self.assertEqual([],
315
 
                         list(bisect_multi_bytes(
316
 
                             missing_bar_at_1_foo_at_3, 4,
317
 
                             ['foo', 'bar'])))
 
309
            list(bisect_multi_bytes(
 
310
                missing_bar_at_1_foo_at_3, 4,
 
311
                ['foo', 'bar'])))
318
312
        self.assertEqual([
319
313
            [(2, 'foo'), (2, 'bar')],
320
314
            [(3, 'foo'), (1, 'bar')],
324
318
        # check that we can search down, up, down again -
325
319
        # so length 8, goes 4, 6, 5
326
320
        calls = []
327
 
 
328
321
        def missing_at_5(location_keys):
329
322
            calls.append(location_keys)
330
323
            result = []
340
333
            return result
341
334
        # given a 8 length file, this should terminate with three calls.
342
335
        self.assertEqual([],
343
 
                         list(bisect_multi_bytes(
344
 
                             missing_at_5, 8,
345
 
                             ['foo', 'bar'])))
 
336
            list(bisect_multi_bytes(
 
337
                missing_at_5, 8,
 
338
                ['foo', 'bar'])))
346
339
        self.assertEqual([
347
340
            [(4, 'foo'), (4, 'bar')],
348
341
            [(6, 'foo'), (6, 'bar')],
349
342
            [(5, 'foo'), (5, 'bar')],
350
343
            ], calls)
 
344