def test_bit_size_correct(self): numbers = [-12, 12, 1200, 120091, 123456789] for num in numbers: if num < 0: size = len(bin(num, None)) - 1 else: size = len(bin(num, None)) self.assertEqual(builtins.integer_bit_size(num), size) self.assertEqual(builtins.integer_bit_size(1023), 10) self.assertEqual(builtins.integer_bit_size(1024), 11) self.assertEqual(builtins.integer_bit_size(1025), 11) self.assertEqual(builtins.integer_bit_size(1 << 1024), 1025) self.assertEqual(builtins.integer_bit_size((1 << 1024) + 1), 1025) self.assertEqual(builtins.integer_bit_size((1 << 1024) - 1), 1024) self.assertEqual(builtins.integer_bit_size((1 << 32) - 1), 32) self.assertEqual(builtins.integer_bit_size((1 << 64) - 1), 64)
def test_bit_size_correct(self): numbers = [ -12, 12, 1200, 120091, 123456789, ] for num in numbers: if num < 0: size = len(bin(num, None)) - 1 else: size = len(bin(num, None)) self.assertEqual(builtins.integer_bit_size(num), size) self.assertEqual(builtins.integer_bit_size(1023), 10) self.assertEqual(builtins.integer_bit_size(1024), 11) self.assertEqual(builtins.integer_bit_size(1025), 11) self.assertEqual(builtins.integer_bit_size(1 << 1024), 1025) self.assertEqual(builtins.integer_bit_size((1 << 1024) + 1), 1025) self.assertEqual(builtins.integer_bit_size((1 << 1024) - 1), 1024) self.assertEqual(builtins.integer_bit_size((1 << 32) - 1), 32) self.assertEqual(builtins.integer_bit_size((1 << 64) - 1), 64)
def generate_random_uint_between(low, high, rand_func=generate_random_bytes): """ Generates a random long integer between low and high, not including high. :param low: Low :param high: High :param rand_func: Random bytes generator function. :returns: Random unsigned long integer value. """ if not (builtins.is_integer(low) and builtins.is_integer(high)): raise TypeError("unsupported argument types(s): %r and %r" % (type(low).__name__, type(high).__name__)) if low >= high: raise ValueError("high value must be greater than low value.") substrate = high - low - 1 bits = builtins.integer_bit_size(substrate) value = generate_random_uint_atmost(bits, rand_func=rand_func) while value > substrate: value = generate_random_uint_atmost(bits, rand_func=rand_func) return low + value
def test_bit_size_0_if_zero(self): self.assertEqual(builtins.integer_bit_size(0), 1)
def test_bit_size_0_if_zero(self): self.assertEqual(integer_bit_size(0), 1)