def test_random_exact_bits(self): for _ in range(1000): a = IntegerNative.random(exact_bits=8) self.failIf(a < 128) self.failIf(a >= 256) for bits_value in range(1024, 1024 + 8): a = IntegerNative.random(exact_bits=bits_value) self.failIf(a < 2**(bits_value - 1)) self.failIf(a >= 2**bits_value)
def test_random_max_bits(self): flag = False for _ in range(1000): a = IntegerNative.random(max_bits=8) flag = flag or a < 128 self.failIf(a >= 256) self.failUnless(flag) for bits_value in range(1024, 1024 + 8): a = IntegerNative.random(max_bits=bits_value) self.failIf(a >= 2**bits_value)
def test_random_max_bits(self): flag = False for _ in range(1000): a = IntegerNative.random(max_bits=8) flag = flag or a < 128 self.failIf(a>=256) self.failUnless(flag) for bits_value in range(1024, 1024 + 8): a = IntegerNative.random(max_bits=bits_value) self.failIf(a >= 2**bits_value)
def test_random_bits_custom_rng(self): class CustomRNG(object): def __init__(self): self.counter = 0 def __call__(self, size): self.counter += size return bchr(0) * size custom_rng = CustomRNG() a = IntegerNative.random(exact_bits=32, randfunc=custom_rng) self.assertEqual(custom_rng.counter, 4)