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