test_basic.py (1655B)
1 import unittest 2 import hashlib 3 import logging 4 5 from moolb import Bloom 6 7 logging.basicConfig(level=logging.DEBUG) 8 logg = logging.getLogger() 9 10 11 def hashround(self, b, s): 12 logg.debug('sha1 hashing {} {}'.format(b.hex(), s.hex())) 13 h = hashlib.sha1() 14 h.update(b) 15 h.update(s) 16 return h.digest() 17 18 19 class Test(unittest.TestCase): 20 21 def test_basic(self): 22 f = Bloom(8192 * 8, 3) 23 f.add(b'1024') 24 self.assertTrue(f.check(b'1024')) 25 self.assertFalse(f.check(b'1023')) 26 27 28 def test_defaul(self): 29 b = bytearray(8192) 30 b[42] = 13 31 f = Bloom(8192 * 8, 3, default_data=b) 32 self.assertEqual(f.filter[42], 13) 33 34 b = bytearray(8193) 35 with self.assertRaises(ValueError): 36 f = Bloom(8192 * 8, 3, default_data=b) 37 38 39 def test_plug(self): 40 f = Bloom(8192 * 8, 3, hashround) 41 f.add(b'1024') 42 self.assertTrue(f.check(b'1024')) 43 self.assertFalse(f.check(b'1023')) 44 45 46 def test_merge(self): 47 f = Bloom(8 * 8, 3, hashround) 48 b = bytearray(8) 49 b[2] = 2 50 b[6] = 4 51 f.merge(b) 52 self.assertEqual(f.filter[2], 2) 53 self.assertEqual(f.filter[6], 4) 54 55 b = bytearray(8) 56 b[2] = 1 57 b[6] = 8 58 f.merge(b) 59 self.assertEqual(f.filter[2], 3) 60 self.assertEqual(f.filter[6], 12) 61 62 b = bytearray(9) 63 with self.assertRaises(ValueError): 64 f.merge(b) 65 66 67 # def test_dump(self): 68 # f = Bloom(8192 * 8, 3) 69 # f.add(b'1024') 70 # logg.debug(f.to_bytes().hex()) 71 72 if __name__ == '__main__': 73 unittest.main()