File: test_identityset.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (200 lines) | stat: -rw-r--r-- 5,338 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import py


class AppTestIdentitySet(object):

    def setup_class(cls):
        from pypy.objspace.std import identitydict
        if cls.runappdirect:
            py.test.skip("interp2app doesn't work on appdirect")

    def w_uses_strategy(self, s , obj):
        import __pypy__
        return s in __pypy__.internal_repr(obj)

    def test_use_identity_strategy(self):

        class Plain(object):
            pass

        class CustomEq(object):
            def __eq__(self, other):
                return True
            __hash__ = object.__hash__

        class CustomHash(object):
            def __hash__(self):
                return 0

        s = set()

        assert not self.uses_strategy('IdentitySetStrategy',s)

        s.add(Plain())

        assert self.uses_strategy('IdentitySetStrategy',s)

        for cls in [CustomEq,CustomHash]:
            s = set()
            s.add(cls())
            assert not self.uses_strategy('IdentitySetStrategy',s)


    def test_use_identity_strategy_list(self):

        class X(object):
            pass

        assert self.uses_strategy('IdentitySetStrategy',set([X(),X()]))
        assert not self.uses_strategy('IdentitySetStrategy',set([X(),""]))
        assert not self.uses_strategy('IdentitySetStrategy',set([X(),""]))
        assert not self.uses_strategy('IdentitySetStrategy',set([X(),1]))

    def test_identity_strategy_add(self):

        class X(object):
            pass

        class NotIdent(object):
            def __eq__(self,other):
                pass
            __hash__ = object.__hash__

        s = set([X(),X()])
        s.add('foo')
        assert not self.uses_strategy('IdentitySetStrategy',s)
        s = set([X(),X()])
        s.add(NotIdent())
        assert not self.uses_strategy('IdentitySetStrategy',s)

    def test_identity_strategy_sanity(self):

        class X(object):
            pass

        class Y(object):
            pass

        a,b,c,d,e,f = X(),Y(),X(),Y(),X(),Y()

        s = set([a,b]).union(set([c]))
        assert self.uses_strategy('IdentitySetStrategy',s)
        assert set([a,b,c]) == s
        s = set([a,b,c,d,e,f]) - set([d,e,f])
        assert self.uses_strategy('IdentitySetStrategy',s)
        assert set([a,b,c]) == s


        s = set([a])
        s.update([b,c])

        assert s == set([a,b,c])


    def test_identity_strategy_iterators(self):

        class X(object):
            pass

        s = set([X() for i in range(10)])
        counter = 0
        for item in s:
            counter += 1
            assert item in s

        assert counter == 10


    def test_identity_strategy_other_cmp(self):

        # test tries to hit positive and negative in
        # may_contain_equal_elements

        class X(object):
            pass

        s = set([X() for i in range(10)])

        assert s.intersection(set([1,2,3])) == set()
        assert s.intersection(set(['a','b','c'])) == set()
        assert s.intersection(set(['a','b','c'])) == set()
        assert s.intersection(set([X(),X()])) == set()

        other = set(['a','b','c',next(s.__iter__())])
        intersect = s.intersection(other)
        assert len(intersect) == 1
        assert next(intersect.__iter__()) in s
        assert next(intersect.__iter__()) in other

    def test_class_monkey_patch(self):

        class X(object):
            pass

        s = set()

        s.add(X())
        assert self.uses_strategy('IdentitySetStrategy',s)
        X.__eq__ = lambda self,other : None
        s.add(X())
        assert not self.uses_strategy('IdentitySetStrategy',s)
        assert not self.uses_strategy('IdentitySetStrategy',set([X(),X()]))
        assert not self.uses_strategy('IdentitySetStrategy',set([X(),""]))
        assert not self.uses_strategy('IdentitySetStrategy',set([X(),""]))
        assert not self.uses_strategy('IdentitySetStrategy',set([X(),1]))

        # An interesting case, add an instance, mutate the class,
        # then add the same instance.

        class X(object):
            pass

        s = set()
        inst = X()
        s.add(inst)
        X.__eq__ = lambda x,y : x is y
        s.add(inst)

        assert len(s) == 1
        assert next(s.__iter__()) is inst
        assert not self.uses_strategy('IdentitySetStrategy',s)


        # Add instance, mutate class, check membership of that instance.

        class X(object):
            pass


        inst = X()
        s = set()
        s.add(inst)
        X.__eq__ = lambda x,y : x is y
        assert inst in s

        # Test Wrong strategy
        # If the strategy is changed by mutation, but the instance
        # does not change, then this tests the methods that call
        # may_contain_equal_elements still function.
        # i.e. same instance in two sets, one with object strategy, one with
        # identity strategy.

        class X(object):
            pass


        inst = X()
        s1 = set()
        s1.add(inst)
        assert self.uses_strategy('IdentitySetStrategy',s1)
        X.__eq__ = lambda x,y : x is y
        s2 = set()
        s2.add(inst)
        assert not self.uses_strategy('IdentitySetStrategy',s2)

        assert s1.intersection(s2) == set([inst])
        assert (s1 - s2) == set()
        assert (s2 - s1) == set()

        s1.difference_update(s2)
        assert s1 == set()