def bitSizeOf(_bitPosition, value): """ Returns length of Zserio varint type stored in the bit stream in bits. :param _bitPosition: Not used. :param value: Zserio varint type value. :returns: Length of given Zserio varint type in bits. """ return getBitSizeOfVarInt(value)
def writeVarInt(self, value: int) -> None: """ Writes a variable signed integer value (up to 9 bytes) to the underlying storage. :param value: Value to write. :raises PythonRuntimeException: If the value is out of the range. """ if value == INT64_MIN: self.writeBits(0x80, 8) # INT64_MIN is stored as -0 else: self._writeVarNum(value, 9, getBitSizeOfVarInt(value) // 8, isSigned=True)
def writeVarInt(self, value): """ Writes a variable signed integer value (up to 9 bytes) to the underlying storage. :param value: Value to write. :raises PythonRuntimeException: If the value is out of the range. """ if value == INT64_MIN: self.writeBits(0x80, 8) # INT64_MIN is stored as -0 else: numBytes = getBitSizeOfVarInt(value) // 8 self._writeVarNum(abs(value), numBytes, VARINT_NUM_BITS, sign=(value < 0))
def testGetBitSizeOfVarInt(self): self.assertEqual(8, getBitSizeOfVarInt(0)) self.assertEqual(8, getBitSizeOfVarInt(-(1 << 6) + 1)) self.assertEqual(8, getBitSizeOfVarInt((1 << 6) - 1)) self.assertEqual(16, getBitSizeOfVarInt(-(1 << 6))) self.assertEqual(16, getBitSizeOfVarInt((1 << 6))) self.assertEqual(16, getBitSizeOfVarInt(-(1 << 13) + 1)) self.assertEqual(16, getBitSizeOfVarInt((1 << 13) - 1)) self.assertEqual(24, getBitSizeOfVarInt(-(1 << 13))) self.assertEqual(24, getBitSizeOfVarInt((1 << 13))) self.assertEqual(24, getBitSizeOfVarInt(-(1 << 20) + 1)) self.assertEqual(24, getBitSizeOfVarInt((1 << 20) - 1)) self.assertEqual(32, getBitSizeOfVarInt(-(1 << 20))) self.assertEqual(32, getBitSizeOfVarInt((1 << 20))) self.assertEqual(32, getBitSizeOfVarInt(-(1 << 27) + 1)) self.assertEqual(32, getBitSizeOfVarInt((1 << 27) - 1)) self.assertEqual(40, getBitSizeOfVarInt(-(1 << 27))) self.assertEqual(40, getBitSizeOfVarInt((1 << 27))) self.assertEqual(40, getBitSizeOfVarInt(-(1 << 34) + 1)) self.assertEqual(40, getBitSizeOfVarInt((1 << 34) - 1)) self.assertEqual(48, getBitSizeOfVarInt(-(1 << 34))) self.assertEqual(48, getBitSizeOfVarInt((1 << 34))) self.assertEqual(48, getBitSizeOfVarInt(-(1 << 41) + 1)) self.assertEqual(48, getBitSizeOfVarInt((1 << 41) - 1)) self.assertEqual(56, getBitSizeOfVarInt(-(1 << 41))) self.assertEqual(56, getBitSizeOfVarInt((1 << 41))) self.assertEqual(56, getBitSizeOfVarInt(-(1 << 48) + 1)) self.assertEqual(56, getBitSizeOfVarInt((1 << 48) - 1)) self.assertEqual(64, getBitSizeOfVarInt(-(1 << 48))) self.assertEqual(64, getBitSizeOfVarInt((1 << 48))) self.assertEqual(64, getBitSizeOfVarInt(-(1 << 55) + 1)) self.assertEqual(64, getBitSizeOfVarInt((1 << 55) - 1)) self.assertEqual(72, getBitSizeOfVarInt(-(1 << 55))) self.assertEqual(72, getBitSizeOfVarInt((1 << 55))) self.assertEqual(72, getBitSizeOfVarInt(-(1 << 63) + 1)) self.assertEqual(72, getBitSizeOfVarInt((1 << 63) - 1)) # special case, INT64_MIN is stored as -0 self.assertEqual(8, getBitSizeOfVarInt(INT64_MIN)) with self.assertRaises(PythonRuntimeException): getBitSizeOfVarInt(INT64_MIN - 1) # below the lower bound with self.assertRaises(PythonRuntimeException): getBitSizeOfVarInt(1 << 63) # above the upper bound