def test_pybien_debug(self): 'Check that Python BiEn has the correct progression of state' with patch('builtins.print' \ if sys.version_info.major > 2\ else '__builtin__.print') as mock_print: pybientropy.bien(Bits('0b1011')) mock_print.assert_has_calls([ call(' 1011 3 4 0.75 0.25 0.31 0.50 0.81 0 1 0.81'), call(' 110 2 3 0.67 0.33 0.39 0.53 0.92 1 2 1.84'), call(' 01 1 2 0.50 0.50 0.50 0.50 1.00 2 4 4.00'), call('6.648') ])
def test_py_bytes_vs_obj(self, max_s_len=24): ''' Check that the Python implementation of BiEn handles binary string input correctly. ''' for s_len in range(1, max_s_len): with self.subTest(s_len=s_len): ti = os.urandom(s_len) with warnings.catch_warnings(): if sys.version_info.major > 2: warnings.simplefilter('ignore') self.assertEqual(pybientropy.bien(Bits(bytes=ti)), pybientropy.bien(ti)) self.assertEqual(pybientropy.tbien(Bits(bytes=ti)), pybientropy.tbien(ti))
def run_large_byte_strings(s_byte_len=128, tolerance=0.001): ti = os.urandom(s_byte_len) with warnings.catch_warnings(): if sys.version_info.major > 2: warnings.simplefilter('ignore') pybien = pybientropy.bien(Bits(bytes=ti)) cbien = cbientropy.bien(ti) assert abs(pybien - cbien) < tolerance pytbien = pybientropy.tbien(Bits(bytes=ti)) ctbien = cbientropy.tbien(ti) assert abs(pytbien - ctbien) < tolerance return ti, cbien, ctbien
def test_odd_sizes(self): ''' Check that the Python and C implementations for BiEn and TBiEn match for bit strings with prime-number lengths. ''' input_set = set() for prime in PRIMES: with self.subTest(prime=prime): rand_s = Bits(bytes=os.urandom(int(prime / 8 + 1)))[:prime] input_set = input_set.union([rand_s]) self.assertEqual(len(rand_s), prime) with warnings.catch_warnings(): if sys.version_info.major > 2: warnings.simplefilter('ignore') self.assertAlmostEqual(cbientropy.bien(rand_s), pybientropy.bien(rand_s)) self.assertAlmostEqual(cbientropy.tbien(rand_s), pybientropy.tbien(rand_s)) # check that all the strings are distinct self.assertEqual(len(input_set), len(PRIMES))
def test_pybientropy_4bit(self): 'Check the Python BiEn implementation with 4-bit strings' for b, r in BIENTROPY_4BITS: with self.subTest(b=b, r=r): self.assertEqual(round_fun(pybientropy.bien(b)), r)