Debian Codegraph @

pypy/objspace/std/test/test_tupleobject.py

raw symbols SCIP
1 import pytest
2 from pypy.interpreter.error import OperationError
3 from pypy.objspace.std.tupleobject import W_TupleObject
4
5
6 class TestW_TupleObject:
7 def test_is_true(self):
8 w = self.space.wrap
9 w_tuple = W_TupleObject([])
10 assert self.space.is_true(w_tuple) is False
11 w_tuple = W_TupleObject([w(5)])
12 assert self.space.is_true(w_tuple) is True
13 w_tuple = W_TupleObject([w(5), w(3)])
14 assert self.space.is_true(w_tuple) is True
15
16 def test_len(self):
17 w = self.space.wrap
18 w_tuple = W_TupleObject([])
19 assert self.space.eq_w(self.space.len(w_tuple), w(0))
20 w_tuple = W_TupleObject([w(5)])
21 assert self.space.eq_w(self.space.len(w_tuple), w(1))
22 w_tuple = W_TupleObject([w(5), w(3), w(99)] * 111)
23 assert self.space.eq_w(self.space.len(w_tuple), w(333))
24
25 def test_getitem(self):
26 w = self.space.wrap
27 w_tuple = W_TupleObject([w(5), w(3)])
28 assert self.space.eq_w(self.space.getitem(w_tuple, w(0)), w(5))
29 assert self.space.eq_w(self.space.getitem(w_tuple, w(1)), w(3))
30 assert self.space.eq_w(self.space.getitem(w_tuple, w(-2)), w(5))
31 assert self.space.eq_w(self.space.getitem(w_tuple, w(-1)), w(3))
32 self.space.raises_w(self.space.w_IndexError,
33 self.space.getitem, w_tuple, w(2))
34 self.space.raises_w(self.space.w_IndexError,
35 self.space.getitem, w_tuple, w(42))
36 self.space.raises_w(self.space.w_IndexError,
37 self.space.getitem, w_tuple, w(-3))
38
39 def test_iter(self):
40 w = self.space.wrap
41 w_tuple = W_TupleObject([w(5), w(3), w(99)])
42 w_iter = self.space.iter(w_tuple)
43 assert self.space.eq_w(self.space.next(w_iter), w(5))
44 assert self.space.eq_w(self.space.next(w_iter), w(3))
45 assert self.space.eq_w(self.space.next(w_iter), w(99))
46 pytest.raises(OperationError, self.space.next, w_iter)
47 pytest.raises(OperationError, self.space.next, w_iter)
48
49 def test_contains(self):
50 w = self.space.wrap
51 w_tuple = W_TupleObject([w(5), w(3), w(99)])
52 assert self.space.eq_w(self.space.contains(w_tuple, w(5)),
53 self.space.w_True)
54 assert self.space.eq_w(self.space.contains(w_tuple, w(99)),
55 self.space.w_True)
56 assert self.space.eq_w(self.space.contains(w_tuple, w(11)),
57 self.space.w_False)
58 assert self.space.eq_w(self.space.contains(w_tuple, w_tuple),
59 self.space.w_False)
60
61 def test_add(self):
62 w = self.space.wrap
63 w_tuple0 = W_TupleObject([])
64 w_tuple1 = W_TupleObject([w(5), w(3), w(99)])
65 w_tuple2 = W_TupleObject([w(-7)] * 111)
66 assert self.space.eq_w(self.space.add(w_tuple1, w_tuple1),
67 W_TupleObject([w(5), w(3), w(99),
68 w(5), w(3), w(99)]))
69 assert self.space.eq_w(self.space.add(w_tuple1, w_tuple2),
70 W_TupleObject([w(5), w(3), w(99)] + [w(-7)] * 111))
71 assert self.space.eq_w(self.space.add(w_tuple1, w_tuple0), w_tuple1)
72 assert self.space.eq_w(self.space.add(w_tuple0, w_tuple2), w_tuple2)
73
74 def test_mul(self):
75 # only testing right mul at the moment
76 w = self.space.wrap
77 arg = w(2)
78 n = 3
79 w_tup = W_TupleObject([arg])
80 w_tup3 = W_TupleObject([arg] * n)
81 w_res = self.space.mul(w_tup, w(n))
82 assert self.space.eq_w(w_tup3, w_res)
83 # commute
84 w_res = self.space.mul(w(n), w_tup)
85 assert self.space.eq_w(w_tup3, w_res)
86 # check tuple*1 is identity (optimisation tested by CPython tests)
87 w_res = self.space.mul(w_tup, w(1))
88 assert w_res is w_tup
89
90 def test_getslice(self):
91 w = self.space.wrap
92
93 def test1(testtuple, start, stop, step, expected):
94 w_slice = self.space.newslice(w(start), w(stop), w(step))
95 w_tuple = W_TupleObject([w(i) for i in testtuple])
96 w_result = self.space.getitem(w_tuple, w_slice)
97 assert self.space.unwrap(w_result) == expected
98
99 for testtuple in [(), (5, 3, 99), tuple(range(5, 555, 10))]:
100 for start in [-2, -1, 0, 1, 10]:
101 for end in [-1, 0, 2, 999]:
102 test1(testtuple, start, end, 1, testtuple[start:end])
103
104 test1((5, 7, 1, 4), 3, 1, -2, (4,))
105 test1((5, 7, 1, 4), 3, 0, -2, (4, 7))
106 test1((5, 7, 1, 4), 3, -1, -2, ())
107 test1((5, 7, 1, 4), -2, 11, 2, (1,))
108 test1((5, 7, 1, 4), -3, 11, 2, (7, 4))
109 test1((5, 7, 1, 4), -5, 11, 2, (5, 1))
110
111 def test_eq(self):
112 w = self.space.wrap
113
114 w_tuple0 = W_TupleObject([])
115 w_tuple1 = W_TupleObject([w(5), w(3), w(99)])
116 w_tuple2 = W_TupleObject([w(5), w(3), w(99)])
117 w_tuple3 = W_TupleObject([w(5), w(3), w(99), w(-1)])
118
119 assert self.space.eq_w(self.space.eq(w_tuple0, w_tuple1),
120 self.space.w_False)
121 assert self.space.eq_w(self.space.eq(w_tuple1, w_tuple0),
122 self.space.w_False)
123 assert self.space.eq_w(self.space.eq(w_tuple1, w_tuple1),
124 self.space.w_True)
125 assert self.space.eq_w(self.space.eq(w_tuple1, w_tuple2),
126 self.space.w_True)
127 assert self.space.eq_w(self.space.eq(w_tuple2, w_tuple3),
128 self.space.w_False)
129
130 def test_ne(self):
131 w = self.space.wrap
132
133 w_tuple0 = W_TupleObject([])
134 w_tuple1 = W_TupleObject([w(5), w(3), w(99)])
135 w_tuple2 = W_TupleObject([w(5), w(3), w(99)])
136 w_tuple3 = W_TupleObject([w(5), w(3), w(99), w(-1)])
137
138 assert self.space.eq_w(self.space.ne(w_tuple0, w_tuple1),
139 self.space.w_True)
140 assert self.space.eq_w(self.space.ne(w_tuple1, w_tuple0),
141 self.space.w_True)
142 assert self.space.eq_w(self.space.ne(w_tuple1, w_tuple1),
143 self.space.w_False)
144 assert self.space.eq_w(self.space.ne(w_tuple1, w_tuple2),
145 self.space.w_False)
146 assert self.space.eq_w(self.space.ne(w_tuple2, w_tuple3),
147 self.space.w_True)
148
149 def test_lt(self):
150 w = self.space.wrap
151
152 w_tuple0 = W_TupleObject([])
153 w_tuple1 = W_TupleObject([w(5), w(3), w(99)])
154 w_tuple2 = W_TupleObject([w(5), w(3), w(99)])
155 w_tuple3 = W_TupleObject([w(5), w(3), w(99), w(-1)])
156 w_tuple4 = W_TupleObject([w(5), w(3), w(9), w(-1)])
157
158 assert self.space.eq_w(self.space.lt(w_tuple0, w_tuple1),
159 self.space.w_True)
160 assert self.space.eq_w(self.space.lt(w_tuple1, w_tuple0),
161 self.space.w_False)
162 assert self.space.eq_w(self.space.lt(w_tuple1, w_tuple1),
163 self.space.w_False)
164 assert self.space.eq_w(self.space.lt(w_tuple1, w_tuple2),
165 self.space.w_False)
166 assert self.space.eq_w(self.space.lt(w_tuple2, w_tuple3),
167 self.space.w_True)
168 assert self.space.eq_w(self.space.lt(w_tuple4, w_tuple3),
169 self.space.w_True)
170
171 def test_ge(self):
172 w = self.space.wrap
173
174 w_tuple0 = W_TupleObject([])
175 w_tuple1 = W_TupleObject([w(5), w(3), w(99)])
176 w_tuple2 = W_TupleObject([w(5), w(3), w(99)])
177 w_tuple3 = W_TupleObject([w(5), w(3), w(99), w(-1)])
178 w_tuple4 = W_TupleObject([w(5), w(3), w(9), w(-1)])
179
180 assert self.space.eq_w(self.space.ge(w_tuple0, w_tuple1),
181 self.space.w_False)
182 assert self.space.eq_w(self.space.ge(w_tuple1, w_tuple0),
183 self.space.w_True)
184 assert self.space.eq_w(self.space.ge(w_tuple1, w_tuple1),
185 self.space.w_True)
186 assert self.space.eq_w(self.space.ge(w_tuple1, w_tuple2),
187 self.space.w_True)
188 assert self.space.eq_w(self.space.ge(w_tuple2, w_tuple3),
189 self.space.w_False)
190 assert self.space.eq_w(self.space.ge(w_tuple4, w_tuple3),
191 self.space.w_False)
192
193 def test_gt(self):
194 w = self.space.wrap
195
196 w_tuple0 = W_TupleObject([])
197 w_tuple1 = W_TupleObject([w(5), w(3), w(99)])
198 w_tuple2 = W_TupleObject([w(5), w(3), w(99)])
199 w_tuple3 = W_TupleObject([w(5), w(3), w(99), w(-1)])
200 w_tuple4 = W_TupleObject([w(5), w(3), w(9), w(-1)])
201
202 assert self.space.eq_w(self.space.gt(w_tuple0, w_tuple1),
203 self.space.w_False)
204 assert self.space.eq_w(self.space.gt(w_tuple1, w_tuple0),
205 self.space.w_True)
206 assert self.space.eq_w(self.space.gt(w_tuple1, w_tuple1),
207 self.space.w_False)
208 assert self.space.eq_w(self.space.gt(w_tuple1, w_tuple2),
209 self.space.w_False)
210 assert self.space.eq_w(self.space.gt(w_tuple2, w_tuple3),
211 self.space.w_False)
212 assert self.space.eq_w(self.space.gt(w_tuple4, w_tuple3),
213 self.space.w_False)
214
215 def test_le(self):
216 w = self.space.wrap
217
218 w_tuple0 = W_TupleObject([])
219 w_tuple1 = W_TupleObject([w(5), w(3), w(99)])
220 w_tuple2 = W_TupleObject([w(5), w(3), w(99)])
221 w_tuple3 = W_TupleObject([w(5), w(3), w(99), w(-1)])
222 w_tuple4 = W_TupleObject([w(5), w(3), w(9), w(-1)])
223
224 assert self.space.eq_w(self.space.le(w_tuple0, w_tuple1),
225 self.space.w_True)
226 assert self.space.eq_w(self.space.le(w_tuple1, w_tuple0),
227 self.space.w_False)
228 assert self.space.eq_w(self.space.le(w_tuple1, w_tuple1),
229 self.space.w_True)
230 assert self.space.eq_w(self.space.le(w_tuple1, w_tuple2),
231 self.space.w_True)
232 assert self.space.eq_w(self.space.le(w_tuple2, w_tuple3),
233 self.space.w_True)
234 assert self.space.eq_w(self.space.le(w_tuple4, w_tuple3),
235 self.space.w_True)
236
237 def test_hash_consistency(self):
238 # make sure that the two copies of the hash implementation are the same
239 w = self.space.wrap
240 w_tuple1 = W_TupleObject([w(5), w(3), w(99)])
241 w_tuple2 = W_TupleObject([w(5), w(3), w(99)])
242 w_tuple3 = W_TupleObject([w(5), w(3), w(99), w(-1)])
243 w_tuple4 = W_TupleObject([w(5), w(3), w(9), w(-1)])
244 for w_tup in w_tuple1, w_tuple2, w_tuple3, w_tuple4:
245 assert w_tup._descr_hash_unroll(self.space) == w_tup._descr_hash_jitdriver(self.space)
246
247
248 class AppTestW_TupleObject:
249 def test_is_true(self):
250 assert not ()
251 assert bool((5,))
252 assert bool((5, 3))
253
254 def test_len(self):
255 assert len(()) == 0
256 assert len((5,)) == 1
257 assert len((5, 3, 99, 1, 2, 3, 4, 5, 6)) == 9
258
259 def test_getitem(self):
260 assert (5, 3)[0] == 5
261 assert (5, 3)[1] == 3
262 assert (5, 3)[-1] == 3
263 assert (5, 3)[-2] == 5
264 raises(IndexError, "(5, 3)[2]")
265 raises(IndexError, "(5,)[1]")
266 raises(IndexError, "()[0]")
267
268 def test_iter(self):
269 t = (5, 3, 99)
270 i = iter(t)
271 assert next(i) == 5
272 assert next(i) == 3
273 assert next(i) == 99
274 raises(StopIteration, next, i)
275
276 def test_contains(self):
277 t = (5, 3, 99)
278 assert 5 in t
279 assert 99 in t
280 assert not 11 in t
281 assert not t in t
282
283 logger = []
284
285 class Foo(object):
286
287 def __init__(self, value, name=None):
288 self.value = value
289 self.name = name or value
290
291 def __repr__(self):
292 return '<Foo %s>' % self.name
293
294 def __eq__(self, other):
295 logger.append((self, other))
296 return self.value == other.value
297
298 foo1, foo2, foo3 = Foo(1), Foo(2), Foo(3)
299 foo42 = Foo(42)
300 foo_tuple = (foo1, foo2, foo3)
301 foo42 in foo_tuple
302 logger_copy = logger[:] # prevent re-evaluation during pytest error print
303 assert logger_copy == [(foo1, foo42), (foo2, foo42), (foo3, foo42)]
304
305 del logger[:]
306 foo2_bis = Foo(2, '2 bis')
307 foo2_bis in foo_tuple
308 logger_copy = logger[:] # prevent re-evaluation during pytest error print
309 assert logger_copy == [(foo1, foo2_bis), (foo2, foo2_bis)]
310
311 def test_add(self):
312 t0 = ()
313 t1 = (5, 3, 99)
314 assert t0 + t0 == t0
315 assert t1 + t0 == t1
316 assert t1 + t1 == (5, 3, 99, 5, 3, 99)
317
318 def test_mul(self):
319 assert () * 10 == ()
320 assert (5,) * 3 == (5, 5, 5)
321 assert (5, 2) * 2 == (5, 2, 5, 2)
322
323 def test_mul_identity(self):
324 t = (1, 2, 3)
325 assert (t * 1) is t
326
327 def test_mul_subtype(self):
328 class T(tuple): pass
329 t = T([1, 2, 3])
330 assert (t * 1) is not t
331 assert (t * 1) == t
332
333 def test_getslice_2(self):
334 assert (5, 2, 3)[1:2] == (2,)
335
336 def test_eq(self):
337 t0 = ()
338 t1 = (5, 3, 99)
339 t2 = (5, 3, 99)
340 t3 = (5, 3, 99, -1)
341 t4 = (5, 3, 9, 1)
342 assert not t0 == t1
343 assert t0 != t1
344 assert t1 == t2
345 assert t2 == t1
346 assert t3 != t2
347 assert not t3 == t2
348 assert not t2 == t3
349 assert t3 > t4
350 assert t2 > t4
351 assert t3 > t2
352 assert t1 > t0
353 assert t0 <= t0
354 assert not t0 < t0
355 assert t4 >= t0
356 assert t3 >= t2
357 assert t2 <= t3
358
359 def test_hash(self):
360 # check that hash behaves as in 3.8
361 import sys
362 is_32 = sys.maxsize == 2 ** 31 - 1
363 def check_one_exact(t, h32, h64):
364 h = hash(t)
365 if is_32:
366 assert h == h32
367 else:
368 assert h == h64
369
370 check_one_exact((), 750394483, 5740354900026072187)
371 check_one_exact((0,), 1214856301, -8753497827991233192)
372 check_one_exact((0, 0), -168982784, -8458139203682520985)
373 check_one_exact((0.5,), 2077348973, -408149959306781352)
374 check_one_exact((0.5, (), (-2, 3, (4, 6))), 714642271,
375 -1845940830829704396)
376
377 def test_getnewargs(self):
378 assert () .__getnewargs__() == ((),)
379
380 def test_repr(self):
381 assert repr((1,)) == '(1,)'
382 assert repr(()) == '()'
383 assert repr((1, 2, 3)) == '(1, 2, 3)'
384 assert repr(('\xe9',)) == "('\xe9',)"
385 assert repr(('\xe9', 1)) == "('\xe9', 1)"
386
387 def test_getslice(self):
388 assert ('a', 'b', 'c')[-17: 2] == ('a', 'b')
389
390 def test_count(self):
391 assert ().count(4) == 0
392 assert (1, 2, 3, 4).count(3) == 1
393 assert (1, 2, 3, 4).count(5) == 0
394 assert (1, 1, 1).count(1) == 3
395
396 def test_index(self):
397 raises(ValueError, ().index, 4)
398 (1, 2).index(1) == 0
399 (3, 4, 5).index(4) == 1
400 raises(ValueError, (1, 2, 3, 4).index, 5)
401 assert (4, 2, 3, 4).index(4, 1) == 3
402 assert (4, 4, 4).index(4, 1, 2) == 1
403 raises(ValueError, (1, 2, 3, 4).index, 4, 0, 2)
404
405 def test_comparison(self):
406 assert (() < ()) is False
407 assert (() <= ()) is True
408 assert (() == ()) is True
409 assert (() != ()) is False
410 assert (() > ()) is False
411 assert (() >= ()) is True
412 assert ((5,) < ()) is False
413 assert ((5,) <= ()) is False
414 assert ((5,) == ()) is False
415 assert ((5,) != ()) is True
416 assert ((5,) > ()) is True
417 assert ((5,) >= ()) is True
418 assert (() < (5,)) is True
419 assert (() <= (5,)) is True
420 assert (() == (5,)) is False
421 assert (() != (5,)) is True
422 assert (() > (5,)) is False
423 assert (() >= (5,)) is False
424 assert ((4,) < (5,)) is True
425 assert ((4,) <= (5,)) is True
426 assert ((4,) == (5,)) is False
427 assert ((4,) != (5,)) is True
428 assert ((4,) > (5,)) is False
429 assert ((4,) >= (5,)) is False
430 assert ((5,) < (5,)) is False
431 assert ((5,) <= (5,)) is True
432 assert ((5,) == (5,)) is True
433 assert ((5,) != (5,)) is False
434 assert ((5,) > (5,)) is False
435 assert ((5,) >= (5,)) is True
436 assert ((6,) < (5,)) is False
437 assert ((6,) <= (5,)) is False
438 assert ((6,) == (5,)) is False
439 assert ((6,) != (5,)) is True
440 assert ((6,) > (5,)) is True
441 assert ((6,) >= (5,)) is True
442 N = float('nan')
443 assert ((N,) < (5,)) is False
444 assert ((N,) <= (5,)) is False
445 assert ((N,) == (5,)) is False
446 assert ((N,) != (5,)) is True
447 assert ((N,) > (5,)) is False
448 assert ((N,) >= (5,)) is False
449 assert ((5,) < (N,)) is False
450 assert ((5,) <= (N,)) is False
451 assert ((5,) == (N,)) is False
452 assert ((5,) != (N,)) is True
453 assert ((5,) > (N,)) is False
454 assert ((5,) >= (N,)) is False
455
456 def test_eq_other_type(self):
457 assert (() == object()) is False
458 assert ((1,) == object()) is False
459 assert ((1, 2) == object()) is False
460 assert (() != object()) is True
461 assert ((1,) != object()) is True
462 assert ((1, 2) != object()) is True
463
464
465 def test_error_message_wrong_self(self):
466 unboundmeth = tuple.__hash__
467 e = raises(TypeError, unboundmeth, 42)
468 assert "tuple" in str(e.value)
469 if hasattr(unboundmeth, 'im_func'):
470 e = raises(TypeError, unboundmeth.im_func, 42)
471 assert "'tuple'" in str(e.value)
472
473 def test_tuple_new_pos_only(self):
474 with raises(TypeError):
475 tuple(sequence=[])
476
477 def test_error_not_iteratable(self):
478 with raises(TypeError) as excinfo:
479 tuple(1)
480 assert str(excinfo.value) == "'int' object is not iterable"
481
482 def test_error_msg_index(self):
483 with raises(TypeError) as excinfo:
484 a = None
485 (1, 2, 3)[a]
486 assert str(excinfo.value) == "tuple indices must be integers or slices, not NoneType"
487
488 def test_subclass_kwarg(self):
489 class bare_subclass(tuple):
490 pass
491 with raises(TypeError):
492 bare_subclass((), newarg=3)
493
494 class subclass_with_new(tuple):
495 def __new__(cls, arg, newarg=None):
496 self = super().__new__(cls, arg)
497 self.newarg = newarg
498 return self
499 u = subclass_with_new([1, 2], newarg=3)
500 assert u.newarg == 3
501
502 class subclass_with_init(tuple):
503 def __init__(self, arg, newarg=None):
504 self.newarg = newarg
505 u = subclass_with_init([1, 2], newarg=3)
506 assert u.newarg == 3