Debian Codegraph @

mypyc/test-data/irbuild-vec-t.test

raw
1 -- Test cases for vec[t] where t is a boxed, non-vec type (PyObject *).
2 -- Also tests for vec[t | None], which uses the same representation.
3
4 [case testVecTConvertToList]
5 from librt.vecs import vec
6
7 def to_list(v: vec[str]) -> list[str]:
8 return list(v)
9 [out]
10 def to_list(v):
11 v :: vec[str]
12 r0 :: list
13 L0:
14 r0 = VecTApi.to_list(v)
15 return r0
16
17 [case testVecTConvertToTuple]
18 from librt.vecs import vec
19
20 def to_tuple(v: vec[str]) -> tuple[str, ...]:
21 return tuple(v)
22 [out]
23 def to_tuple(v):
24 v :: vec[str]
25 r0 :: tuple
26 L0:
27 r0 = VecTApi.to_tuple(v)
28 return r0
29
30 [case testVecTCreateEmpty]
31 from librt.vecs import vec, append
32
33 class C: pass
34
35 def primitive() -> vec[str]:
36 return vec[str]()
37
38 def native_class() -> vec[C]:
39 return vec[C]()
40 [out]
41 def primitive():
42 r0 :: object
43 r1 :: ptr
44 r2 :: vec[str]
45 L0:
46 r0 = load_address PyUnicode_Type
47 r1 = r0
48 r2 = VecTApi.alloc(0, 0, r1)
49 return r2
50 def native_class():
51 r0 :: object
52 r1 :: ptr
53 r2 :: vec[__main__.C]
54 L0:
55 r0 = __main__.C :: type
56 r1 = r0
57 r2 = VecTApi.alloc(0, 0, r1)
58 return r2
59
60 [case testVecTAppend]
61 from librt.vecs import vec, append
62
63 def f(v: vec[str]) -> vec[str]:
64 return append(v, 'x')
65 [out]
66 def f(v):
67 v :: vec[str]
68 r0 :: str
69 r1 :: object
70 r2 :: ptr
71 r3 :: vec[str]
72 L0:
73 r0 = 'x'
74 r1 = load_address PyUnicode_Type
75 r2 = r1
76 r3 = VecTApi.append(v, r0, r2)
77 return r3
78
79 [case testVecTExtend]
80 from librt.vecs import vec, extend
81 from typing import Iterable
82
83 def f(v: vec[str], it: Iterable[str]) -> vec[str]:
84 return extend(v, it)
85 [out]
86 def f(v, it):
87 v :: vec[str]
88 it, r0 :: object
89 r1 :: ptr
90 r2 :: vec[str]
91 L0:
92 r0 = load_address PyUnicode_Type
93 r1 = r0
94 r2 = VecTApi.extend(v, it, r1)
95 return r2
96
97 [case testVecTExtendVec]
98 from librt.vecs import vec, extend
99
100 def f(v: vec[str], v2: vec[str]) -> vec[str]:
101 return extend(v, v2)
102 [out]
103 def f(v, v2):
104 v, v2 :: vec[str]
105 r0 :: object
106 r1 :: ptr
107 r2 :: vec[str]
108 L0:
109 r0 = load_address PyUnicode_Type
110 r1 = r0
111 r2 = VecTApi.extend_vec(v, v2, r1)
112 return r2
113
114 [case testVecTOptionalCreateEmpty]
115 from librt.vecs import vec, append
116 from typing import Optional
117
118 class C: pass
119
120 def primitive() -> vec[Optional[str]]:
121 return vec[Optional[str]]()
122
123 def native_class() -> vec[Optional[C]]:
124 return vec[Optional[C]]()
125 [out]
126 def primitive():
127 r0 :: object
128 r1, r2 :: ptr
129 r3 :: vec[str | None]
130 L0:
131 r0 = load_address PyUnicode_Type
132 r1 = r0
133 r2 = r1 | 1
134 r3 = VecTApi.alloc(0, 0, r2)
135 return r3
136 def native_class():
137 r0 :: object
138 r1, r2 :: ptr
139 r3 :: vec[__main__.C | None]
140 L0:
141 r0 = __main__.C :: type
142 r1 = r0
143 r2 = r1 | 1
144 r3 = VecTApi.alloc(0, 0, r2)
145 return r3
146
147 [case testVecTOptionalAppend]
148 from librt.vecs import vec, append
149 from typing import Optional
150
151 def f(v: vec[Optional[str]]) -> vec[Optional[str]]:
152 v = append(v, 'x')
153 return append(v, None)
154 [out]
155 def f(v):
156 v :: vec[str | None]
157 r0 :: str
158 r1 :: object
159 r2, r3 :: ptr
160 r4 :: vec[str | None]
161 r5, r6 :: object
162 r7, r8 :: ptr
163 r9 :: vec[str | None]
164 L0:
165 r0 = 'x'
166 r1 = load_address PyUnicode_Type
167 r2 = r1
168 r3 = r2 | 1
169 r4 = VecTApi.append(v, r0, r3)
170 v = r4
171 r5 = box(None, 1)
172 r6 = load_address PyUnicode_Type
173 r7 = r6
174 r8 = r7 | 1
175 r9 = VecTApi.append(v, r5, r8)
176 return r9
177
178 [case testVecTLen_64bit]
179 from librt.vecs import vec
180 from mypy_extensions import i64
181 from typing import Optional
182
183 def f(v: vec[str]) -> i64:
184 return len(v)
185
186 def g(v: vec[Optional[str]]) -> i64:
187 return len(v)
188 [out]
189 def f(v):
190 v :: vec[str]
191 r0 :: native_int
192 L0:
193 r0 = v.len
194 return r0
195 def g(v):
196 v :: vec[str | None]
197 r0 :: native_int
198 L0:
199 r0 = v.len
200 return r0
201
202 [case testVecTGetItem_64bit]
203 from librt.vecs import vec
204 from mypy_extensions import i64
205
206 def f(v: vec[str], n: i64) -> str:
207 return v[n]
208 [out]
209 def f(v, n):
210 v :: vec[str]
211 n :: i64
212 r0 :: native_int
213 r1 :: bit
214 r2 :: i64
215 r3 :: bit
216 r4 :: bool
217 r5 :: i64
218 r6 :: ptr
219 r7 :: i64
220 r8 :: ptr
221 r9 :: str
222 L0:
223 r0 = v.len
224 r1 = n < r0 :: unsigned
225 if r1 goto L4 else goto L1 :: bool
226 L1:
227 r2 = n + r0
228 r3 = r2 < r0 :: unsigned
229 if r3 goto L3 else goto L2 :: bool
230 L2:
231 r4 = raise IndexError
232 unreachable
233 L3:
234 r5 = r2
235 goto L5
236 L4:
237 r5 = n
238 L5:
239 r6 = v.items
240 r7 = r5 * 8
241 r8 = r6 + r7
242 r9 = load_mem r8 :: builtins.str*
243 keep_alive v
244 return r9
245
246 [case testVecTOptionalGetItem_64bit]
247 from librt.vecs import vec
248 from mypy_extensions import i64
249 from typing import Optional
250
251 def f(v: vec[Optional[str]], n: i64) -> Optional[str]:
252 return v[n]
253 [out]
254 def f(v, n):
255 v :: vec[str | None]
256 n :: i64
257 r0 :: native_int
258 r1 :: bit
259 r2 :: i64
260 r3 :: bit
261 r4 :: bool
262 r5 :: i64
263 r6 :: ptr
264 r7 :: i64
265 r8 :: ptr
266 r9 :: union[str, None]
267 L0:
268 r0 = v.len
269 r1 = n < r0 :: unsigned
270 if r1 goto L4 else goto L1 :: bool
271 L1:
272 r2 = n + r0
273 r3 = r2 < r0 :: unsigned
274 if r3 goto L3 else goto L2 :: bool
275 L2:
276 r4 = raise IndexError
277 unreachable
278 L3:
279 r5 = r2
280 goto L5
281 L4:
282 r5 = n
283 L5:
284 r6 = v.items
285 r7 = r5 * 8
286 r8 = r6 + r7
287 r9 = load_mem r8 :: union*
288 keep_alive v
289 return r9
290
291 [case testNewTPopLast]
292 from typing import Tuple
293 from librt.vecs import vec, pop
294
295 def pop_last(v: vec[str]) -> Tuple[vec[str], str]:
296 return pop(v)
297 [out]
298 def pop_last(v):
299 v :: vec[str]
300 r0 :: tuple[vec[str], str]
301 L0:
302 r0 = VecTApi.pop(v, -1)
303 return r0
304
305 [case testVecTConstructFromListComprehension]
306 from librt.vecs import vec
307 from mypy_extensions import i64
308
309 def f(n: i64) -> vec[str]:
310 return vec[str](['x' for x in range(i64(5))])
311 [out]
312 def f(n):
313 n :: i64
314 r0 :: object
315 r1 :: ptr
316 r2, r3 :: vec[str]
317 r4, x :: i64
318 r5 :: bit
319 r6 :: str
320 r7 :: object
321 r8 :: ptr
322 r9 :: vec[str]
323 r10 :: i64
324 L0:
325 r0 = load_address PyUnicode_Type
326 r1 = r0
327 r2 = VecTApi.alloc(0, 0, r1)
328 r3 = r2
329 r4 = 0
330 x = r4
331 L1:
332 r5 = r4 < 5 :: signed
333 if r5 goto L2 else goto L4 :: bool
334 L2:
335 x = r4
336 r6 = 'x'
337 r7 = load_address PyUnicode_Type
338 r8 = r7
339 r9 = VecTApi.append(r3, r6, r8)
340 r3 = r9
341 L3:
342 r10 = r4 + 1
343 r4 = r10
344 goto L1
345 L4:
346 return r3
347
348 [case testVecTCheckItemType]
349 from librt.vecs import vec
350 from typing import Tuple, Any, List, TypeVar
351
352 def bad1(v: vec[Tuple[str, str]]) -> None: pass
353 def bad2(v: vec[int]) -> None: pass
354 def bad3(v: vec) -> None: pass
355 def bad4(v: vec[Any]) -> None: pass
356 T = TypeVar("T")
357 def bad5(v: vec[T]) -> None: pass
358
359 def ok1(v: vec[str], v2: vec[bytes]) -> None: pass
360 def ok2(v: vec[Tuple[str, ...]]) -> None: pass
361 def ok3(v: vec[object]) -> None: pass
362 def ok4(v: vec[List[Any]]) -> None: pass
363 def ok5(v: vec[vec[str]]) -> None: pass
364 [out]
365 main:4: error: Invalid item type for "vec"
366 main:5: error: Invalid item type for "vec"
367 main:6: error: Invalid item type for "vec"
368 main:7: error: Invalid item type for "vec"
369 main:9: error: Invalid item type for "vec"
370
371 [case testVecTCheckItemTypeUnion]
372 from librt.vecs import vec
373 from typing import Tuple, Union, Optional, Any, TypeVar
374 from mypy_extensions import i64
375
376 def bad1(v: vec[Union[str, bytes]]) -> None: pass
377 def bad2(v: vec[Union[str, bytes, None]]) -> None: pass
378 def bad3(v: vec[Union[int, None]]) -> None: pass
379 def bad4(v: vec[Union[None, int]]) -> None: pass
380 def bad5(v: vec[Union[i64, None]]) -> None: pass
381 def bad6(v: vec[Union[None, i64]]) -> None: pass
382 def bad7(v: vec[Union[bool, None]]) -> None: pass
383 def bad9(v: vec[Union[float, None]]) -> None: pass
384 def bad10(v: vec[Union[vec[str], None]]) -> None: pass
385 def bad11(v: vec[Optional[vec[str]]]) -> None: pass
386 def bad12(v: vec[Union[str, Any]]) -> None: pass
387 T = TypeVar("T")
388 def bad13(v: vec[Union[str, T]]) -> None: pass
389
390 def ok1(v: vec[Union[str, None]]) -> None: pass
391 def ok2(v: vec[Union[None, str]]) -> None: pass
392 [out]
393 main:5: error: Invalid item type for "vec"
394 main:6: error: Invalid item type for "vec"
395 main:7: error: Invalid item type for "vec"
396 main:8: error: Invalid item type for "vec"
397 main:9: error: Invalid item type for "vec"
398 main:10: error: Invalid item type for "vec"
399 main:11: error: Invalid item type for "vec"
400 main:12: error: Invalid item type for "vec"
401 main:13: error: Invalid item type for "vec"
402 main:14: error: Invalid item type for "vec"
403 main:15: error: Invalid item type for "vec"
404 main:17: error: Invalid item type for "vec"
405
406 [case testVecTCheckItemTypeDuringConstruction]
407 from librt.vecs import vec
408 from typing import Optional, Union
409
410 def f() -> None:
411 vec[Optional[int]]()
412 vec[Union[str, bytes]]()
413 vec[Optional[vec[str]]]()
414
415 [out]
416 main:5: error: Invalid item type for "vec"
417 main:6: error: Invalid item type for "vec"
418 main:7: error: Invalid item type for "vec"
419
420 [case testVecTCreateWithCap_64bit]
421 from librt.vecs import vec
422 from mypy_extensions import i64
423
424 def cap_only() -> vec[str]:
425 return vec[str](capacity=5)
426
427 def cap_variable(n: i64) -> vec[str]:
428 return vec[str](capacity=n)
429 [out]
430 def cap_only():
431 r0 :: object
432 r1 :: ptr
433 r2 :: vec[str]
434 L0:
435 r0 = load_address PyUnicode_Type
436 r1 = r0
437 r2 = VecTApi.alloc(0, 5, r1)
438 return r2
439 def cap_variable(n):
440 n :: i64
441 r0 :: object
442 r1 :: ptr
443 r2 :: vec[str]
444 L0:
445 r0 = load_address PyUnicode_Type
446 r1 = r0
447 r2 = VecTApi.alloc(0, n, r1)
448 return r2
449
450 [case testVecTCreateFromListWithCap_64bit]
451 from librt.vecs import vec
452 from mypy_extensions import i64
453
454 def list_with_cap() -> vec[str]:
455 return vec[str](['a', 'b'], capacity=5)
456 [out]
457 def list_with_cap():
458 r0, r1 :: str
459 r2 :: object
460 r3 :: ptr
461 r4 :: vec[str]
462 r5, r6, r7 :: ptr
463 L0:
464 r0 = 'a'
465 r1 = 'b'
466 r2 = load_address PyUnicode_Type
467 r3 = r2
468 r4 = VecTApi.alloc(2, 5, r3)
469 r5 = r4.items
470 set_mem r5, r0 :: builtins.str*
471 r6 = r5 + 8
472 set_mem r6, r1 :: builtins.str*
473 r7 = r6 + 8
474 keep_alive r4
475 return r4
476
477 [case testVecTConstructFromIterable]
478 from typing import Iterable
479 from librt.vecs import vec
480
481 def f(x: Iterable[str]) -> vec[str]:
482 return vec[str](x)
483 [out]
484 def f(x):
485 x, r0 :: object
486 r1 :: ptr
487 r2 :: vec[str]
488 L0:
489 r0 = load_address PyUnicode_Type
490 r1 = r0
491 r2 = VecTApi.from_iterable(r1, x, 0)
492 return r2
493
494 [case testVecTCreateFromListObject]
495 from librt.vecs import vec
496
497 def from_list(a: list[str]) -> vec[str]:
498 return vec[str](a)
499 [out]
500 def from_list(a):
501 a :: list
502 r0 :: object
503 r1 :: ptr
504 r2 :: vec[str]
505 L0:
506 r0 = load_address PyUnicode_Type
507 r1 = r0
508 r2 = VecTApi.from_iterable(r1, a, 0)
509 return r2
510
511 [case testVecTCreateFromTupleObject]
512 from librt.vecs import vec
513
514 def from_tuple(a: tuple[str, ...]) -> vec[str]:
515 return vec[str](a)
516 [out]
517 def from_tuple(a):
518 a :: tuple
519 r0 :: object
520 r1 :: ptr
521 r2 :: vec[str]
522 L0:
523 r0 = load_address PyUnicode_Type
524 r1 = r0
525 r2 = VecTApi.from_iterable(r1, a, 0)
526 return r2