Debian Codegraph @

mypyc/test-data/run-vecs-misc.test

raw
1 -- Test cases for packed/specialized vec item types other than i64, such as
2 -- vec[i32] and vec[float]. Since many of the code paths are the same as for
3 -- vec[i64], only test a subset of functionality.
4 --
5 -- vec[i64] test cases are in run-vecs-i64.test.
6
7 [case testVecMiscBasicOps_librt]
8 # mypy: allow-redefinition-old
9
10 from typing import Any, Iterable, cast
11 from mypy_extensions import i64, i32, i16, u8
12 from librt.vecs import vec, append, extend, remove, pop
13 from testutil import assertRaises
14
15 def vec_u8_from_any(x: Any) -> vec[u8]:
16 return vec[u8](x)
17
18 def test_create_empty() -> None:
19 assert str(vec[i32]()) == "vec[i32]([])"
20 assert str(vec[i16]()) == "vec[i16]([])"
21 assert str(vec[u8]()) == "vec[u8]([])"
22 assert str(vec[float]()) == "vec[float]([])"
23 assert str(vec[bool]()) == "vec[bool]([])"
24
25 def test_create_from_values() -> None:
26 assert str(vec[i32]([5, -12])) == "vec[i32]([5, -12])"
27 assert str(vec[i16]([6, -8])) == "vec[i16]([6, -8])"
28 assert str(vec[u8]([0, 123, 255])) == "vec[u8]([0, 123, 255])"
29 assert str(vec[float]([1.5, -3.5])) == "vec[float]([1.5, -3.5])"
30 assert str(vec[bool]([False, True])) == "vec[bool]([False, True])"
31
32 def test_get_item() -> None:
33 assert vec[i32]([5, -12])[1] == -12
34 assert vec[i16]([6, -8])[1] == -8
35 assert vec[u8]([0, 123, 255])[2] == 255
36 assert vec[float]([1.5, -3.5])[1] == -3.5
37 assert vec[bool]([False, True])[1] == True
38
39 def test_get_item_negative() -> None:
40 assert vec[i32]([5, -12])[-2] == 5
41 assert vec[i16]([6, -8])[-2] == 6
42 assert vec[u8]([0, 123, 255])[-2] == 123
43 assert vec[float]([1.5, -3.5])[-2] == 1.5
44 assert vec[bool]([False, True])[-2] == False
45
46 def test_append() -> None:
47 assert append(vec[i32](), 123) == vec[i32]([123])
48 assert append(vec[i16](), 123) == vec[i16]([123])
49 assert append(vec[u8](), 123) == vec[u8]([123])
50 assert append(vec[float](), 123.5) == vec[float]([123.5])
51 assert append(vec[bool](), True) == vec[bool]([True])
52
53 def test_unbox() -> None:
54 v: vec[i64]
55
56 a = cast(Any, vec[i32]([1, 2]))
57 v_i32: vec[i32] = a
58 assert v_i32 == vec[i32]([1, 2])
59 with assertRaises(TypeError):
60 v = a
61
62 a = cast(Any, vec[i16]([1, 2]))
63 v_i16: vec[i16] = a
64 assert v_i16 == vec[i16]([1, 2])
65 with assertRaises(TypeError):
66 v = a
67
68 a = cast(Any, vec[u8]([1, 2]))
69 v_u8: vec[u8] = a
70 assert v_u8 == vec[u8]([1, 2])
71 with assertRaises(TypeError):
72 v = a
73
74 a = cast(Any, vec[float]([1, 2]))
75 v_float: vec[float] = a
76 assert v_float == vec[float]([1, 2])
77 with assertRaises(TypeError):
78 v = a
79
80 a = cast(Any, vec[bool]([True, False]))
81 v_bool: vec[bool] = a
82 assert v_bool == vec[bool]([True, False])
83 with assertRaises(TypeError):
84 v = a
85
86 def test_set_item() -> None:
87 v = vec[i32]([5, -12])
88 v[1] = 55
89 assert v[0] == 5 and v[1] == 55
90
91 v = vec[i16]([5, -12])
92 v[1] = 55
93 assert v[0] == 5 and v[1] == 55
94
95 v = vec[u8]([5, 237])
96 v[1] = 55
97 assert v[0] == 5 and v[1] == 55
98
99 v = vec[float]([1.5, -3.5])
100 v[1] = 5.5
101 assert v[0] == 1.5 and v[1] == 5.5
102
103 v = vec[bool]([True, False])
104 v[1] = True
105 assert v[0] and v[1]
106
107 def test_set_item_negative() -> None:
108 v = vec[i32]([5, -12])
109 v[-1] = 55
110 assert v[0] == 5 and v[1] == 55
111
112 v = vec[i16]([5, -12])
113 v[-1] = 55
114 assert v[0] == 5 and v[1] == 55
115
116 v = vec[u8]([5, 237])
117 v[-1] = 55
118 assert v[0] == 5 and v[1] == 55
119
120 v = vec[float]([1.5, -3.5])
121 v[-1] = 5.5
122 assert v[0] == 1.5 and v[1] == 5.5
123
124 v = vec[bool]([True, False])
125 v[-1] = True
126 assert v[0] and v[1]
127
128 def test_for_loop() -> None:
129 a: Any = []
130 for x in vec[float]([1.5, -3.5]):
131 a.append(x)
132 assert a == [1.5, -3.5]
133
134 a = []
135 for x in vec[bool]([True, False, False, True]):
136 a.append(x)
137 assert a == [True, False, False, True]
138
139 a = []
140 for x in vec[u8]([0, 5, 237, 255]):
141 a.append(x)
142 assert a == [0, 5, 237, 255]
143
144 def test_comprehension() -> None:
145 v = vec[i32]([x + 1 for x in [1, 4, -5]])
146 assert list(v) == [2, 5, -4]
147
148 v = vec[float]([x + 1.5 for x in range(4)])
149 assert list(v) == [1.5, 2.5, 3.5, 4.5]
150
151 def len_union(x: vec[i64] | vec[i32]) -> int:
152 return len(x)
153
154 def test_union_of_vecs() -> None:
155 assert len_union(vec[i64]()) == 0
156 assert len_union(vec[i64]([7, 8])) == 2
157 assert len_union(vec[i32]([7, 8, 9])) == 3
158 with assertRaises(TypeError):
159 a1: Any = vec[i16]()
160 len_union(a1)
161 with assertRaises(TypeError):
162 a2: Any = []
163 len_union(a2)
164
165 def test_nested_basics() -> None:
166 v = vec[vec[i32]]([vec[i32]([5, 6]),
167 vec[i32]([-12])])
168 assert str(v) == "vec[vec[i32]]([[5, 6], [-12]])"
169 assert v[0][1] == 6
170 assert v[1][0] == -12
171
172 v = vec[vec[i16]]([vec[i16]([5, 6]),
173 vec[i16]([-12])])
174 assert str(v) == "vec[vec[i16]]([[5, 6], [-12]])"
175 assert v[0][1] == 6
176 assert v[1][0] == -12
177
178 v = vec[vec[u8]]([vec[u8]([5, 6]),
179 vec[u8]([237])])
180 assert str(v) == "vec[vec[u8]]([[5, 6], [237]])"
181 assert v[0][1] == 6
182 assert v[1][0] == 237
183
184 v = vec[vec[float]]([vec[float]([1.5, -3.5]),
185 vec[float]([3.0])])
186 assert str(v) == "vec[vec[float]]([[1.5, -3.5], [3.0]])"
187 assert v[0][1] == -3.5
188 assert v[1][0] == 3.0
189
190 v = vec[vec[bool]]([vec[bool]([False, True]),
191 vec[bool]([False])])
192 assert str(v) == "vec[vec[bool]]([[False, True], [False]])"
193 assert v[0][1] == True
194 assert v[1][0] == False
195
196 def test_nested_unbox() -> None:
197 vv: vec[vec[i64]]
198
199 a = cast(Any, vec[vec[i32]]([vec[i32]([5])]))
200 v_i32: vec[vec[i32]] = a
201 assert str(v_i32) == "vec[vec[i32]]([[5]])"
202 with assertRaises(TypeError):
203 vv = a
204
205 a = cast(Any, vec[vec[i16]]([vec[i16]([5])]))
206 v_i16: vec[vec[i16]] = a
207 with assertRaises(TypeError):
208 vv = a
209
210 a = cast(Any, vec[vec[u8]]([vec[u8]([5])]))
211 v_u8: vec[vec[u8]] = a
212 with assertRaises(TypeError):
213 vv = a
214
215 a = cast(Any, vec[vec[float]]([vec[float]([5])]))
216 v_float: vec[vec[float]] = a
217 with assertRaises(TypeError):
218 vv = a
219
220 a = cast(Any, vec[vec[bool]]([vec[bool]([True])]))
221 v_bool: vec[vec[bool]] = a
222 with assertRaises(TypeError):
223 vv = a
224
225 def test_nested_misc() -> None:
226 v = vec[vec[float]]()
227 assert len(v) == 0
228 v = append(v, vec[float]([1.5]))
229 assert len(v) == 1
230 for x in v:
231 assert x == vec[float]([1.5])
232 vv, x = pop(v)
233 assert vv == vec[vec[float]]()
234 assert x == vec[float]([1.5])
235
236 # Since pop doesn't change the length of v, the following should work
237 v[0] = vec[float]([-2.5])
238 assert v[0] == vec[float]([-2.5])
239 v[0][0] = 3.5
240 assert v[0] == vec[float]([3.5])
241 assert v[:5] == vec[vec[float]]([vec[float]([3.5])])
242
243 def test_cap_misc() -> None:
244 v_f = vec[float](capacity=3)
245 assert len(v_f) == 0
246 v_f = append(v_f, 1.5)
247 assert v_f[0] == 1.5
248
249 v_u8 = vec[u8]([1, 2], capacity=5)
250 assert len(v_u8) == 2
251 assert v_u8[0] == 1
252
253 v_i16 = vec[i16](capacity=2)
254 v_i16 = append(v_i16, i16(10))
255 assert v_i16[0] == 10
256
257 v_i32 = vec[i32]([i32(7)], capacity=3)
258 assert v_i32[0] == 7
259
260 v_bool = vec[bool](capacity=4)
261 assert len(v_bool) == 0
262 v_bool = append(v_bool, True)
263 assert v_bool[0] == True
264
265 v_nested = vec[vec[float]](capacity=2)
266 assert len(v_nested) == 0
267 v_nested = append(v_nested, vec[float]([1.0]))
268 assert v_nested[0] == vec[float]([1.0])
269
270 def extend_float(v: vec[float], it: Iterable[float]) -> vec[float]:
271 return extend(v, it)
272
273 def extend_u8(v: vec[u8], it: Iterable[u8]) -> vec[u8]:
274 return extend(v, it)
275
276 def extend_i32(v: vec[i32], it: Iterable[i32]) -> vec[i32]:
277 return extend(v, it)
278
279 def test_extend_vec_float() -> None:
280 v = extend_float(vec[float]([1.0]), vec[float]([2.5, 3.5]))
281 assert v == vec[float]([1.0, 2.5, 3.5])
282
283 def test_extend_from_array() -> None:
284 import array
285 v = extend_i32(vec[i32]([1, 2]), array.array('i', [3, 4, 5]))
286 assert v == vec[i32]([1, 2, 3, 4, 5])
287
288 v2 = extend_float(vec[float]([1.0]), array.array('d', [2.5, 3.5]))
289 assert v2 == vec[float]([1.0, 2.5, 3.5])
290
291 def test_extend_from_array_empty() -> None:
292 import array
293 v = extend_i32(vec[i32]([1, 2]), array.array('i', []))
294 assert v == vec[i32]([1, 2])
295
296 v2 = extend_i32(vec[i32](), array.array('i', [10, 20]))
297 assert v2 == vec[i32]([10, 20])
298
299 def test_extend_from_bytes() -> None:
300 v = extend_u8(vec[u8]([1, 2]), b'\x03\x04\x05')
301 assert v == vec[u8]([1, 2, 3, 4, 5])
302
303 def test_extend_from_bytearray() -> None:
304 v = extend_u8(vec[u8]([10]), bytearray(b'\x14\x1e'))
305 assert v == vec[u8]([10, 20, 30])
306
307 def test_extend_from_bytes_empty() -> None:
308 v = extend_u8(vec[u8]([1, 2]), b'')
309 assert v == vec[u8]([1, 2])
310
311 v2 = extend_u8(vec[u8](), b'\x01\x02')
312 assert v2 == vec[u8]([1, 2])
313
314 def test_construct_from_array() -> None:
315 import array
316
317 # vec[i32]
318 a_i32 = array.array('i', [10, -20, 30])
319 v_i32 = vec[i32](a_i32)
320 assert len(v_i32) == 3
321 assert v_i32[0] == 10
322 assert v_i32[1] == -20
323 assert v_i32[2] == 30
324
325 # vec[i16]
326 a_i16 = array.array('h', [100, -200, 300])
327 v_i16 = vec[i16](a_i16)
328 assert len(v_i16) == 3
329 assert v_i16[0] == 100
330 assert v_i16[1] == -200
331 assert v_i16[2] == 300
332
333 # vec[u8]
334 a_u8 = array.array('B', [0, 127, 255])
335 v_u8 = vec[u8](a_u8)
336 assert len(v_u8) == 3
337 assert v_u8[0] == 0
338 assert v_u8[1] == 127
339 assert v_u8[2] == 255
340
341 # vec[float]
342 a_f = array.array('d', [1.5, -2.5, 3.0])
343 v_f = vec[float](a_f)
344 assert len(v_f) == 3
345 assert v_f[0] == 1.5
346 assert v_f[1] == -2.5
347 assert v_f[2] == 3.0
348
349 # Empty arrays
350 assert len(vec[i32](array.array('i'))) == 0
351 assert len(vec[i16](array.array('h'))) == 0
352 assert len(vec[u8](array.array('B'))) == 0
353 assert len(vec[float](array.array('d'))) == 0
354
355 # With capacity
356 v_cap = vec[i32](array.array('i', [1, 2]), capacity=100)
357 assert len(v_cap) == 2
358 assert v_cap[0] == 1
359 assert v_cap[1] == 2
360
361 def test_construct_from_bytes() -> None:
362 # vec[u8] from bytes
363 v = vec[u8](b'\x00\x01\x7f\xff')
364 assert len(v) == 4
365 assert v[0] == 0
366 assert v[1] == 1
367 assert v[2] == 127
368 assert v[3] == 255
369
370 # vec[u8] from empty bytes
371 assert len(vec[u8](b'')) == 0
372
373 # vec[u8] from bytearray
374 v2 = vec[u8](bytearray(b'\x05\x0a\x0f'))
375 assert len(v2) == 3
376 assert v2[0] == 5
377 assert v2[1] == 10
378 assert v2[2] == 15
379
380 # vec[u8] from empty bytearray
381 assert len(vec[u8](bytearray())) == 0
382
383 # With capacity
384 v3 = vec[u8](b'\x01\x02', capacity=50)
385 assert len(v3) == 2
386 assert v3[0] == 1
387 assert v3[1] == 2
388
389 # Capacity is exact: appending at capacity reallocates.
390 v4 = vec[u8](b'\x01\x02', capacity=2)
391 old = v4
392 v4 = append(v4, 3)
393 v4[0] = 99
394 assert old[0] == 1
395
396 with assertRaises(ValueError):
397 vec[u8](b'\x01', capacity=-1)
398
399 memoryview_ = eval('memoryview')
400 view_2d = memoryview_(bytearray(b'\x01\x02\x03\x04')).cast('B', shape=(2, 2))
401 with assertRaises(NotImplementedError):
402 vec_u8_from_any(view_2d)
403
404 def test_construct_from_array_type_mismatch() -> None:
405 import array
406
407 # Mismatched itemsize: falls back to element-wise iteration with
408 # value conversion. These all have compatible int types, just
409 # different widths.
410 v_i16 = vec[i16](array.array('i', [1, 2, -3]))
411 assert list(v_i16) == [1, 2, -3]
412
413 v_i32 = vec[i32](array.array('h', [10, -20]))
414 assert list(v_i32) == [10, -20]
415
416 v_u8 = vec[u8](array.array('h', [0, 127, 255]))
417 assert list(v_u8) == [0, 127, 255]
418
419 v_i64 = vec[i64](array.array('i', [5, 6, 7]))
420 assert list(v_i64) == [5, 6, 7]
421
422 # 32-bit float array -> vec[float] (64-bit double): itemsize
423 # mismatch, falls back to iter with float->double conversion
424 v_float = vec[float](array.array('f', [1.5, -2.5]))
425 assert v_float[0] == array.array('f', [1.5])[0]
426 assert v_float[1] == array.array('f', [-2.5])[0]
427
428 def test_vec_u8_to_bytes() -> None:
429 assert bytes(vec[u8]([72, 101, 108, 108, 111])) == b"Hello"
430 assert bytes(vec[u8]()) == b""
431 assert bytes(vec[u8]([0, 255, 1])) == b"\x00\xff\x01"
432
433 def u8_to_list(v: vec[u8]) -> list[u8]:
434 return list(v)
435
436 def u8_to_tuple(v: vec[u8]) -> tuple[u8, ...]:
437 return tuple(v)
438
439 def test_vec_u8_to_list() -> None:
440 assert u8_to_list(vec[u8]()) == []
441 assert u8_to_list(vec[u8]([0, 128, 255])) == [0, 128, 255]
442
443 def test_vec_u8_to_tuple() -> None:
444 assert u8_to_tuple(vec[u8]()) == ()
445 assert u8_to_tuple(vec[u8]([0, 128, 255])) == (0, 128, 255)