/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: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009, 2010, 2011 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
18
18
 
19
19
import sys
20
20
 
21
 
from bzrlib import (
22
 
    errors,
23
 
    osutils,
 
21
from breezy import (
24
22
    tests,
25
23
    )
 
24
from breezy.tests import (
 
25
    features,
 
26
    )
26
27
 
27
28
try:
28
 
    from bzrlib import _simple_set_pyx
 
29
    from breezy import _simple_set_pyx
29
30
except ImportError:
30
31
    _simple_set_pyx = None
31
32
 
34
35
    """A simple object which has a fixed hash value.
35
36
 
36
37
    We could have used an 'int', but it turns out that Int objects don't
37
 
    implement tp_richcompare...
 
38
    implement tp_richcompare in Python 2.
38
39
    """
39
40
 
40
41
    def __init__(self, the_hash):
68
69
    def __eq__(self, other):
69
70
        raise RuntimeError('I refuse to play nice')
70
71
 
 
72
    __hash__ = _Hashable.__hash__
 
73
 
71
74
 
72
75
class _NoImplementCompare(_Hashable):
73
76
 
74
77
    def __eq__(self, other):
75
78
        return NotImplemented
76
79
 
 
80
    __hash__ = _Hashable.__hash__
 
81
 
77
82
 
78
83
# Even though this is an extension, we don't permute the tests for a python
79
84
# version. As the plain python version is just a dict or set
80
 
compiled_simpleset_feature = tests.ModuleAvailableFeature(
81
 
                                'bzrlib._simple_set_pyx')
 
85
compiled_simpleset_feature = features.ModuleAvailableFeature(
 
86
    'breezy._simple_set_pyx')
82
87
 
83
88
 
84
89
class TestSimpleSet(tests.TestCase):
86
91
    _test_needs_features = [compiled_simpleset_feature]
87
92
    module = _simple_set_pyx
88
93
 
89
 
    def assertIn(self, obj, container):
90
 
        self.assertTrue(obj in container,
91
 
            '%s not found in %s' % (obj, container))
92
 
 
93
 
    def assertNotIn(self, obj, container):
94
 
        self.assertTrue(obj not in container,
95
 
            'We found %s in %s' % (obj, container))
96
 
 
97
94
    def assertFillState(self, used, fill, mask, obj):
98
95
        self.assertEqual((used, fill, mask), (obj.used, obj.fill, obj.mask))
99
96
 
110
107
        # I'm not sure why the offset is 3, but I've check that in the caller,
111
108
        # an offset of 1 works, which is expected. Not sure why assertRefcount
112
109
        # is incrementing/decrementing 2 times
113
 
        self.assertEqual(count, sys.getrefcount(obj)-3)
 
110
        self.assertEqual(count, sys.getrefcount(obj) - 3)
114
111
 
115
112
    def test_initial(self):
116
113
        obj = self.module.SimpleSet()
117
114
        self.assertEqual(0, len(obj))
118
 
        st = ('foo', 'bar')
119
115
        self.assertFillState(0, 0, 0x3ff, obj)
120
116
 
121
117
    def test__lookup(self):
124
120
        obj = self.module.SimpleSet()
125
121
        self.assertLookup(643, '<null>', obj, _Hashable(643))
126
122
        self.assertLookup(643, '<null>', obj, _Hashable(643 + 1024))
127
 
        self.assertLookup(643, '<null>', obj, _Hashable(643 + 50*1024))
 
123
        self.assertLookup(643, '<null>', obj, _Hashable(643 + 50 * 1024))
128
124
 
129
125
    def test__lookup_collision(self):
130
126
        obj = self.module.SimpleSet()
144
140
        obj.add(k2)
145
141
        self.assertLookup(643, k1, obj, k1)
146
142
        self.assertLookup(644, k2, obj, k2)
147
 
        obj._py_resize(2047) # resized to 2048
 
143
        obj._py_resize(2047)  # resized to 2048
148
144
        self.assertEqual(2048, obj.mask + 1)
149
145
        self.assertLookup(643, k1, obj, k1)
150
 
        self.assertLookup(643+1024, k2, obj, k2)
151
 
        obj._py_resize(1023) # resized back to 1024
 
146
        self.assertLookup(643 + 1024, k2, obj, k2)
 
147
        obj._py_resize(1023)  # resized back to 1024
152
148
        self.assertEqual(1024, obj.mask + 1)
153
149
        self.assertLookup(643, k1, obj, k1)
154
150
        self.assertLookup(644, k2, obj, k2)
158
154
 
159
155
        h1 = 643
160
156
        h2 = 643 + 1024
161
 
        h3 = 643 + 1024*50
162
 
        h4 = 643 + 1024*25
 
157
        h3 = 643 + 1024 * 50
 
158
        h4 = 643 + 1024 * 25
163
159
        h5 = 644
164
160
        h6 = 644 + 1024
165
161
 
240
236
        # doesn't add anything, so the counters shouldn't be adjusted
241
237
        self.assertIs(k1, obj.add(k2))
242
238
        self.assertFillState(1, 1, 0x3ff, obj)
243
 
        self.assertRefcount(2, k1) # not changed
244
 
        self.assertRefcount(1, k2) # not incremented
 
239
        self.assertRefcount(2, k1)  # not changed
 
240
        self.assertRefcount(1, k2)  # not incremented
245
241
        self.assertIs(k1, obj[k1])
246
242
        self.assertIs(k1, obj[k2])
247
243
        self.assertRefcount(2, k1)
280
276
 
281
277
    def test__resize(self):
282
278
        obj = self.module.SimpleSet()
283
 
        k1 = ('foo',)
284
 
        k2 = ('bar',)
285
 
        k3 = ('baz',)
 
279
        # Need objects with exact hash as checking offset of <null> later
 
280
        k1 = _Hashable(501)
 
281
        k2 = _Hashable(591)
 
282
        k3 = _Hashable(2051)
286
283
        obj.add(k1)
287
284
        obj.add(k2)
288
285
        obj.add(k3)
341
338
 
342
339
    def test_add_and_remove_lots_of_items(self):
343
340
        obj = self.module.SimpleSet()
344
 
        chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
 
341
        chars = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 
342
                 'abcdefghijklmnopqrstuvwxyz1234567890')
345
343
        for i in chars:
346
344
            for j in chars:
347
345
                k = (i, j)
348
346
                obj.add(k)
349
 
        num = len(chars)*len(chars)
 
347
        num = len(chars) * len(chars)
350
348
        self.assertFillState(num, num, 0x1fff, obj)
351
349
        # Now delete all of the entries and it should shrink again
352
350
        for i in chars:
372
370
            all.add(key)
373
371
        self.assertEqual(sorted([k1, k2, k3]), sorted(all))
374
372
        iterator = iter(obj)
375
 
        iterator.next()
 
373
        self.assertIn(next(iterator), all)
376
374
        obj.add(('foo',))
377
375
        # Set changed size
378
 
        self.assertRaises(RuntimeError, iterator.next)
 
376
        self.assertRaises(RuntimeError, next, iterator)
379
377
        # And even removing an item still causes it to fail
380
378
        obj.discard(k2)
381
 
        self.assertRaises(RuntimeError, iterator.next)
 
379
        self.assertRaises(RuntimeError, next, iterator)
 
380
 
 
381
    def test__sizeof__(self):
 
382
        # SimpleSet needs a custom sizeof implementation, because it allocates
 
383
        # memory that Python cannot directly see (_table).
 
384
        # Too much variability in platform sizes for us to give a fixed size
 
385
        # here. However without a custom implementation, __sizeof__ would give
 
386
        # us only the size of the object, and not its table. We know the table
 
387
        # is at least 4bytes*1024entries in size.
 
388
        obj = self.module.SimpleSet()
 
389
        self.assertTrue(obj.__sizeof__() > 4096)