| 1 |
[case testLibrtVecsTInterpreted_librt] |
| 2 |
# Test cases for vec[<ref type>], using generic operations (simulates use from interpreted code). |
| 3 |
import sys |
| 4 |
from typing import cast, List, Any |
| 5 |
import typing |
| 6 |
|
| 7 |
import librt.vecs |
| 8 |
import mypy_extensions |
| 9 |
|
| 10 |
from testutil import assertRaises, is_gil_disabled, is_64_bit_platform |
| 11 |
from helpers import ClassWithCustomMeta |
| 12 |
|
| 13 |
vec: Any = librt.vecs.vec |
| 14 |
append: Any = librt.vecs.append |
| 15 |
remove: Any = librt.vecs.remove |
| 16 |
pop: Any = librt.vecs.pop |
| 17 |
extend: Any = librt.vecs.extend |
| 18 |
getsizeof: Any = getattr(sys, "getsizeof") |
| 19 |
i64: Any = mypy_extensions.i64 |
| 20 |
version_info: Any = sys.version_info |
| 21 |
|
| 22 |
# Work around test stub limitations |
| 23 |
Optional: Any = getattr(getattr(sys, "modules")["typing"], "Optional") |
| 24 |
|
| 25 |
def assert_eq(x: object, y: object) -> None: |
| 26 |
assert x == y |
| 27 |
assert y == x |
| 28 |
b = x != y |
| 29 |
assert not b |
| 30 |
b = y != x |
| 31 |
assert not b |
| 32 |
|
| 33 |
def assert_neq(x: object, y: object) -> None: |
| 34 |
assert x != y |
| 35 |
assert y != x |
| 36 |
b = x == y |
| 37 |
assert not b |
| 38 |
b = y == x |
| 39 |
assert not b |
| 40 |
|
| 41 |
@mypy_extensions.mypyc_attr(native_class=False) |
| 42 |
class StringSubclass(str): |
| 43 |
pass |
| 44 |
|
| 45 |
class ClassWithRaisingEq: |
| 46 |
def __eq__(self, other): |
| 47 |
raise RuntimeError() |
| 48 |
|
| 49 |
def test_vec_indexing() -> None: |
| 50 |
assert vec[str] is not vec[i64] |
| 51 |
assert type(vec[str]) is not type |
| 52 |
assert type(vec[Optional[str]]) is type(vec[str]) |
| 53 |
|
| 54 |
def test_type_str() -> None: |
| 55 |
assert str(vec[str]) == "<class_proxy 'vec[str]'>" |
| 56 |
assert str(vec[Optional[str]]) == "<class_proxy 'vec[str | None]'>" |
| 57 |
|
| 58 |
def test_construct_empty() -> None: |
| 59 |
v = vec[str]() |
| 60 |
assert len(v) == 0 |
| 61 |
v2 = vec[Optional[str]]() |
| 62 |
assert len(v2) == 0 |
| 63 |
assert type(v) is type(v2) |
| 64 |
|
| 65 |
def test_construct_or_union() -> None: |
| 66 |
if version_info >= (3, 10): |
| 67 |
v = vec[str | None]() |
| 68 |
assert len(v) == 0 |
| 69 |
assert str(v) == 'vec[str | None]([])' |
| 70 |
|
| 71 |
def test_construct_from_initializer() -> None: |
| 72 |
v = vec[str](['xyz']) |
| 73 |
assert len(v) == 1 |
| 74 |
assert v[0] == 'xyz' |
| 75 |
v = vec[str](x + '!' for x in ['x', '', 'foo', 'bar']) |
| 76 |
assert len(v) == 4 |
| 77 |
assert v[0] == 'x!' |
| 78 |
assert v[1] == '!' |
| 79 |
assert v[2] == 'foo!' |
| 80 |
assert v[3] == 'bar!' |
| 81 |
with assertRaises(TypeError): |
| 82 |
vec[str](1) |
| 83 |
with assertRaises(TypeError): |
| 84 |
vec[str]([1]) |
| 85 |
with assertRaises(TypeError): |
| 86 |
vec[str]([None]) |
| 87 |
|
| 88 |
def test_construct_from_tuple() -> None: |
| 89 |
v = vec[str](()) |
| 90 |
assert len(v) == 0 |
| 91 |
v = vec[str](('a', 'bb', 'ccc')) |
| 92 |
assert len(v) == 3 |
| 93 |
assert v[0] == 'a' |
| 94 |
assert v[1] == 'bb' |
| 95 |
assert v[2] == 'ccc' |
| 96 |
with assertRaises(TypeError): |
| 97 |
vec[str]((1,)) |
| 98 |
with assertRaises(TypeError): |
| 99 |
vec[str]((None,)) |
| 100 |
with assertRaises(TypeError): |
| 101 |
vec[str](('a', 1, 'b')) |
| 102 |
|
| 103 |
def test_construct_from_tuple_optional() -> None: |
| 104 |
v = vec[Optional[str]]((None, 'xyz')) |
| 105 |
assert len(v) == 2 |
| 106 |
assert v[0] is None |
| 107 |
assert v[1] == 'xyz' |
| 108 |
v = vec[Optional[str]](('a', None, 'b')) |
| 109 |
assert len(v) == 3 |
| 110 |
assert v[0] == 'a' |
| 111 |
assert v[1] is None |
| 112 |
assert v[2] == 'b' |
| 113 |
|
| 114 |
def test_construct_from_initializer_optional() -> None: |
| 115 |
v = vec[Optional[str]]([None, 'xyz']) |
| 116 |
assert len(v) == 2 |
| 117 |
assert v[0] is None |
| 118 |
assert v[1] == 'xyz' |
| 119 |
v = vec[Optional[str]](x for x in ['x', None, 'foo', 'bar']) |
| 120 |
assert len(v) == 4 |
| 121 |
assert v[0] == 'x' |
| 122 |
assert v[1] is None |
| 123 |
assert v[2] == 'foo' |
| 124 |
assert v[3] == 'bar' |
| 125 |
with assertRaises(TypeError): |
| 126 |
vec[Optional[str]](1) |
| 127 |
with assertRaises(TypeError): |
| 128 |
vec[Optional[str]]([1]) |
| 129 |
|
| 130 |
def test_isinstance() -> None: |
| 131 |
assert isinstance(vec[str](), vec) |
| 132 |
assert isinstance(vec[Optional[str]](), vec) |
| 133 |
|
| 134 |
def test_append() -> None: |
| 135 |
v = vec[str]() |
| 136 |
v = append(v, '1') |
| 137 |
assert str(v) == "vec[str](['1'])" |
| 138 |
for n in range(2, 10): |
| 139 |
v = append(v, str(n)) |
| 140 |
assert str(v) == "vec[str](['1', '2', '3', '4', '5', '6', '7', '8', '9'])" |
| 141 |
|
| 142 |
v2 = vec[Optional[str]]() |
| 143 |
v2 = append(v2, 'x') |
| 144 |
v2 = append(v2, None) |
| 145 |
assert str(v2) == "vec[str | None](['x', None])" |
| 146 |
|
| 147 |
def test_append_error() -> None: |
| 148 |
v: Any = vec[str]() |
| 149 |
with assertRaises(TypeError): |
| 150 |
v = append(v, b'x') |
| 151 |
with assertRaises(TypeError): |
| 152 |
v = append(v, ['x']) |
| 153 |
with assertRaises(TypeError): |
| 154 |
v = append(v, None) |
| 155 |
assert len(v) == 0 |
| 156 |
|
| 157 |
v2: Any = vec[Optional[str]]() |
| 158 |
with assertRaises(TypeError): |
| 159 |
v2 = append(v2, b'x') |
| 160 |
|
| 161 |
def test_repr() -> None: |
| 162 |
assert str(vec[str]()) == "vec[str]([])" |
| 163 |
assert str(vec[Optional[str]]([None, 'x'])) == "vec[str | None]([None, 'x'])" |
| 164 |
|
| 165 |
def test_size() -> None: |
| 166 |
# Only test on 64-bit, non-free-threading builds |
| 167 |
if not is_gil_disabled() and is_64_bit_platform(): |
| 168 |
v = vec[str]() |
| 169 |
assert getsizeof(v) == 48 |
| 170 |
v = append(v, '123') |
| 171 |
assert getsizeof(v) == 48 |
| 172 |
|
| 173 |
def test_get_item() -> None: |
| 174 |
v = append(vec[str](), '44') |
| 175 |
assert v[0] == '44' |
| 176 |
v = append(v, '56') |
| 177 |
assert v[0] == '44' |
| 178 |
assert v[1] == '56' |
| 179 |
|
| 180 |
v2 = vec[Optional[str]]([None, '12']) |
| 181 |
assert v2[0] is None |
| 182 |
assert v2[1] == '12' |
| 183 |
|
| 184 |
def test_get_item_error() -> None: |
| 185 |
v = append(vec[str](), '1') |
| 186 |
v = append(v, '2') |
| 187 |
v = append(v, '3') |
| 188 |
with assertRaises(IndexError): |
| 189 |
v[3] |
| 190 |
with assertRaises(IndexError): |
| 191 |
v[-4] |
| 192 |
n = 2 ** 100 |
| 193 |
with assertRaises(IndexError): |
| 194 |
v[n] |
| 195 |
|
| 196 |
def test_get_item_negative() -> None: |
| 197 |
v = vec[str](['2', '5', '7']) |
| 198 |
assert v[-1] == '7' |
| 199 |
assert v[-2] == '5' |
| 200 |
assert v[-3] == '2' |
| 201 |
with assertRaises(IndexError): |
| 202 |
v[-4] |
| 203 |
with assertRaises(IndexError): |
| 204 |
v[-2**62] |
| 205 |
|
| 206 |
def test_len() -> None: |
| 207 |
v = vec[str]() |
| 208 |
assert len(v) == 0 |
| 209 |
v = append(v, '1') |
| 210 |
assert len(v) == 1 |
| 211 |
v = append(v, '2') |
| 212 |
assert len(v) == 2 |
| 213 |
for i in range(100): |
| 214 |
v = append(v, 'i') |
| 215 |
assert len(v) == i + 3 |
| 216 |
|
| 217 |
def test_set_item() -> None: |
| 218 |
v = vec[str]() |
| 219 |
v = append(v, '1') |
| 220 |
v = append(v, '2') |
| 221 |
v[0] = '3' |
| 222 |
assert v[0] == '3' |
| 223 |
assert v[1] == '2' |
| 224 |
v[1] = '5' |
| 225 |
assert v[0] == '3' |
| 226 |
assert v[1] == '5' |
| 227 |
|
| 228 |
def test_set_item_optional() -> None: |
| 229 |
v = vec[Optional[str]]() |
| 230 |
v = append(v, '1') |
| 231 |
v = append(v, None) |
| 232 |
v[0] = '3' |
| 233 |
assert v[0] == '3' |
| 234 |
assert v[1 + int()] is None # TODO: The '+ int()' is due to mypy type narrowing bug |
| 235 |
v[1] = '5' |
| 236 |
assert v[0] == '3' |
| 237 |
assert v[1] == '5' |
| 238 |
v[1] = None |
| 239 |
assert v[0] == '3' |
| 240 |
assert v[1] is None |
| 241 |
|
| 242 |
def test_set_item_error() -> None: |
| 243 |
v = append(vec[str](), '1') |
| 244 |
v = append(v, '2') |
| 245 |
v = append(v, '3') |
| 246 |
with assertRaises(IndexError): |
| 247 |
v[3] = '1' |
| 248 |
with assertRaises(TypeError): |
| 249 |
v[0] = b'x' |
| 250 |
with assertRaises(TypeError): |
| 251 |
v[0] = vec[str]() |
| 252 |
with assertRaises(TypeError): |
| 253 |
v[0] = None |
| 254 |
assert v[0] == '1' |
| 255 |
|
| 256 |
def test_set_item_error_optional() -> None: |
| 257 |
v = append(vec[Optional[str]](), '1') |
| 258 |
v = append(v, None) |
| 259 |
v = append(v, '3') |
| 260 |
with assertRaises(IndexError): |
| 261 |
v[3] = '1' |
| 262 |
with assertRaises(IndexError): |
| 263 |
v[3] = None |
| 264 |
with assertRaises(IndexError): |
| 265 |
v[-4] = '1' |
| 266 |
with assertRaises(IndexError): |
| 267 |
v[-4] = None |
| 268 |
with assertRaises(TypeError): |
| 269 |
v[0] = b'x' |
| 270 |
with assertRaises(TypeError): |
| 271 |
v[0] = vec[str]() |
| 272 |
with assertRaises(TypeError): |
| 273 |
v[0] = vec[Optional[str]]() |
| 274 |
assert v[0] == '1' |
| 275 |
|
| 276 |
def test_set_item_negative() -> None: |
| 277 |
v = vec[str]() |
| 278 |
v = append(v, '1') |
| 279 |
v = append(v, '2') |
| 280 |
v[-2] = '3' |
| 281 |
assert v[0] == '3' |
| 282 |
assert v[1] == '2' |
| 283 |
v[-1] = '5' |
| 284 |
assert v[0] == '3' |
| 285 |
assert v[1] == '5' |
| 286 |
with assertRaises(IndexError): |
| 287 |
v[-3] = '1' |
| 288 |
|
| 289 |
def test_nested_in_type() -> None: |
| 290 |
cast(List[vec[str]], []) |
| 291 |
|
| 292 |
def test_cannot_overload_isinstance() -> None: |
| 293 |
assert isinstance('', ClassWithCustomMeta) |
| 294 |
v = vec[ClassWithCustomMeta]() |
| 295 |
v = append(v, ClassWithCustomMeta()) |
| 296 |
with assertRaises(TypeError): |
| 297 |
v = append(v, '') |
| 298 |
with assertRaises(TypeError): |
| 299 |
v[0] = '' |
| 300 |
|
| 301 |
def test_add_subclass_instance() -> None: |
| 302 |
v = vec[str]() |
| 303 |
v = append(v, StringSubclass()) |
| 304 |
v[0] = StringSubclass() |
| 305 |
|
| 306 |
v2 = vec[StringSubclass]() |
| 307 |
with assertRaises(TypeError): |
| 308 |
v2 = append(v2, '') |
| 309 |
|
| 310 |
def test_equality() -> None: |
| 311 |
assert_eq(vec[str](), vec[str]()) |
| 312 |
assert_eq(vec[str](['x']), vec[str](['x'])) |
| 313 |
v = vec[str](['x']) |
| 314 |
assert_eq(v, v) |
| 315 |
assert_eq(vec[str](['x', 'y']), vec[str](['x', 'y'])) |
| 316 |
|
| 317 |
assert_neq(vec[str](), vec[str](['x'])) |
| 318 |
assert_neq(vec[str](['x', 'y']), vec[str](['x'])) |
| 319 |
assert_neq(vec[str](['x']), vec[str](['y'])) |
| 320 |
assert_neq(vec[str](['x', 'y']), vec[str](['y', 'y'])) |
| 321 |
assert_neq(vec[str](['x', 'y']), vec[str](['x', 'x'])) |
| 322 |
|
| 323 |
assert_eq(vec[Optional[str]](), vec[Optional[str]]()) |
| 324 |
assert_eq(vec[Optional[str]](['x', None]), vec[Optional[str]](['x', None])) |
| 325 |
assert_neq(vec[Optional[str]](['x', None]), vec[Optional[str]](['x', 'y'])) |
| 326 |
|
| 327 |
def test_equality_different_types() -> None: |
| 328 |
assert_neq(vec[str](['x']), ['x']) |
| 329 |
assert_neq(vec[str](['x']), ('x',)) |
| 330 |
assert_neq(vec[str](['x']), vec[object](['x'])) |
| 331 |
assert_neq(vec[str](), vec[object]()) |
| 332 |
assert_neq(vec[str](), vec[Optional[str]]()) |
| 333 |
|
| 334 |
def test_exception_from_eq() -> None: |
| 335 |
with assertRaises(RuntimeError): |
| 336 |
vec[ClassWithRaisingEq]([ClassWithRaisingEq()]) == vec[ClassWithRaisingEq]([ClassWithRaisingEq()]) |
| 337 |
|
| 338 |
def test_iteration() -> None: |
| 339 |
for x in vec[str](): |
| 340 |
assert False |
| 341 |
a = [] |
| 342 |
for x in vec[str](['x']): |
| 343 |
a.append(x) |
| 344 |
assert a == ['x'] |
| 345 |
a = [] |
| 346 |
for x in vec[str](['11', '23', '45', '64']): |
| 347 |
a.append(x) |
| 348 |
assert a == ['11', '23', '45', '64'] |
| 349 |
a = [] |
| 350 |
for x in vec[Optional[str]](['11', None, '45', '64']): |
| 351 |
a.append(x) |
| 352 |
assert a == ['11', None, '45', '64'] |
| 353 |
|
| 354 |
def test_iterator_protocol() -> None: |
| 355 |
# Test iter() and next() functions explicitly |
| 356 |
v = vec[str](['a', 'b', 'c']) |
| 357 |
it = iter(v) |
| 358 |
assert next(it) == 'a' |
| 359 |
assert next(it) == 'b' |
| 360 |
assert next(it) == 'c' |
| 361 |
with assertRaises(StopIteration): |
| 362 |
next(it) |
| 363 |
|
| 364 |
# Test multiple iterators on same vec |
| 365 |
v = vec[str](['x', 'y']) |
| 366 |
it1 = iter(v) |
| 367 |
it2 = iter(v) |
| 368 |
assert next(it1) == 'x' |
| 369 |
assert next(it2) == 'x' |
| 370 |
assert next(it1) == 'y' |
| 371 |
assert next(it2) == 'y' |
| 372 |
|
| 373 |
# Test empty vec iterator |
| 374 |
it = iter(vec[str]()) |
| 375 |
with assertRaises(StopIteration): |
| 376 |
next(it) |
| 377 |
|
| 378 |
# Test iteration with optional type |
| 379 |
v = vec[Optional[str]](['a', None, 'b']) |
| 380 |
it = iter(v) |
| 381 |
assert next(it) == 'a' |
| 382 |
assert next(it) is None |
| 383 |
assert next(it) == 'b' |
| 384 |
|
| 385 |
def test_iterator_length_hint() -> None: |
| 386 |
v = vec[str](['a', 'b', 'c', 'd', 'e']) |
| 387 |
it = iter(v) |
| 388 |
op: Any = getattr(sys, "modules")["operator"] |
| 389 |
length_hint = op.length_hint |
| 390 |
assert length_hint(it) == 5 |
| 391 |
next(it) |
| 392 |
assert length_hint(it) == 4 |
| 393 |
next(it) |
| 394 |
next(it) |
| 395 |
assert length_hint(it) == 2 |
| 396 |
next(it) |
| 397 |
next(it) |
| 398 |
assert length_hint(it) == 0 |
| 399 |
|
| 400 |
def test_slicing() -> None: |
| 401 |
v = vec[str](str(i) for i in range(5)) |
| 402 |
assert v[1:4] == vec[str](['1', '2', '3']) |
| 403 |
assert v[2:-1] == vec[str](['2', '3']) |
| 404 |
assert v[-2:-1] == vec[str](['3']) |
| 405 |
assert v[1:] == vec[str](['1', '2', '3', '4']) |
| 406 |
assert v[:-1] == vec[str](['0', '1', '2', '3']) |
| 407 |
assert v[:] == v |
| 408 |
assert v[:] is not v |
| 409 |
assert v[0:5] ==v |
| 410 |
assert v[0:5] is not v |
| 411 |
assert v[2:100] == vec[str](['2', '3', '4']) |
| 412 |
assert v[-100:2] == vec[str](['0', '1']) |
| 413 |
assert v[5:100] == vec[str]([]) |
| 414 |
assert v[50:100] == vec[str]([]) |
| 415 |
assert v[-100:-50] == vec[str]([]) |
| 416 |
|
| 417 |
def test_slicing_optional() -> None: |
| 418 |
v = vec[Optional[str]](['x', None, 'y']) |
| 419 |
assert v[:2] == vec[Optional[str]](['x', None]) |
| 420 |
|
| 421 |
def test_slicing_with_step() -> None: |
| 422 |
v = vec[str](str(i) for i in range(10)) |
| 423 |
assert v[1:5:2] == vec[str](['1', '3']) |
| 424 |
assert v[1:6:2] == vec[str](['1', '3', '5']) |
| 425 |
assert v[5:1:-2] == vec[str](['5', '3']) |
| 426 |
assert v[6:1:-2] == vec[str](['6', '4', '2']) |
| 427 |
v = vec[str](str(i) for i in range(5)) |
| 428 |
assert v[::-1] == vec[str](['4', '3', '2', '1', '0']) |
| 429 |
with assertRaises(ValueError): |
| 430 |
v[1:3:0] |
| 431 |
|
| 432 |
def test_contains() -> None: |
| 433 |
v = vec[str](str(i) for i in range(2, 7)) |
| 434 |
for i in range(2, 7): |
| 435 |
assert str(i) in v |
| 436 |
assert 'x' not in v |
| 437 |
assert '22' not in v |
| 438 |
vv = vec[str]() |
| 439 |
assert '3' not in vv |
| 440 |
|
| 441 |
v2 = vec[Optional[str]]() |
| 442 |
assert '3' not in v2 |
| 443 |
assert None not in v2 |
| 444 |
v2 = vec[Optional[str]](['3', None]) |
| 445 |
assert '3' in v2 |
| 446 |
assert None in v2 |
| 447 |
assert '4' not in v2 |
| 448 |
|
| 449 |
def test_contains_identity() -> None: |
| 450 |
# Test that identity check works (short-circuits before __eq__) |
| 451 |
o = ClassWithRaisingEq() |
| 452 |
v = vec[object]([o]) |
| 453 |
assert o in v # Should use identity, not __eq__ |
| 454 |
|
| 455 |
def test_contains_equality() -> None: |
| 456 |
# Test that equality is used when identity doesn't match |
| 457 |
s = 'hello world!' |
| 458 |
v = vec[str]([s]) |
| 459 |
# Create a new string object with same value (different identity) |
| 460 |
s2 = 'hello' + str() + ' world!' |
| 461 |
assert s2 in v |
| 462 |
|
| 463 |
def test_contains_exception_from_eq() -> None: |
| 464 |
o1 = ClassWithRaisingEq() |
| 465 |
o2 = ClassWithRaisingEq() |
| 466 |
v = vec[object]([o1]) |
| 467 |
with assertRaises(RuntimeError): |
| 468 |
o2 in v |
| 469 |
|
| 470 |
def test_contains_wrong_type() -> None: |
| 471 |
# Wrong type should return False, not raise |
| 472 |
v: Any = vec[str](['x']) |
| 473 |
assert 123 not in v |
| 474 |
assert b'x' not in v |
| 475 |
assert None not in v |
| 476 |
assert [] not in v |
| 477 |
|
| 478 |
def test_contains_subclass() -> None: |
| 479 |
v = vec[str]([StringSubclass('abc')]) |
| 480 |
assert 'abc' in v |
| 481 |
assert StringSubclass('abc') in v |
| 482 |
assert 'xyz' not in v |
| 483 |
|
| 484 |
def test_remove() -> None: |
| 485 |
a = ['4x', '7x', '9x'] |
| 486 |
for i in a: |
| 487 |
v = vec[str](a) |
| 488 |
v = remove(v, i) |
| 489 |
assert v == vec[str]([j for j in a if j != i]) |
| 490 |
v = vec[str](a) |
| 491 |
# Make sure we have a different id |
| 492 |
v = remove(v, (i + 'x')[:-1]) |
| 493 |
assert v == vec[str]([j for j in a if j != i]) |
| 494 |
v = vec[str](a) |
| 495 |
v = remove(v, '4x') |
| 496 |
v = remove(v, '7x') |
| 497 |
v = remove(v, '9x') |
| 498 |
assert v == vec[str]() |
| 499 |
with assertRaises(ValueError): |
| 500 |
remove(v, '4') |
| 501 |
v = append(v, '5') |
| 502 |
with assertRaises(ValueError): |
| 503 |
remove(v, '7') |
| 504 |
v = remove(v, '5') |
| 505 |
assert len(v) == 0 |
| 506 |
v = vec[str](['x', 'x', 'x']) |
| 507 |
v = remove(v, 'x') |
| 508 |
assert v == vec[str](['x', 'x']) |
| 509 |
v = remove(v, 'x') |
| 510 |
assert v == vec[str](['x']) |
| 511 |
v = remove(v, 'x') |
| 512 |
assert v == vec[str]() |
| 513 |
b: Any = b'x' |
| 514 |
with assertRaises(TypeError): |
| 515 |
remove(v, b) |
| 516 |
|
| 517 |
def test_remove_optional() -> None: |
| 518 |
v = vec[Optional[str]](['x', None]) |
| 519 |
v = remove(v, 'x') |
| 520 |
assert str(v) == "vec[str | None]([None])" |
| 521 |
v = remove(v, None) |
| 522 |
assert str(v) == "vec[str | None]([])" |
| 523 |
|
| 524 |
def test_pop_last() -> None: |
| 525 |
v = vec[str](['4', '7', '9']) |
| 526 |
v, item = pop(v) |
| 527 |
assert item == '9' |
| 528 |
assert v == vec[str](['4', '7']) |
| 529 |
v, item = pop(v) |
| 530 |
assert item == '7' |
| 531 |
assert v == vec[str](['4']) |
| 532 |
v, item = pop(v) |
| 533 |
assert item == '4' |
| 534 |
assert v == vec[str]() |
| 535 |
with assertRaises(IndexError): |
| 536 |
pop(v) |
| 537 |
|
| 538 |
def test_pop_index() -> None: |
| 539 |
v = vec[str](['4', '7', '9', '15', '22']) |
| 540 |
v, item = pop(v, 0) |
| 541 |
assert item == '4' |
| 542 |
assert v == vec[str](['7', '9', '15', '22']) |
| 543 |
v, item = pop(v, -1) |
| 544 |
assert item == '22' |
| 545 |
assert v == vec[str](['7', '9', '15']) |
| 546 |
v, item = pop(v, 1) |
| 547 |
assert item == '9' |
| 548 |
assert v == vec[str](['7', '15']) |
| 549 |
|
| 550 |
with assertRaises(IndexError): |
| 551 |
pop(v, 2) |
| 552 |
|
| 553 |
with assertRaises(IndexError): |
| 554 |
pop(v, -3) |
| 555 |
|
| 556 |
v, item = pop(v, -2) |
| 557 |
assert item == '7' |
| 558 |
assert v == vec[str](['15']) |
| 559 |
v, item = pop(v, 0) |
| 560 |
assert item == '15' |
| 561 |
assert v == vec[str]() |
| 562 |
|
| 563 |
def test_append_with_alias() -> None: |
| 564 |
v = vec[str]() |
| 565 |
v = append(v, 'a') |
| 566 |
alias = v |
| 567 |
v = append(v, 'b') |
| 568 |
v = append(v, 'c') |
| 569 |
v = append(v, 'd') |
| 570 |
assert alias[0] == 'a' |
| 571 |
assert len(alias) == 1 |
| 572 |
assert v[0] == 'a' |
| 573 |
assert len(v) == 4 |
| 574 |
|
| 575 |
def test_cap_basic() -> None: |
| 576 |
v = vec[str](capacity=3) |
| 577 |
assert len(v) == 0 |
| 578 |
assert v == vec[str]() |
| 579 |
v = vec[str](capacity=0) |
| 580 |
assert len(v) == 0 |
| 581 |
v = vec[str](['a', 'b'], capacity=3) |
| 582 |
assert v == vec[str](['a', 'b']) |
| 583 |
v = vec[str](['x'], capacity=0) |
| 584 |
assert v == vec[str](['x']) |
| 585 |
v = vec[str](['a', 'b', 'c'], capacity=1) |
| 586 |
assert len(v) == 3 |
| 587 |
assert v[0] == 'a' |
| 588 |
|
| 589 |
def test_cap_negative() -> None: |
| 590 |
with assertRaises(ValueError): |
| 591 |
vec[str](capacity=-1) |
| 592 |
with assertRaises(ValueError): |
| 593 |
vec[str](['x'], capacity=-1) |
| 594 |
|
| 595 |
def test_cap_buffer_reuse() -> None: |
| 596 |
v = vec[str](capacity=3) |
| 597 |
old = v |
| 598 |
v = append(v, 'a') |
| 599 |
v = append(v, 'b') |
| 600 |
v = append(v, 'c') |
| 601 |
assert len(v) == 3 |
| 602 |
old = append(old, 'Q') |
| 603 |
assert v[0] == 'Q' |
| 604 |
|
| 605 |
v = vec[str](['a', 'b'], capacity=3) |
| 606 |
old = v |
| 607 |
v = append(v, 'c') |
| 608 |
v[0] = 'z' |
| 609 |
assert old[0] == 'z' |
| 610 |
|
| 611 |
def test_cap_reallocation() -> None: |
| 612 |
# At capacity -> next append reallocates, aliases become independent |
| 613 |
v = vec[str](['a', 'b'], capacity=2) |
| 614 |
old = v |
| 615 |
v = append(v, 'c') |
| 616 |
old = append(old, 'Q') |
| 617 |
assert v[2] == 'c' |
| 618 |
assert old[2] == 'Q' |
| 619 |
|
| 620 |
# Old alias retains original items after reallocation |
| 621 |
v = vec[str](['a', 'b', 'c'], capacity=3) |
| 622 |
old = v |
| 623 |
v = append(v, 'd') |
| 624 |
v[0] = 'z' |
| 625 |
assert old[0] == 'a' |
| 626 |
assert old[2] == 'c' |
| 627 |
|
| 628 |
def test_cap_optional() -> None: |
| 629 |
v = vec[Optional[str]](capacity=3) |
| 630 |
v = append(v, 'x') |
| 631 |
v = append(v, None) |
| 632 |
assert v[0] == 'x' |
| 633 |
assert v[1] is None |
| 634 |
|
| 635 |
v = vec[Optional[str]](['x', None], capacity=3) |
| 636 |
assert v[0] == 'x' |
| 637 |
assert v[1] is None |
| 638 |
|
| 639 |
def test_extend_from_list() -> None: |
| 640 |
v = vec[str](['a', 'b']) |
| 641 |
v = extend(v, ['c', 'd']) |
| 642 |
assert v == vec[str](['a', 'b', 'c', 'd']) |
| 643 |
|
| 644 |
def test_extend_empty_vec() -> None: |
| 645 |
v = vec[str]() |
| 646 |
v = extend(v, ['x', 'y']) |
| 647 |
assert v == vec[str](['x', 'y']) |
| 648 |
|
| 649 |
def test_extend_with_empty_iterable() -> None: |
| 650 |
v = vec[str](['a']) |
| 651 |
v = extend(v, []) |
| 652 |
assert v == vec[str](['a']) |
| 653 |
|
| 654 |
def test_extend_from_vec() -> None: |
| 655 |
v = vec[str](['a']) |
| 656 |
v = extend(v, vec[str](['b', 'c'])) |
| 657 |
assert v == vec[str](['a', 'b', 'c']) |
| 658 |
|
| 659 |
def test_extend_type_error() -> None: |
| 660 |
v = vec[str](['a']) |
| 661 |
n: Any = 123 |
| 662 |
with assertRaises(TypeError): |
| 663 |
extend(v, [n]) |
| 664 |
|
| 665 |
def test_extend_shared_buffer_after_pop() -> None: |
| 666 |
v0 = vec[str](['a', 'b', 'c']) |
| 667 |
v1, _ = pop(v0) |
| 668 |
# v1 and v0 share a buffer |
| 669 |
v = extend(v1, vec[str](['d', 'e', 'f', 'g'])) |
| 670 |
assert v == vec[str](['a', 'b', 'd', 'e', 'f', 'g']) |
| 671 |
# v0 must not be corrupted |
| 672 |
assert v0 == vec[str](['a', 'b', 'c']) |
| 673 |
|
| 674 |
[file helpers.py] |
| 675 |
# Test helper classes |
| 676 |
|
| 677 |
class MetaWithInstanceCheck(type): |
| 678 |
def __instancecheck__(cls, instance): |
| 679 |
return isinstance(instance, str) |
| 680 |
|
| 681 |
class ClassWithCustomMeta(metaclass=MetaWithInstanceCheck): |
| 682 |
pass |