@@ -46,100 +46,32 @@ def test_bloom_filter_update_and_query(self):
4646 # Query for item not in filter
4747 self .assertFalse (bf .query ("other_item" ))
4848
49- def test_bloom_filter_multiple_items (self ):
50- """Test adding multiple items to the bloom filter"""
49+ def test_bloom_filter_serialize_deserialize (self ):
50+ """Test that we can serialize a bloom filter and restore it afterwards """
5151 bf = create_bloom_filter (1000 , 0.01 )
52-
53- items = ["item1" , "item2" , "item3" , "item4" , "item5" ]
54-
55- # Add all items
56- for item in items :
57- bf .update (item )
58-
59- # Check that all items are found
60- for item in items :
61- self .assertTrue (bf .query (item ), f"Item { item } should be found" )
62-
63- # Check that items not added are not found
64- non_items = ["not_item1" , "not_item2" , "not_item3" ]
65- for item in non_items :
66- self .assertFalse (bf .query (item ), f"Item { item } should not be found" )
52+ bf .update ("test_item" )
53+ serialized = bf .serialize ()
54+ self .assertIsNotNone (serialized )
55+ self .assertTrue (len (serialized ) > 0 )
6756
68- def test_bloom_filter_false_positives (self ):
69- """Test that bloom filter can have false positives (this is expected behavior)"""
70- bf = create_bloom_filter (10 , 0.1 ) # Small filter, higher false positive rate
71-
72- # Add a few items
73- bf .update ("item1" )
74- bf .update ("item2" )
75-
76- # Check that added items are found
77- self .assertTrue (bf .query ("item1" ))
78- self .assertTrue (bf .query ("item2" ))
79-
80- # With a small filter and high false positive rate, we might get false positives
81- # This is expected behavior for bloom filters
82- # We're not testing for specific false positives, just that the filter works
57+ bf = create_bloom_filter (1000 , 0.01 )
58+ items = ["alpha" , "beta" , "gamma" ]
59+ for it in items :
60+ bf .update (it )
8361
84- def test_bloom_filter_parameters (self ):
85- """Test creating bloom filters with different parameters"""
86- # Test with different sizes and false positive rates
87- test_cases = [
88- (100 , 0.01 ),
89- (1000 , 0.05 ),
90- (10000 , 0.001 ),
91- (100 , 0.1 ),
92- ]
93-
94- for max_items , false_positive_rate in test_cases :
95- with self .subTest (max_items = max_items , false_positive_rate = false_positive_rate ):
96- bf = create_bloom_filter (max_items , false_positive_rate )
97- self .assertIsNotNone (bf )
98- self .assertTrue (bf .is_empty ())
62+ payload = bf .serialize ()
63+ self .assertTrue (len (payload ) > 0 )
9964
100- def test_bloom_filter_string_types (self ):
101- """Test that bloom filter works with different string types"""
102- bf = create_bloom_filter (1000 , 0.01 )
103-
104- # Test with different string types
105- test_strings = [
106- "simple" ,
107- "string with spaces" ,
108- "string_with_underscores" ,
109- "string-with-dashes" ,
110- "string123with456numbers" ,
111- "string.with.dots" ,
112- "string!with@special#chars$" ,
113- ]
114-
115- for test_string in test_strings :
116- with self .subTest (test_string = test_string ):
117- bf .update (test_string )
118- self .assertTrue (bf .query (test_string ))
119-
120- # Test empty string separately - it might be ignored by the implementation
121- bf .update ("" )
122- # Note: Empty strings might be ignored by the bloom filter implementation
123- # This is common behavior, so we don't assert on the result
65+ restored = bf .deserialize (payload )
66+ self .assertFalse (restored .is_empty ())
67+
68+ # Inserted items should come back as "might be present" (very high probability true)
69+ for it in items :
70+ self .assertTrue (restored .query (it ), f"Expected present after round-trip: { it } " )
71+
72+ # A not-inserted key should usually be absent (Bloom could FP, but unlikely here)
73+ self .assertFalse (restored .query ("not_inserted" ))
12474
125- def test_bloom_filter_edge_cases (self ):
126- """Test edge cases for bloom filter"""
127- bf = create_bloom_filter (1000 , 0.01 )
128-
129- # Test with very long strings
130- long_string = "a" * 1000
131- bf .update (long_string )
132- self .assertTrue (bf .query (long_string ))
133-
134- # Test with unicode strings
135- unicode_string = "café résumé naïve"
136- bf .update (unicode_string )
137- self .assertTrue (bf .query (unicode_string ))
138-
139- # Test with numbers as strings
140- number_string = "12345"
141- bf .update (number_string )
142- self .assertTrue (bf .query (number_string ))
14375
14476if __name__ == '__main__' :
14577 unittest .main ()
0 commit comments