@@ -144,37 +144,6 @@ def test_map_int_to_item_create(self, wrapped_container_module):
144144 assert result [1 ].value_ == 10
145145 assert result [2 ].value_ == 20
146146
147- def test_map_int_to_item_lookup (self , wrapped_container_module ):
148- """Test looking up Item by key in map."""
149- m = wrapped_container_module
150- t = m .WrappedContainerTest ()
151-
152- item1 = m .Item (100 )
153- item2 = m .Item (200 )
154- item3 = m .Item (300 )
155- map_data = {1 : item1 , 5 : item2 , 10 : item3 }
156-
157- # Lookup existing keys
158- assert t .lookupMapIntToItem (map_data , 1 ) == 100
159- assert t .lookupMapIntToItem (map_data , 5 ) == 200
160- assert t .lookupMapIntToItem (map_data , 10 ) == 300
161-
162- # Lookup missing key
163- assert t .lookupMapIntToItem (map_data , 999 ) == - 1
164-
165- def test_map_int_to_item_has_key (self , wrapped_container_module ):
166- """Test checking if key exists in map."""
167- m = wrapped_container_module
168- t = m .WrappedContainerTest ()
169-
170- item1 = m .Item (100 )
171- item2 = m .Item (200 )
172- map_data = {1 : item1 , 2 : item2 }
173-
174- assert t .hasKeyMapIntToItem (map_data , 1 ) is True
175- assert t .hasKeyMapIntToItem (map_data , 2 ) is True
176- assert t .hasKeyMapIntToItem (map_data , 3 ) is False
177- assert t .hasKeyMapIntToItem (map_data , 999 ) is False
178147
179148
180149class TestMapWithWrappedClassKey :
@@ -193,16 +162,25 @@ def test_map_item_to_int_sum(self, wrapped_container_module):
193162 assert result == 30 # 10 + 20 (sum of keys)
194163
195164 def test_map_item_to_int_create (self , wrapped_container_module ):
196- """Test returning map<Item, int>."""
165+ """Test returning map<Item, int> and using Python d[key] lookup ."""
197166 m = wrapped_container_module
198167 t = m .WrappedContainerTest ()
199168
200169 result = t .createMapItemToInt (3 )
201170 assert len (result ) == 3
202- # Values should be 0, 10, 20 for keys with value_ 0, 1, 2
203- keys_values = [(k .value_ , v ) for k , v in result .items ()]
204- keys_values .sort ()
205- assert keys_values == [(0 , 0 ), (1 , 10 ), (2 , 20 )]
171+
172+ # Direct Python dict lookup with wrapped class key - NOT iteration!
173+ key0 = m .Item (0 )
174+ key1 = m .Item (1 )
175+ key2 = m .Item (2 )
176+ assert result [key0 ] == 0
177+ assert result [key1 ] == 10
178+ assert result [key2 ] == 20
179+
180+ # Test 'in' operator
181+ assert key1 in result
182+ missing = m .Item (999 )
183+ assert missing not in result
206184
207185
208186class TestNestedVectorOfWrappedClass :
@@ -300,16 +278,26 @@ def test_unordered_map_item_to_int_sum(self, wrapped_container_module):
300278 assert result == 30 # 10 + 20 (sum of keys)
301279
302280 def test_unordered_map_item_to_int_create (self , wrapped_container_module ):
303- """Test returning unordered_map<Item, int>."""
281+ """Test returning unordered_map<Item, int> and using Python d[key] lookup ."""
304282 m = wrapped_container_module
305283 t = m .WrappedContainerTest ()
306284
307285 result = t .createUnorderedMapItemToInt (3 )
308286 assert len (result ) == 3
309- # Values should be 0, 10, 20 for keys with value_ 0, 1, 2
310- keys_values = [(k .value_ , v ) for k , v in result .items ()]
311- keys_values .sort ()
312- assert keys_values == [(0 , 0 ), (1 , 10 ), (2 , 20 )]
287+
288+ # Direct Python dict lookup with wrapped class key - NOT iteration!
289+ # This tests that the wrapped Item class has proper __hash__ and __eq__
290+ key0 = m .Item (0 )
291+ key1 = m .Item (1 )
292+ key2 = m .Item (2 )
293+ assert result [key0 ] == 0
294+ assert result [key1 ] == 10
295+ assert result [key2 ] == 20
296+
297+ # Test 'in' operator (uses hash)
298+ assert key1 in result
299+ missing = m .Item (999 )
300+ assert missing not in result
313301
314302
315303class TestUnorderedMapWithWrappedClassValue :
@@ -338,37 +326,6 @@ def test_unordered_map_int_to_item_create(self, wrapped_container_module):
338326 assert result [1 ].value_ == 10
339327 assert result [2 ].value_ == 20
340328
341- def test_unordered_map_int_to_item_lookup (self , wrapped_container_module ):
342- """Test looking up Item by key in unordered_map (hash-based O(1) lookup)."""
343- m = wrapped_container_module
344- t = m .WrappedContainerTest ()
345-
346- item1 = m .Item (100 )
347- item2 = m .Item (200 )
348- item3 = m .Item (300 )
349- map_data = {1 : item1 , 5 : item2 , 10 : item3 }
350-
351- # Lookup existing keys - tests hash-based access
352- assert t .lookupUnorderedMapIntToItem (map_data , 1 ) == 100
353- assert t .lookupUnorderedMapIntToItem (map_data , 5 ) == 200
354- assert t .lookupUnorderedMapIntToItem (map_data , 10 ) == 300
355-
356- # Lookup missing key
357- assert t .lookupUnorderedMapIntToItem (map_data , 999 ) == - 1
358-
359- def test_unordered_map_int_to_item_has_key (self , wrapped_container_module ):
360- """Test checking if key exists in unordered_map (hash-based O(1) lookup)."""
361- m = wrapped_container_module
362- t = m .WrappedContainerTest ()
363-
364- item1 = m .Item (100 )
365- item2 = m .Item (200 )
366- map_data = {1 : item1 , 2 : item2 }
367-
368- assert t .hasKeyUnorderedMapIntToItem (map_data , 1 ) is True
369- assert t .hasKeyUnorderedMapIntToItem (map_data , 2 ) is True
370- assert t .hasKeyUnorderedMapIntToItem (map_data , 3 ) is False
371- assert t .hasKeyUnorderedMapIntToItem (map_data , 999 ) is False
372329
373330
374331class TestUnorderedMapWithWrappedClassBoth :
@@ -491,63 +448,34 @@ def test_unordered_set_items_sum(self, wrapped_container_module):
491448 assert result == 60
492449
493450 def test_unordered_set_items_create (self , wrapped_container_module ):
494- """Test returning unordered_set<Item>."""
451+ """Test returning unordered_set<Item> and using Python 'in' operator ."""
495452 m = wrapped_container_module
496453 t = m .WrappedContainerTest ()
497454
498455 items = t .createUnorderedSetItems (3 )
499456 assert len (items ) == 3
500- values = sorted ([item .value_ for item in items ])
501- assert values == [0 , 10 , 20 ]
502-
503- def test_unordered_set_has_item (self , wrapped_container_module ):
504- """Test checking if Item exists in unordered_set (hash-based O(1) membership test)."""
505- m = wrapped_container_module
506- t = m .WrappedContainerTest ()
507457
458+ # Direct Python 'in' operator with wrapped class - NOT iteration!
459+ # This tests that the wrapped Item class has proper __hash__ and __eq__
460+ item0 = m .Item (0 )
508461 item1 = m .Item (10 )
509462 item2 = m .Item (20 )
510- item3 = m .Item (30 )
511- items = {item1 , item2 , item3 }
512-
513- # Items that should be found (tests hash function)
514- search_item1 = m .Item (10 )
515- search_item2 = m .Item (20 )
516- assert t .hasItemUnorderedSet (items , search_item1 ) is True
517- assert t .hasItemUnorderedSet (items , search_item2 ) is True
518-
519- # Item that should NOT be found
520- missing_item = m .Item (999 )
521- assert t .hasItemUnorderedSet (items , missing_item ) is False
522-
523- def test_unordered_set_find_item (self , wrapped_container_module ):
524- """Test finding Item in unordered_set and returning its value (hash-based O(1) lookup)."""
525- m = wrapped_container_module
526- t = m .WrappedContainerTest ()
463+ assert item0 in items
464+ assert item1 in items
465+ assert item2 in items
527466
528- item1 = m .Item (10 )
529- item2 = m .Item (20 )
530- item3 = m .Item (30 )
531- items = {item1 , item2 , item3 }
532-
533- # Find existing items - tests hash-based lookup
534- search_item1 = m .Item (10 )
535- search_item2 = m .Item (30 )
536- assert t .findItemUnorderedSet (items , search_item1 ) == 10
537- assert t .findItemUnorderedSet (items , search_item2 ) == 30
538-
539- # Find missing item should return -1
540- missing_item = m .Item (999 )
541- assert t .findItemUnorderedSet (items , missing_item ) == - 1
467+ # Test missing item
468+ missing = m .Item (999 )
469+ assert missing not in items
542470
543471 def test_unordered_set_two_member_hash (self , wrapped_container_module ):
544472 """Test that hash function uses both value_ AND name_ members.
545473
546474 This verifies that items with the same value_ but different name_
547475 are treated as different items (different hash + equality check).
476+ Uses Python's native 'in' operator - no C++ lookup functions.
548477 """
549478 m = wrapped_container_module
550- t = m .WrappedContainerTest ()
551479
552480 # Create items with same value but different names
553481 item_alice = m .Item (100 , b"alice" )
@@ -559,24 +487,22 @@ def test_unordered_set_two_member_hash(self, wrapped_container_module):
559487 # All three should be in the set (even though two have same value_)
560488 assert len (items ) == 3 , "Set should have 3 items despite same value_"
561489
562- # Search for exact match (value_ AND name_ must match)
490+ # Search for exact match using Python 'in' (value_ AND name_ must match)
563491 search_alice = m .Item (100 , b"alice" )
564492 search_bob = m .Item (100 , b"bob" )
565493
566- assert t .hasItemUnorderedSet (items , search_alice ) is True , \
567- "Should find alice (100, 'alice')"
568- assert t .hasItemUnorderedSet (items , search_bob ) is True , \
569- "Should find bob (100, 'bob')"
494+ assert search_alice in items , "Should find alice (100, 'alice')"
495+ assert search_bob in items , "Should find bob (100, 'bob')"
570496
571497 # Search with wrong name should NOT find item
572498 # Same value_ but different name_ = different hash/different item
573499 wrong_name = m .Item (100 , b"eve" )
574- assert t . hasItemUnorderedSet ( items , wrong_name ) is False , \
500+ assert wrong_name not in items , \
575501 "Should NOT find (100, 'eve') - name doesn't match"
576502
577503 # Search with wrong value should NOT find item
578504 wrong_value = m .Item (999 , b"alice" )
579- assert t . hasItemUnorderedSet ( items , wrong_value ) is False , \
505+ assert wrong_value not in items , \
580506 "Should NOT find (999, 'alice') - value doesn't match"
581507
582508
0 commit comments