| 1 |
import collections.abc |
| 2 |
import gc |
| 3 |
import pickle |
| 4 |
import random |
| 5 |
import sys |
| 6 |
import weakref |
| 7 |
import pytest |
| 8 |
|
| 9 |
from _immutables_map import Map |
| 10 |
|
| 11 |
|
| 12 |
class HashKey: |
| 13 |
_crasher = None |
| 14 |
|
| 15 |
def __init__(self, hash, name, *, error_on_eq_to=None): |
| 16 |
assert hash != -1 |
| 17 |
self.name = name |
| 18 |
self.hash = hash |
| 19 |
self.error_on_eq_to = error_on_eq_to |
| 20 |
|
| 21 |
def __repr__(self): |
| 22 |
if self._crasher is not None and self._crasher.error_on_repr: |
| 23 |
raise ReprError |
| 24 |
return '<Key name:{} hash:{}>'.format(self.name, self.hash) |
| 25 |
|
| 26 |
def __hash__(self): |
| 27 |
if self._crasher is not None and self._crasher.error_on_hash: |
| 28 |
raise HashingError |
| 29 |
|
| 30 |
return self.hash |
| 31 |
|
| 32 |
def __eq__(self, other): |
| 33 |
if not isinstance(other, HashKey): |
| 34 |
return NotImplemented |
| 35 |
|
| 36 |
if self._crasher is not None and self._crasher.error_on_eq: |
| 37 |
raise EqError |
| 38 |
|
| 39 |
if self.error_on_eq_to is not None and self.error_on_eq_to is other: |
| 40 |
raise ValueError('cannot compare {!r} to {!r}'.format(self, other)) |
| 41 |
if other.error_on_eq_to is not None and other.error_on_eq_to is self: |
| 42 |
raise ValueError('cannot compare {!r} to {!r}'.format(other, self)) |
| 43 |
|
| 44 |
return (self.name, self.hash) == (other.name, other.hash) |
| 45 |
|
| 46 |
|
| 47 |
class KeyStr(str): |
| 48 |
|
| 49 |
def __hash__(self): |
| 50 |
if HashKey._crasher is not None and HashKey._crasher.error_on_hash: |
| 51 |
raise HashingError |
| 52 |
return super().__hash__() |
| 53 |
|
| 54 |
def __eq__(self, other): |
| 55 |
if HashKey._crasher is not None and HashKey._crasher.error_on_eq: |
| 56 |
raise EqError |
| 57 |
return super().__eq__(other) |
| 58 |
|
| 59 |
def __repr__(self, other): |
| 60 |
if HashKey._crasher is not None and HashKey._crasher.error_on_repr: |
| 61 |
raise ReprError |
| 62 |
return super().__eq__(other) |
| 63 |
|
| 64 |
|
| 65 |
class HashKeyCrasher: |
| 66 |
|
| 67 |
def __init__(self, *, error_on_hash=False, error_on_eq=False, |
| 68 |
error_on_repr=False): |
| 69 |
self.error_on_hash = error_on_hash |
| 70 |
self.error_on_eq = error_on_eq |
| 71 |
self.error_on_repr = error_on_repr |
| 72 |
|
| 73 |
def __enter__(self): |
| 74 |
if HashKey._crasher is not None: |
| 75 |
raise RuntimeError('cannot nest crashers') |
| 76 |
HashKey._crasher = self |
| 77 |
|
| 78 |
def __exit__(self, *exc): |
| 79 |
HashKey._crasher = None |
| 80 |
|
| 81 |
|
| 82 |
class HashingError(Exception): |
| 83 |
pass |
| 84 |
|
| 85 |
|
| 86 |
class EqError(Exception): |
| 87 |
pass |
| 88 |
|
| 89 |
|
| 90 |
class ReprError(Exception): |
| 91 |
pass |
| 92 |
|
| 93 |
|
| 94 |
class BaseMapTest: |
| 95 |
|
| 96 |
def test_hashkey_helper_1(self): |
| 97 |
k1 = HashKey(10, 'aaa') |
| 98 |
k2 = HashKey(10, 'bbb') |
| 99 |
|
| 100 |
assert k1 != k2 |
| 101 |
assert hash(k1) == hash(k2) |
| 102 |
|
| 103 |
d = dict() |
| 104 |
d[k1] = 'a' |
| 105 |
d[k2] = 'b' |
| 106 |
|
| 107 |
assert d[k1] == 'a' |
| 108 |
assert d[k2] == 'b' |
| 109 |
|
| 110 |
def test_map_basics_1(self): |
| 111 |
h = self.Map() |
| 112 |
h = None |
| 113 |
|
| 114 |
def test_map_basics_2(self): |
| 115 |
h = self.Map() |
| 116 |
assert len(h) == 0 |
| 117 |
|
| 118 |
h2 = h.set('a', 'b') |
| 119 |
assert h is not h2 |
| 120 |
assert len(h) == 0 |
| 121 |
assert len(h2) == 1 |
| 122 |
|
| 123 |
assert h.get('a') is None |
| 124 |
assert h.get('a', 42) == 42 |
| 125 |
|
| 126 |
assert h2.get('a') == 'b' |
| 127 |
|
| 128 |
h3 = h2.set('b', 10) |
| 129 |
assert h2 is not h3 |
| 130 |
assert len(h) == 0 |
| 131 |
assert len(h2) == 1 |
| 132 |
assert len(h3) == 2 |
| 133 |
assert h3.get('a') == 'b' |
| 134 |
assert h3.get('b') == 10 |
| 135 |
|
| 136 |
assert h.get('b') is None |
| 137 |
assert h2.get('b') is None |
| 138 |
|
| 139 |
assert h.get('a') is None |
| 140 |
assert h2.get('a') == 'b' |
| 141 |
|
| 142 |
h = h2 = h3 = None |
| 143 |
|
| 144 |
def test_map_basics_3(self): |
| 145 |
h = self.Map() |
| 146 |
o = object() |
| 147 |
h1 = h.set('1', o) |
| 148 |
h2 = h1.set('1', o) |
| 149 |
assert h1 is h2 |
| 150 |
|
| 151 |
def test_map_basics_4(self): |
| 152 |
h = self.Map() |
| 153 |
h1 = h.set('key', []) |
| 154 |
h2 = h1.set('key', []) |
| 155 |
assert h1 is not h2 |
| 156 |
assert len(h1) == 1 |
| 157 |
assert len(h2) == 1 |
| 158 |
assert h1.get('key') is not h2.get('key') |
| 159 |
|
| 160 |
def test_map_collision_1(self): |
| 161 |
k1 = HashKey(10, 'aaa') |
| 162 |
k2 = HashKey(10, 'bbb') |
| 163 |
k3 = HashKey(10, 'ccc') |
| 164 |
|
| 165 |
h = self.Map() |
| 166 |
h2 = h.set(k1, 'a') |
| 167 |
h3 = h2.set(k2, 'b') |
| 168 |
|
| 169 |
assert h.get(k1) == None |
| 170 |
assert h.get(k2) == None |
| 171 |
|
| 172 |
assert h2.get(k1) == 'a' |
| 173 |
assert h2.get(k2) == None |
| 174 |
|
| 175 |
assert h3.get(k1) == 'a' |
| 176 |
assert h3.get(k2) == 'b' |
| 177 |
|
| 178 |
h4 = h3.set(k2, 'cc') |
| 179 |
h5 = h4.set(k3, 'aa') |
| 180 |
|
| 181 |
assert h3.get(k1) == 'a' |
| 182 |
assert h3.get(k2) == 'b' |
| 183 |
assert h4.get(k1) == 'a' |
| 184 |
assert h4.get(k2) == 'cc' |
| 185 |
assert h4.get(k3) == None |
| 186 |
assert h5.get(k1) == 'a' |
| 187 |
assert h5.get(k2) == 'cc' |
| 188 |
assert h5.get(k2) == 'cc' |
| 189 |
assert h5.get(k3) == 'aa' |
| 190 |
|
| 191 |
assert len(h) == 0 |
| 192 |
assert len(h2) == 1 |
| 193 |
assert len(h3) == 2 |
| 194 |
assert len(h4) == 2 |
| 195 |
assert len(h5) == 3 |
| 196 |
|
| 197 |
def test_map_collision_2(self): |
| 198 |
A = HashKey(100, 'A') |
| 199 |
B = HashKey(101, 'B') |
| 200 |
C = HashKey(0b011000011100000100, 'C') |
| 201 |
D = HashKey(0b011000011100000100, 'D') |
| 202 |
E = HashKey(0b1011000011100000100, 'E') |
| 203 |
|
| 204 |
h = self.Map() |
| 205 |
h = h.set(A, 'a') |
| 206 |
h = h.set(B, 'b') |
| 207 |
h = h.set(C, 'c') |
| 208 |
h = h.set(D, 'd') |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
h = h.set(E, 'e') |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
def test_map_stress(self): |
| 242 |
COLLECTION_SIZE = 7000 |
| 243 |
TEST_ITERS_EVERY = 647 |
| 244 |
CRASH_HASH_EVERY = 97 |
| 245 |
CRASH_EQ_EVERY = 11 |
| 246 |
RUN_XTIMES = 3 |
| 247 |
|
| 248 |
for _ in range(RUN_XTIMES): |
| 249 |
h = self.Map() |
| 250 |
d = dict() |
| 251 |
|
| 252 |
for i in range(COLLECTION_SIZE): |
| 253 |
key = KeyStr(i) |
| 254 |
|
| 255 |
if not (i % CRASH_HASH_EVERY): |
| 256 |
with HashKeyCrasher(error_on_hash=True): |
| 257 |
with pytest.raises(HashingError): |
| 258 |
h.set(key, i) |
| 259 |
|
| 260 |
h = h.set(key, i) |
| 261 |
|
| 262 |
if not (i % CRASH_EQ_EVERY): |
| 263 |
with HashKeyCrasher(error_on_eq=True): |
| 264 |
with pytest.raises(EqError): |
| 265 |
h.get(KeyStr(i)) |
| 266 |
|
| 267 |
d[key] = i |
| 268 |
assert len(d) == len(h) |
| 269 |
|
| 270 |
if not (i % TEST_ITERS_EVERY): |
| 271 |
assert set(h.items()) == set(d.items()) |
| 272 |
assert len(h.items()) == len(d.items()) |
| 273 |
|
| 274 |
assert len(h) == COLLECTION_SIZE |
| 275 |
|
| 276 |
for key in range(COLLECTION_SIZE): |
| 277 |
assert h.get(KeyStr(key), 'not found') == key |
| 278 |
|
| 279 |
keys_to_delete = list(range(COLLECTION_SIZE)) |
| 280 |
random.shuffle(keys_to_delete) |
| 281 |
for iter_i, i in enumerate(keys_to_delete): |
| 282 |
key = KeyStr(i) |
| 283 |
|
| 284 |
if not (iter_i % CRASH_HASH_EVERY): |
| 285 |
with HashKeyCrasher(error_on_hash=True): |
| 286 |
with pytest.raises(HashingError): |
| 287 |
h.delete(key) |
| 288 |
|
| 289 |
if not (iter_i % CRASH_EQ_EVERY): |
| 290 |
with HashKeyCrasher(error_on_eq=True): |
| 291 |
with pytest.raises(EqError): |
| 292 |
h.delete(KeyStr(i)) |
| 293 |
|
| 294 |
h = h.delete(key) |
| 295 |
assert h.get(key, 'not found') == 'not found' |
| 296 |
del d[key] |
| 297 |
assert len(d) == len(h) |
| 298 |
|
| 299 |
if iter_i == COLLECTION_SIZE // 2: |
| 300 |
hm = h |
| 301 |
dm = d.copy() |
| 302 |
|
| 303 |
if not (iter_i % TEST_ITERS_EVERY): |
| 304 |
assert set(h.keys()) == set(d.keys()) |
| 305 |
assert len(h.keys()) == len(d.keys()) |
| 306 |
|
| 307 |
assert len(d) == 0 |
| 308 |
assert len(h) == 0 |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
for key in dm: |
| 313 |
assert hm.get(str(key)) == dm[key] |
| 314 |
assert len(dm) == len(hm) |
| 315 |
|
| 316 |
for i, key in enumerate(keys_to_delete): |
| 317 |
if str(key) in dm: |
| 318 |
hm = hm.delete(str(key)) |
| 319 |
dm.pop(str(key)) |
| 320 |
assert hm.get(str(key), 'not found') == 'not found' |
| 321 |
assert len(d) == len(h) |
| 322 |
|
| 323 |
if not (i % TEST_ITERS_EVERY): |
| 324 |
assert set(h.values()) == set(d.values()) |
| 325 |
assert len(h.values()) == len(d.values()) |
| 326 |
|
| 327 |
assert len(d) == 0 |
| 328 |
assert len(h) == 0 |
| 329 |
assert list(h.items()) == [] |
| 330 |
|
| 331 |
def test_map_delete_1(self): |
| 332 |
A = HashKey(100, 'A') |
| 333 |
B = HashKey(101, 'B') |
| 334 |
C = HashKey(102, 'C') |
| 335 |
D = HashKey(103, 'D') |
| 336 |
E = HashKey(104, 'E') |
| 337 |
Z = HashKey(-100, 'Z') |
| 338 |
|
| 339 |
Er = HashKey(103, 'Er', error_on_eq_to=D) |
| 340 |
|
| 341 |
h = self.Map() |
| 342 |
h = h.set(A, 'a') |
| 343 |
h = h.set(A, 'a') |
| 344 |
h = h.set(B, 'b') |
| 345 |
h = h.set(C, 'c') |
| 346 |
h = h.set(D, 'd') |
| 347 |
h = h.set(E, 'e') |
| 348 |
|
| 349 |
orig_len = len(h) |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
h = h.delete(C) |
| 359 |
assert len(h) == orig_len - 1 |
| 360 |
|
| 361 |
with pytest.raises(ValueError, match='cannot compare'): |
| 362 |
h.delete(Er) |
| 363 |
|
| 364 |
h = h.delete(D) |
| 365 |
assert len(h) == orig_len - 2 |
| 366 |
|
| 367 |
with pytest.raises(KeyError) as ex: |
| 368 |
h.delete(Z) |
| 369 |
assert ex.value.args[0] is Z |
| 370 |
|
| 371 |
h = h.delete(A) |
| 372 |
assert len(h) == orig_len - 3 |
| 373 |
|
| 374 |
assert h.get(A, 42) == 42 |
| 375 |
assert h.get(B) == 'b' |
| 376 |
assert h.get(E) == 'e' |
| 377 |
|
| 378 |
def test_map_delete_2(self): |
| 379 |
A = HashKey(100, 'A') |
| 380 |
B = HashKey(201001, 'B') |
| 381 |
C = HashKey(101001, 'C') |
| 382 |
BLike = HashKey(201001, 'B-like') |
| 383 |
D = HashKey(103, 'D') |
| 384 |
E = HashKey(104, 'E') |
| 385 |
Z = HashKey(-100, 'Z') |
| 386 |
|
| 387 |
Er = HashKey(201001, 'Er', error_on_eq_to=B) |
| 388 |
|
| 389 |
h = self.Map() |
| 390 |
h = h.set(A, 'a') |
| 391 |
h = h.set(B, 'b') |
| 392 |
h = h.set(C, 'c') |
| 393 |
h = h.set(D, 'd') |
| 394 |
h = h.set(E, 'e') |
| 395 |
|
| 396 |
h = h.set(B, 'b') |
| 397 |
|
| 398 |
with pytest.raises(KeyError): |
| 399 |
h.delete(BLike) |
| 400 |
|
| 401 |
orig_len = len(h) |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
with pytest.raises(ValueError, match='cannot compare'): |
| 413 |
h.delete(Er) |
| 414 |
|
| 415 |
with pytest.raises(KeyError) as ex: |
| 416 |
h.delete(Z) |
| 417 |
assert ex.value.args[0] is Z |
| 418 |
assert len(h) == orig_len |
| 419 |
|
| 420 |
h = h.delete(C) |
| 421 |
assert len(h) == orig_len - 1 |
| 422 |
|
| 423 |
h = h.delete(B) |
| 424 |
assert len(h) == orig_len - 2 |
| 425 |
|
| 426 |
h = h.delete(A) |
| 427 |
assert len(h) == orig_len - 3 |
| 428 |
|
| 429 |
assert h.get(D) == 'd' |
| 430 |
assert h.get(E) == 'e' |
| 431 |
|
| 432 |
with pytest.raises(KeyError): |
| 433 |
h = h.delete(A) |
| 434 |
with pytest.raises(KeyError): |
| 435 |
h = h.delete(B) |
| 436 |
h = h.delete(D) |
| 437 |
h = h.delete(E) |
| 438 |
assert len(h) == 0 |
| 439 |
|
| 440 |
def test_map_delete_3(self): |
| 441 |
A = HashKey(0b00000000001100100, 'A') |
| 442 |
B = HashKey(0b00000000001100101, 'B') |
| 443 |
|
| 444 |
C = HashKey(0b11000011100000100, 'C') |
| 445 |
D = HashKey(0b11000011100000100, 'D') |
| 446 |
X = HashKey(0b01000011100000100, 'Z') |
| 447 |
Y = HashKey(0b11000011100000100, 'Y') |
| 448 |
|
| 449 |
E = HashKey(0b00000000001101000, 'E') |
| 450 |
|
| 451 |
h = self.Map() |
| 452 |
h = h.set(A, 'a') |
| 453 |
h = h.set(B, 'b') |
| 454 |
h = h.set(C, 'c') |
| 455 |
h = h.set(D, 'd') |
| 456 |
h = h.set(E, 'e') |
| 457 |
|
| 458 |
assert len(h) == 5 |
| 459 |
h = h.set(C, 'c') |
| 460 |
assert len(h) == 5 |
| 461 |
|
| 462 |
orig_len = len(h) |
| 463 |
|
| 464 |
with pytest.raises(KeyError): |
| 465 |
h.delete(X) |
| 466 |
with pytest.raises(KeyError): |
| 467 |
h.delete(Y) |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
h = h.delete(A) |
| 481 |
assert len(h) == orig_len - 1 |
| 482 |
|
| 483 |
h = h.delete(E) |
| 484 |
assert len(h) == orig_len - 2 |
| 485 |
|
| 486 |
assert h.get(C) == 'c' |
| 487 |
assert h.get(B) == 'b' |
| 488 |
|
| 489 |
h2 = h.delete(C) |
| 490 |
assert len(h2) == orig_len - 3 |
| 491 |
|
| 492 |
h2 = h.delete(D) |
| 493 |
assert len(h2) == orig_len - 3 |
| 494 |
|
| 495 |
assert len(h) == orig_len - 2 |
| 496 |
|
| 497 |
def test_map_delete_4(self): |
| 498 |
A = HashKey(100, 'A') |
| 499 |
B = HashKey(101, 'B') |
| 500 |
C = HashKey(100100, 'C') |
| 501 |
D = HashKey(100100, 'D') |
| 502 |
E = HashKey(100100, 'E') |
| 503 |
|
| 504 |
h = self.Map() |
| 505 |
h = h.set(A, 'a') |
| 506 |
h = h.set(B, 'b') |
| 507 |
h = h.set(C, 'c') |
| 508 |
h = h.set(D, 'd') |
| 509 |
h = h.set(E, 'e') |
| 510 |
|
| 511 |
orig_len = len(h) |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
h = h.delete(D) |
| 525 |
assert len(h) == orig_len - 1 |
| 526 |
|
| 527 |
h = h.delete(E) |
| 528 |
assert len(h) == orig_len - 2 |
| 529 |
|
| 530 |
h = h.delete(C) |
| 531 |
assert len(h) == orig_len - 3 |
| 532 |
|
| 533 |
h = h.delete(A) |
| 534 |
assert len(h) == orig_len - 4 |
| 535 |
|
| 536 |
h = h.delete(B) |
| 537 |
assert len(h) == 0 |
| 538 |
|
| 539 |
def test_map_delete_5(self): |
| 540 |
h = self.Map() |
| 541 |
|
| 542 |
keys = [] |
| 543 |
for i in range(17): |
| 544 |
key = HashKey(i, str(i)) |
| 545 |
keys.append(key) |
| 546 |
h = h.set(key, 'val-{}'.format(i)) |
| 547 |
|
| 548 |
collision_key16 = HashKey(16, '18') |
| 549 |
h = h.set(collision_key16, 'collision') |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
assert len(h) == 18 |
| 570 |
|
| 571 |
h = h.delete(keys[2]) |
| 572 |
assert len(h) == 17 |
| 573 |
|
| 574 |
h = h.delete(collision_key16) |
| 575 |
assert len(h) == 16 |
| 576 |
h = h.delete(keys[16]) |
| 577 |
assert len(h) == 15 |
| 578 |
|
| 579 |
h = h.delete(keys[1]) |
| 580 |
assert len(h) == 14 |
| 581 |
with pytest.raises(KeyError) as ex: |
| 582 |
h.delete(keys[1]) |
| 583 |
assert ex.value.args[0] is keys[1] |
| 584 |
assert len(h) == 14 |
| 585 |
|
| 586 |
for key in keys: |
| 587 |
if key in h: |
| 588 |
h = h.delete(key) |
| 589 |
assert len(h) == 0 |
| 590 |
|
| 591 |
def test_map_delete_6(self): |
| 592 |
h = self.Map() |
| 593 |
h = h.set(1, 1) |
| 594 |
h = h.delete(1) |
| 595 |
assert len(h) == 0 |
| 596 |
assert h == self.Map() |
| 597 |
|
| 598 |
def test_map_items_1(self): |
| 599 |
A = HashKey(100, 'A') |
| 600 |
B = HashKey(201001, 'B') |
| 601 |
C = HashKey(101001, 'C') |
| 602 |
D = HashKey(103, 'D') |
| 603 |
E = HashKey(104, 'E') |
| 604 |
F = HashKey(110, 'F') |
| 605 |
|
| 606 |
h = self.Map() |
| 607 |
h = h.set(A, 'a') |
| 608 |
h = h.set(B, 'b') |
| 609 |
h = h.set(C, 'c') |
| 610 |
h = h.set(D, 'd') |
| 611 |
h = h.set(E, 'e') |
| 612 |
h = h.set(F, 'f') |
| 613 |
|
| 614 |
it = h.items() |
| 615 |
assert set(list(it)) == \ |
| 616 |
{(A, 'a'), (B, 'b'), (C, 'c'), (D, 'd'), (E, 'e'), (F, 'f')} |
| 617 |
|
| 618 |
def test_map_items_2(self): |
| 619 |
A = HashKey(100, 'A') |
| 620 |
B = HashKey(101, 'B') |
| 621 |
C = HashKey(100100, 'C') |
| 622 |
D = HashKey(100100, 'D') |
| 623 |
E = HashKey(100100, 'E') |
| 624 |
F = HashKey(110, 'F') |
| 625 |
|
| 626 |
h = self.Map() |
| 627 |
h = h.set(A, 'a') |
| 628 |
h = h.set(B, 'b') |
| 629 |
h = h.set(C, 'c') |
| 630 |
h = h.set(D, 'd') |
| 631 |
h = h.set(E, 'e') |
| 632 |
h = h.set(F, 'f') |
| 633 |
|
| 634 |
it = h.items() |
| 635 |
assert set(list(it)) == \ |
| 636 |
{(A, 'a'), (B, 'b'), (C, 'c'), (D, 'd'), (E, 'e'), (F, 'f')} |
| 637 |
|
| 638 |
def test_map_items_3(self): |
| 639 |
h = self.Map() |
| 640 |
assert len(h.items()) == 0 |
| 641 |
assert list(h.items()) == [] |
| 642 |
|
| 643 |
def test_map_items_4(self): |
| 644 |
h = self.Map(a=1, b=2, c=3) |
| 645 |
k = h.items() |
| 646 |
assert set(k) == {('a', 1), ('b', 2), ('c', 3)} |
| 647 |
assert set(k) == {('a', 1), ('b', 2), ('c', 3)} |
| 648 |
|
| 649 |
def test_map_keys_1(self): |
| 650 |
A = HashKey(100, 'A') |
| 651 |
B = HashKey(101, 'B') |
| 652 |
C = HashKey(100100, 'C') |
| 653 |
D = HashKey(100100, 'D') |
| 654 |
E = HashKey(100100, 'E') |
| 655 |
F = HashKey(110, 'F') |
| 656 |
|
| 657 |
h = self.Map() |
| 658 |
h = h.set(A, 'a') |
| 659 |
h = h.set(B, 'b') |
| 660 |
h = h.set(C, 'c') |
| 661 |
h = h.set(D, 'd') |
| 662 |
h = h.set(E, 'e') |
| 663 |
h = h.set(F, 'f') |
| 664 |
|
| 665 |
assert set(list(h.keys())) == {A, B, C, D, E, F} |
| 666 |
assert set(list(h)) == {A, B, C, D, E, F} |
| 667 |
|
| 668 |
def test_map_keys_2(self): |
| 669 |
h = self.Map(a=1, b=2, c=3) |
| 670 |
k = h.keys() |
| 671 |
assert set(k) == {'a', 'b', 'c'} |
| 672 |
assert set(k) == {'a', 'b', 'c'} |
| 673 |
|
| 674 |
def test_map_values_1(self): |
| 675 |
A = HashKey(100, 'A') |
| 676 |
B = HashKey(101, 'B') |
| 677 |
C = HashKey(100100, 'C') |
| 678 |
D = HashKey(100100, 'D') |
| 679 |
E = HashKey(100100, 'E') |
| 680 |
F = HashKey(110, 'F') |
| 681 |
|
| 682 |
h = self.Map() |
| 683 |
h = h.set(A, 'a') |
| 684 |
h = h.set(B, 'b') |
| 685 |
h = h.set(C, 'c') |
| 686 |
h = h.set(D, 'd') |
| 687 |
h = h.set(E, 'e') |
| 688 |
h = h.set(F, 'f') |
| 689 |
|
| 690 |
assert set(list(h.values())) == {'a', 'b', 'c', 'd', 'e', 'f'} |
| 691 |
|
| 692 |
def test_map_values_2(self): |
| 693 |
h = self.Map(a=1, b=2, c=3) |
| 694 |
k = h.values() |
| 695 |
assert set(k) == {1, 2, 3} |
| 696 |
assert set(k) == {1, 2, 3} |
| 697 |
|
| 698 |
def test_map_eq_1(self): |
| 699 |
A = HashKey(100, 'A') |
| 700 |
B = HashKey(101, 'B') |
| 701 |
C = HashKey(100100, 'C') |
| 702 |
D = HashKey(100100, 'D') |
| 703 |
E = HashKey(120, 'E') |
| 704 |
|
| 705 |
h1 = self.Map() |
| 706 |
h1 = h1.set(A, 'a') |
| 707 |
h1 = h1.set(B, 'b') |
| 708 |
h1 = h1.set(C, 'c') |
| 709 |
h1 = h1.set(D, 'd') |
| 710 |
|
| 711 |
h2 = self.Map() |
| 712 |
h2 = h2.set(A, 'a') |
| 713 |
|
| 714 |
assert not (h1 == h2) |
| 715 |
assert h1 != h2 |
| 716 |
|
| 717 |
h2 = h2.set(B, 'b') |
| 718 |
assert not (h1 == h2) |
| 719 |
assert h1 != h2 |
| 720 |
|
| 721 |
h2 = h2.set(C, 'c') |
| 722 |
assert not (h1 == h2) |
| 723 |
assert h1 != h2 |
| 724 |
|
| 725 |
h2 = h2.set(D, 'd2') |
| 726 |
assert not (h1 == h2) |
| 727 |
assert h1 != h2 |
| 728 |
|
| 729 |
h2 = h2.set(D, 'd') |
| 730 |
assert h1 == h2 |
| 731 |
assert not (h1 != h2) |
| 732 |
|
| 733 |
h2 = h2.set(E, 'e') |
| 734 |
assert not (h1 == h2) |
| 735 |
assert h1 != h2 |
| 736 |
|
| 737 |
h2 = h2.delete(D) |
| 738 |
assert not (h1 == h2) |
| 739 |
assert h1 != h2 |
| 740 |
|
| 741 |
h2 = h2.set(E, 'd') |
| 742 |
assert not (h1 == h2) |
| 743 |
assert h1 != h2 |
| 744 |
|
| 745 |
def test_map_eq_2(self): |
| 746 |
A = HashKey(100, 'A') |
| 747 |
Er = HashKey(100, 'Er', error_on_eq_to=A) |
| 748 |
|
| 749 |
h1 = self.Map() |
| 750 |
h1 = h1.set(A, 'a') |
| 751 |
|
| 752 |
h2 = self.Map() |
| 753 |
h2 = h2.set(Er, 'a') |
| 754 |
|
| 755 |
with pytest.raises(ValueError, match='cannot compare'): |
| 756 |
h1 == h2 |
| 757 |
|
| 758 |
with pytest.raises(ValueError, match='cannot compare'): |
| 759 |
h1 != h2 |
| 760 |
|
| 761 |
def test_map_eq_3(self): |
| 762 |
assert self.Map() != 1 |
| 763 |
|
| 764 |
def test_map_gc_1(self): |
| 765 |
A = HashKey(100, 'A') |
| 766 |
|
| 767 |
h = self.Map() |
| 768 |
h = h.set(0, 0) |
| 769 |
ref = weakref.ref(h) |
| 770 |
|
| 771 |
a = [] |
| 772 |
a.append(a) |
| 773 |
a.append(h) |
| 774 |
b = [] |
| 775 |
a.append(b) |
| 776 |
b.append(a) |
| 777 |
h = h.set(A, b) |
| 778 |
|
| 779 |
del h, a, b |
| 780 |
|
| 781 |
gc.collect() |
| 782 |
gc.collect() |
| 783 |
gc.collect() |
| 784 |
|
| 785 |
assert ref() is None |
| 786 |
|
| 787 |
def test_map_gc_2(self): |
| 788 |
A = HashKey(100, 'A') |
| 789 |
|
| 790 |
h = self.Map() |
| 791 |
h = h.set(A, 'a') |
| 792 |
h = h.set(A, h) |
| 793 |
|
| 794 |
ref = weakref.ref(h) |
| 795 |
hi = iter(h.items()) |
| 796 |
next(hi) |
| 797 |
|
| 798 |
del h, hi |
| 799 |
|
| 800 |
gc.collect() |
| 801 |
gc.collect() |
| 802 |
gc.collect() |
| 803 |
|
| 804 |
assert ref() is None |
| 805 |
|
| 806 |
def test_map_in_1(self): |
| 807 |
A = HashKey(100, 'A') |
| 808 |
AA = HashKey(100, 'A') |
| 809 |
|
| 810 |
B = HashKey(101, 'B') |
| 811 |
|
| 812 |
h = self.Map() |
| 813 |
h = h.set(A, 1) |
| 814 |
|
| 815 |
assert A in h |
| 816 |
assert not (B in h) |
| 817 |
|
| 818 |
with pytest.raises(EqError): |
| 819 |
with HashKeyCrasher(error_on_eq=True): |
| 820 |
AA in h |
| 821 |
|
| 822 |
with pytest.raises(HashingError): |
| 823 |
with HashKeyCrasher(error_on_hash=True): |
| 824 |
AA in h |
| 825 |
|
| 826 |
def test_map_getitem_1(self): |
| 827 |
A = HashKey(100, 'A') |
| 828 |
AA = HashKey(100, 'A') |
| 829 |
|
| 830 |
B = HashKey(101, 'B') |
| 831 |
|
| 832 |
h = self.Map() |
| 833 |
h = h.set(A, 1) |
| 834 |
|
| 835 |
assert h[A] == 1 |
| 836 |
assert h[AA] == 1 |
| 837 |
|
| 838 |
with pytest.raises(KeyError): |
| 839 |
h[B] |
| 840 |
|
| 841 |
with pytest.raises(EqError): |
| 842 |
with HashKeyCrasher(error_on_eq=True): |
| 843 |
h[AA] |
| 844 |
|
| 845 |
with pytest.raises(HashingError): |
| 846 |
with HashKeyCrasher(error_on_hash=True): |
| 847 |
h[AA] |
| 848 |
|
| 849 |
def test_repr_1(self): |
| 850 |
h = self.Map() |
| 851 |
assert repr(h).startswith('<immutables.Map({}) at 0x') |
| 852 |
|
| 853 |
h = h.set(1, 2).set(2, 3).set(3, 4) |
| 854 |
assert repr(h).startswith( |
| 855 |
'<immutables.Map({1: 2, 2: 3, 3: 4}) at 0x') |
| 856 |
|
| 857 |
def test_repr_2(self): |
| 858 |
h = self.Map() |
| 859 |
A = HashKey(100, 'A') |
| 860 |
|
| 861 |
with pytest.raises(ReprError): |
| 862 |
with HashKeyCrasher(error_on_repr=True): |
| 863 |
repr(h.set(1, 2).set(A, 3).set(3, 4)) |
| 864 |
|
| 865 |
with pytest.raises(ReprError): |
| 866 |
with HashKeyCrasher(error_on_repr=True): |
| 867 |
repr(h.set(1, 2).set(2, A).set(3, 4)) |
| 868 |
|
| 869 |
def test_repr_3(self): |
| 870 |
class Key: |
| 871 |
def __init__(self): |
| 872 |
self.val = None |
| 873 |
|
| 874 |
def __hash__(self): |
| 875 |
return 123 |
| 876 |
|
| 877 |
def __repr__(self): |
| 878 |
return repr(self.val) |
| 879 |
|
| 880 |
h = self.Map() |
| 881 |
k = Key() |
| 882 |
h = h.set(k, 1) |
| 883 |
k.val = h |
| 884 |
|
| 885 |
assert repr(h).startswith( |
| 886 |
'<immutables.Map({{...}: 1}) at 0x') |
| 887 |
|
| 888 |
def test_hash_1(self): |
| 889 |
h = self.Map() |
| 890 |
assert hash(h) != -1 |
| 891 |
assert hash(h) == hash(h) |
| 892 |
|
| 893 |
h = h.set(1, 2).set('a', 'b') |
| 894 |
assert hash(h) != -1 |
| 895 |
assert hash(h) == hash(h) |
| 896 |
|
| 897 |
assert hash(h.set(1, 2).set('a', 'b')) == \ |
| 898 |
hash(h.set('a', 'b').set(1, 2)) |
| 899 |
|
| 900 |
def test_hash_2(self): |
| 901 |
h = self.Map() |
| 902 |
A = HashKey(100, 'A') |
| 903 |
|
| 904 |
m = h.set(1, 2).set(A, 3).set(3, 4) |
| 905 |
with pytest.raises(HashingError): |
| 906 |
with HashKeyCrasher(error_on_hash=True): |
| 907 |
hash(m) |
| 908 |
|
| 909 |
m = h.set(1, 2).set(2, A).set(3, 4) |
| 910 |
with pytest.raises(HashingError): |
| 911 |
with HashKeyCrasher(error_on_hash=True): |
| 912 |
hash(m) |
| 913 |
|
| 914 |
def test_abc_1(self): |
| 915 |
assert issubclass(self.Map, collections.abc.Mapping) |
| 916 |
|
| 917 |
def test_map_mut_1(self): |
| 918 |
h = self.Map() |
| 919 |
h = h.set('a', 1) |
| 920 |
|
| 921 |
hm1 = h.mutate() |
| 922 |
hm2 = h.mutate() |
| 923 |
|
| 924 |
assert not isinstance(hm1, self.Map) |
| 925 |
|
| 926 |
assert hm1 is not hm2 |
| 927 |
assert hm1['a'] == 1 |
| 928 |
assert hm2['a'] == 1 |
| 929 |
|
| 930 |
hm1.set('b', 2) |
| 931 |
hm1.set('c', 3) |
| 932 |
|
| 933 |
hm2.set('x', 100) |
| 934 |
hm2.set('a', 1000) |
| 935 |
|
| 936 |
assert hm1['a'] == 1 |
| 937 |
assert hm1.get('x', -1) == -1 |
| 938 |
|
| 939 |
assert hm2['a'] == 1000 |
| 940 |
assert 'x' in hm2 |
| 941 |
|
| 942 |
h1 = hm1.finish() |
| 943 |
h2 = hm2.finish() |
| 944 |
|
| 945 |
assert isinstance(h1, self.Map) |
| 946 |
|
| 947 |
assert dict(h.items()) == {'a': 1} |
| 948 |
assert dict(h1.items()) == {'a': 1, 'b': 2, 'c': 3} |
| 949 |
assert dict(h2.items()) == {'a': 1000, 'x': 100} |
| 950 |
|
| 951 |
def test_map_mut_2(self): |
| 952 |
h = self.Map() |
| 953 |
h = h.set('a', 1) |
| 954 |
|
| 955 |
hm1 = h.mutate() |
| 956 |
hm1.set('a', 2) |
| 957 |
hm1.set('a', 3) |
| 958 |
hm1.set('a', 4) |
| 959 |
h2 = hm1.finish() |
| 960 |
|
| 961 |
assert dict(h.items()) == {'a': 1} |
| 962 |
assert dict(h2.items()) == {'a': 4} |
| 963 |
|
| 964 |
def test_map_mut_3(self): |
| 965 |
h = self.Map() |
| 966 |
h = h.set('a', 1) |
| 967 |
hm1 = h.mutate() |
| 968 |
|
| 969 |
assert repr(hm1).startswith( |
| 970 |
"<immutables.MapMutation({'a': 1})") |
| 971 |
|
| 972 |
with pytest.raises(TypeError, match='unhashable type'): |
| 973 |
hash(hm1) |
| 974 |
|
| 975 |
def test_map_mut_4(self): |
| 976 |
h = self.Map() |
| 977 |
h = h.set('a', 1) |
| 978 |
h = h.set('b', 2) |
| 979 |
|
| 980 |
hm1 = h.mutate() |
| 981 |
hm2 = h.mutate() |
| 982 |
|
| 983 |
assert hm1 == hm2 |
| 984 |
|
| 985 |
hm1.set('a', 10) |
| 986 |
assert hm1 != hm2 |
| 987 |
|
| 988 |
hm2.set('a', 10) |
| 989 |
assert hm1 == hm2 |
| 990 |
|
| 991 |
assert hm2.pop('a') == 10 |
| 992 |
assert hm1 != hm2 |
| 993 |
|
| 994 |
def test_map_mut_5(self): |
| 995 |
h = self.Map({'a': 1, 'b': 2}, z=100) |
| 996 |
assert isinstance(h, self.Map) |
| 997 |
assert dict(h.items()) == {'a': 1, 'b': 2, 'z': 100} |
| 998 |
|
| 999 |
h2 = h.update(z=200, y=-1) |
| 1000 |
assert dict(h.items()) == {'a': 1, 'b': 2, 'z': 100} |
| 1001 |
assert dict(h2.items()) == {'a': 1, 'b': 2, 'z': 200, 'y': -1} |
| 1002 |
|
| 1003 |
h3 = h2.update([(1, 2), (3, 4)]) |
| 1004 |
assert dict(h.items()) == {'a': 1, 'b': 2, 'z': 100} |
| 1005 |
assert dict(h2.items()) == {'a': 1, 'b': 2, 'z': 200, 'y': -1} |
| 1006 |
assert dict(h3.items()) == \ |
| 1007 |
{'a': 1, 'b': 2, 'z': 200, 'y': -1, 1: 2, 3: 4} |
| 1008 |
|
| 1009 |
h4 = h3.update() |
| 1010 |
assert h4 is h3 |
| 1011 |
|
| 1012 |
h5 = h4.update(self.Map({'zzz': 'yyz'})) |
| 1013 |
|
| 1014 |
assert dict(h5.items()) == \ |
| 1015 |
{'a': 1, 'b': 2, 'z': 200, 'y': -1, 1: 2, 3: 4, |
| 1016 |
'zzz': 'yyz'} |
| 1017 |
|
| 1018 |
def test_map_mut_6(self): |
| 1019 |
h = self.Map({'a': 1, 'b': 2}, z=100) |
| 1020 |
assert dict(h.items()) == {'a': 1, 'b': 2, 'z': 100} |
| 1021 |
|
| 1022 |
with pytest.raises(TypeError, match='not iterable'): |
| 1023 |
h.update(1) |
| 1024 |
|
| 1025 |
with pytest.raises(ValueError, match='map update sequence element'): |
| 1026 |
h.update([(1, 2), (3, 4, 5)]) |
| 1027 |
|
| 1028 |
with pytest.raises(TypeError, match='cannot convert map update'): |
| 1029 |
h.update([(1, 2), 1]) |
| 1030 |
|
| 1031 |
assert dict(h.items()) == {'a': 1, 'b': 2, 'z': 100} |
| 1032 |
|
| 1033 |
def test_map_mut_7(self): |
| 1034 |
key = HashKey(123, 'aaa') |
| 1035 |
|
| 1036 |
h = self.Map({'a': 1, 'b': 2}, z=100) |
| 1037 |
assert dict(h.items()) == {'a': 1, 'b': 2, 'z': 100} |
| 1038 |
|
| 1039 |
upd = {key: 1} |
| 1040 |
with HashKeyCrasher(error_on_hash=True): |
| 1041 |
with pytest.raises(HashingError): |
| 1042 |
h.update(upd) |
| 1043 |
|
| 1044 |
upd = self.Map({key: 'zzz'}) |
| 1045 |
with HashKeyCrasher(error_on_hash=True): |
| 1046 |
with pytest.raises(HashingError): |
| 1047 |
h.update(upd) |
| 1048 |
|
| 1049 |
upd = [(1, 2), (key, 'zzz')] |
| 1050 |
with HashKeyCrasher(error_on_hash=True): |
| 1051 |
with pytest.raises(HashingError): |
| 1052 |
h.update(upd) |
| 1053 |
|
| 1054 |
assert dict(h.items()) == {'a': 1, 'b': 2, 'z': 100} |
| 1055 |
|
| 1056 |
def test_map_mut_8(self): |
| 1057 |
key1 = HashKey(123, 'aaa') |
| 1058 |
key2 = HashKey(123, 'bbb') |
| 1059 |
|
| 1060 |
h = self.Map({key1: 123}) |
| 1061 |
assert dict(h.items()) == {key1: 123} |
| 1062 |
|
| 1063 |
upd = {key2: 1} |
| 1064 |
with HashKeyCrasher(error_on_eq=True): |
| 1065 |
with pytest.raises(EqError): |
| 1066 |
h.update(upd) |
| 1067 |
|
| 1068 |
upd = self.Map({key2: 'zzz'}) |
| 1069 |
with HashKeyCrasher(error_on_eq=True): |
| 1070 |
with pytest.raises(EqError): |
| 1071 |
h.update(upd) |
| 1072 |
|
| 1073 |
upd = [(1, 2), (key2, 'zzz')] |
| 1074 |
with HashKeyCrasher(error_on_eq=True): |
| 1075 |
with pytest.raises(EqError): |
| 1076 |
h.update(upd) |
| 1077 |
|
| 1078 |
assert dict(h.items()) == {key1: 123} |
| 1079 |
|
| 1080 |
def test_map_mut_9(self): |
| 1081 |
key1 = HashKey(123, 'aaa') |
| 1082 |
|
| 1083 |
src = {key1: 123} |
| 1084 |
with HashKeyCrasher(error_on_hash=True): |
| 1085 |
with pytest.raises(HashingError): |
| 1086 |
self.Map(src) |
| 1087 |
|
| 1088 |
src = [(1, 2), (key1, 123)] |
| 1089 |
with HashKeyCrasher(error_on_hash=True): |
| 1090 |
with pytest.raises(HashingError): |
| 1091 |
self.Map(src) |
| 1092 |
|
| 1093 |
def test_map_mut_10(self): |
| 1094 |
key1 = HashKey(123, 'aaa') |
| 1095 |
|
| 1096 |
m = self.Map({key1: 123}) |
| 1097 |
|
| 1098 |
mm = m.mutate() |
| 1099 |
with HashKeyCrasher(error_on_hash=True): |
| 1100 |
with pytest.raises(HashingError): |
| 1101 |
del mm[key1] |
| 1102 |
|
| 1103 |
mm = m.mutate() |
| 1104 |
with HashKeyCrasher(error_on_hash=True): |
| 1105 |
with pytest.raises(HashingError): |
| 1106 |
mm.pop(key1, None) |
| 1107 |
|
| 1108 |
mm = m.mutate() |
| 1109 |
with HashKeyCrasher(error_on_hash=True): |
| 1110 |
with pytest.raises(HashingError): |
| 1111 |
mm.set(key1, 123) |
| 1112 |
|
| 1113 |
def test_map_mut_11(self): |
| 1114 |
m = self.Map({'a': 1, 'b': 2}) |
| 1115 |
|
| 1116 |
mm = m.mutate() |
| 1117 |
assert mm.pop('a', 1) == 1 |
| 1118 |
assert mm.finish() == self.Map({'b': 2}) |
| 1119 |
|
| 1120 |
mm = m.mutate() |
| 1121 |
assert mm.pop('b', 1) == 2 |
| 1122 |
assert mm.finish() == self.Map({'a': 1}) |
| 1123 |
|
| 1124 |
mm = m.mutate() |
| 1125 |
assert mm.pop('b', 1) == 2 |
| 1126 |
del mm['a'] |
| 1127 |
assert mm.finish() == self.Map() |
| 1128 |
|
| 1129 |
def test_map_mut_12(self): |
| 1130 |
m = self.Map({'a': 1, 'b': 2}) |
| 1131 |
|
| 1132 |
mm = m.mutate() |
| 1133 |
mm.finish() |
| 1134 |
|
| 1135 |
with pytest.raises(ValueError, match='has been finished'): |
| 1136 |
mm.pop('a') |
| 1137 |
|
| 1138 |
with pytest.raises(ValueError, match='has been finished'): |
| 1139 |
del mm['a'] |
| 1140 |
|
| 1141 |
with pytest.raises(ValueError, match='has been finished'): |
| 1142 |
mm.set('a', 'b') |
| 1143 |
|
| 1144 |
with pytest.raises(ValueError, match='has been finished'): |
| 1145 |
mm['a'] = 'b' |
| 1146 |
|
| 1147 |
with pytest.raises(ValueError, match='has been finished'): |
| 1148 |
mm.update(a='b') |
| 1149 |
|
| 1150 |
def test_map_mut_13(self): |
| 1151 |
key1 = HashKey(123, 'aaa') |
| 1152 |
key2 = HashKey(123, 'aaa') |
| 1153 |
|
| 1154 |
m = self.Map({key1: 123}) |
| 1155 |
|
| 1156 |
mm = m.mutate() |
| 1157 |
with HashKeyCrasher(error_on_eq=True): |
| 1158 |
with pytest.raises(EqError): |
| 1159 |
del mm[key2] |
| 1160 |
|
| 1161 |
mm = m.mutate() |
| 1162 |
with HashKeyCrasher(error_on_eq=True): |
| 1163 |
with pytest.raises(EqError): |
| 1164 |
mm.pop(key2, None) |
| 1165 |
|
| 1166 |
mm = m.mutate() |
| 1167 |
with HashKeyCrasher(error_on_eq=True): |
| 1168 |
with pytest.raises(EqError): |
| 1169 |
mm.set(key2, 123) |
| 1170 |
|
| 1171 |
def test_map_mut_14(self): |
| 1172 |
m = self.Map(a=1, b=2) |
| 1173 |
|
| 1174 |
with m.mutate() as mm: |
| 1175 |
mm['z'] = 100 |
| 1176 |
del mm['a'] |
| 1177 |
|
| 1178 |
assert mm.finish() == self.Map(z=100, b=2) |
| 1179 |
|
| 1180 |
def test_map_mut_15(self): |
| 1181 |
m = self.Map(a=1, b=2) |
| 1182 |
|
| 1183 |
with pytest.raises(ZeroDivisionError): |
| 1184 |
with m.mutate() as mm: |
| 1185 |
mm['z'] = 100 |
| 1186 |
del mm['a'] |
| 1187 |
1 / 0 |
| 1188 |
|
| 1189 |
assert mm.finish() == self.Map(z=100, b=2) |
| 1190 |
assert m == self.Map(a=1, b=2) |
| 1191 |
|
| 1192 |
def test_map_mut_16(self): |
| 1193 |
m = self.Map(a=1, b=2) |
| 1194 |
hash(m) |
| 1195 |
|
| 1196 |
m2 = self.Map(m) |
| 1197 |
m3 = self.Map(m, c=3) |
| 1198 |
|
| 1199 |
assert m == m2 |
| 1200 |
assert len(m) == len(m2) |
| 1201 |
assert hash(m) == hash(m2) |
| 1202 |
|
| 1203 |
assert m is not m2 |
| 1204 |
assert m3 == self.Map(a=1, b=2, c=3) |
| 1205 |
|
| 1206 |
def test_map_mut_17(self): |
| 1207 |
m = self.Map(a=1) |
| 1208 |
with m.mutate() as mm: |
| 1209 |
with pytest.raises(TypeError, match='cannot create Maps from MapMutations'): |
| 1210 |
self.Map(mm) |
| 1211 |
|
| 1212 |
def test_map_mut_18(self): |
| 1213 |
m = self.Map(a=1, b=2) |
| 1214 |
with m.mutate() as mm: |
| 1215 |
mm.update(self.Map(x=1), z=2) |
| 1216 |
mm.update(c=3) |
| 1217 |
mm.update({'n': 100, 'a': 20}) |
| 1218 |
m2 = mm.finish() |
| 1219 |
|
| 1220 |
expected = self.Map( |
| 1221 |
{'b': 2, 'c': 3, 'n': 100, 'z': 2, 'x': 1, 'a': 20}) |
| 1222 |
|
| 1223 |
assert len(m2) == 6 |
| 1224 |
assert m2 == expected |
| 1225 |
assert m == self.Map(a=1, b=2) |
| 1226 |
|
| 1227 |
def test_map_mut_19(self): |
| 1228 |
m = self.Map(a=1, b=2) |
| 1229 |
m2 = m.update({'a': 20}) |
| 1230 |
assert len(m2) == 2 |
| 1231 |
|
| 1232 |
def test_map_mut_stress(self): |
| 1233 |
COLLECTION_SIZE = 7000 |
| 1234 |
TEST_ITERS_EVERY = 647 |
| 1235 |
RUN_XTIMES = 3 |
| 1236 |
|
| 1237 |
for _ in range(RUN_XTIMES): |
| 1238 |
h = self.Map() |
| 1239 |
d = dict() |
| 1240 |
|
| 1241 |
for i in range(COLLECTION_SIZE // TEST_ITERS_EVERY): |
| 1242 |
|
| 1243 |
hm = h.mutate() |
| 1244 |
for j in range(TEST_ITERS_EVERY): |
| 1245 |
key = random.randint(1, 100000) |
| 1246 |
key = HashKey(key % 271, str(key)) |
| 1247 |
|
| 1248 |
hm.set(key, key) |
| 1249 |
d[key] = key |
| 1250 |
|
| 1251 |
assert len(hm) == len(d) |
| 1252 |
|
| 1253 |
h2 = hm.finish() |
| 1254 |
assert dict(h2.items()) == d |
| 1255 |
h = h2 |
| 1256 |
|
| 1257 |
assert dict(h.items()) == d |
| 1258 |
assert len(h) == len(d) |
| 1259 |
|
| 1260 |
it = iter(tuple(d.keys())) |
| 1261 |
for i in range(COLLECTION_SIZE // TEST_ITERS_EVERY): |
| 1262 |
|
| 1263 |
hm = h.mutate() |
| 1264 |
for j in range(TEST_ITERS_EVERY): |
| 1265 |
try: |
| 1266 |
key = next(it) |
| 1267 |
except StopIteration: |
| 1268 |
break |
| 1269 |
|
| 1270 |
del d[key] |
| 1271 |
del hm[key] |
| 1272 |
|
| 1273 |
assert len(hm) == len(d) |
| 1274 |
|
| 1275 |
h2 = hm.finish() |
| 1276 |
assert dict(h2.items()) == d |
| 1277 |
h = h2 |
| 1278 |
|
| 1279 |
assert dict(h.items()) == d |
| 1280 |
assert len(h) == len(d) |
| 1281 |
|
| 1282 |
def test_map_pickle(self): |
| 1283 |
h = self.Map(a=1, b=2) |
| 1284 |
for proto in range(pickle.HIGHEST_PROTOCOL): |
| 1285 |
p = pickle.dumps(h, proto) |
| 1286 |
uh = pickle.loads(p) |
| 1287 |
|
| 1288 |
assert isinstance(uh, self.Map) |
| 1289 |
assert h == uh |
| 1290 |
|
| 1291 |
with pytest.raises(TypeError, match="can('t|not) pickle"): |
| 1292 |
pickle.dumps(h.mutate()) |
| 1293 |
|
| 1294 |
def test_map_is_subscriptable(self): |
| 1295 |
assert self.Map[int, str] is self.Map |
| 1296 |
|
| 1297 |
class TestPyMap(BaseMapTest): |
| 1298 |
Map = Map |