/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 breezy/tests/test_bisect_multi.py

  • Committer: Jelmer Vernooij
  • Date: 2019-06-02 02:35:46 UTC
  • mfrom: (7309 work)
  • mto: This revision was merged to the branch mainline in revision 7319.
  • Revision ID: jelmer@jelmer.uk-20190602023546-lqco868tnv26d8ow
merge trunk.

Show diffs side-by-side

added added

removed removed

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