def test_decode_package_synchronization_bits(self): # all bytes ok packets = [0x00, 0x80] + [0x80] * 7 CMS50Dplus.decode_package(packets) # first byte wrong packets = [0x80, 0x80] + [0x80] * 7 self.assertRaises(ValueError, CMS50Dplus.decode_package, [1] * 9) # other bytes wrong for position in range(1, 9): packets = [0x00, 0x80] + [0x80] * 7 packets[position] = 0 self.assertRaises(ValueError, CMS50Dplus.decode_package, packets)
def test_decode_package_packet_length(self): packets = [0x00, 0x80] + [0x80] * 11 # length too short for length in range(0, 3): self.assertRaisesRegex(ValueError, 'too short', CMS50Dplus.decode_package, packets[:length]) # length ok for length in range(3, 10): CMS50Dplus.decode_package(packets[:length]) # length too long for length in range(10, 13): self.assertRaisesRegex(ValueError, 'too long', CMS50Dplus.decode_package, packets[:length])
def test_encode_package_decode_package(self): package_type = 0x00 for run in range(0, 10): original_package = test_package() packets = CMS50Dplus.encode_package(package_type, original_package) _, decoded_package = CMS50Dplus.decode_package(packets) self.assertEqual(decoded_package, original_package)
def test_decode_package_high_byte(self): # high: 0b11111111, packets: 0b10000000 => package: 0b10000000 packets = [0, 0xff] + [0x80] * 7 package_type, package = CMS50Dplus.decode_package(packets) for byte in package: self.assertEqual(byte, 0x80) # high: 0b11111111, packets: 0b11111111 => package: 0b11111111 packets = [0, 0xff] + [0xff] * 7 package_type, package = CMS50Dplus.decode_package(packets) for byte in package: self.assertEqual(byte, 0xff) # high: 0b10000000, packets: 0b10000000 => package: 0b00000000 packets = [0, 0x80] + [0x80] * 7 package_type, package = CMS50Dplus.decode_package(packets) for byte in package: self.assertEqual(byte, 0x00) # high: 0b10000000, packets: 0b11111111 => package: 0b01111111 packets = [0, 0x80] + [0xff] * 7 package_type, package = CMS50Dplus.decode_package(packets) for byte in package: self.assertEqual(byte, 0x7f)
def test_decode_package_package_types(self): for byte in range(0x00, 0x80): packets = [byte, 0x80] + [0x80] * 7 package_type, package = CMS50Dplus.decode_package(packets) self.assertEqual(byte, package_type)