Debian Codegraph @

pypy/objspace/std/test/test_liststrategies.py

raw symbols SCIP
1 import sys
2 import py
3 from pypy.objspace.std.listobject import (
4 W_ListObject, EmptyListStrategy, ObjectListStrategy, IntegerListStrategy,
5 FloatListStrategy, BytesListStrategy, RangeListStrategy,
6 SimpleRangeListStrategy, make_range_list, AsciiListStrategy,
7 IntOrFloatListStrategy)
8 from pypy.objspace.std import listobject
9 from pypy.objspace.std.test.test_listobject import TestW_ListObject
10 from pypy.objspace.std.intobject import W_IntObject
11 from pypy.objspace.std.longobject import W_LongObject
12 from pypy.objspace.std.floatobject import W_FloatObject
13 from rpython.rlib.rbigint import rbigint
14
15
16 class TestW_ListStrategies(TestW_ListObject):
17 def test_check_strategy(self):
18 space = self.space
19 w = space.wrap
20 wb = space.newbytes
21 assert isinstance(W_ListObject(space, []).strategy, EmptyListStrategy)
22 assert isinstance(W_ListObject(space, [w(1),wb('a')]).strategy, ObjectListStrategy)
23 assert isinstance(W_ListObject(space, [w(1),w(2),w(3)]).strategy,
24 IntegerListStrategy)
25 assert isinstance(W_ListObject(space, [wb('a'), wb('b')]).strategy,
26 BytesListStrategy)
27 assert isinstance(W_ListObject(space, [w(u'a'), w(u'b')]).strategy,
28 AsciiListStrategy)
29 assert isinstance(W_ListObject(space, [w(u'a'), wb('b')]).strategy,
30 ObjectListStrategy) # mixed unicode and bytes
31
32 def test_empty_to_any(self):
33 space = self.space
34 w = space.wrap
35 wb = space.newbytes
36 l = W_ListObject(space, [])
37 assert isinstance(l.strategy, EmptyListStrategy)
38 l.append(w((1,3)))
39 assert isinstance(l.strategy, ObjectListStrategy)
40
41 l = W_ListObject(space, [])
42 assert isinstance(l.strategy, EmptyListStrategy)
43 l.append(w(1))
44 assert isinstance(l.strategy, IntegerListStrategy)
45
46 l = W_ListObject(space, [])
47 assert isinstance(l.strategy, EmptyListStrategy)
48 l.append(wb('a'))
49 assert isinstance(l.strategy, BytesListStrategy)
50
51 l = W_ListObject(space, [])
52 assert isinstance(l.strategy, EmptyListStrategy)
53 l.append(w(u'a'))
54 assert isinstance(l.strategy, AsciiListStrategy)
55
56 l = W_ListObject(space, [])
57 assert isinstance(l.strategy, EmptyListStrategy)
58 l.append(w(1.2))
59 assert isinstance(l.strategy, FloatListStrategy)
60
61 def test_int_to_any(self):
62 l = W_ListObject(self.space,
63 [self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
64 assert isinstance(l.strategy, IntegerListStrategy)
65 l.append(self.space.wrap(4))
66 assert isinstance(l.strategy, IntegerListStrategy)
67 l.append(self.space.wrap('a'))
68 assert isinstance(l.strategy, ObjectListStrategy)
69
70 def test_string_to_any(self):
71 l = W_ListObject(self.space,
72 [self.space.newbytes('a'), self.space.newbytes('b'),
73 self.space.newbytes('c')])
74 assert isinstance(l.strategy, BytesListStrategy)
75 l.append(self.space.newbytes('d'))
76 assert isinstance(l.strategy, BytesListStrategy)
77 l.append(self.space.wrap(3))
78 assert isinstance(l.strategy, ObjectListStrategy)
79
80 def test_unicode_to_any(self):
81 space = self.space
82 l = W_ListObject(space, [space.wrap(u'a'), space.wrap(u'b'), space.wrap(u'c')])
83 assert isinstance(l.strategy, AsciiListStrategy)
84 l.append(space.wrap(u'd'))
85 assert isinstance(l.strategy, AsciiListStrategy)
86 l.append(space.wrap(3))
87 assert isinstance(l.strategy, ObjectListStrategy)
88
89 def test_float_to_any(self):
90 l = W_ListObject(self.space,
91 [self.space.wrap(1.1),self.space.wrap(2.2),self.space.wrap(3.3)])
92 assert isinstance(l.strategy, FloatListStrategy)
93 l.append(self.space.wrap(4.4))
94 assert isinstance(l.strategy, FloatListStrategy)
95 l.append(self.space.wrap("a"))
96 assert isinstance(l.strategy, ObjectListStrategy)
97
98 def test_setitem(self):
99 space = self.space
100 w = space.wrap
101 wb = space.newbytes
102 # This should work if test_listobject.py passes
103 l = W_ListObject(space, [w('a'),w('b'),w('c')])
104 assert space.eq_w(l.getitem(0), w('a'))
105 l.setitem(0, w('d'))
106 assert space.eq_w(l.getitem(0), w('d'))
107
108 assert isinstance(l.strategy, AsciiListStrategy)
109
110 # IntStrategy to ObjectStrategy
111 l = W_ListObject(space, [w(1),w(2),w(3)])
112 assert isinstance(l.strategy, IntegerListStrategy)
113 l.setitem(0, w('d'))
114 assert isinstance(l.strategy, ObjectListStrategy)
115
116 # BytesStrategy to ObjectStrategy
117 l = W_ListObject(space, [wb('a'),wb('b'),wb('c')])
118 assert isinstance(l.strategy, BytesListStrategy)
119 l.setitem(0, w(2))
120 assert isinstance(l.strategy, ObjectListStrategy)
121
122 # FloatStrategy to ObjectStrategy
123 l = W_ListObject(space, [w(1.2),w(2.3),w(3.4)])
124 assert isinstance(l.strategy, FloatListStrategy)
125 l.setitem(0, w("a"))
126 assert isinstance(l.strategy, ObjectListStrategy)
127
128 def test_insert(self):
129 space = self.space
130 w = space.wrap
131 wb = space.newbytes
132 # no change
133 l = W_ListObject(space, [w(1),w(2),w(3)])
134 assert isinstance(l.strategy, IntegerListStrategy)
135 l.insert(3, w(4))
136 assert isinstance(l.strategy, IntegerListStrategy)
137
138 # BytesStrategy
139 l = W_ListObject(space, [wb('a'),wb('b'),wb('c')])
140 assert isinstance(l.strategy, BytesListStrategy)
141 l.insert(3, w(2))
142 assert isinstance(l.strategy, ObjectListStrategy)
143
144 # UnicodeStrategy
145 l = W_ListObject(space, [w(u'a'),w(u'b'),w(u'c')])
146 assert isinstance(l.strategy, AsciiListStrategy)
147 l.insert(3, w(2))
148 assert isinstance(l.strategy, ObjectListStrategy)
149
150 # IntegerStrategy
151 l = W_ListObject(space, [w(1),w(2),w(3)])
152 assert isinstance(l.strategy, IntegerListStrategy)
153 l.insert(3, w('d'))
154 assert isinstance(l.strategy, ObjectListStrategy)
155
156 # FloatStrategy
157 l = W_ListObject(space, [w(1.1),w(2.2),w(3.3)])
158 assert isinstance(l.strategy, FloatListStrategy)
159 l.insert(3, w('d'))
160 assert isinstance(l.strategy, ObjectListStrategy)
161
162 # EmptyStrategy
163 l = W_ListObject(space, [])
164 assert isinstance(l.strategy, EmptyListStrategy)
165 l.insert(0, wb('a'))
166 assert isinstance(l.strategy, BytesListStrategy)
167
168 l = W_ListObject(space, [])
169 assert isinstance(l.strategy, EmptyListStrategy)
170 l.insert(0, w(2))
171 assert isinstance(l.strategy, IntegerListStrategy)
172
173 def test_list_empty_after_delete(self):
174 py.test.skip("return to emptyliststrategy is not supported anymore")
175 l = W_ListObject(self.space, [self.space.wrap(3)])
176 assert isinstance(l.strategy, IntegerListStrategy)
177 l.deleteitem(0)
178 assert isinstance(l.strategy, EmptyListStrategy)
179
180 l = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2)])
181 assert isinstance(l.strategy, IntegerListStrategy)
182 l.deleteslice(0, 1, 2)
183 assert isinstance(l.strategy, EmptyListStrategy)
184
185 l = W_ListObject(self.space, [self.space.wrap(1)])
186 assert isinstance(l.strategy, IntegerListStrategy)
187 l.pop(-1)
188 assert isinstance(l.strategy, EmptyListStrategy)
189
190 def test_setslice(self):
191 space = self.space
192 w = space.wrap
193 wb = space.newbytes
194
195 l = W_ListObject(space, [])
196 assert isinstance(l.strategy, EmptyListStrategy)
197 l.setslice(0, 1, 2, W_ListObject(space, [w(1), w(2), w(3)]))
198 assert isinstance(l.strategy, IntegerListStrategy)
199
200 # IntegerStrategy to IntegerStrategy
201 l = W_ListObject(space, [w(1), w(2), w(3)])
202 assert isinstance(l.strategy, IntegerListStrategy)
203 l.setslice(0, 1, 2, W_ListObject(space, [w(4), w(5), w(6)]))
204 assert isinstance(l.strategy, IntegerListStrategy)
205
206 # ObjectStrategy to ObjectStrategy
207 l = W_ListObject(space, [w(1), w('b'), w(3)])
208 assert isinstance(l.strategy, ObjectListStrategy)
209 l.setslice(0, 1, 2, W_ListObject(space, [w(1), w(2), w(3)]))
210 assert isinstance(l.strategy, ObjectListStrategy)
211
212 # IntegerStrategy to ObjectStrategy
213 l = W_ListObject(space, [w(1), w(2), w(3)])
214 assert isinstance(l.strategy, IntegerListStrategy)
215 l.setslice(0, 1, 2, W_ListObject(space, [w('a'), w('b'), w('c')]))
216 assert isinstance(l.strategy, ObjectListStrategy)
217
218 # BytesStrategy to ObjectStrategy
219 l = W_ListObject(space, [wb('a'), wb('b'), wb('c')])
220 assert isinstance(l.strategy, BytesListStrategy)
221 l.setslice(0, 1, 2, W_ListObject(space, [w(1), w(2), w(3)]))
222 assert isinstance(l.strategy, ObjectListStrategy)
223
224 # UnicodeStrategy to ObjectStrategy
225 l = W_ListObject(space, [w(u'a'), w(u'b'), w(u'c')])
226 assert isinstance(l.strategy, AsciiListStrategy)
227 l.setslice(0, 1, 2, W_ListObject(space, [w(1), w(2), w(3)]))
228 assert isinstance(l.strategy, ObjectListStrategy)
229
230 # FloatStrategy to ObjectStrategy
231 l = W_ListObject(space, [w(1.1), w(2.2), w(3.3)])
232 assert isinstance(l.strategy, FloatListStrategy)
233 l.setslice(0, 1, 2, W_ListObject(space, [w('a'), w(2), w(3)]))
234 assert isinstance(l.strategy, ObjectListStrategy)
235
236 def test_setslice_int_range(self):
237 space = self.space
238 w = space.wrap
239 l = W_ListObject(space, [w(1), w(2), w(3)])
240 assert isinstance(l.strategy, IntegerListStrategy)
241 l.setslice(0, 1, 2, make_range_list(space, 5, 1, 4))
242 assert isinstance(l.strategy, IntegerListStrategy)
243
244 def test_setslice_List(self):
245 space = self.space
246
247 def wrapitems(items, bytes=False):
248 items_w = []
249 for i in items:
250 w_item = space.newbytes(i) if bytes else space.wrap(i)
251 items_w.append(w_item)
252 return items_w
253
254 def keep_other_strategy(w_list, start, step, length, w_other):
255 other_strategy = w_other.strategy
256 w_list.setslice(start, step, length, w_other)
257 assert w_other.strategy is other_strategy
258
259 l = W_ListObject(space, wrapitems([1,2,3,4,5]))
260 other = W_ListObject(space, wrapitems(["a", "b", "c"]))
261 keep_other_strategy(l, 0, 2, other.length(), other)
262 assert l.strategy is space.fromcache(ObjectListStrategy)
263
264 l = W_ListObject(space, wrapitems([1,2,3,4,5]))
265 other = W_ListObject(space, wrapitems([6, 6, 6]))
266 keep_other_strategy(l, 0, 2, other.length(), other)
267 assert l.strategy is space.fromcache(IntegerListStrategy)
268
269 l = W_ListObject(space, wrapitems(["a","b","c","d","e"], bytes=True))
270 other = W_ListObject(space, wrapitems(["a", "b", "c"], bytes=True))
271 keep_other_strategy(l, 0, 2, other.length(), other)
272 assert l.strategy is space.fromcache(BytesListStrategy)
273
274 l = W_ListObject(space, wrapitems([u"a",u"b",u"c",u"d",u"e"]))
275 other = W_ListObject(space, wrapitems([u"a", u"b", u"c"]))
276 keep_other_strategy(l, 0, 2, other.length(), other)
277 assert l.strategy is space.fromcache(AsciiListStrategy)
278
279 l = W_ListObject(space, wrapitems([1.1, 2.2, 3.3, 4.4, 5.5]))
280 other = W_ListObject(space, [])
281 keep_other_strategy(l, 0, 1, l.length(), other)
282 assert l.strategy is space.fromcache(FloatListStrategy)
283
284 l = W_ListObject(space, wrapitems(["a",3,"c",4,"e"]))
285 other = W_ListObject(space, wrapitems(["a", "b", "c"]))
286 keep_other_strategy(l, 0, 2, other.length(), other)
287 assert l.strategy is space.fromcache(ObjectListStrategy)
288
289 l = W_ListObject(space, wrapitems(["a",3,"c",4,"e"]))
290 other = W_ListObject(space, [])
291 keep_other_strategy(l, 0, 1, l.length(), other)
292 assert l.strategy is space.fromcache(ObjectListStrategy)
293
294 def test_empty_setslice_with_objectlist(self):
295 space = self.space
296 w = space.wrap
297
298 l = W_ListObject(space, [])
299 o = W_ListObject(space, [space.wrap(1), space.wrap("2"), space.wrap(3)])
300 l.setslice(0, 1, o.length(), o)
301 assert l.getitems() == o.getitems()
302 l.append(space.wrap(17))
303 assert l.getitems() != o.getitems()
304
305 def test_extend(self):
306 space = self.space
307 w = space.wrap
308
309 l = W_ListObject(space, [])
310 assert isinstance(l.strategy, EmptyListStrategy)
311 l.extend(W_ListObject(space, [w(1), w(2), w(3)]))
312 assert isinstance(l.strategy, IntegerListStrategy)
313
314 l = W_ListObject(space, [w(1), w(2), w(3)])
315 assert isinstance(l.strategy, IntegerListStrategy)
316 l.extend(W_ListObject(space, [w('a'), w('b'), w('c')]))
317 assert isinstance(l.strategy, ObjectListStrategy)
318
319 l = W_ListObject(space, [w(1), w(2), w(3)])
320 assert isinstance(l.strategy, IntegerListStrategy)
321 l.extend(W_ListObject(space, [w(4), w(5), w(6)]))
322 assert isinstance(l.strategy, IntegerListStrategy)
323
324 l = W_ListObject(space, [w(1.1), w(2.2), w(3.3)])
325 assert isinstance(l.strategy, FloatListStrategy)
326 l.extend(W_ListObject(space, [w("abc"), w("def"), w("ghi")]))
327 assert isinstance(l.strategy, ObjectListStrategy)
328
329 def test_empty_extend_with_any(self):
330 space = self.space
331 w = space.wrap
332 wb = space.newbytes
333
334 empty = W_ListObject(space, [])
335 assert isinstance(empty.strategy, EmptyListStrategy)
336 empty.extend(W_ListObject(space, [w(1), w(2), w(3)]))
337 assert isinstance(empty.strategy, IntegerListStrategy)
338
339 empty = W_ListObject(space, [])
340 assert isinstance(empty.strategy, EmptyListStrategy)
341 empty.extend(W_ListObject(space, [wb("a"), wb("b"), wb("c")]))
342 assert isinstance(empty.strategy, BytesListStrategy)
343
344 empty = W_ListObject(space, [])
345 assert isinstance(empty.strategy, EmptyListStrategy)
346 empty.extend(W_ListObject(space, [w(u"a"), w(u"b"), w(u"c")]))
347 assert isinstance(empty.strategy, AsciiListStrategy)
348
349 empty = W_ListObject(space, [])
350 assert isinstance(empty.strategy, EmptyListStrategy)
351 r = make_range_list(space, 1,3,7)
352 empty.extend(r)
353 assert isinstance(empty.strategy, RangeListStrategy)
354 print empty.getitem(6)
355 assert space.is_true(space.eq(empty.getitem(1), w(4)))
356
357 empty = W_ListObject(space, [])
358 assert isinstance(empty.strategy, EmptyListStrategy)
359 r = make_range_list(space, 0, 1, 10)
360 empty.extend(r)
361 assert isinstance(empty.strategy, SimpleRangeListStrategy)
362 assert space.is_true(space.eq(empty.getitem(1), w(1)))
363
364 empty = W_ListObject(space, [])
365 assert isinstance(empty.strategy, EmptyListStrategy)
366 empty.extend(W_ListObject(space, [w(1), w(2), w(3)]))
367 assert isinstance(empty.strategy, IntegerListStrategy)
368
369 empty = W_ListObject(space, [])
370 assert isinstance(empty.strategy, EmptyListStrategy)
371 empty.extend(W_ListObject(space, [w(1.1), w(2.2), w(3.3)]))
372 assert isinstance(empty.strategy, FloatListStrategy)
373
374 empty = W_ListObject(space, [])
375 assert isinstance(empty.strategy, EmptyListStrategy)
376 empty.extend(W_ListObject(space, []))
377 assert isinstance(empty.strategy, EmptyListStrategy)
378
379 def test_extend_other_with_empty(self):
380 space = self.space
381 w = space.wrap
382 l = W_ListObject(space, [w(1), w(2), w(3)])
383 assert isinstance(l.strategy, IntegerListStrategy)
384 l.extend(W_ListObject(space, []))
385 assert isinstance(l.strategy, IntegerListStrategy)
386
387 def test_rangelist(self):
388 l = make_range_list(self.space, 1,3,7)
389 assert isinstance(l.strategy, RangeListStrategy)
390 v = l.pop(5)
391 assert self.space.eq_w(v, self.space.wrap(16))
392 assert isinstance(l.strategy, IntegerListStrategy)
393
394 l = make_range_list(self.space, 1,3,7)
395 assert isinstance(l.strategy, RangeListStrategy)
396 v = l.pop(0)
397 assert self.space.eq_w(v, self.space.wrap(1))
398 assert isinstance(l.strategy, RangeListStrategy)
399 v = l.pop(l.length() - 1)
400 assert self.space.eq_w(v, self.space.wrap(19))
401 assert isinstance(l.strategy, RangeListStrategy)
402 v = l.pop_end()
403 assert self.space.eq_w(v, self.space.wrap(16))
404 assert isinstance(l.strategy, RangeListStrategy)
405
406 l = make_range_list(self.space, 1,3,7)
407 assert isinstance(l.strategy, RangeListStrategy)
408 l.append(self.space.wrap("string"))
409 assert isinstance(l.strategy, ObjectListStrategy)
410
411 l = make_range_list(self.space, 1,1,5)
412 assert isinstance(l.strategy, RangeListStrategy)
413 l.append(self.space.wrap(19))
414 assert isinstance(l.strategy, IntegerListStrategy)
415
416 def test_simplerangelist(self):
417 l = make_range_list(self.space, 0, 1, 10)
418 assert isinstance(l.strategy, SimpleRangeListStrategy)
419 v = l.pop(5)
420 assert self.space.eq_w(v, self.space.wrap(5))
421 assert isinstance(l.strategy, IntegerListStrategy)
422
423 l = make_range_list(self.space, 0, 1, 10)
424 assert isinstance(l.strategy, SimpleRangeListStrategy)
425 v = l.pop(0)
426 assert self.space.eq_w(v, self.space.wrap(0))
427 assert isinstance(l.strategy, IntegerListStrategy)
428
429 l = make_range_list(self.space, 0, 1, 10)
430 assert isinstance(l.strategy, SimpleRangeListStrategy)
431 v = l.pop_end()
432 assert self.space.eq_w(v, self.space.wrap(9))
433 assert isinstance(l.strategy, SimpleRangeListStrategy)
434 v = l.pop_end()
435 assert self.space.eq_w(v, self.space.wrap(8))
436 assert isinstance(l.strategy, SimpleRangeListStrategy)
437
438 l = make_range_list(self.space, 0, 1, 5)
439 assert isinstance(l.strategy, SimpleRangeListStrategy)
440 l.append(self.space.wrap("string"))
441 assert isinstance(l.strategy, ObjectListStrategy)
442
443 l = make_range_list(self.space, 0,1,5)
444 assert isinstance(l.strategy, SimpleRangeListStrategy)
445 l.append(self.space.wrap(19))
446 assert isinstance(l.strategy, IntegerListStrategy)
447
448 l = make_range_list(self.space, 0,1,5)
449 assert isinstance(l.strategy, SimpleRangeListStrategy)
450 assert l.find_or_count(self.space.wrap(0)) == 0
451 assert l.find_or_count(self.space.wrap(4)) == 4
452
453 try:
454 l.find_or_count(self.space.wrap(5))
455 except ValueError:
456 pass
457 else:
458 assert False, "Did not raise ValueError"
459
460 try:
461 l.find_or_count(self.space.wrap(0), 5, 6)
462 except ValueError:
463 pass
464 else:
465 assert False, "Did not raise ValueError"
466
467 assert l.length() == 5
468
469 l = make_range_list(self.space, 0, 1, 1)
470 assert self.space.eq_w(l.pop(0), self.space.wrap(0))
471
472 l = make_range_list(self.space, 0, 1, 10)
473 l.sort(False)
474 assert isinstance(l.strategy, SimpleRangeListStrategy)
475
476 assert self.space.eq_w(l.getitem(5), self.space.wrap(5))
477
478 l = make_range_list(self.space, 0, 1, 1)
479 assert self.space.eq_w(l.pop_end(), self.space.wrap(0))
480 assert isinstance(l.strategy, EmptyListStrategy)
481
482 def test_keep_range(self):
483 # simple list
484 l = make_range_list(self.space, 1,1,5)
485 assert isinstance(l.strategy, RangeListStrategy)
486 x = l.pop(0)
487 assert self.space.eq_w(x, self.space.wrap(1))
488 assert isinstance(l.strategy, RangeListStrategy)
489 l.pop(l.length()-1)
490 assert isinstance(l.strategy, RangeListStrategy)
491 l.append(self.space.wrap(5))
492 assert isinstance(l.strategy, IntegerListStrategy)
493
494 # complex list
495 l = make_range_list(self.space, 1,3,5)
496 assert isinstance(l.strategy, RangeListStrategy)
497 l.append(self.space.wrap(16))
498 assert isinstance(l.strategy, IntegerListStrategy)
499
500 def test_empty_range(self):
501 l = make_range_list(self.space, 0, 0, 0)
502 assert isinstance(l.strategy, EmptyListStrategy)
503
504 l = make_range_list(self.space, 1, 1, 10)
505 for i in l.getitems():
506 assert isinstance(l.strategy, RangeListStrategy)
507 l.pop(l.length()-1)
508
509 assert isinstance(l.strategy, RangeListStrategy)
510
511 def test_range_getslice_ovf(self):
512 l = make_range_list(self.space, -sys.maxint, sys.maxint // 10, 21)
513 assert isinstance(l.strategy, RangeListStrategy)
514 l2 = l.getslice(0, 21, 11, 2)
515 assert isinstance(l2.strategy, IntegerListStrategy)
516
517 def test_range_setslice(self):
518 l = make_range_list(self.space, 1, 3, 5)
519 assert isinstance(l.strategy, RangeListStrategy)
520 l.setslice(0, 1, 3, W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]))
521 assert isinstance(l.strategy, IntegerListStrategy)
522
523 def test_range_reverse_ovf(self):
524 l = make_range_list(self.space, 0, -sys.maxint - 1, 1)
525 assert isinstance(l.strategy, RangeListStrategy)
526 l.reverse()
527 assert isinstance(l.strategy, IntegerListStrategy)
528
529 l = make_range_list(self.space, 0, -sys.maxint - 1, 1)
530 l.sort(False)
531 assert isinstance(l.strategy, IntegerListStrategy)
532
533 def test_copy_list(self):
534 l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
535 l2 = l1.clone()
536 l2.append(self.space.wrap(4))
537 assert not l2 == l1.getitems()
538
539 def test_getitems_does_not_copy_object_list(self):
540 l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap("two"), self.space.wrap(3)])
541 l2 = l1.getitems()
542 l2.append(self.space.wrap("four"))
543 assert l2 == l1.getitems()
544
545 def test_clone(self):
546 l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
547 clone = l1.clone()
548 assert isinstance(clone.strategy, IntegerListStrategy)
549 clone.append(self.space.wrap(7))
550 assert not self.space.eq_w(l1, clone)
551
552 def test_add_does_not_use_getitems(self):
553 l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
554 l1.getitems = None
555 l2 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
556 l2.getitems = None
557 l3 = self.space.add(l1, l2)
558 l4 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
559 assert self.space.eq_w(l3, l4)
560
561 def test_add_of_range_and_int(self):
562 l1 = make_range_list(self.space, 0, 1, 100)
563 l2 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
564 l3 = self.space.add(l2, l1)
565 assert l3.strategy is l2.strategy
566
567 def test_mul(self):
568 l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
569 l2 = l1.mul(2)
570 l3 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
571 assert self.space.eq_w(l2, l3)
572
573 l4 = make_range_list(self.space, 1, 1, 3)
574 assert self.space.eq_w(l4, l1)
575
576 l5 = l4.mul(2)
577 assert self.space.eq_w(l5, l3)
578
579 def test_mul_same_strategy_but_different_object(self):
580 l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
581 l2 = l1.mul(1)
582 assert self.space.eq_w(l1, l2)
583 l1.setitem(0, self.space.wrap(5))
584 assert not self.space.eq_w(l1, l2)
585
586 def test_weird_rangelist_bug(self):
587 space = self.space
588 l = make_range_list(space, 1, 1, 3)
589 # should not raise
590 w_slice = space.newslice(space.wrap(15), space.wrap(2222), space.wrap(1))
591 assert l.descr_getitem(space, w_slice).strategy == space.fromcache(EmptyListStrategy)
592
593 def test_add_to_rangelist(self):
594 l1 = make_range_list(self.space, 1, 1, 3)
595 l2 = W_ListObject(self.space, [self.space.wrap(4), self.space.wrap(5)])
596 l3 = l1.descr_add(self.space, l2)
597 assert self.space.eq_w(l3, W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(4), self.space.wrap(5)]))
598
599 def test_unicode(self):
600 l1 = W_ListObject(self.space, [self.space.newbytes("eins"), self.space.newbytes("zwei")])
601 assert isinstance(l1.strategy, BytesListStrategy)
602 l2 = W_ListObject(self.space, [self.space.newutf8("eins", 4), self.space.newutf8("zwei", 4)])
603 assert isinstance(l2.strategy, AsciiListStrategy)
604 l3 = W_ListObject(self.space, [self.space.newbytes("eins"), self.space.newutf8("zwei", 4)])
605 assert isinstance(l3.strategy, ObjectListStrategy)
606
607 def test_listview_bytes(self):
608 space = self.space
609 assert space.listview_bytes(space.wrap(1)) == None
610 w_l = self.space.newlist([self.space.newbytes('a'), self.space.newbytes('b')])
611 assert space.listview_bytes(w_l) == ["a", "b"]
612
613 def test_listview_unicode(self):
614 space = self.space
615 assert space.listview_ascii(space.wrap(1)) == None
616 w_l = self.space.newlist([self.space.wrap(u'a'), self.space.wrap(u'b')])
617 assert space.listview_ascii(w_l) == ["a", "b"]
618
619 def test_string_join_uses_listview_bytes(self):
620 space = self.space
621 w_l = self.space.newlist([self.space.wrap('a'), self.space.wrap('b')])
622 w_l.getitems = None
623 assert space.text_w(space.call_method(space.wrap("c"), "join", w_l)) == "acb"
624 #
625 # the same for unicode
626 w_l = self.space.newlist([self.space.wrap(u'a'), self.space.wrap(u'b')])
627 w_l.getitems = None
628 assert space.utf8_w(space.call_method(space.wrap(u"c"), "join", w_l)) == "acb"
629
630 def test_string_join_returns_same_instance(self):
631 space = self.space
632 w_text = space.wrap("text")
633 w_l = self.space.newlist([w_text])
634 w_l.getitems = None
635 assert space.is_w(space.call_method(space.wrap(" -- "), "join", w_l), w_text)
636 #
637 # the same for unicode
638 w_text = space.wrap(u"text")
639 w_l = self.space.newlist([w_text])
640 w_l.getitems = None
641 assert space.is_w(space.call_method(space.wrap(u" -- "), "join", w_l), w_text)
642
643 def test_newlist_bytes(self):
644 space = self.space
645 l = ['a', 'b']
646 w_l = self.space.newlist_bytes(l)
647 assert isinstance(w_l.strategy, BytesListStrategy)
648 assert space.listview_bytes(w_l) is l
649
650 def test_string_uses_newlist_bytes(self):
651 space = self.space
652 w_s = space.newbytes("a b c")
653 space.newlist = None
654 try:
655 w_l = space.call_method(w_s, "split")
656 w_l2 = space.call_method(w_s, "split", space.newbytes(" "))
657 w_l3 = space.call_method(w_s, "rsplit")
658 w_l4 = space.call_method(w_s, "rsplit", space.newbytes(" "))
659 finally:
660 del space.newlist
661 assert space.listview_bytes(w_l) == ["a", "b", "c"]
662 assert space.listview_bytes(w_l2) == ["a", "b", "c"]
663 assert space.listview_bytes(w_l3) == ["a", "b", "c"]
664 assert space.listview_bytes(w_l4) == ["a", "b", "c"]
665
666 def test_unicode_uses_newlist_unicode(self):
667 space = self.space
668 w_u = space.wrap(u"a b c")
669 space.newlist = None
670 try:
671 w_l = space.call_method(w_u, "split")
672 w_l2 = space.call_method(w_u, "split", space.wrap(" "))
673 w_l3 = space.call_method(w_u, "rsplit")
674 w_l4 = space.call_method(w_u, "rsplit", space.wrap(" "))
675 finally:
676 del space.newlist
677 assert space.listview_ascii(w_l) == [u"a", u"b", u"c"]
678 assert space.listview_ascii(w_l2) == [u"a", u"b", u"c"]
679 assert space.listview_ascii(w_l3) == [u"a", u"b", u"c"]
680 assert space.listview_ascii(w_l4) == [u"a", u"b", u"c"]
681
682 def test_pop_without_argument_is_fast(self):
683 space = self.space
684 w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])
685 w_l.pop = None
686 w_res = w_l.descr_pop(space)
687 assert space.unwrap(w_res) == 3
688
689 def test_create_list_from_set(self):
690 from pypy.objspace.std.setobject import W_SetObject
691 from pypy.objspace.std.setobject import _initialize_set
692
693 space = self.space
694 w = space.wrap
695
696 w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])
697
698 w_set = W_SetObject(self.space)
699 _initialize_set(self.space, w_set, w_l)
700 w_set.iter = None # make sure fast path is used
701
702 w_l2 = W_ListObject(space, [])
703 space.call_method(w_l2, "__init__", w_set)
704
705 w_l2.sort(False)
706 assert space.eq_w(w_l, w_l2)
707
708 w_l = W_ListObject(space, [space.wrap("a"), space.wrap("b"), space.wrap("c")])
709 _initialize_set(self.space, w_set, w_l)
710
711 space.call_method(w_l2, "__init__", w_set)
712
713 w_l2.sort(False)
714 assert space.eq_w(w_l, w_l2)
715
716 def test_listview_bytes_list(self):
717 space = self.space
718 w_l = W_ListObject(space, [space.newbytes("a"), space.newbytes("b")])
719 assert self.space.listview_bytes(w_l) == ["a", "b"]
720
721 def test_listview_unicode_list(self):
722 space = self.space
723 w_l = W_ListObject(space, [space.wrap(u"a"), space.wrap(u"b")])
724 assert self.space.listview_ascii(w_l) == [u"a", u"b"]
725
726 def test_listview_int_list(self):
727 space = self.space
728 w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])
729 assert self.space.listview_int(w_l) == [1, 2, 3]
730
731 def test_listview_float_list(self):
732 space = self.space
733 w_l = W_ListObject(space, [space.wrap(1.1), space.wrap(2.2), space.wrap(3.3)])
734 assert self.space.listview_float(w_l) == [1.1, 2.2, 3.3]
735
736 def test_unpackiterable_int_list(self):
737 space = self.space
738 w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])
739 list_orig = self.space.listview_int(w_l)
740 list_copy = self.space.unpackiterable_int(w_l)
741 assert list_orig == list_copy == [1, 2, 3]
742 list_copy[0] = 42
743 assert list_orig == [1, 2, 3]
744
745 def test_int_or_float_special_nan(self):
746 from rpython.rlib import longlong2float, rarithmetic
747 space = self.space
748 ll = rarithmetic.r_longlong(0xfffffffe12345678 - 2**64)
749 specialnan = longlong2float.longlong2float(ll)
750 w_l = W_ListObject(space, [space.wrap(1), space.wrap(specialnan)])
751 assert isinstance(w_l.strategy, ObjectListStrategy)
752
753 def test_int_or_float_int_overflow(self):
754 if sys.maxint == 2147483647:
755 py.test.skip("only on 64-bit")
756 space = self.space
757 ok1 = 2**31 - 1
758 ok2 = -2**31
759 ovf1 = ok1 + 1
760 ovf2 = ok2 - 1
761 w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(ovf1)])
762 assert isinstance(w_l.strategy, ObjectListStrategy)
763 w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(ovf2)])
764 assert isinstance(w_l.strategy, ObjectListStrategy)
765 w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(ok1)])
766 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
767 w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(ok2)])
768 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
769
770 def test_int_or_float_base(self):
771 from rpython.rlib.rfloat import INFINITY, NAN
772 space = self.space
773 w = space.wrap
774 w_l = W_ListObject(space, [space.wrap(1), space.wrap(2.3)])
775 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
776 w_l.append(w(int(2**31-1)))
777 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
778 w_l.append(w(-5.1))
779 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
780 assert space.int_w(w_l.getitem(2)) == 2**31-1
781 assert space.float_w(w_l.getitem(3)) == -5.1
782 w_l.append(w(INFINITY))
783 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
784 w_l.append(w(NAN))
785 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
786 w_l.append(w(-NAN))
787 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
788 w_l.append(space.newlist([]))
789 assert isinstance(w_l.strategy, ObjectListStrategy)
790
791 def test_int_or_float_from_integer(self):
792 space = self.space
793 w = space.wrap
794 w_l = W_ListObject(space, [space.wrap(int(-2**31))])
795 assert isinstance(w_l.strategy, IntegerListStrategy)
796 w_l.append(w(-5.1))
797 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
798 assert space.int_w(w_l.getitem(0)) == -2**31
799 assert space.float_w(w_l.getitem(1)) == -5.1
800 assert space.len_w(w_l) == 2
801
802 def test_int_or_float_from_integer_overflow(self):
803 if sys.maxint == 2147483647:
804 py.test.skip("only on 64-bit")
805 space = self.space
806 w = space.wrap
807 ovf1 = -2**31 - 1
808 w_l = W_ListObject(space, [space.wrap(ovf1)])
809 assert isinstance(w_l.strategy, IntegerListStrategy)
810 w_l.append(w(-5.1))
811 assert isinstance(w_l.strategy, ObjectListStrategy)
812 assert space.int_w(w_l.getitem(0)) == ovf1
813 assert space.float_w(w_l.getitem(1)) == -5.1
814 assert space.len_w(w_l) == 2
815
816 def test_int_or_float_from_integer_special_nan(self):
817 from rpython.rlib import longlong2float, rarithmetic
818 space = self.space
819 w = space.wrap
820 w_l = W_ListObject(space, [space.wrap(int(-2**31))])
821 assert isinstance(w_l.strategy, IntegerListStrategy)
822 ll = rarithmetic.r_longlong(0xfffffffe12345678 - 2**64)
823 specialnan = longlong2float.longlong2float(ll)
824 w_l.append(w(specialnan))
825 assert isinstance(w_l.strategy, ObjectListStrategy)
826 assert space.int_w(w_l.getitem(0)) == -2**31
827 assert space.len_w(w_l) == 2
828
829 def test_int_or_float_from_float(self):
830 space = self.space
831 w = space.wrap
832 w_l = W_ListObject(space, [space.wrap(-42.5)])
833 assert isinstance(w_l.strategy, FloatListStrategy)
834 w_l.append(w(-15))
835 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
836 assert space.float_w(w_l.getitem(0)) == -42.5
837 assert space.int_w(w_l.getitem(1)) == -15
838 assert space.len_w(w_l) == 2
839
840 def test_int_or_float_from_float_int_overflow(self):
841 if sys.maxint == 2147483647:
842 py.test.skip("only on 64-bit")
843 space = self.space
844 w = space.wrap
845 ovf1 = 2 ** 31
846 w_l = W_ListObject(space, [space.wrap(1.2)])
847 assert isinstance(w_l.strategy, FloatListStrategy)
848 w_l.append(w(ovf1))
849 assert isinstance(w_l.strategy, ObjectListStrategy)
850 assert space.float_w(w_l.getitem(0)) == 1.2
851 assert space.int_w(w_l.getitem(1)) == ovf1
852 assert space.len_w(w_l) == 2
853
854 def test_int_or_float_from_float_special_nan(self):
855 from rpython.rlib import longlong2float, rarithmetic
856 space = self.space
857 w = space.wrap
858 ll = rarithmetic.r_longlong(0xfffffffe12345678 - 2**64)
859 specialnan = longlong2float.longlong2float(ll)
860 w_l = W_ListObject(space, [space.wrap(specialnan)])
861 assert isinstance(w_l.strategy, FloatListStrategy)
862 w_l.append(w(42))
863 assert isinstance(w_l.strategy, ObjectListStrategy)
864 assert space.int_w(w_l.getitem(1)) == 42
865 assert space.len_w(w_l) == 2
866
867 def test_int_or_float_extend(self):
868 space = self.space
869 w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
870 w_l2 = W_ListObject(space, [space.wrap(3), space.wrap(4.5)])
871 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
872 assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
873 w_l1.extend(w_l2)
874 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
875 assert space.unwrap(w_l1) == [0, 1.2, 3, 4.5]
876
877 def test_int_or_float_extend_mixed_1(self):
878 space = self.space
879 w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
880 w_l2 = W_ListObject(space, [space.wrap(3)])
881 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
882 assert isinstance(w_l2.strategy, IntegerListStrategy)
883 w_l1.extend(w_l2)
884 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
885 assert [(type(x), x) for x in space.unwrap(w_l1)] == [
886 (int, 0), (float, 1.2), (int, 3)]
887
888 def test_int_or_float_extend_mixed_2(self):
889 space = self.space
890 w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
891 w_l2 = W_ListObject(space, [space.wrap(3.4)])
892 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
893 assert isinstance(w_l2.strategy, FloatListStrategy)
894 w_l1.extend(w_l2)
895 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
896 assert space.unwrap(w_l1) == [0, 1.2, 3.4]
897
898 def test_int_or_float_extend_mixed_3(self):
899 space = self.space
900 w_l1 = W_ListObject(space, [space.wrap(0)])
901 w_l2 = W_ListObject(space, [space.wrap(3.4)])
902 assert isinstance(w_l1.strategy, IntegerListStrategy)
903 assert isinstance(w_l2.strategy, FloatListStrategy)
904 w_l1.extend(w_l2)
905 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
906 assert space.unwrap(w_l1) == [0, 3.4]
907
908 def test_int_or_float_extend_mixed_4(self):
909 space = self.space
910 w_l1 = W_ListObject(space, [space.wrap(0)])
911 w_l2 = W_ListObject(space, [space.wrap(3.4), space.wrap(-2)])
912 assert isinstance(w_l1.strategy, IntegerListStrategy)
913 assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
914 w_l1.extend(w_l2)
915 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
916 assert space.unwrap(w_l1) == [0, 3.4, -2]
917
918 def test_int_or_float_extend_mixed_5(self):
919 space = self.space
920 w_l1 = W_ListObject(space, [space.wrap(-2.5)])
921 w_l2 = W_ListObject(space, [space.wrap(42)])
922 assert isinstance(w_l1.strategy, FloatListStrategy)
923 assert isinstance(w_l2.strategy, IntegerListStrategy)
924 w_l1.extend(w_l2)
925 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
926 assert space.unwrap(w_l1) == [-2.5, 42]
927
928 def test_int_or_float_extend_mixed_5_overflow(self):
929 if sys.maxint == 2147483647:
930 py.test.skip("only on 64-bit")
931 space = self.space
932 ovf1 = 2 ** 35
933 w_l1 = W_ListObject(space, [space.wrap(-2.5)])
934 w_l2 = W_ListObject(space, [space.wrap(ovf1)])
935 assert isinstance(w_l1.strategy, FloatListStrategy)
936 assert isinstance(w_l2.strategy, IntegerListStrategy)
937 w_l1.extend(w_l2)
938 assert isinstance(w_l1.strategy, ObjectListStrategy)
939 assert space.unwrap(w_l1) == [-2.5, ovf1]
940
941 def test_int_or_float_extend_mixed_6(self):
942 space = self.space
943 w_l1 = W_ListObject(space, [space.wrap(-2.5)])
944 w_l2 = W_ListObject(space, [space.wrap(3.4), space.wrap(-2)])
945 assert isinstance(w_l1.strategy, FloatListStrategy)
946 assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
947 w_l1.extend(w_l2)
948 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
949 assert space.unwrap(w_l1) == [-2.5, 3.4, -2]
950
951 def test_int_or_float_setslice(self):
952 space = self.space
953 w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
954 w_l2 = W_ListObject(space, [space.wrap(3), space.wrap(4.5)])
955 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
956 assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
957 w_l1.setslice(0, 1, 1, w_l2)
958 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
959 assert space.unwrap(w_l1) == [3, 4.5, 1.2]
960
961 def test_int_or_float_setslice_mixed_1(self):
962 space = self.space
963 w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(12)])
964 w_l2 = W_ListObject(space, [space.wrap(3.2), space.wrap(4.5)])
965 assert isinstance(w_l1.strategy, IntegerListStrategy)
966 assert isinstance(w_l2.strategy, FloatListStrategy)
967 w_l1.setslice(0, 1, 1, w_l2)
968 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
969 assert space.unwrap(w_l1) == [3.2, 4.5, 12]
970
971 def test_int_or_float_setslice_mixed_2(self):
972 space = self.space
973 w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(12)])
974 w_l2 = W_ListObject(space, [space.wrap(3.2), space.wrap(45)])
975 assert isinstance(w_l1.strategy, IntegerListStrategy)
976 assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
977 w_l1.setslice(0, 1, 1, w_l2)
978 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
979 assert space.unwrap(w_l1) == [3.2, 45, 12]
980
981 def test_int_or_float_setslice_mixed_3(self):
982 space = self.space
983 w_l1 = W_ListObject(space, [space.wrap(0.1), space.wrap(1.2)])
984 w_l2 = W_ListObject(space, [space.wrap(32), space.wrap(45)])
985 assert isinstance(w_l1.strategy, FloatListStrategy)
986 assert isinstance(w_l2.strategy, IntegerListStrategy)
987 w_l1.setslice(0, 1, 1, w_l2)
988 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
989 assert space.unwrap(w_l1) == [32, 45, 1.2]
990
991 def test_int_or_float_setslice_mixed_4(self):
992 space = self.space
993 w_l1 = W_ListObject(space, [space.wrap(0.1), space.wrap(1.2)])
994 w_l2 = W_ListObject(space, [space.wrap(3.2), space.wrap(45)])
995 assert isinstance(w_l1.strategy, FloatListStrategy)
996 assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
997 w_l1.setslice(0, 1, 1, w_l2)
998 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
999 assert space.unwrap(w_l1) == [3.2, 45, 1.2]
1000
1001 def test_int_or_float_setslice_mixed_5(self):
1002 space = self.space
1003 w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
1004 w_l2 = W_ListObject(space, [space.wrap(32), space.wrap(45)])
1005 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
1006 assert isinstance(w_l2.strategy, IntegerListStrategy)
1007 w_l1.setslice(0, 1, 1, w_l2)
1008 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
1009 assert space.unwrap(w_l1) == [32, 45, 1.2]
1010
1011 def test_int_or_float_setslice_mixed_5_overflow(self):
1012 if sys.maxint == 2147483647:
1013 py.test.skip("only on 64-bit")
1014 space = self.space
1015 ovf1 = 2 ** 35
1016 w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
1017 w_l2 = W_ListObject(space, [space.wrap(32), space.wrap(ovf1)])
1018 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
1019 assert isinstance(w_l2.strategy, IntegerListStrategy)
1020 w_l1.setslice(0, 1, 1, w_l2)
1021 assert isinstance(w_l1.strategy, ObjectListStrategy)
1022 assert space.unwrap(w_l1) == [32, ovf1, 1.2]
1023
1024 def test_int_or_float_setslice_mixed_6(self):
1025 space = self.space
1026 w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
1027 w_l2 = W_ListObject(space, [space.wrap(3.2), space.wrap(4.5)])
1028 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
1029 assert isinstance(w_l2.strategy, FloatListStrategy)
1030 w_l1.setslice(0, 1, 1, w_l2)
1031 assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
1032 assert space.unwrap(w_l1) == [3.2, 4.5, 1.2]
1033
1034 def test_int_or_float_sort(self):
1035 space = self.space
1036 w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(1),
1037 space.wrap(1.0), space.wrap(5)])
1038 w_l.sort(False)
1039 assert [(type(x), x) for x in space.unwrap(w_l)] == [
1040 (int, 1), (float, 1.0), (float, 1.2), (int, 5)]
1041 w_l.sort(True)
1042 assert [(type(x), x) for x in space.unwrap(w_l)] == [
1043 (int, 5), (float, 1.2), (int, 1), (float, 1.0)]
1044
1045 def test_stringstrategy_wraps_bytes(self):
1046 space = self.space
1047 wb = space.newbytes
1048 l = W_ListObject(space, [wb('a'), wb('b')])
1049 w_item = l.getitem(0)
1050 assert isinstance(w_item, space.StringObjectCls)
1051
1052 def test_integer_strategy_with_w_long(self):
1053 # tests all calls to is_plain_int1() so far
1054 space = self.space
1055 w = W_LongObject(rbigint.fromlong(42))
1056 w_l = space.newlist([])
1057 space.call_method(w_l, 'append', w)
1058 assert isinstance(w_l.strategy, IntegerListStrategy)
1059 assert isinstance(space.getitem(w_l, space.wrap(0)), W_IntObject)
1060 #
1061 w_l = space.newlist([w, w])
1062 assert isinstance(w_l.strategy, IntegerListStrategy)
1063 assert isinstance(space.getitem(w_l, space.wrap(0)), W_IntObject)
1064 assert isinstance(space.getitem(w_l, space.wrap(1)), W_IntObject)
1065 #
1066 w_f = space.newfloat(42.0)
1067 w_l = space.newlist([w_f, w])
1068 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
1069 assert isinstance(space.getitem(w_l, space.wrap(0)), W_FloatObject)
1070 assert isinstance(space.getitem(w_l, space.wrap(1)), W_IntObject)
1071 space.call_method(w_l, 'append', w)
1072 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
1073 assert isinstance(space.getitem(w_l, space.wrap(2)), W_IntObject)
1074 #
1075 w_l = make_range_list(space, 0, 1, 10)
1076 space.call_method(w_l, 'append', w)
1077 assert isinstance(w_l.strategy, IntegerListStrategy)
1078 assert isinstance(space.getitem(w_l, space.wrap(-1)), W_IntObject)
1079 #
1080 w_l = make_range_list(space, 30, 2, 45)
1081 assert space.eq_w(space.call_method(w_l, 'index', w), space.wrap(6))
1082 #
1083 w_l = make_range_list(space, 0, 1, 45)
1084 assert space.eq_w(space.call_method(w_l, 'index', w), space.wrap(42))
1085 #
1086 w_f = space.newfloat(42.0)
1087 w_l = space.newlist([w_f])
1088 space.call_method(w_l, 'append', w)
1089 assert isinstance(w_l.strategy, IntOrFloatListStrategy)
1090 assert isinstance(space.getitem(w_l, space.wrap(0)), W_FloatObject)
1091 assert isinstance(space.getitem(w_l, space.wrap(1)), W_IntObject)
1092
1093
1094 class TestW_ListStrategiesDisabled:
1095 spaceconfig = {"objspace.std.withliststrategies": False}
1096
1097 def test_check_strategy(self):
1098 assert isinstance(W_ListObject(self.space, []).strategy, ObjectListStrategy)
1099 assert isinstance(W_ListObject(self.space, [self.space.wrap(1),self.space.wrap('a')]).strategy, ObjectListStrategy)
1100 assert isinstance(W_ListObject(self.space, [self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)]).strategy, ObjectListStrategy)
1101 assert isinstance(W_ListObject(self.space, [self.space.wrap('a'), self.space.wrap('b')]).strategy, ObjectListStrategy)