Debian Codegraph @

mypyc/test-data/run-lists.test

raw
1 # Test cases for lists (compile and run)
2
3 [case testListPlusEquals]
4 from typing import Any
5 def append(x: Any) -> None:
6 x += [1]
7
8 [file driver.py]
9 from native import append
10 x = []
11 append(x)
12 assert x == [1]
13
14 [case testListSum]
15 from typing import List
16 def sum(a: List[int], l: int) -> int:
17 sum = 0
18 i = 0
19 while i < l:
20 sum = sum + a[i]
21 i = i + 1
22 return sum
23 [file driver.py]
24 from native import sum
25 print(sum([], 0))
26 print(sum([3], 1))
27 print(sum([5, 6, -4], 3))
28 print(sum([2**128 + 5, -2**127 - 8], 2))
29 [out]
30 0
31 3
32 7
33 170141183460469231731687303715884105725
34
35 [case testListSet]
36 from typing import List
37 def copy(a: List[int], b: List[int], l: int) -> int:
38 i = 0
39 while i < l:
40 a[i] = b[i]
41 i = i + 1
42 return 0
43 [file driver.py]
44 from native import copy
45 a = [0, '']
46 copy(a, [-1, 5], 2)
47 print(1, a)
48 copy(a, [2**128 + 5, -2**127 - 8], 2)
49 print(2, a)
50 [out]
51 1 [-1, 5]
52 2 [340282366920938463463374607431768211461, -170141183460469231731687303715884105736]
53
54 [case testListClear]
55 from typing import List, Any
56 from copysubclass import subc
57
58 def test_list_clear() -> None:
59 l1 = [1, 2, 3, -4, 5]
60 l1.clear()
61 assert l1 == []
62 l1.clear()
63 assert l1 == []
64 l2: List[Any] = []
65 l2.clear()
66 assert l2 == []
67 l3 = [1, 2, 3, "abcdef"]
68 l3.clear()
69 assert l3 == []
70 # subclass testing
71 l4: subc = subc([1, 2, 3])
72 l4.clear()
73 assert l4 == []
74
75 [file copysubclass.py]
76 from typing import Any
77 class subc(list[Any]):
78 pass
79
80 [case testListCopy]
81 from typing import List
82 from copysubclass import subc
83
84 def test_list_copy() -> None:
85 l1 = [1, 2, 3, -4, 5]
86 l2 = l1.copy()
87 assert l1.copy() == l1
88 assert l1.copy() == l2
89 assert l1 == l2
90 assert l1.copy() == l2.copy()
91 l1 = l2.copy()
92 assert l1 == l2
93 assert l1.copy() == l2
94 assert l1 == [1, 2, 3, -4, 5]
95 l2 = [1, 2, -3]
96 l1 = []
97 assert l1.copy() == []
98 assert l2.copy() != l1
99 assert l2 == l2.copy()
100 l1 = l2
101 assert l1.copy().copy() == l2.copy().copy().copy()
102 assert l1.copy() == l2.copy()
103 l1 == [1, 2, -3].copy()
104 assert l1 == l2
105 l2 = [1, 2, 3].copy()
106 assert l2 != l1
107 l1 = [1, 2, 3]
108 assert l1.copy() == l2.copy()
109 l3 = [1, 2 , 3, "abcdef"]
110 assert l3 == l3.copy()
111 l4 = ["abc", 5, 10]
112 l4 = l3.copy()
113 assert l4 == l3
114 #subclass testing
115 l5: subc = subc([1, 2, 3])
116 l6 = l5.copy()
117 assert l6 == l5
118 l6 = [1, 2, "3", 4, 5]
119 l5 = subc([1,2,"3",4,5])
120 assert l5.copy() == l6.copy()
121 l6 = l5.copy()
122 assert l5 == l6
123
124 [file copysubclass.py]
125 from typing import Any
126 class subc(list[Any]):
127 pass
128
129 [case testSieve]
130 from typing import List
131
132 def primes(n: int) -> List[int]:
133 a = [1] * (n + 1)
134 a[0] = 0
135 a[1] = 0
136 i = 0
137 while i < n:
138 if a[i] == 1:
139 j = i * i
140 while j < n:
141 a[j] = 0
142 j = j + i
143 i = i + 1
144 return a
145 [file driver.py]
146 from native import primes
147 print(primes(3))
148 print(primes(13))
149 [out]
150 \[0, 0, 1, 1]
151 \[0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1]
152
153 [case testListPrimitives]
154 from testutil import assertRaises
155
156 def test_list_build() -> None:
157 # Currently LIST_BUILDING_EXPANSION_THRESHOLD equals to 10
158 # long list built by list_build_op
159 l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
160 l1.pop()
161 l1.append(100)
162 assert l1 == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]
163 # short list built by Setmem
164 l2 = [1, 2]
165 l2.append(3)
166 l2.pop()
167 l2.pop()
168 assert l2 == [1]
169 # empty list
170 l3 = []
171 l3.append('a')
172 assert l3 == ['a']
173
174 class C:
175 def __init__(self, s: str) -> None:
176 self.s = s
177
178 a = [C("a"), C("b"), C("c")]
179
180 def test_get_item() -> None:
181 zero = int()
182 assert a[zero].s == "a"
183 assert a[zero + 1].s == "b"
184 assert a[zero + 2].s == "c"
185 assert a[zero - 1].s == "c"
186 assert a[zero - 2].s == "b"
187 assert a[zero - 3].s == "a"
188 with assertRaises(IndexError):
189 a[zero + 3]
190 with assertRaises(IndexError):
191 a[zero - 4]
192 # TODO: Raise IndexError?
193 with assertRaises(OverflowError):
194 a[zero + 2**90]
195 with assertRaises(OverflowError):
196 a[zero - 2**90]
197
198 def test_get_item_literal() -> None:
199 assert a[0].s == "a"
200 assert a[1].s == "b"
201 assert a[2].s == "c"
202 assert a[-1].s == "c"
203 assert a[-2].s == "b"
204 assert a[-3].s == "a"
205 with assertRaises(IndexError):
206 a[3]
207 with assertRaises(IndexError):
208 a[-4]
209 # TODO: Raise IndexError?
210 with assertRaises(OverflowError):
211 a[2**90]
212 with assertRaises(OverflowError):
213 a[-2**90]
214
215 def test_set_item() -> None:
216 l = a.copy()
217 zero = int()
218 zero2 = int()
219 l[zero] = C("x")
220 assert l[zero2].s == "x"
221 l[zero + 2] = C("y")
222 assert l[zero2 + 2].s == "y"
223 l[zero - 1] = C("t")
224 assert l[zero2 - 1].s == "t"
225 l[zero - 3] = C("u")
226 assert l[zero2 - 3].s == "u"
227 with assertRaises(IndexError):
228 a[zero + 3] = C("z")
229 with assertRaises(IndexError):
230 a[zero - 4] = C("z")
231 with assertRaises(OverflowError):
232 a[zero + 2**90] = C("z")
233 with assertRaises(OverflowError):
234 a[zero - 2**90] = C("z")
235
236 def test_append() -> None:
237 l = [1, 2]
238 l.append(10)
239 assert l == [1, 2, 10]
240 l.append(3)
241 l.append(4)
242 l.append(5)
243 assert l == [1, 2, 10, 3, 4, 5]
244
245 def test_pop_last() -> None:
246 l = [1, 2, 10, 3, 4, 5]
247 l.pop()
248 l.pop()
249 assert l == [1, 2, 10, 3]
250
251 def test_pop_index() -> None:
252 l = [1, 2, 10, 3]
253 assert l.pop(2) == 10
254 assert l == [1, 2, 3]
255 assert l.pop(-2) == 2
256 assert l == [1, 3]
257 assert l.pop(-2) == 1
258 assert l.pop(0) == 3
259 assert l == []
260 l = [int() + 1000, int() + 1001, int() + 1002]
261 assert l.pop(0) == 1000
262 assert l.pop(-1) == 1002
263 assert l == [1001]
264
265 def test_pop_index_errors() -> None:
266 l = [int() + 1000]
267 with assertRaises(IndexError):
268 l.pop(1)
269 with assertRaises(IndexError):
270 l.pop(-2)
271 with assertRaises(OverflowError):
272 l.pop(1 << 100)
273 with assertRaises(OverflowError):
274 l.pop(-(1 << 100))
275
276 def test_count() -> None:
277 l = [1, 3]
278 assert l.count(1) == 1
279 assert l.count(2) == 0
280
281 def test_insert() -> None:
282 l = [1, 3]
283 l.insert(0, 0)
284 assert l == [0, 1, 3]
285 l.insert(2, 2)
286 assert l == [0, 1, 2, 3]
287 l.insert(4, 4)
288 assert l == [0, 1, 2, 3, 4]
289 l.insert(-1, 5)
290 assert l == [0, 1, 2, 3, 5, 4]
291 l = [1, 3]
292 l.insert(100, 5)
293 assert l == [1, 3, 5]
294 l.insert(-100, 6)
295 assert l == [6, 1, 3, 5]
296 for long_int in 1 << 100, -(1 << 100):
297 try:
298 l.insert(long_int, 5)
299 except Exception as e:
300 # The error message is used by CPython
301 assert type(e).__name__ == 'OverflowError'
302 assert str(e) == 'Python int too large to convert to C ssize_t'
303 else:
304 assert False
305
306 def test_sort() -> None:
307 l = [1, 4, 3, 6, -1]
308 l.sort()
309 assert l == [-1, 1, 3, 4, 6]
310 l.sort()
311 assert l == [-1, 1, 3, 4, 6]
312 l = []
313 l.sort()
314 assert l == []
315
316 def test_reverse() -> None:
317 l = [1, 4, 3, 6, -1]
318 l.reverse()
319 assert l == [-1, 6, 3, 4, 1]
320 l.reverse()
321 assert l == [1, 4, 3, 6, -1]
322 l = []
323 l.reverse()
324 assert l == []
325
326 def test_remove() -> None:
327 l = [1, 3, 4, 3]
328 l.remove(3)
329 assert l == [1, 4, 3]
330 l.remove(3)
331 assert l == [1, 4]
332 try:
333 l.remove(3)
334 except ValueError:
335 pass
336 else:
337 assert False
338
339 def test_index() -> None:
340 l = [1, 3, 4, 3]
341 assert l.index(1) == 0
342 assert l.index(3) == 1
343 assert l.index(4) == 2
344 try:
345 l.index(0)
346 except ValueError:
347 pass
348 else:
349 assert False
350
351 [case testListOfUserDefinedClass]
352 class C:
353 x: int
354
355 def f() -> int:
356 c = C()
357 c.x = 5
358 a = [c]
359 d = a[0]
360 return d.x + 1
361
362 def g() -> int:
363 a = [C()]
364 a[0].x = 3
365 return a[0].x + 4
366 [file driver.py]
367 from native import f, g
368 print(f())
369 print(g())
370 [out]
371 6
372 7
373
374 [case testListOps]
375 from typing import Any, cast
376 from testutil import assertRaises
377
378 def test_slicing() -> None:
379 # Use dummy adds to avoid constant folding
380 zero = int()
381 two = zero + 2
382 s = ["f", "o", "o", "b", "a", "r"]
383 assert s[two:] == ["o", "b", "a", "r"]
384 assert s[:two] == ["f", "o"]
385 assert s[two:-two] == ["o", "b"]
386 assert s[two:two] == []
387 assert s[two:two + 1] == ["o"]
388 assert s[-two:] == ["a", "r"]
389 assert s[:-two] == ["f", "o", "o", "b"]
390 assert s[:] == ["f", "o", "o", "b", "a", "r"]
391 assert s[two:333] == ["o", "b", "a", "r"]
392 assert s[333:two] == []
393 assert s[two:-333] == []
394 assert s[-333:two] == ["f", "o"]
395 long_int: int = 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000
396 assert s[1:long_int] == ["o", "o", "b", "a", "r"]
397 assert s[long_int:] == []
398 assert s[-long_int:-1] == ["f", "o", "o", "b", "a"]
399
400 def in_place_add(l2: Any) -> list[Any]:
401 l1 = [1, 2]
402 l1 += l2
403 return l1
404
405 def test_add() -> None:
406 res = [1, 2, 3, 4]
407 assert [1, 2] + [3, 4] == res
408 with assertRaises(TypeError, 'can only concatenate list (not "tuple") to list'):
409 assert [1, 2] + cast(Any, (3, 4)) == res
410 l1 = [1, 2]
411 id_l1 = id(l1)
412 l1 += [3, 4]
413 assert l1 == res
414 assert id_l1 == id(l1)
415 assert in_place_add([3, 4]) == res
416 assert in_place_add((3, 4)) == res
417 assert in_place_add({3, 4}) == res
418 assert in_place_add({3: "", 4: ""}) == res
419 assert in_place_add(range(3, 5)) == res
420
421 def test_multiply() -> None:
422 l1 = [1]
423 assert l1 * 3 == [1, 1, 1]
424 assert 3 * l1 == [1, 1, 1]
425 l1 *= 3
426 assert l1 == [1, 1, 1]
427
428 [case testOperatorInExpression]
429 def tuple_in_int0(i: int) -> bool:
430 return i in []
431
432 def tuple_in_int1(i: int) -> bool:
433 return i in (1,)
434
435 def tuple_in_int3(i: int) -> bool:
436 return i in (1, 2, 3)
437
438 def tuple_not_in_int0(i: int) -> bool:
439 return i not in []
440
441 def tuple_not_in_int1(i: int) -> bool:
442 return i not in (1,)
443
444 def tuple_not_in_int3(i: int) -> bool:
445 return i not in (1, 2, 3)
446
447 def tuple_in_str(s: "str") -> bool:
448 return s in ("foo", "bar", "baz")
449
450 def tuple_not_in_str(s: "str") -> bool:
451 return s not in ("foo", "bar", "baz")
452
453 def list_in_int0(i: int) -> bool:
454 return i in []
455
456 def list_in_int1(i: int) -> bool:
457 return i in (1,)
458
459 def list_in_int3(i: int) -> bool:
460 return i in (1, 2, 3)
461
462 def list_not_in_int0(i: int) -> bool:
463 return i not in []
464
465 def list_not_in_int1(i: int) -> bool:
466 return i not in (1,)
467
468 def list_not_in_int3(i: int) -> bool:
469 return i not in (1, 2, 3)
470
471 def list_in_str(s: "str") -> bool:
472 return s in ("foo", "bar", "baz")
473
474 def list_not_in_str(s: "str") -> bool:
475 return s not in ("foo", "bar", "baz")
476
477 def list_in_mixed(i: object):
478 return i in [[], (), "", 0, 0.0, False, 0j, {}, set(), type]
479
480 def test_in_operator_various_cases() -> None:
481 assert not tuple_in_int0(0)
482 assert not tuple_in_int1(0)
483 assert tuple_in_int1(1)
484 assert not tuple_in_int3(0)
485 assert tuple_in_int3(1)
486 assert tuple_in_int3(2)
487 assert tuple_in_int3(3)
488 assert not tuple_in_int3(4)
489
490 assert tuple_not_in_int0(0)
491 assert tuple_not_in_int1(0)
492 assert not tuple_not_in_int1(1)
493 assert tuple_not_in_int3(0)
494 assert not tuple_not_in_int3(1)
495 assert not tuple_not_in_int3(2)
496 assert not tuple_not_in_int3(3)
497 assert tuple_not_in_int3(4)
498
499 assert tuple_in_str("foo")
500 assert tuple_in_str("bar")
501 assert tuple_in_str("baz")
502 assert not tuple_in_str("apple")
503 assert not tuple_in_str("pie")
504 assert not tuple_in_str("\0")
505 assert not tuple_in_str("")
506
507 assert not list_in_int0(0)
508 assert not list_in_int1(0)
509 assert list_in_int1(1)
510 assert not list_in_int3(0)
511 assert list_in_int3(1)
512 assert list_in_int3(2)
513 assert list_in_int3(3)
514 assert not list_in_int3(4)
515
516 assert list_not_in_int0(0)
517 assert list_not_in_int1(0)
518 assert not list_not_in_int1(1)
519 assert list_not_in_int3(0)
520 assert not list_not_in_int3(1)
521 assert not list_not_in_int3(2)
522 assert not list_not_in_int3(3)
523 assert list_not_in_int3(4)
524
525 assert list_in_str("foo")
526 assert list_in_str("bar")
527 assert list_in_str("baz")
528 assert not list_in_str("apple")
529 assert not list_in_str("pie")
530 assert not list_in_str("\0")
531 assert not list_in_str("")
532
533 assert list_in_mixed(0)
534 assert list_in_mixed([])
535 assert list_in_mixed({})
536 assert list_in_mixed(())
537 assert list_in_mixed(False)
538 assert list_in_mixed(0.0)
539 assert not list_in_mixed([1])
540 assert not list_in_mixed(object)
541 assert list_in_mixed(type)
542
543 [case testListBuiltFromGenerator]
544 from typing import Final
545 abc: Final = "abc"
546 def test_from_gen() -> None:
547 source_a = ["a", "b", "c"]
548 a = list(x + "f2" for x in source_a)
549 assert a == ["af2", "bf2", "cf2"]
550 source_b = [1, 2, 3, 4, 5]
551 b = [x * 2 for x in source_b]
552 assert b == [2, 4, 6, 8, 10]
553 source_c = [10, 20, 30]
554 c = [x + "f4" for x in (str(y) + "yy" for y in source_c)]
555 assert c == ["10yyf4", "20yyf4", "30yyf4"]
556 source_d = [True, False]
557 d = [not x for x in source_d]
558 assert d == [False, True]
559 source_e = [0, 1, 2]
560 e = list((x ** 2) for x in (y + 2 for y in source_e))
561 assert e == [4, 9, 16]
562 source_str = "abcd"
563 f = list("str:" + x for x in source_str)
564 assert f == ["str:a", "str:b", "str:c", "str:d"]
565 def test_known_length() -> None:
566 # not built from generator but doesnt need its own test either
567 built = [str(x) for x in [*abc, *"def", *b"ghi", ("j", "k"), *("l", "m", "n")]]
568 assert built == ['a', 'b', 'c', 'd', 'e', 'f', '103', '104', '105', "('j', 'k')", 'l', 'm', 'n']
569
570 [case testNext]
571 from typing import List
572
573 def get_next(x: List[int]) -> int:
574 return next((i for i in x), -1)
575
576 def test_next() -> None:
577 assert get_next([]) == -1
578 assert get_next([1]) == 1
579 assert get_next([3,2,1]) == 3
580
581 [case testListGetItemWithBorrow]
582 from typing import List
583
584 class D:
585 def __init__(self, n: int) -> None:
586 self.n = n
587
588 class C:
589 def __init__(self, d: D) -> None:
590 self.d = d
591
592 def test_index_with_literal() -> None:
593 d1 = D(1)
594 d2 = D(2)
595 a = [C(d1), C(d2)]
596 d = a[0].d
597 assert d is d1
598 d = a[1].d
599 assert d is d2
600 d = a[-1].d
601 assert d is d2
602 d = a[-2].d
603 assert d is d1
604
605 [case testSorted]
606 from typing import List
607
608 def test_list_sort() -> None:
609 l1 = [2, 1, 3]
610 id_l1 = id(l1)
611 l1.sort()
612 assert l1 == [1, 2, 3]
613 assert id_l1 == id(l1)
614
615 def test_sorted() -> None:
616 res = [1, 2, 3]
617 l1 = [2, 1, 3]
618 id_l1 = id(l1)
619 s_l1 = sorted(l1)
620 assert s_l1 == res
621 assert id_l1 != id(s_l1)
622 assert l1 == [2, 1, 3]
623 assert sorted((2, 1, 3)) == res
624 assert sorted({2, 1, 3}) == res
625 assert sorted({2: "", 1: "", 3: ""}) == res
626
627 [case testIsInstance]
628 from copysubclass import subc
629 def test_built_in() -> None:
630 assert isinstance([], list)
631 assert isinstance([1,2,3], list)
632 assert isinstance(['a','b'], list)
633 assert isinstance(subc(), list)
634 assert isinstance(subc([1,2,3]), list)
635 assert isinstance(subc(['a','b']), list)
636
637 assert not isinstance({}, list)
638 assert not isinstance((), list)
639 assert not isinstance((1,2,3), list)
640 assert not isinstance(('a','b'), list)
641 assert not isinstance(1, list)
642 assert not isinstance('a', list)
643
644 def test_user_defined() -> None:
645 from userdefinedlist import list
646
647 assert isinstance(list(), list)
648 assert not isinstance([list()], list)
649
650 [file copysubclass.py]
651 from typing import Any
652 class subc(list[Any]):
653 pass
654
655 [file userdefinedlist.py]
656 class list:
657 pass