Debian Codegraph @

pypy/objspace/std/test/test_specialisedtupleobject.py

raw symbols SCIP
1 from pypy.objspace.std.specialisedtupleobject import _specialisations
2 from pypy.objspace.std.test import test_tupleobject
3 from pypy.objspace.std.tupleobject import W_TupleObject
4 from pypy.objspace.std.longobject import W_LongObject
5 from rpython.rlib.rbigint import rbigint
6
7
8 for cls in _specialisations:
9 globals()[cls.__name__] = cls
10
11
12 class TestW_SpecialisedTupleObject():
13 spaceconfig = {"objspace.std.withspecialisedtuple": True}
14
15 def test_isspecialisedtupleobjectintint(self):
16 w_tuple = self.space.newtuple([self.space.wrap(1), self.space.wrap(2)])
17 assert isinstance(w_tuple, W_SpecialisedTupleObject_ii)
18
19 def test_isnotspecialisedtupleobject(self):
20 w_tuple = self.space.newtuple([self.space.wrap({})])
21 assert not 'W_SpecialisedTupleObject' in type(w_tuple).__name__
22
23 def test_specialisedtupleclassname(self):
24 w_tuple = self.space.newtuple([self.space.wrap(1), self.space.wrap(2)])
25 assert w_tuple.__class__.__name__ == 'W_SpecialisedTupleObject_ii'
26
27 def test_integer_strategy_with_w_long(self):
28 w = W_LongObject(rbigint.fromlong(42))
29 w_tuple = self.space.newtuple([w, w])
30 assert w_tuple.__class__.__name__ == 'W_SpecialisedTupleObject_ii'
31
32 def hash_test(self, values, must_be_specialized):
33 N_values_w = [self.space.wrap(value) for value in values]
34 S_values_w = [self.space.wrap(value) for value in values]
35 N_w_tuple = W_TupleObject(N_values_w)
36 S_w_tuple = self.space.newtuple(S_values_w)
37
38 if must_be_specialized:
39 assert 'W_SpecialisedTupleObject' in type(S_w_tuple).__name__
40 assert self.space.is_true(self.space.eq(N_w_tuple, S_w_tuple))
41 assert self.space.is_true(
42 self.space.eq(self.space.hash(N_w_tuple),
43 self.space.hash(S_w_tuple)))
44
45 def test_hash_against_normal_tuple(self):
46 def hash_test(values, must_be_specialized=True):
47 self.hash_test(values, must_be_specialized=must_be_specialized)
48 hash_test([-1, -1])
49 hash_test([-1.0, -1.0])
50 hash_test([1, 2])
51 hash_test([1.5, 2.8])
52 hash_test([1.0, 2.0])
53 hash_test(['arbitrary', 'strings'])
54 hash_test([1, (1, 2, 3, 4)])
55 hash_test([1, (1, 2)])
56 hash_test([1, ('a', 2)])
57 hash_test([1, ()])
58 hash_test([1, 2, 3], must_be_specialized=False)
59 hash_test([1 << 62, 0])
60
61 try:
62 from hypothesis import given, strategies
63 except ImportError:
64 pass
65 else:
66 _int_float_text = strategies.one_of(
67 strategies.integers(),
68 strategies.floats(),
69 strategies.text())
70 @given(_int_float_text, _int_float_text)
71 def test_hash_with_hypothesis(self, x, y):
72 self.hash_test([x, y], must_be_specialized=False)
73
74
75 class AppTestW_SpecialisedTupleObject:
76 spaceconfig = {"objspace.std.withspecialisedtuple": True}
77
78 def w_isspecialised(self, obj, expected=''):
79 import __pypy__
80 r = __pypy__.internal_repr(obj)
81 print(obj, '==>', r, ' (expected: %r)' % expected)
82 return ("SpecialisedTupleObject" + expected) in r
83
84 def test_createspecialisedtuple(self):
85 have = ['ii', 'ff', 'oo']
86 #
87 spec = {int: 'i',
88 float: 'f',
89 str: 's',
90 list: 'o'}
91 #
92 for x in [42, 4.2, "foo", []]:
93 for y in [43, 4.3, "bar", []]:
94 expected1 = spec[type(x)]
95 expected2 = spec[type(y)]
96 if expected1 + expected2 not in have:
97 expected1 = expected2 = 'o'
98 obj = (x, y)
99 assert self.isspecialised(obj, '_' + expected1 + expected2)
100 #
101 if 'ooo' in have:
102 obj = (1, 2, 3)
103 assert self.isspecialised(obj, '_ooo')
104
105 def test_len(self):
106 t = (42, 43)
107 assert len(t) == 2
108
109 def test_notspecialisedtuple(self):
110 assert not self.isspecialised((42, 43, 44, 45))
111 assert not self.isspecialised((1.5,))
112
113 def test_slicing_to_specialised(self):
114 t = (1, 2, 3)
115 assert self.isspecialised(t[0:2])
116 t = (1, '2', 3)
117 assert self.isspecialised(t[0:5:2])
118
119 def test_adding_to_specialised(self):
120 t = (1,)
121 assert self.isspecialised(t + (2,))
122
123 def test_multiply_to_specialised(self):
124 t = (1,)
125 assert self.isspecialised(t * 2)
126
127 def test_slicing_from_specialised(self):
128 t = (1, 2, 3)
129 assert t[0:2:1] == (1, 2)
130
131 def test_eq_no_delegation(self):
132 t = (1,)
133 a = t + (2,)
134 b = (1, 2)
135 assert a == b
136
137 c = (2, 1)
138 assert not a == c
139
140 def test_eq_can_delegate(self):
141 a = (1, 2)
142 b = (1, 3, 2)
143 assert not a == b
144
145 values = [2, 2.0, 1, 1.0]
146 for x in values:
147 for y in values:
148 assert ((1, 2) == (x, y)) == (1 == x and 2 == y)
149
150 def test_neq(self):
151 a = (1, 2)
152 b = (1,)
153 b = b + (2,)
154 assert not a != b
155
156 c = (1, 3)
157 assert a != c
158
159 def test_ordering(self):
160 a = (1, 2)
161 assert a < (2, 2)
162 assert a < (1, 3)
163 assert not a < (1, 2)
164
165 assert a <= (2, 2)
166 assert a <= (1, 2)
167 assert not a <= (1, 1)
168
169 assert a >= (0, 2)
170 assert a >= (1, 2)
171 assert not a >= (1, 3)
172
173 assert a > (0, 2)
174 assert a > (1, 1)
175 assert not a > (1, 3)
176
177 assert (2, 2) > a
178 assert (1, 3) > a
179 assert not (1, 2) > a
180
181 assert (2, 2) >= a
182 assert (1, 2) >= a
183 assert not (1, 1) >= a
184
185 assert (0, 2) <= a
186 assert (1, 2) <= a
187 assert not (1, 3) <= a
188
189 assert (0, 2) < a
190 assert (1, 1) < a
191 assert not (1, 3) < a
192
193 def test_hash(self):
194 a = (1, 2)
195 b = (1,)
196 b += (2,) # else a and b refer to same constant
197 assert hash(a) == hash(b)
198
199 c = (2, 4)
200 assert hash(a) != hash(c)
201
202 assert hash(a) == hash((1, 2)) == hash((1.0, 2.0)) == hash((1.0, 2))
203
204 d = tuple([-1, 1])
205 e = (-1, 1)
206 assert d == e
207 assert hash(d) == hash(e)
208
209 x = (-1, -1)
210 y = tuple([-1, -1])
211 assert hash(x) == hash(y)
212
213 def test_getitem(self):
214 t = (5, 3)
215 assert (t)[0] == 5
216 assert (t)[1] == 3
217 assert (t)[-1] == 3
218 assert (t)[-2] == 5
219 raises(IndexError, "t[2]")
220 raises(IndexError, "t[-3]")
221
222 def test_three_tuples(self):
223 if not self.isspecialised((1, 2, 3)):
224 skip("don't have specialization for 3-tuples")
225 b = (1, 2, 3)
226 c = (1,)
227 d = c + (2, 3)
228 assert self.isspecialised(d)
229 assert b == d
230
231 def test_mongrel(self):
232 a = (2.2, '333')
233 assert self.isspecialised(a)
234 assert len(a) == 2
235 assert a[0] == 2.2 and a[1] == '333'
236 b = ('333',)
237 assert a == (2.2,) + b
238 assert not a != (2.2,) + b
239 #
240 if not self.isspecialised((1, 2, 3)):
241 skip("don't have specialization for 3-tuples")
242 a = (1, 2.2, '333')
243 assert self.isspecialised(a)
244 assert len(a) == 3
245 assert a[0] == 1 and a[1] == 2.2 and a[2] == '333'
246 b = ('333',)
247 assert a == (1, 2.2,) + b
248 assert not a != (1, 2.2) + b
249
250 def test_subclasses(self):
251 class I(int): pass
252 class F(float): pass
253 t = (I(42), I(43))
254 assert type(t[0]) is I
255 t = (F(42), F(43))
256 assert type(t[0]) is F
257
258 def test_ovfl_bug(self):
259 # previously failed
260 a = (0xffffffffffffffff, 0)
261
262 def test_bug_tuples_of_nans(self):
263 N = float('nan')
264 T = (N, N)
265 assert N in T
266 assert T == (N, N)
267 assert (0.0, 0.0) == (-0.0, -0.0)
268
269 def test_issue3301_exactly_two_bases(self):
270 # used to fail because the 2-tuple of bases gets specialized;
271 # the test would always pass with any number of bases != 2...
272 class BaseA: pass
273 class BaseB: pass
274 class Foo(BaseA, BaseB): pass
275 assert not hasattr(Foo, '__orig_bases__')
276
277
278 class AppTestAll(test_tupleobject.AppTestW_TupleObject):
279 spaceconfig = {"objspace.std.withspecialisedtuple": True}