/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: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

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
 
280
277
 
281
278
    def test__resize(self):
282
279
        obj = self.module.SimpleSet()
283
 
        k1 = ('foo',)
284
 
        k2 = ('bar',)
285
 
        k3 = ('baz',)
 
280
        # Need objects with exact hash as checking offset of <null> later
 
281
        k1 = _Hashable(501)
 
282
        k2 = _Hashable(591)
 
283
        k3 = _Hashable(2051)
286
284
        obj.add(k1)
287
285
        obj.add(k2)
288
286
        obj.add(k3)
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)