Debian Codegraph @

mypyc/test-data/run-vecs-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 testVecTBasicOps_librt]
5 from typing import Final, Any, Iterable, Optional, Tuple
6
7 from mypy_extensions import i64
8 from librt.vecs import vec, append, extend, remove, pop
9
10 from testutil import assertRaises
11
12 def test_create_empty() -> None:
13 v = vec[str]()
14 assert len(v) == 0
15 v2 = vec[Optional[str]]()
16 assert len(v2) == 0
17
18 def test_create_from_list_and_get_item() -> None:
19 v = vec[str]([])
20 assert len(v) == 0
21
22 v = vec[str](["xyz", "0" + str(), str(), "foo"])
23 assert len(v) == 4
24 assert v[0] == "xyz"
25 assert v[1] == "0"
26 assert v[2] == ""
27 assert v[3] == "foo"
28
29 v = vec[str](["1", "2", "3", "4", "5", "6", "7", "8", "9"])
30 assert len(v) == 9
31 for i in range(i64(9)):
32 assert v[i] == str(i + 1)
33
34 def test_create_optional_and_get_item() -> None:
35 v = vec[Optional[str]](["xyz", None])
36 assert len(v) == 2
37 assert v[0] == "xyz"
38 assert v[1] is None
39
40 def test_append() -> None:
41 v = vec[str]()
42 v = append(v, str() + "3")
43 assert len(v) == 1
44 x = "xyz" + str()
45 v = append(v, x)
46 assert len(v) == 2
47 assert v[0] == "3"
48 assert v[1] == "xyz"
49 v = vec[str]()
50 for i in range(1024):
51 v = append(v, str(i * 3))
52 assert len(v) == 1024
53 for i in range(1024):
54 assert v[i] == str(i * 3)
55
56 def test_get_item() -> None:
57 v = vec[str](["3", "4"])
58 x: i64 = int()
59 assert v[x] == "3"
60 assert v[int()] == "3"
61 x += 1
62 assert v[x] == "4"
63 assert v[int() + 1] == "4"
64 with assertRaises(IndexError):
65 v[x + 1]
66 with assertRaises(IndexError):
67 v[2]
68 with assertRaises(IndexError):
69 v[2**63 - 1]
70
71 def test_set_item() -> None:
72 v = vec[str](["3", "4"])
73 v[0] = "0"
74 assert v[0] == "0"
75
76 x: i64 = int()
77
78 v[x] = "4"
79 assert v[x] == "4"
80
81 v[int()] = "5"
82 assert v[int()] == "5"
83
84 x += 1
85
86 v[x] = "-5"
87 assert v[x] == "-5"
88
89 v[1 + int()] = "xyz"
90 assert v[1] == "xyz"
91
92 with assertRaises(IndexError):
93 v[1 + x] = "6"
94 with assertRaises(IndexError):
95 v[2 + int()] = "6"
96
97 def test_set_item_optional() -> None:
98 v = vec[Optional[str]](["3", None])
99 v[1] = str() + "a"
100 assert v[1] == "a"
101 v[0] = None
102 assert v[0] is None
103
104 def test_box_and_unbox() -> None:
105 v = vec[str](["3", "5"])
106 o: Any = v
107 assert len(o) == 2
108 assert o[0] == "3"
109 assert o[1] == "5"
110 o[1] = "6"
111 v2: vec[str] = o
112 assert len(v2) == 2
113 assert v2[0] == "3"
114 assert v2[1] == "6"
115 o2: Any = None
116 with assertRaises(TypeError, "vec[str] object expected; got None"):
117 v2 = o2
118 o3: Any = vec[bytes]([])
119 with assertRaises(TypeError, "vec[str] object expected; got vec"):
120 v2 = o3
121
122 def test_box_and_unbox_optional() -> None:
123 v = vec[Optional[str]](["3", "4"])
124 o: Any = v
125 o[0] = None
126 v = o
127 assert v == vec[Optional[str]]([None, "4"])
128
129 v2 = vec[str](["4"])
130 o2: Any = v2
131 o2[0] = "5"
132 v2 = o2
133 assert v2 == vec[str](["5"])
134
135 o3: Any = vec[str]()
136 with assertRaises(TypeError, "vec[str | None] object expected; got vec"):
137 v = o3
138
139 o4: Any = vec[Optional[str]](["3"])
140 with assertRaises(TypeError, "vec[str] object expected; got vec"):
141 v2 = o4
142
143 def test_construct_from_list_multiply() -> None:
144 for i in range(50):
145 v = vec[str]([str(i + 1)] * i)
146 assert len(v) == i
147 for j in range(i):
148 assert v[j] == str(i + 1)
149 for i in range(50):
150 v = vec[str](i * [str(i - 1)])
151 assert len(v) == i
152 for j in range(i):
153 assert v[j] == str(i - 1)
154
155 def test_construct_from_list_comprehension() -> None:
156 for i in range(50):
157 l = [i * i for i in range(i)]
158 v = vec[str]([str(n + 5) for n in l])
159 assert len(v) == i
160 for j in range(i):
161 assert v[j] == str(j * j + 5)
162
163 def test_construct_from_list() -> None:
164 a: list[str] = []
165 assert vec[str](a) == vec[str]()
166 b = ["x", "y"]
167 v = vec[str](b)
168 assert len(v) == 2
169 assert v[0] == "x"
170 assert v[1] == "y"
171 for i in range(50):
172 c = [str(j * j) for j in range(i)]
173 v = vec[str](c)
174 assert len(v) == i
175 for j in range(i):
176 assert v[j] == str(j * j)
177
178 def test_construct_from_tuple() -> None:
179 a: tuple[str, ...] = ()
180 assert vec[str](a) == vec[str]()
181 b: tuple[str, ...] = ("x", "y")
182 v = vec[str](b)
183 assert len(v) == 2
184 assert v[0] == "x"
185 assert v[1] == "y"
186
187 def test_construct_from_iterable() -> None:
188 for i in range(50):
189 it: Iterable[str] = iter([str(i * i) for i in range(i)])
190 v = vec[str](it)
191 assert len(v) == i
192 for j in range(i):
193 assert v[j] == str(j * j)
194
195 def test_equality() -> None:
196 v0 = vec[str]()
197 v0b = vec[str]()
198 v1 = vec[str](["1"])
199 v1b = vec[str](["3"])
200 assert v0 == v0
201 assert v1 == v1
202 assert v0 == v0b
203 assert v0 != v1
204 assert v1 != v1b
205 assert v1 != v0
206
207 def test_equality_optional() -> None:
208 v = vec[str]()
209 vo = vec[Optional[str]]()
210 assert v != vo
211 assert vo == vo
212 vo2 = vec[Optional[str]](["x", None])
213 assert vo2 == vec[Optional[str]](["x", None])
214 assert vo2 != vec[Optional[str]](["x", "y"])
215 assert vo2 != vec[Optional[str]]([None, None])
216 assert vo2 != vec[Optional[str]](["x", None, None])
217
218 def test_str_conversion() -> None:
219 v = vec[str]()
220 assert str(v) == "vec[str]([])"
221 assert repr(v) == "vec[str]([])"
222 v = vec[str](["126"])
223 assert str(v) == "vec[str](['126'])"
224 v = append(v, "5")
225 assert str(v) == "vec[str](['126', '5'])"
226 v = append(v, "xyz")
227 assert str(v) == "vec[str](['126', '5', 'xyz'])"
228 v2 = vec[Optional[str]]()
229 assert str(v2) == "vec[str | None]([])"
230 v2 = vec[Optional[str]](["x", None])
231 assert str(v2) == "vec[str | None](['x', None])"
232
233 def test_for_loop() -> None:
234 for n in vec[str]():
235 assert False
236 a = []
237 for n in vec[str](["5"]):
238 a.append(n)
239 assert a == ["5"]
240 v = vec[str](["xyz", "9", "8"])
241 a = []
242 for n in v:
243 a.append(n)
244 assert a == ["xyz", "9", "8"]
245 assert len(v) == 3
246
247 def test_contains() -> None:
248 v = vec[str]()
249 x = str()
250 assert x not in v
251 v = vec[str](["x"])
252 assert x + "x" in v
253 assert x not in v
254 v2 = vec[str](["xyz", "7", "9"])
255 assert x + "xyz" in v2
256 assert x + "7" in v2
257 assert x + "9" in v2
258 assert x not in v2
259 assert x + "x" not in v2
260
261 def test_slicing() -> None:
262 v = vec[str]()
263 assert v[:] == vec[str]([])
264 assert v[1:] == vec[str]([])
265 assert v[:-5] == vec[str]([])
266 v = vec[str](["0", "1", "2", "3", "4"])
267 assert v[1:4] == vec[str](["1", "2", "3"])
268 assert v[2:-1] == vec[str](["2", "3"])
269 assert v[-2:-1] == vec[str](["3"])
270 assert v[1:] == vec[str](["1", "2", "3", "4"])
271 assert v[:-1] == vec[str](["0", "1", "2", "3"])
272 assert v[:] == v
273 assert v[:] is not v
274 assert v[5:] == vec[str]()
275 assert v[100:] == vec[str]()
276 assert v[0:5] ==v
277 assert v[0:5] is not v
278 assert v[2:100] == vec[str](["2", "3", "4"])
279 assert v[-100:2] == vec[str](["0", "1"])
280 assert v[5:100] == vec[str]([])
281 assert v[50:100] == vec[str]([])
282 assert v[-100:-50] == vec[str]([])
283
284 def test_slicing_with_step() -> None:
285 v = vec[str](["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
286 assert v[1:5:2] == vec[str](["1", "3"])
287 assert v[1:6:2] == vec[str](["1", "3", "5"])
288 assert v[5:1:-2] == vec[str](["5", "3"])
289 assert v[6:1:-2] == vec[str](["6", "4", "2"])
290 v = vec[str](["0", "1", "2", "3", "4"])
291 assert v[::-1] == vec[str](["4", "3", "2", "1", "0"])
292 with assertRaises(ValueError):
293 v[1:3:0]
294
295 def test_remove() -> None:
296 a = ["4", "7", "xyz"]
297 for i in a:
298 v = vec[str](a)
299 v = remove(v, i)
300 assert v == vec[str]([j for j in a if j != i])
301 v = vec[str](a)
302 v = remove(v, "4")
303 v = remove(v, "7")
304 v = remove(v, "xyz")
305 assert v == vec[str]()
306 with assertRaises(ValueError):
307 remove(v, "4")
308 v = append(v, "5")
309 with assertRaises(ValueError):
310 remove(v, "7")
311 v = remove(v, "5")
312 assert len(v) == 0
313 v = vec[str](["1", "1", "1"])
314 v = remove(v, "1")
315 assert v == vec[str](["1", "1"])
316 v = remove(v, "1")
317 assert v == vec[str](["1"])
318 v = remove(v, "1")
319 assert v == vec[str]()
320 f: Any = 1.1
321 with assertRaises(TypeError):
322 remove(v, f)
323 s: Any = None
324 with assertRaises(TypeError):
325 remove(v, s)
326
327 def test_pop_last() -> None:
328 v = vec[str](["4", "7", "xyz"])
329 v, n = pop(v)
330 assert n == "xyz"
331 assert v == vec[str](["4", "7"])
332 v, n = pop(v)
333 assert n == "7"
334 assert v == vec[str](["4"])
335 v, n = pop(v)
336 assert n == "4"
337 assert v == vec[str]()
338 with assertRaises(IndexError):
339 pop(v)
340
341 def test_pop_index() -> None:
342 v = vec[str](["4", "7", "9", "15", "22"])
343 v, n = pop(v, 0)
344 assert n == "4"
345 assert v == vec[str](["7", "9", "15", "22"])
346 v, n = pop(v, -1)
347 assert n == "22"
348 assert v == vec[str](["7", "9", "15"])
349 v, n = pop(v, 1)
350 assert n == "9"
351 assert v == vec[str](["7", "15"])
352
353 with assertRaises(IndexError):
354 pop(v, 2)
355
356 with assertRaises(IndexError):
357 pop(v, -3)
358
359 v, n = pop(v, -2)
360 assert n == "7"
361 assert v == vec[str](["15"])
362 v, n = pop(v, 0)
363 assert n == "15"
364 assert v == vec[str]()
365
366 def test_pop_optional() -> None:
367 v = vec[Optional[str]](["x" + str(), None])
368 v, s = pop(v)
369 assert s is None
370 v, s = pop(v)
371 assert s == "x"
372 assert len(v) == 0
373 v = vec[Optional[str]](["x" + str(), None])
374 v, s = pop(v, 0)
375 assert s == "x"
376 v, s = pop(v, -1)
377 assert s is None
378
379 def test_iter_and_next() -> None:
380 v = vec[str](["s1" + str(), "s2" + str(), "s3" + str()])
381 it = iter(v)
382 assert next(it) == "s1"
383 assert next(it) == "s2"
384 assert next(it) == "s3"
385 with assertRaises(StopIteration):
386 next(it)
387
388 def f(x: vec[str]) -> None:
389 pass
390
391 def test_wrapper_arg_check() -> None:
392 f_any: Any = f
393 with assertRaises(TypeError):
394 f_any([])
395
396 v: Final = vec[str](["x", "y"])
397
398 def test_final() -> None:
399 assert v == vec[str](["x", "y"])
400
401 def fdefault(v: vec[str] = vec[str](["x"])) -> vec[str]:
402 return v
403
404 def test_default_arg() -> None:
405 assert fdefault() == vec[str](["x"])
406 assert fdefault(vec[str](["y"])) == vec[str](["y"])
407 f_any: Any = fdefault
408 assert f_any() == vec[str](["x"])
409 assert f_any(vec[str](["y"])) == vec[str](["y"])
410
411 def ftuple(b: bool) -> Tuple[vec[str], vec[str]]:
412 if b:
413 raise RuntimeError()
414 return vec[str](["x"]), vec[str]()
415
416 def test_tuple() -> None:
417 t = ftuple(False)
418 assert len(t) == 2
419 assert t[0] == vec[str](["x"])
420 assert t[1] == vec[str]()
421 a, b = t
422 assert a == t[0]
423 assert b == t[1]
424 with assertRaises(RuntimeError):
425 ftuple(True)
426 f_any: Any = ftuple
427 t2 = f_any(False)
428 assert t == t2
429 with assertRaises(RuntimeError):
430 f_any(True)
431
432 def test_optional_item_new_syntax() -> None:
433 v = vec[str | None]()
434 assert len(v) == 0
435 assert str(v) == "vec[str | None]([])"
436 v = append(v, 'x')
437 v = append(v, None)
438 assert str(v) == "vec[str | None](['x', None])"
439
440 class Foo:
441 def __init__(self, s: str) -> None:
442 self.s = s
443
444 def len_union(x: vec[str] | vec[Optional[Foo]]) -> int:
445 return len(x)
446
447 def test_union_of_vecs() -> None:
448 assert len_union(vec[str]()) == 0
449 assert len_union(vec[str](["x", "y"])) == 2
450 assert len_union(vec[Optional[Foo]]([Foo("x"), None, Foo("y")])) == 3
451 with assertRaises(TypeError):
452 a1: Any = vec[Optional[str]]()
453 len_union(a1)
454 with assertRaises(TypeError):
455 a2: Any = []
456 len_union(a2)
457 with assertRaises(TypeError):
458 a3: Any = vec[bytes]()
459 len_union(a3)
460 with assertRaises(TypeError):
461 a4: Any = vec[Foo]()
462 len_union(a4)
463
464 def test_append_with_alias() -> None:
465 v = vec[str]()
466 v = append(v, 'a')
467 alias = v
468 v = append(v, 'b')
469 v = append(v, 'c')
470 v = append(v, 'd')
471 assert alias[0] == 'a'
472 assert len(alias) == 1
473 assert v[0] == 'a'
474 assert len(v) == 4
475
476 def test_cap_empty() -> None:
477 v = vec[str](capacity=3)
478 assert len(v) == 0
479 assert v == vec[str]()
480
481 def test_cap_with_initializer() -> None:
482 v = vec[str](['a', 'b'], capacity=3)
483 assert len(v) == 2
484 assert v[0] == 'a'
485 assert v[1] == 'b'
486
487 def test_cap_from_comprehension_and_iterable() -> None:
488 v = vec[str]([str(i) for i in range(2)], capacity=3)
489 old = v
490 v = append(v, '2')
491 v[0] = 'x'
492 assert old[0] == 'x'
493 assert v == vec[str](['x', '1', '2'])
494
495 v = vec[str]((str(i) for i in range(2)), capacity=3)
496 old = v
497 v = append(v, '2')
498 v[0] = 'y'
499 assert old[0] == 'y'
500 assert v == vec[str](['y', '1', '2'])
501
502 def test_cap_variable(n: i64 = 3) -> None:
503 v = vec[str](capacity=n)
504 assert len(v) == 0
505 v = append(v, 'x')
506 assert v[0] == 'x'
507
508 def test_cap_buffer_reuse() -> None:
509 v = vec[str](['a', 'b'], capacity=3)
510 old = v
511 v = append(v, 'c')
512 v[0] = 'z'
513 assert old[0] == 'z' # shared buffer
514
515 def test_cap_below_initializer_length() -> None:
516 v = vec[str](['a', 'b', 'c'], capacity=1)
517 assert len(v) == 3
518 assert v[0] == 'a'
519 assert v[1] == 'b'
520 assert v[2] == 'c'
521
522 def test_cap_negative() -> None:
523 with assertRaises(ValueError):
524 vec[str](capacity=-1)
525 with assertRaises(ValueError):
526 vec[str](['x'], capacity=-1)
527
528 def extend_vec(v: vec[str], v2: vec[str]) -> vec[str]:
529 return extend(v, v2)
530
531 def extend_iterable(v: vec[str], it: Iterable[str]) -> vec[str]:
532 return extend(v, it)
533
534 def test_extend_vec() -> None:
535 v = extend_vec(vec[str](['a', 'b']), vec[str](['c', 'd']))
536 assert v == vec[str](['a', 'b', 'c', 'd'])
537
538 def test_extend_vec_empty_src() -> None:
539 v = extend_vec(vec[str](['a']), vec[str]())
540 assert v == vec[str](['a'])
541
542 def test_extend_vec_empty_dst() -> None:
543 v = extend_vec(vec[str](), vec[str](['x', 'y']))
544 assert v == vec[str](['x', 'y'])
545
546 def test_extend_vec_fits_in_capacity() -> None:
547 v = vec[str](['a', 'b'], capacity=3)
548 v = extend_vec(v, vec[str](['c']))
549 assert v == vec[str](['a', 'b', 'c'])
550
551 def test_extend_vec_exceeds_capacity() -> None:
552 v = vec[str](['a', 'b'], capacity=3)
553 v = extend_vec(v, vec[str](['c', 'd', 'e']))
554 assert v == vec[str](['a', 'b', 'c', 'd', 'e'])
555
556 def test_extend_vec_multiple_doublings() -> None:
557 v = vec[str](['x'], capacity=1)
558 src = vec[str]()
559 for i in range(20):
560 src = append(src, str(i))
561 v = extend_vec(v, src)
562 assert len(v) == 21
563 assert v[0] == 'x'
564 assert v[20] == '19'
565
566 def test_extend_vec_self() -> None:
567 v = vec[str]()
568 v = append(v, 'a')
569 v = append(v, 'b')
570 v = extend_vec(v, v)
571 assert v == vec[str](['a', 'b', 'a', 'b'])
572
573 def test_extend_vec_shared_buffer_after_pop() -> None:
574 v0 = vec[str]()
575 v0 = append(v0, 'a')
576 v0 = append(v0, 'b')
577 v0 = append(v0, 'c')
578 v1, _ = pop(v0)
579 # v1 and v0 share a buffer; v1.len=2, v0.len=3
580 v = extend_vec(v1, v0)
581 assert v == vec[str](['a', 'b', 'a', 'b', 'c'])
582 # src (v0) must not be corrupted
583 assert v0 == vec[str](['a', 'b', 'c'])
584
585 def test_extend_iterable_runtime_vec_shared_buffer_after_pop() -> None:
586 v0 = vec[str]()
587 v0 = append(v0, 'a')
588 v0 = append(v0, 'b')
589 v0 = append(v0, 'c')
590 v1, _ = pop(v0)
591 # Statically this uses VecTApi.extend, but at runtime the iterable is a vec.
592 it: Iterable[str] = v0
593 v = extend_iterable(v1, it)
594 assert v == vec[str](['a', 'b', 'a', 'b', 'c'])
595 assert v0 == vec[str](['a', 'b', 'c'])
596
597 def to_list(v: vec[str]) -> list[str]:
598 return list(v)
599
600 def to_list_optional(v: vec[Optional[str]]) -> list[Optional[str]]:
601 return list(v)
602
603 def test_to_list_empty() -> None:
604 assert to_list(vec[str]()) == []
605
606 def test_to_list_single() -> None:
607 assert to_list(vec[str](['hello'])) == ['hello']
608
609 def test_to_list_multiple() -> None:
610 assert to_list(vec[str](['a', 'b', 'c'])) == ['a', 'b', 'c']
611
612 def test_to_list_optional() -> None:
613 assert to_list_optional(vec[Optional[str]](['x', None, 'y'])) == ['x', None, 'y']
614
615 def test_to_list_does_not_consume() -> None:
616 v = vec[str](['a', 'b'])
617 l1 = to_list(v)
618 l2 = to_list(v)
619 assert l1 == l2 == ['a', 'b']
620
621 def test_to_list_shared_buffer() -> None:
622 v0 = vec[str](['a', 'b', 'c'])
623 v1, item = pop(v0)
624 assert item == 'c'
625 # v0 and v1 share a buffer (v0.len=3, v1.len=2)
626 result = to_list(v1)
627 assert result == ['a', 'b']
628 # v0 must still be valid
629 assert v0 == vec[str](['a', 'b', 'c'])
630
631 def to_tuple(v: vec[str]) -> Tuple[str, ...]:
632 return tuple(v)
633
634 def to_tuple_optional(v: vec[Optional[str]]) -> Tuple[Optional[str], ...]:
635 return tuple(v)
636
637 def test_to_tuple_empty() -> None:
638 assert to_tuple(vec[str]()) == ()
639
640 def test_to_tuple_single() -> None:
641 assert to_tuple(vec[str](['hello'])) == ('hello',)
642
643 def test_to_tuple_multiple() -> None:
644 assert to_tuple(vec[str](['a', 'b', 'c'])) == ('a', 'b', 'c')
645
646 def test_to_tuple_optional() -> None:
647 assert to_tuple_optional(vec[Optional[str]](['x', None, 'y'])) == ('x', None, 'y')
648
649 def test_to_tuple_does_not_consume() -> None:
650 v = vec[str](['a', 'b'])
651 t1 = to_tuple(v)
652 t2 = to_tuple(v)
653 assert t1 == t2 == ('a', 'b')
654
655 def test_to_tuple_shared_buffer() -> None:
656 v0 = vec[str](['a', 'b', 'c'])
657 v1, item = pop(v0)
658 assert item == 'c'
659 # v0 and v1 share a buffer (v0.len=3, v1.len=2)
660 result = to_tuple(v1)
661 assert result == ('a', 'b')
662 # v0 must still be valid
663 assert v0 == vec[str](['a', 'b', 'c'])