Forráskód Böngészése

Test the &= and |= operators. Also incidentally test += operator. Bugfixes for same

dstromberg 14 éve
szülő
commit
cc03719548
2 módosított fájl, 83 hozzáadás és 8 törlés
  1. 10 4
      bloom_filter_mod.py
  2. 73 4
      test-bloom-filter

+ 10 - 4
bloom_filter_mod.py

@@ -53,6 +53,10 @@ class Bloom_filter:
 		for i, mask in self.probe_func(self, key):
 			self.array_[i] |= mask
 
+	def __iadd__(self, key):
+		self.add(key)
+		return self
+
 	def _match_template(self, bfilter):
 		'''Compare a sort of signature for two bloom filters.  Used in preparation for binary operations'''
 		return (self.num_bits == bfilter.num_bits \
@@ -67,8 +71,9 @@ class Bloom_filter:
 			# Union b/w two unrelated bloom filter raises this
 			raise ValueError("Mismatched bloom filters")
 
-	def __or__(self, bfilter):
-		return self.union(bfilter)
+	def __ior__(self, bfilter):
+		self.union(bfilter)
+		return self
 
 	def intersection(self, bfilter):
 		'''Compute the set intersection of two bloom filters'''
@@ -78,8 +83,9 @@ class Bloom_filter:
 			# Intersection b/w two unrelated bloom filter raises this
 			raise ValueError("Mismatched bloom filters")
 
-	def __and__(self, bfilter):
-		return self.intersection(bfilter)
+	def __iand__(self, bfilter):
+		self.intersection(bfilter)
+		return self
 
 	def __contains__(self, key):
 		return all(self.array_[i] & mask for i, mask in self.probe_func(self, key))

+ 73 - 4
test-bloom-filter

@@ -20,7 +20,7 @@ def my_range(maximum):
 		yield value
 		value += 1
 
-def test(order, included, trials, error_rate):
+def primary_test(order, included, trials, error_rate):
 	'''Some quick automatic tests for the bloom filter class'''
 
 	all_good = True
@@ -79,6 +79,71 @@ def random_string():
 		list_.append(character)
 	return ''.join(list_)
 
+def and_test():
+	'''Test the & operator'''
+
+	all_good = True
+
+	abc = bloom_filter_mod.Bloom_filter(ideal_num_elements=100, error_rate=0.01)
+	for character in [ 'a', 'b', 'c' ]:
+		abc += character
+
+	bcd = bloom_filter_mod.Bloom_filter(ideal_num_elements=100, error_rate=0.01)
+	for character in [ 'b', 'c', 'd' ]:
+		bcd += character
+
+	abc_and_bcd = abc
+	abc_and_bcd &= bcd
+
+	if 'a' in abc_and_bcd:
+		sys.stderr.write('a in abc_and_bcd, but should not be')
+		all_good = False
+	if not 'b' in abc_and_bcd:
+		sys.stderr.write('b not in abc_and_bcd, but should be')
+		all_good = False
+	if not 'c' in abc_and_bcd:
+		sys.stderr.write('c not in abc_and_bcd, but should be')
+		all_good = False
+	if 'd' in abc_and_bcd:
+		sys.stderr.write('d in abc_and_bcd, but should not be')
+		all_good = False
+
+	return all_good
+	
+def or_test():
+	'''Test the | operator'''
+
+	all_good = True
+
+	abc = bloom_filter_mod.Bloom_filter(ideal_num_elements=100, error_rate=0.01)
+	for character in [ 'a', 'b', 'c' ]:
+		abc += character
+
+	bcd = bloom_filter_mod.Bloom_filter(ideal_num_elements=100, error_rate=0.01)
+	for character in [ 'b', 'c', 'd' ]:
+		bcd += character
+
+	abc_and_bcd = abc
+	abc_and_bcd |= bcd
+
+	if not 'a' in abc_and_bcd:
+		sys.stderr.write('a not in abc_and_bcd, but should be')
+		all_good = False
+	if not 'b' in abc_and_bcd:
+		sys.stderr.write('b not in abc_and_bcd, but should be')
+		all_good = False
+	if not 'c' in abc_and_bcd:
+		sys.stderr.write('c not in abc_and_bcd, but should be')
+		all_good = False
+	if not 'd' in abc_and_bcd:
+		sys.stderr.write('d not in abc_and_bcd, but should be')
+		all_good = False
+	if 'e' in abc_and_bcd:
+		sys.stderr.write('e in abc_and_bcd, but should be')
+		all_good = False
+
+	return all_good
+	
 def main():
 	'''Unit tests for Bloom_filter class'''
 
@@ -94,11 +159,15 @@ def main():
 
 	random_content = [ random_string() for dummy in range(1000) ]
 
-	all_good &= test([1, 2], states, trials=100000, error_rate=0.01)
+	all_good &= primary_test([1, 2], states, trials=10000, error_rate=0.01)
+
+	all_good &= primary_test([1, 2], random_content, trials=10000, error_rate=0.1)
+
+	all_good &= primary_test([2, 1], [ 'a', 'b', 'c'], trials=100, error_rate=0.000001)
 
-	all_good &= test([1, 2], random_content, trials=1000000, error_rate=0.1)
+	all_good &= and_test()
 
-	all_good &= test([2, 1], [ 'a', 'b', 'c'], trials=100, error_rate=0.000001)
+	all_good &= or_test()
 
 	if all_good:
 		sys.stderr.write('%s: All tests passed\n' % sys.argv[0])