Пример #1
0
 def test_add(self):
     aset = AddressSet(self.TABLE_LEN)
     addr = "".join(chr(b) for b in xrange(20))
     self.assertNotIn(addr, aset)
     aset.add(addr)
     self.assertIn(addr, aset)
     self.assertEqual(len(aset), 1)
Пример #2
0
 def test_false_positives(self):
     aset = AddressSet(131072, bytes_per_addr=5)  # reduce bytes_per_addr to increase failure probability
     rand_byte_count = aset._hash_bytes + aset._bytes_per_addr
     nonrand_prefix  = (20 - rand_byte_count) * "\0"
     for i in xrange(aset._max_len):
         aset.add(nonrand_prefix + "".join(chr(random.randrange(256)) for i in xrange(rand_byte_count)))
     for i in xrange(524288):
         self.assertNotIn(
             nonrand_prefix + "".join(chr(random.randrange(256)) for i in xrange(rand_byte_count)),
             aset)
Пример #3
0
 def test_file(self):
     aset = AddressSet(self.TABLE_LEN)
     addr = "".join(chr(b) for b in xrange(20))
     aset.add(addr)
     dbfile = tempfile.TemporaryFile()
     aset.tofile(dbfile)
     dbfile.seek(0)
     aset = AddressSet.fromfile(dbfile)
     self.assertTrue(dbfile.closed)  # should be closed by AddressSet in read-only mode
     self.assertIn(addr, aset)
     self.assertEqual(len(aset), 1)
Пример #4
0
 def test_false_positives(self):
     aset = AddressSet(1024, bytes_per_addr=8)
     rand_byte_count = aset._hash_bytes + aset._bytes_per_addr
     nonrand_prefix = (20 - rand_byte_count) * "\0"
     for i in range(aset._max_len):
         aset.add(nonrand_prefix + "".join(
             chr(random.randrange(256)) for i in range(rand_byte_count)))
     for i in range(8192):
         self.assertNotIn(
             nonrand_prefix + "".join(
                 chr(random.randrange(256))
                 for i in range(rand_byte_count)), aset)
Пример #5
0
 def test_pickle_mmap(self):
     aset = AddressSet(self.TABLE_LEN)
     addr = "".join(chr(b) for b in xrange(20))
     aset.add(addr)
     dbfile = tempfile.NamedTemporaryFile(delete=False)
     try:
         aset.tofile(dbfile)
         dbfile.seek(0)
         aset = AddressSet.fromfile(dbfile)  # now it's an mmap
         pickled = pickle.dumps(aset, protocol=pickle.HIGHEST_PROTOCOL)
         aset.close()  # also closes the file
         aset = pickle.loads(pickled)
         self.assertIn(addr, aset)
         self.assertEqual(len(aset), 1)
     finally:
         aset.close()
         dbfile.close()
         os.remove(dbfile.name)
Пример #6
0
 def test_file_update(self):
     aset = AddressSet(self.TABLE_LEN)
     dbfile = tempfile.NamedTemporaryFile(delete=False)
     try:
         aset.tofile(dbfile)
         dbfile.seek(0)
         aset = AddressSet.fromfile(dbfile, mmap_access=mmap.ACCESS_WRITE)
         addr = "".join(chr(b) for b in xrange(20))
         aset.add(addr)
         aset.close()
         self.assertTrue(dbfile.closed)
         dbfile = open(dbfile.name, "rb")
         aset = AddressSet.fromfile(dbfile)
         self.assertIn(addr, aset)
         self.assertEqual(len(aset), 1)
     finally:
         aset.close()
         dbfile.close()
         os.remove(dbfile.name)
Пример #7
0
 def test_null(self):
     aset = AddressSet(self.TABLE_LEN)
     addr = 20 * "\0"
     aset.add(addr)
     self.assertNotIn(addr, aset)
     self.assertEqual(len(aset), 0)