def testAlignTo(self): writer = BitStreamWriter() writer.writeBits(5, 3) writer.alignTo(8) self.assertEqual(8, writer.getBitPosition()) writer.writeBits(0, 1) writer.alignTo(16) self.assertEqual(16, writer.getBitPosition()) writer.writeBits(0xaa, 9) writer.alignTo(32) self.assertEqual(32, writer.getBitPosition()) writer.writeBits(0xaca, 13) writer.alignTo(64) self.assertEqual(64, writer.getBitPosition()) writer.writeBits(0xcafe, 16) reader = BitStreamReader(buffer=writer.getByteArray()) self.assertEqual(5, reader.readBits(3)) reader.alignTo(8) self.assertEqual(8, reader.getBitPosition()) self.assertEqual(0, reader.readBits(1)) reader.alignTo(16) self.assertEqual(16, reader.getBitPosition()) self.assertEqual(0xaa, reader.readBits(9)) reader.alignTo(32) self.assertEqual(32, reader.getBitPosition()) self.assertEqual(0xaca, reader.readBits(13)) reader.alignTo(64) self.assertEqual(64, reader.getBitPosition()) self.assertEqual(0xcafe, reader.readBits(16))
def write(self, writer: BitStreamWriter, value: int) -> None: """ Writes unsigned fixed integer Zserio type to the bit stream. :param writer: Bit stream where to write. :param value: Unsigned fixed integer Zserio type to write. """ writer.writeBits(value, self._numBits)
def testFile(self): testFilename = "BitStreamTest.bin" writer = BitStreamWriter() writer.writeBits(13, 7) writer.writeString(testFilename) writer.writeVarInt(-123456) writer.toFile(testFilename) reader = BitStreamReader.fromFile(testFilename) self.assertEqual(13, reader.readBits(7)) self.assertEqual(testFilename, reader.readString()) self.assertEqual(-123456, reader.readVarInt())
def _testImpl(self, writeMethod, readMethod, values, maxStartBitPos): for bitPos in range(maxStartBitPos): writer = BitStreamWriter() if bitPos > 0: writer.writeBits(0, bitPos) for value in values: writeMethod(writer, value) reader = BitStreamReader(buffer=writer.getByteArray()) if bitPos > 0: reader.readBits(bitPos) for value in values: self.assertEqual(value, readMethod(reader), "[bitPos=%d]" % bitPos)
def _testBitsImpl(self, writeMethod, readMethod, values, numBits): for bitPos in range(numBits): writer = BitStreamWriter() if bitPos > 0: writer.writeBits(0, bitPos) for value in values: writeMethod(writer, value, numBits) reader = BitStreamReader(buffer=writer.getByteArray()) if bitPos > 0: reader.readBits(bitPos) for value in values: self.assertEqual(value, readMethod(reader, numBits), "[numBits=%d, bitPos=%d]" % (numBits, bitPos))
def testWriteUnalignedData(self): # number expected to be written at offset testValue = 123 for offset in range(65): writer = BitStreamWriter() if offset != 0: writer.writeBits(0, offset) writer.writeBits(testValue, 8) # check written value buffer = writer.getByteArray() writtenTestValue = buffer[offset // 8] << (offset % 8) if offset % 8 != 0: writtenTestValue |= buffer[offset // 8 + 1] >> (8 - (offset % 8)) self.assertEqual(testValue, writtenTestValue, msg=("Offset: " + str(offset)))
def testBitPosition(self): writer = BitStreamWriter() writer.writeBits(0xaaaa, 16) self.assertEqual(16, writer.getBitPosition()) writer.writeBits(0xff, 8) self.assertEqual(24, writer.getBitPosition()) reader = BitStreamReader(buffer=writer.getByteArray()) self.assertEqual(0xaaaa, reader.readBits(16)) self.assertEqual(16, reader.getBitPosition()) reader.setBitPosition(8) self.assertEqual(8, reader.getBitPosition()) self.assertEqual(0xaaff, reader.readBits(16)) reader.setBitPosition(13) self.assertEqual(13, reader.getBitPosition()) self.assertEqual(0x02, reader.readBits(3)) self.assertEqual(16, reader.getBitPosition()) self.assertEqual(0xff, reader.readBits(8)) self.assertEqual(24, reader.getBitPosition()) reader.setBitPosition(0) self.assertEqual(0, reader.getBitPosition()) self.assertEqual(0xaaaaff, reader.readBits(24))
def testWriteBits(self): writer = BitStreamWriter() writer.writeBits(0, 8) writer.writeBits(255, 8) writer.writeBits(1, 1) writer.writeBits(0x3f, 6) writer.writeBits(1, 1) self.assertEqual(b'\x00\xff\xff', writer.getByteArray()) self.assertEqual(3 * 8, writer.getBitPosition()) writer.writeBits(0xff, 8) self.assertEqual(b'\x00\xff\xff\xff', writer.getByteArray()) self.assertEqual(4 * 8, writer.getBitPosition()) writer.writeBits(0, 4) self.assertEqual(b'\x00\xff\xff\xff\x00', writer.getByteArray()) self.assertEqual(4 * 8 + 4, writer.getBitPosition()) writer.writeBits(0x0f, 4) self.assertEqual(b'\x00\xff\xff\xff\x0f', writer.getByteArray()) self.assertEqual(5 * 8, writer.getBitPosition()) writer.writeBits(0x80, 8) self.assertEqual(b'\x00\xff\xff\xff\x0f\x80', writer.getByteArray()) self.assertEqual(6 * 8, writer.getBitPosition()) with self.assertRaises(PythonRuntimeException): writer.writeBits(1, 0) # zero bits! with self.assertRaises(PythonRuntimeException): writer.writeBits(1, -1) # negative number of bits! with self.assertRaises(PythonRuntimeException): writer.writeBits(256, 8) # above the upper bound with self.assertRaises(PythonRuntimeException): writer.writeBits(-1, 8) # below the lower bound