Skip to content

Commit c72e46c

Browse files
committed
Add d[key] lookup test with multi-member wrapped class keys
Test verifies that Python dict d[key] lookup works correctly when keys are wrapped classes with multiple members (value_ and name_). Both members must match for the lookup to succeed.
1 parent 042bf81 commit c72e46c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/test_wrapped_containers.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,41 @@ def test_unordered_set_two_member_hash(self, wrapped_container_module):
569569
assert wrong_value not in items, \
570570
"Should NOT find (999, 'alice') - value doesn't match"
571571

572+
def test_dict_lookup_with_multi_member_key(self, wrapped_container_module):
573+
"""Test d[key] lookup where key is a wrapped class with multiple members.
574+
575+
This verifies that Python dict lookup uses the full hash (both value_ and name_)
576+
to find the correct value, not just one member.
577+
"""
578+
m = wrapped_container_module
579+
580+
# Create keys with multiple members
581+
key_alice = m.Item(100, b"alice")
582+
key_bob = m.Item(100, b"bob") # Same value_, different name_
583+
key_charlie = m.Item(200, b"charlie")
584+
585+
# Build a dict with multi-member keys
586+
data = {key_alice: 1000, key_bob: 2000, key_charlie: 3000}
587+
assert len(data) == 3 # All three are distinct keys
588+
589+
# Direct d[key] lookup with new Item objects (not the same object!)
590+
lookup_alice = m.Item(100, b"alice")
591+
lookup_bob = m.Item(100, b"bob")
592+
lookup_charlie = m.Item(200, b"charlie")
593+
594+
# These lookups must find the correct values based on BOTH members
595+
assert data[lookup_alice] == 1000, "d[Item(100, 'alice')] should be 1000"
596+
assert data[lookup_bob] == 2000, "d[Item(100, 'bob')] should be 2000"
597+
assert data[lookup_charlie] == 3000, "d[Item(200, 'charlie')] should be 3000"
598+
599+
# Lookup with wrong name should raise KeyError
600+
wrong_name = m.Item(100, b"eve")
601+
try:
602+
_ = data[wrong_name]
603+
assert False, "Should have raised KeyError for wrong name"
604+
except KeyError:
605+
pass # Expected
606+
572607
def test_unordered_set_has_item(self, wrapped_container_module):
573608
"""Test checking if Item exists in unordered_set (hash-based O(1) membership test)."""
574609
m = wrapped_container_module

0 commit comments

Comments
 (0)