MCPcopy Index your code
hub / github.com/python/cpython / test_delitem_hash_collision

Method test_delitem_hash_collision

Lib/test/test_ordered_dict.py:521–558  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

519 self.assertEqual(deleted, list(reversed(range(100))))
520
521 def test_delitem_hash_collision(self):
522 OrderedDict = self.OrderedDict
523
524 class Key:
525 def __init__(self, hash):
526 self._hash = hash
527 self.value = str(id(self))
528 def __hash__(self):
529 return self._hash
530 def __eq__(self, other):
531 try:
532 return self.value == other.value
533 except AttributeError:
534 return False
535 def __repr__(self):
536 return self.value
537
538 def blocking_hash(hash):
539 # See the collision-handling in lookdict (in Objects/dictobject.c).
540 MINSIZE = 8
541 i = (hash & MINSIZE-1)
542 return (i << 2) + i + hash + 1
543
544 COLLIDING = 1
545
546 key = Key(COLLIDING)
547 colliding = Key(COLLIDING)
548 blocking = Key(blocking_hash(COLLIDING))
549
550 od = OrderedDict()
551 od[key] = ...
552 od[blocking] = ...
553 od[colliding] = ...
554 od['after'] = ...
555
556 del od[blocking]
557 del od[colliding]
558 self.assertEqual(list(od.items()), [(key, ...), ('after', ...)])
559
560 def test_issue24347(self):
561 OrderedDict = self.OrderedDict

Callers

nothing calls this directly

Calls 5

itemsMethod · 0.95
listClass · 0.85
KeyClass · 0.70
OrderedDictClass · 0.70
assertEqualMethod · 0.45

Tested by

no test coverage detected