/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__simple_set.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    """A simple object which has a fixed hash value.
36
36
 
37
37
    We could have used an 'int', but it turns out that Int objects don't
38
 
    implement tp_richcompare in Python 2.
 
38
    implement tp_richcompare...
39
39
    """
40
40
 
41
41
    def __init__(self, the_hash):
69
69
    def __eq__(self, other):
70
70
        raise RuntimeError('I refuse to play nice')
71
71
 
72
 
    __hash__ = _Hashable.__hash__
73
 
 
74
72
 
75
73
class _NoImplementCompare(_Hashable):
76
74
 
77
75
    def __eq__(self, other):
78
76
        return NotImplemented
79
77
 
80
 
    __hash__ = _Hashable.__hash__
81
 
 
82
78
 
83
79
# Even though this is an extension, we don't permute the tests for a python
84
80
# version. As the plain python version is just a dict or set
85
81
compiled_simpleset_feature = features.ModuleAvailableFeature(
86
 
    'breezy._simple_set_pyx')
 
82
                                'breezy._simple_set_pyx')
87
83
 
88
84
 
89
85
class TestSimpleSet(tests.TestCase):
91
87
    _test_needs_features = [compiled_simpleset_feature]
92
88
    module = _simple_set_pyx
93
89
 
 
90
    def assertIn(self, obj, container):
 
91
        self.assertTrue(obj in container,
 
92
            '%s not found in %s' % (obj, container))
 
93
 
 
94
    def assertNotIn(self, obj, container):
 
95
        self.assertTrue(obj not in container,
 
96
            'We found %s in %s' % (obj, container))
 
97
 
94
98
    def assertFillState(self, used, fill, mask, obj):
95
99
        self.assertEqual((used, fill, mask), (obj.used, obj.fill, obj.mask))
96
100
 
107
111
        # I'm not sure why the offset is 3, but I've check that in the caller,
108
112
        # an offset of 1 works, which is expected. Not sure why assertRefcount
109
113
        # is incrementing/decrementing 2 times
110
 
        self.assertEqual(count, sys.getrefcount(obj) - 3)
 
114
        self.assertEqual(count, sys.getrefcount(obj)-3)
111
115
 
112
116
    def test_initial(self):
113
117
        obj = self.module.SimpleSet()
114
118
        self.assertEqual(0, len(obj))
 
119
        st = ('foo', 'bar')
115
120
        self.assertFillState(0, 0, 0x3ff, obj)
116
121
 
117
122
    def test__lookup(self):
120
125
        obj = self.module.SimpleSet()
121
126
        self.assertLookup(643, '<null>', obj, _Hashable(643))
122
127
        self.assertLookup(643, '<null>', obj, _Hashable(643 + 1024))
123
 
        self.assertLookup(643, '<null>', obj, _Hashable(643 + 50 * 1024))
 
128
        self.assertLookup(643, '<null>', obj, _Hashable(643 + 50*1024))
124
129
 
125
130
    def test__lookup_collision(self):
126
131
        obj = self.module.SimpleSet()
140
145
        obj.add(k2)
141
146
        self.assertLookup(643, k1, obj, k1)
142
147
        self.assertLookup(644, k2, obj, k2)
143
 
        obj._py_resize(2047)  # resized to 2048
 
148
        obj._py_resize(2047) # resized to 2048
144
149
        self.assertEqual(2048, obj.mask + 1)
145
150
        self.assertLookup(643, k1, obj, k1)
146
 
        self.assertLookup(643 + 1024, k2, obj, k2)
147
 
        obj._py_resize(1023)  # resized back to 1024
 
151
        self.assertLookup(643+1024, k2, obj, k2)
 
152
        obj._py_resize(1023) # resized back to 1024
148
153
        self.assertEqual(1024, obj.mask + 1)
149
154
        self.assertLookup(643, k1, obj, k1)
150
155
        self.assertLookup(644, k2, obj, k2)
154
159
 
155
160
        h1 = 643
156
161
        h2 = 643 + 1024
157
 
        h3 = 643 + 1024 * 50
158
 
        h4 = 643 + 1024 * 25
 
162
        h3 = 643 + 1024*50
 
163
        h4 = 643 + 1024*25
159
164
        h5 = 644
160
165
        h6 = 644 + 1024
161
166
 
236
241
        # doesn't add anything, so the counters shouldn't be adjusted
237
242
        self.assertIs(k1, obj.add(k2))
238
243
        self.assertFillState(1, 1, 0x3ff, obj)
239
 
        self.assertRefcount(2, k1)  # not changed
240
 
        self.assertRefcount(1, k2)  # not incremented
 
244
        self.assertRefcount(2, k1) # not changed
 
245
        self.assertRefcount(1, k2) # not incremented
241
246
        self.assertIs(k1, obj[k1])
242
247
        self.assertIs(k1, obj[k2])
243
248
        self.assertRefcount(2, k1)
276
281
 
277
282
    def test__resize(self):
278
283
        obj = self.module.SimpleSet()
279
 
        # Need objects with exact hash as checking offset of <null> later
280
 
        k1 = _Hashable(501)
281
 
        k2 = _Hashable(591)
282
 
        k3 = _Hashable(2051)
 
284
        k1 = ('foo',)
 
285
        k2 = ('bar',)
 
286
        k3 = ('baz',)
283
287
        obj.add(k1)
284
288
        obj.add(k2)
285
289
        obj.add(k3)
338
342
 
339
343
    def test_add_and_remove_lots_of_items(self):
340
344
        obj = self.module.SimpleSet()
341
 
        chars = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
342
 
                 'abcdefghijklmnopqrstuvwxyz1234567890')
 
345
        chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
343
346
        for i in chars:
344
347
            for j in chars:
345
348
                k = (i, j)
346
349
                obj.add(k)
347
 
        num = len(chars) * len(chars)
 
350
        num = len(chars)*len(chars)
348
351
        self.assertFillState(num, num, 0x1fff, obj)
349
352
        # Now delete all of the entries and it should shrink again
350
353
        for i in chars:
370
373
            all.add(key)
371
374
        self.assertEqual(sorted([k1, k2, k3]), sorted(all))
372
375
        iterator = iter(obj)
373
 
        self.assertIn(next(iterator), all)
 
376
        next(iterator)
374
377
        obj.add(('foo',))
375
378
        # Set changed size
376
379
        self.assertRaises(RuntimeError, next, iterator)