def test_c_bytes_vs_obj(self, max_s_len=128): ''' Check that the C Bien and TBiEn compute the same result for both string and bitstring input types ''' 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(cbientropy.bien(Bits(bytes=ti)), cbientropy.bien(ti)) self.assertEqual(cbientropy.tbien(Bits(bytes=ti)), cbientropy.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_cbientropy_4bit(self): 'Check the C BiEn function against the 4-bit strings' for b, r in BIENTROPY_4BITS: with self.subTest(b=b, r=r): self.assertEqual(round_fun(cbientropy.bien(b)), r)