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 def test_append() -> None:
175 l = [1, 2]
176 l.append(10)
177 assert l == [1, 2, 10]
178 l.append(3)
179 l.append(4)
180 l.append(5)
181 assert l == [1, 2, 10, 3, 4, 5]
182
183 def test_pop_last() -> None:
184 l = [1, 2, 10, 3, 4, 5]
185 l.pop()
186 l.pop()
187 assert l == [1, 2, 10, 3]
188
189 def test_pop_index() -> None:
190 l = [1, 2, 10, 3]
191 assert l.pop(2) == 10
192 assert l == [1, 2, 3]
193 assert l.pop(-2) == 2
194 assert l == [1, 3]
195 assert l.pop(-2) == 1
196 assert l.pop(0) == 3
197 assert l == []
198 l = [int() + 1000, int() + 1001, int() + 1002]
199 assert l.pop(0) == 1000
200 assert l.pop(-1) == 1002
201 assert l == [1001]
202
203 def test_pop_index_errors() -> None:
204 l = [int() + 1000]
205 with assertRaises(IndexError):
206 l.pop(1)
207 with assertRaises(IndexError):
208 l.pop(-2)
209 with assertRaises(OverflowError):
210 l.pop(1 << 100)
211 with assertRaises(OverflowError):
212 l.pop(-(1 << 100))
213
214 def test_count() -> None:
215 l = [1, 3]
216 assert l.count(1) == 1
217 assert l.count(2) == 0
218
219 def test_insert() -> None:
220 l = [1, 3]
221 l.insert(0, 0)
222 assert l == [0, 1, 3]
223 l.insert(2, 2)
224 assert l == [0, 1, 2, 3]
225 l.insert(4, 4)
226 assert l == [0, 1, 2, 3, 4]
227 l.insert(-1, 5)
228 assert l == [0, 1, 2, 3, 5, 4]
229 l = [1, 3]
230 l.insert(100, 5)
231 assert l == [1, 3, 5]
232 l.insert(-100, 6)
233 assert l == [6, 1, 3, 5]
234 for long_int in 1 << 100, -(1 << 100):
235 try:
236 l.insert(long_int, 5)
237 except Exception as e:
238 # The error message is used by CPython
239 assert type(e).__name__ == 'OverflowError'
240 assert str(e) == 'Python int too large to convert to C ssize_t'
241 else:
242 assert False
243
244 def test_sort() -> None:
245 l = [1, 4, 3, 6, -1]
246 l.sort()
247 assert l == [-1, 1, 3, 4, 6]
248 l.sort()
249 assert l == [-1, 1, 3, 4, 6]
250 l = []
251 l.sort()
252 assert l == []
253
254 def test_reverse() -> None:
255 l = [1, 4, 3, 6, -1]
256 l.reverse()
257 assert l == [-1, 6, 3, 4, 1]
258 l.reverse()
259 assert l == [1, 4, 3, 6, -1]
260 l = []
261 l.reverse()
262 assert l == []
263
264 def test_remove() -> None:
265 l = [1, 3, 4, 3]
266 l.remove(3)
267 assert l == [1, 4, 3]
268 l.remove(3)
269 assert l == [1, 4]
270 try:
271 l.remove(3)
272 except ValueError:
273 pass
274 else:
275 assert False
276
277 def test_index() -> None:
278 l = [1, 3, 4, 3]
279 assert l.index(1) == 0
280 assert l.index(3) == 1
281 assert l.index(4) == 2
282 try:
283 l.index(0)
284 except ValueError:
285 pass
286 else:
287 assert False
288
289 [case testListOfUserDefinedClass]
290 class C:
291 x: int
292
293 def f() -> int:
294 c = C()
295 c.x = 5
296 a = [c]
297 d = a[0]
298 return d.x + 1
299
300 def g() -> int:
301 a = [C()]
302 a[0].x = 3
303 return a[0].x + 4
304 [file driver.py]
305 from native import f, g
306 print(f())
307 print(g())
308 [out]
309 6
310 7
311
312 [case testListOps]
313 from typing import Any, cast
314 from testutil import assertRaises
315
316 def test_slicing() -> None:
317 # Use dummy adds to avoid constant folding
318 zero = int()
319 two = zero + 2
320 s = ["f", "o", "o", "b", "a", "r"]
321 assert s[two:] == ["o", "b", "a", "r"]
322 assert s[:two] == ["f", "o"]
323 assert s[two:-two] == ["o", "b"]
324 assert s[two:two] == []
325 assert s[two:two + 1] == ["o"]
326 assert s[-two:] == ["a", "r"]
327 assert s[:-two] == ["f", "o", "o", "b"]
328 assert s[:] == ["f", "o", "o", "b", "a", "r"]
329 assert s[two:333] == ["o", "b", "a", "r"]
330 assert s[333:two] == []
331 assert s[two:-333] == []
332 assert s[-333:two] == ["f", "o"]
333 long_int: int = 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000
334 assert s[1:long_int] == ["o", "o", "b", "a", "r"]
335 assert s[long_int:] == []
336 assert s[-long_int:-1] == ["f", "o", "o", "b", "a"]
337
338 def in_place_add(l2: Any) -> list[Any]:
339 l1 = [1, 2]
340 l1 += l2
341 return l1
342
343 def test_add() -> None:
344 res = [1, 2, 3, 4]
345 assert [1, 2] + [3, 4] == res
346 with assertRaises(TypeError, 'can only concatenate list (not "tuple") to list'):
347 assert [1, 2] + cast(Any, (3, 4)) == res
348 l1 = [1, 2]
349 id_l1 = id(l1)
350 l1 += [3, 4]
351 assert l1 == res
352 assert id_l1 == id(l1)
353 assert in_place_add([3, 4]) == res
354 assert in_place_add((3, 4)) == res
355 assert in_place_add({3, 4}) == res
356 assert in_place_add({3: "", 4: ""}) == res
357 assert in_place_add(range(3, 5)) == res
358
359 def test_multiply() -> None:
360 l1 = [1]
361 assert l1 * 3 == [1, 1, 1]
362 assert 3 * l1 == [1, 1, 1]
363 l1 *= 3
364 assert l1 == [1, 1, 1]
365
366 [case testOperatorInExpression]
367 def tuple_in_int0(i: int) -> bool:
368 return i in []
369
370 def tuple_in_int1(i: int) -> bool:
371 return i in (1,)
372
373 def tuple_in_int3(i: int) -> bool:
374 return i in (1, 2, 3)
375
376 def tuple_not_in_int0(i: int) -> bool:
377 return i not in []
378
379 def tuple_not_in_int1(i: int) -> bool:
380 return i not in (1,)
381
382 def tuple_not_in_int3(i: int) -> bool:
383 return i not in (1, 2, 3)
384
385 def tuple_in_str(s: "str") -> bool:
386 return s in ("foo", "bar", "baz")
387
388 def tuple_not_in_str(s: "str") -> bool:
389 return s not in ("foo", "bar", "baz")
390
391 def list_in_int0(i: int) -> bool:
392 return i in []
393
394 def list_in_int1(i: int) -> bool:
395 return i in (1,)
396
397 def list_in_int3(i: int) -> bool:
398 return i in (1, 2, 3)
399
400 def list_not_in_int0(i: int) -> bool:
401 return i not in []
402
403 def list_not_in_int1(i: int) -> bool:
404 return i not in (1,)
405
406 def list_not_in_int3(i: int) -> bool:
407 return i not in (1, 2, 3)
408
409 def list_in_str(s: "str") -> bool:
410 return s in ("foo", "bar", "baz")
411
412 def list_not_in_str(s: "str") -> bool:
413 return s not in ("foo", "bar", "baz")
414
415 def list_in_mixed(i: object):
416 return i in [[], (), "", 0, 0.0, False, 0j, {}, set(), type]
417
418 def test_in_operator_various_cases() -> None:
419 assert not tuple_in_int0(0)
420 assert not tuple_in_int1(0)
421 assert tuple_in_int1(1)
422 assert not tuple_in_int3(0)
423 assert tuple_in_int3(1)
424 assert tuple_in_int3(2)
425 assert tuple_in_int3(3)
426 assert not tuple_in_int3(4)
427
428 assert tuple_not_in_int0(0)
429 assert tuple_not_in_int1(0)
430 assert not tuple_not_in_int1(1)
431 assert tuple_not_in_int3(0)
432 assert not tuple_not_in_int3(1)
433 assert not tuple_not_in_int3(2)
434 assert not tuple_not_in_int3(3)
435 assert tuple_not_in_int3(4)
436
437 assert tuple_in_str("foo")
438 assert tuple_in_str("bar")
439 assert tuple_in_str("baz")
440 assert not tuple_in_str("apple")
441 assert not tuple_in_str("pie")
442 assert not tuple_in_str("\0")
443 assert not tuple_in_str("")
444
445 assert not list_in_int0(0)
446 assert not list_in_int1(0)
447 assert list_in_int1(1)
448 assert not list_in_int3(0)
449 assert list_in_int3(1)
450 assert list_in_int3(2)
451 assert list_in_int3(3)
452 assert not list_in_int3(4)
453
454 assert list_not_in_int0(0)
455 assert list_not_in_int1(0)
456 assert not list_not_in_int1(1)
457 assert list_not_in_int3(0)
458 assert not list_not_in_int3(1)
459 assert not list_not_in_int3(2)
460 assert not list_not_in_int3(3)
461 assert list_not_in_int3(4)
462
463 assert list_in_str("foo")
464 assert list_in_str("bar")
465 assert list_in_str("baz")
466 assert not list_in_str("apple")
467 assert not list_in_str("pie")
468 assert not list_in_str("\0")
469 assert not list_in_str("")
470
471 assert list_in_mixed(0)
472 assert list_in_mixed([])
473 assert list_in_mixed({})
474 assert list_in_mixed(())
475 assert list_in_mixed(False)
476 assert list_in_mixed(0.0)
477 assert not list_in_mixed([1])
478 assert not list_in_mixed(object)
479 assert list_in_mixed(type)
480
481 [case testListBuiltFromGenerator]
482 from typing import Final
483 abc: Final = "abc"
484 def test_from_gen() -> None:
485 source_a = ["a", "b", "c"]
486 a = list(x + "f2" for x in source_a)
487 assert a == ["af2", "bf2", "cf2"]
488 source_b = [1, 2, 3, 4, 5]
489 b = [x * 2 for x in source_b]
490 assert b == [2, 4, 6, 8, 10]
491 source_c = [10, 20, 30]
492 c = [x + "f4" for x in (str(y) + "yy" for y in source_c)]
493 assert c == ["10yyf4", "20yyf4", "30yyf4"]
494 source_d = [True, False]
495 d = [not x for x in source_d]
496 assert d == [False, True]
497 source_e = [0, 1, 2]
498 e = list((x ** 2) for x in (y + 2 for y in source_e))
499 assert e == [4, 9, 16]
500 source_str = "abcd"
501 f = list("str:" + x for x in source_str)
502 assert f == ["str:a", "str:b", "str:c", "str:d"]
503 def test_known_length() -> None:
504 # not built from generator but doesnt need its own test either
505 built = [str(x) for x in [*abc, *"def", *b"ghi", ("j", "k"), *("l", "m", "n")]]
506 assert built == ['a', 'b', 'c', 'd', 'e', 'f', '103', '104', '105', "('j', 'k')", 'l', 'm', 'n']
507
508 [case testNext]
509 from typing import List
510
511 def get_next(x: List[int]) -> int:
512 return next((i for i in x), -1)
513
514 def test_next() -> None:
515 assert get_next([]) == -1
516 assert get_next([1]) == 1
517 assert get_next([3,2,1]) == 3
518
519 [case testListGetItemWithBorrow]
520 from typing import List
521
522 class D:
523 def __init__(self, n: int) -> None:
524 self.n = n
525
526 class C:
527 def __init__(self, d: D) -> None:
528 self.d = d
529
530 def test_index_with_literal() -> None:
531 d1 = D(1)
532 d2 = D(2)
533 a = [C(d1), C(d2)]
534 d = a[0].d
535 assert d is d1
536 d = a[1].d
537 assert d is d2
538 d = a[-1].d
539 assert d is d2
540 d = a[-2].d
541 assert d is d1
542
543 [case testSorted]
544 from typing import List
545
546 def test_list_sort() -> None:
547 l1 = [2, 1, 3]
548 id_l1 = id(l1)
549 l1.sort()
550 assert l1 == [1, 2, 3]
551 assert id_l1 == id(l1)
552
553 def test_sorted() -> None:
554 res = [1, 2, 3]
555 l1 = [2, 1, 3]
556 id_l1 = id(l1)
557 s_l1 = sorted(l1)
558 assert s_l1 == res
559 assert id_l1 != id(s_l1)
560 assert l1 == [2, 1, 3]
561 assert sorted((2, 1, 3)) == res
562 assert sorted({2, 1, 3}) == res
563 assert sorted({2: "", 1: "", 3: ""}) == res
564
565 [case testIsInstance]
566 from copysubclass import subc
567 def test_built_in() -> None:
568 assert isinstance([], list)
569 assert isinstance([1,2,3], list)
570 assert isinstance(['a','b'], list)
571 assert isinstance(subc(), list)
572 assert isinstance(subc([1,2,3]), list)
573 assert isinstance(subc(['a','b']), list)
574
575 assert not isinstance({}, list)
576 assert not isinstance((), list)
577 assert not isinstance((1,2,3), list)
578 assert not isinstance(('a','b'), list)
579 assert not isinstance(1, list)
580 assert not isinstance('a', list)
581
582 def test_user_defined() -> None:
583 from userdefinedlist import list
584
585 assert isinstance(list(), list)
586 assert not isinstance([list()], list)
587
588 [file copysubclass.py]
589 from typing import Any
590 class subc(list[Any]):
591 pass
592
593 [file userdefinedlist.py]
594 class list:
595 pass