def test_byte_size_correctness(self): numbers = [-12, 12, 1200, 120091, 123456789] for num in numbers: if num < 0: bit_length = len(bin(num, None)) - 1 else: bit_length = len(bin(num, None)) count = int(math.ceil(bit_length / 8.0)) self.assertEqual(integer_byte_size(num), count, "Boom. for number %d, expected %d" % (num, count)) self.assertEqual(integer_byte_size(1 << 1023), 128) self.assertEqual(integer_byte_size((1 << 1024) - 1), 128) self.assertEqual(integer_byte_size(1 << 1024), 129)
def test_bit_length_correct(self): numbers = [ -12, 12, 1200, 120091, 123456789, ] for num in numbers: if num < 0: length = len(bin(num, None)) - 1 else: length = len(bin(num, None)) self.assertEqual(integer_bit_length(num), length) self.assertEqual(integer_bit_length_shift_counting(num), length) self.assertEqual(integer_bit_length_word_aligned(num), length) self.assertEqual(integer_bit_length(1023), 10) self.assertEqual(integer_bit_length(1024), 11) self.assertEqual(integer_bit_length(1025), 11) self.assertEqual(integer_bit_length(1 << 1024), 1025) self.assertEqual(integer_bit_length((1 << 1024) + 1), 1025) self.assertEqual(integer_bit_length((1 << 1024) - 1), 1024) self.assertEqual(integer_bit_length((1 << 32) - 1), 32) self.assertEqual(integer_bit_length((1 << 64) - 1), 64) self.assertEqual(integer_bit_length_shift_counting(1023), 10) self.assertEqual(integer_bit_length_shift_counting(1024), 11) self.assertEqual(integer_bit_length_shift_counting(1025), 11) self.assertEqual(integer_bit_length_shift_counting(1 << 1024), 1025) self.assertEqual(integer_bit_length_shift_counting((1 << 1024) + 1), 1025) self.assertEqual(integer_bit_length_shift_counting((1 << 1024) - 1), 1024) self.assertEqual(integer_bit_length_shift_counting((1 << 32) - 1), 32) self.assertEqual(integer_bit_length_shift_counting((1 << 64) - 1), 64) self.assertEqual(integer_bit_length_word_aligned(1023), 10) self.assertEqual(integer_bit_length_word_aligned(1024), 11) self.assertEqual(integer_bit_length_word_aligned(1025), 11) self.assertEqual(integer_bit_length_word_aligned(1 << 1024), 1025) self.assertEqual(integer_bit_length_word_aligned((1 << 1024) + 1), 1025) self.assertEqual(integer_bit_length_word_aligned((1 << 1024) - 1), 1024) self.assertEqual(integer_bit_length_word_aligned((1 << 32) - 1), 32) self.assertEqual(integer_bit_length_word_aligned((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(integer_bit_size(num), size) self.assertEqual(integer_bit_size(1023), 10) self.assertEqual(integer_bit_size(1024), 11) self.assertEqual(integer_bit_size(1025), 11) self.assertEqual(integer_bit_size(1 << 1024), 1025) self.assertEqual(integer_bit_size((1 << 1024) + 1), 1025) self.assertEqual(integer_bit_size((1 << 1024) - 1), 1024) self.assertEqual(integer_bit_size((1 << 32) - 1), 32) self.assertEqual(integer_bit_size((1 << 64) - 1), 64)
def test_binary_value(self): self.assertEqual(bin(12), '0b1100') self.assertEqual(bin(2 ** 32), '0b100000000000000000000000000000000')
def test_binary_0_1_and_minus_1(self): self.assertEqual(bin(0), '0b0') self.assertEqual(bin(1), '0b1') self.assertEqual(bin(-1), '-0b1')
def test_binary_no_prefix(self): self.assertEqual(bin(0, None), '0') self.assertEqual(bin(1, ''), '1') self.assertEqual(bin(12, None), '1100') self.assertEqual(bin(2 ** 32, None), '100000000000000000000000000000000') self.assertEqual(bin(-1200, None), '-10010110000')
def test_binary_custom_prefix(self): self.assertEqual(bin(0, 'B'), 'B0') self.assertEqual(bin(1, 'B'), 'B1') self.assertEqual(bin(12, 'B'), 'B1100') self.assertEqual(bin(2 ** 32, 'B'), 'B100000000000000000000000000000000') self.assertEqual(bin(-1200, 'B'), '-B10010110000')
def test_binary_default_prefix(self): self.assertEqual(bin(0), '0b0') self.assertEqual(bin(1), '0b1') self.assertEqual(bin(12), '0b1100') self.assertEqual(bin(2 ** 32), '0b100000000000000000000000000000000') self.assertEqual(bin(-1200), '-0b10010110000')
def test_binary_negative_value(self): self.assertEqual(bin(-1200), '-0b10010110000')