def test_rbf_push_pop(self): """test forcing push and pop""" blm = RotatingBloomFilter(est_elements=10, false_positive_rate=0.05, max_queue_size=5) self.assertEqual(blm.current_queue_size, 1) blm.add("test") blm.push() self.assertEqual(blm.current_queue_size, 2) self.assertEqual("test" in blm, True) blm.push() self.assertEqual(blm.current_queue_size, 3) self.assertEqual("test" in blm, True) blm.push() self.assertEqual(blm.current_queue_size, 4) self.assertEqual("test" in blm, True) blm.push() self.assertEqual(blm.current_queue_size, 5) self.assertEqual("test" in blm, True) blm.push() self.assertEqual(blm.current_queue_size, 5) self.assertEqual("test" in blm, False) # test popping blm.add("that") blm.pop() self.assertEqual(blm.current_queue_size, 4) self.assertEqual("that" in blm, True) blm.pop() self.assertEqual(blm.current_queue_size, 3) self.assertEqual("that" in blm, True) blm.pop() self.assertEqual(blm.current_queue_size, 2) self.assertEqual("that" in blm, True) blm.pop() self.assertEqual(blm.current_queue_size, 1) self.assertEqual("that" in blm, True)
def test_rbf_push_pop(self): blm = RotatingBloomFilter(est_elements=10, false_positive_rate=0.05, max_queue_size=5) self.assertEqual(blm.current_queue_size, 1) blm.add('test') blm.push() self.assertEqual(blm.current_queue_size, 2) self.assertEqual('test' in blm, True) blm.push() self.assertEqual(blm.current_queue_size, 3) self.assertEqual('test' in blm, True) blm.push() self.assertEqual(blm.current_queue_size, 4) self.assertEqual('test' in blm, True) blm.push() self.assertEqual(blm.current_queue_size, 5) self.assertEqual('test' in blm, True) blm.push() self.assertEqual(blm.current_queue_size, 5) self.assertEqual('test' in blm, False) # test popping blm.add("that") blm.pop() self.assertEqual(blm.current_queue_size, 4) self.assertEqual('that' in blm, True) blm.pop() self.assertEqual(blm.current_queue_size, 3) self.assertEqual('that' in blm, True) blm.pop() self.assertEqual(blm.current_queue_size, 2) self.assertEqual('that' in blm, True) blm.pop() self.assertEqual(blm.current_queue_size, 1) self.assertEqual('that' in blm, True)
def test_rfb_from_bytes(self): """basic rotating Bloom Filter export bytes test""" blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05, max_queue_size=3) for i in range(105): blm.add(str(i)) bytes_out = bytes(blm) blm2 = RotatingBloomFilter.frombytes(bytes_out, max_queue_size=3) self.assertEqual(blm2.expansions, 2) self.assertEqual(blm2.false_positive_rate, 0.05000000074505806) self.assertEqual(blm2.estimated_elements, 25) self.assertEqual(blm2.elements_added, 105) self.assertEqual(blm2.current_queue_size, 3) self.assertEqual(bytes(blm2), bytes(blm)) for i in range(105): self.assertEqual(blm.check(str(i)), blm2.check(str(i)))
def test_rbf_non_basic_import(self): ''' test that the imported rotating Bloom filter is correct ''' blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05) for i in range(15): blm.add('{}'.format(i)) blm.push() blm.export('test.rbf') blm2 = RotatingBloomFilter(filepath='test.rbf') # test those that should be popped off... for i in range(5): self.assertEqual('{}'.format(i) in blm2, False) # test things that would not be popped for i in range(6, 15): self.assertEqual('{}'.format(i) in blm2, True) self.assertEqual(blm2.current_queue_size, 10) self.assertEqual(blm2.expansions, 9) os.remove('test.rbf')
def test_rbf_non_basic_import(self): """test that the imported rotating Bloom filter is correct""" with NamedTemporaryFile(dir=os.getcwd(), suffix=".rbf", delete=DELETE_TEMP_FILES) as fobj: blm = RotatingBloomFilter(est_elements=25, false_positive_rate=0.05) for i in range(15): blm.add("{}".format(i)) blm.push() blm.export(fobj.name) blm2 = RotatingBloomFilter(filepath=fobj.name) # test those that should be popped off... for i in range(5): self.assertEqual("{}".format(i) in blm2, False) # test things that would not be popped for i in range(6, 15): self.assertEqual("{}".format(i) in blm2, True) self.assertEqual(blm2.current_queue_size, 10) self.assertEqual(blm2.expansions, 9) self.assertEqual(blm2.elements_added, 15)
def test_rbf_rotate(self): ''' test that the bloom filter rotates the first bloom off the stack ''' blm = RotatingBloomFilter(est_elements=10, false_positive_rate=0.05, max_queue_size=5) self.assertEqual(blm.expansions, 0) blm.add('test') self.assertEqual(blm.expansions, 0) for i in range(10): blm.add('{}'.format(i), force=True) self.assertEqual(blm.expansions, 1) self.assertEqual(blm.current_queue_size, 2) self.assertEqual(blm.check('test'), True) for i in range(10, 20): blm.add('{}'.format(i), force=True) self.assertEqual(blm.check('test'), True) self.assertEqual(blm.current_queue_size, 3) for i in range(20, 30): blm.add('{}'.format(i), force=True) self.assertEqual(blm.check('test'), True) self.assertEqual(blm.current_queue_size, 4) for i in range(30, 40): blm.add('{}'.format(i), force=True) self.assertEqual(blm.check('test'), True) self.assertEqual(blm.current_queue_size, 5) for i in range(40, 50): blm.add('{}'.format(i), force=True) self.assertEqual(blm.check('test'), False) # it should roll off self.assertEqual(blm.current_queue_size, 5)