def test_another_example(self): assert get_fixed_bits(Decimal('12.9375'), 'h', 4) == (192 + 15).to_bytes(2, 'little') assert get_fixed_bits(Decimal('12.9375'), 'i', 4) == (192 + 15).to_bytes(4, 'little') assert get_fixed_bits(Decimal('12.9375'), 'q', 4) == (192 + 15).to_bytes(8, 'little')
def test_invalid_higher_bits(self): with self.assertRaises(ValueError): get_fixed_bits(257, 'i', 8) with self.assertRaises(ValueError): get_fixed_bits(256, 'i', 8) with self.assertRaises(ValueError): get_fixed_bits('hello', 'i', 3) with self.assertRaises(ValueError): get_fixed_bits(Decimal('20'), 'h', 4) with self.assertRaises(ValueError): get_fixed_bits(42, 'i', 12)
def test_invalid_higher_bits(self): with self.assertRaises(ValueError): get_fixed_bits(257, 'i', 32) with self.assertRaises(ValueError): get_fixed_bits(256, 'i', 32) with self.assertRaises(ValueError): get_fixed_bits('hello', 'i', 3) with self.assertRaises(ValueError): get_fixed_bits(Decimal('20'), 'h', 17) with self.assertRaises(ValueError): get_fixed_bits(42, 'i', 33)
def test_basic_example(self): assert get_fixed_bits(Decimal('0.9375'), 'B', 4) == (15).to_bytes(1, 'little')
def test_zero(self): assert get_fixed_bits(0, 'H', 8) == (0).to_bytes(2, 'big')
def test_valid_higher_bits(self): """Just make sure these all don't fail""" get_fixed_bits(255, 'I', 8) get_fixed_bits(15.5, 'I', 4) get_fixed_bits(22.75, 'i', 11) get_fixed_bits(Decimal('13.0'), 'q', 16)
def test_valid_formats(self): get_fixed_bits(5, 'h', 1) get_fixed_bits(15, 'L', 0)
def test_invalid_formats(self): with self.assertRaises(ValueError): get_fixed_bits(5, 'Z', 1) with self.assertRaises(ValueError): get_fixed_bits(8, 'f', 4)