Example #1
0
    def bitsizeof(self, bitposition: int) -> int:
        """
        Returns length of array stored in the bit stream in bits.

        :param bitposition: Current bit stream position.
        :returns: Length of the array stored in the bit stream in bits.
        """

        end_bitposition = bitposition
        size = len(self._raw_array)
        if self._is_auto:
            end_bitposition += bitsizeof_varsize(size)

        if self._array_traits.HAS_BITSIZEOF_CONSTANT and size > 0:
            element_size = self._array_traits.bitsizeof()
            if self._set_offset_method is None:
                end_bitposition += size * element_size
            else:
                end_bitposition = alignto(8, end_bitposition)
                end_bitposition += element_size + (size - 1) * alignto(
                    8, element_size)
        else:
            for element in self._raw_array:
                if self._set_offset_method is not None:
                    end_bitposition = alignto(8, end_bitposition)
                end_bitposition += self._array_traits.bitsizeof(
                    end_bitposition, element)

        return end_bitposition - bitposition
Example #2
0
    def bitsizeof(_bitposition: int, value: int) -> int:
        """
        Returns length of Zserio varsize type stored in the bit stream in bits.

        :param _bitposition: Not used.
        :param value: Zserio varsize type value.
        :returns: Length of given Zserio varsize type in bits.
        """

        return bitsizeof_varsize(value)
Example #3
0
    def write_varsize(self, value: int) -> None:
        """
        Writes a variable size integer value to the underlying storage.

        :param value: Value to write.
        :raises PythonRuntimeException: If the value is out of the range.
        """

        self._write_varnum(value,
                           5,
                           bitsizeof_varsize(value) // 8,
                           is_signed=False)
Example #4
0
    def _test_array(self, array_traits, array1_values, array1_bitsizeof,
                    array1_aligned_bitsizeof, array2_values):
        self._test_eq(array_traits, array1_values, array2_values)
        self._test_hashcode(array_traits, array1_values, array2_values)
        self._test_len(array_traits, array1_values)
        self._test_get_item(array_traits, array1_values)
        self._test_set_item(array_traits, array1_values)
        self._test_raw_array(array_traits, array1_values, array2_values)

        auto_bitsize = bitsizeof_varsize(len(array1_values))

        self._test_array_normal(array_traits, array1_values, array1_bitsizeof)
        self._test_array_auto(array_traits, array1_values,
                              auto_bitsize + array1_bitsizeof)
        self._test_array_aligned(array_traits, array1_values,
                                 array1_aligned_bitsizeof)
        self._test_array_aligned_auto(array_traits, array1_values,
                                      auto_bitsize + array1_aligned_bitsizeof)
        self._test_array_implicit(array_traits, array1_values,
                                  array1_bitsizeof)
Example #5
0
    def initialize_offsets(self, bitposition: int) -> int:
        """
        Initializes indexed offsets for the array.

        :param bitposition: Current bit stream position.
        :returns: Updated bit stream position which points to the first bit after the array.
        """

        end_bitposition = bitposition
        size = len(self._raw_array)
        if self._is_auto:
            end_bitposition += bitsizeof_varsize(size)

        for index in range(size):
            if self._set_offset_method is not None:
                end_bitposition = alignto(8, end_bitposition)
                self._set_offset_method(index, end_bitposition)
            end_bitposition = self._array_traits.initialize_offsets(
                end_bitposition, self._raw_array[index])

        return end_bitposition
Example #6
0
    def _test_packed_array(self,
                           array_traits,
                           array_values,
                           array_bitsizeof=None,
                           array_aligned_bitsizeof=None):

        self._test_packed_array_normal(array_traits, array_values,
                                       array_bitsizeof)

        auto_size_bitsize = bitsizeof_varsize(len(array_values))
        auto_bitsize = auto_size_bitsize + array_bitsizeof if array_bitsizeof is not None else None
        self._test_packed_array_auto(array_traits, array_values, auto_bitsize)

        self._test_packed_array_aligned(array_traits, array_values,
                                        array_aligned_bitsizeof)

        auto_aligned_bitsize = (auto_size_bitsize + array_aligned_bitsizeof if
                                array_aligned_bitsizeof is not None else None)
        self._test_packed_array_aligned_auto(array_traits, array_values,
                                             auto_aligned_bitsize)

        self._test_packed_array_implicit(array_traits, array_values,
                                         array_bitsizeof)
Example #7
0
    def test_bitsizeof_varsize(self):
        self.assertEqual(1 * 8, bitsizeof_varsize(0))

        self.assertEqual(1 * 8, bitsizeof_varsize(1 << (0)))
        self.assertEqual(1 * 8, bitsizeof_varsize((1 << (7)) - 1))

        self.assertEqual(2 * 8, bitsizeof_varsize(1 << (7)))
        self.assertEqual(2 * 8, bitsizeof_varsize((1 << (7 + 7)) - 1))

        self.assertEqual(3 * 8, bitsizeof_varsize(1 << (7 + 7)))
        self.assertEqual(3 * 8, bitsizeof_varsize((1 << (7 + 7 + 7)) - 1))

        self.assertEqual(4 * 8, bitsizeof_varsize(1 << (7 + 7 + 7)))
        self.assertEqual(4 * 8, bitsizeof_varsize((1 << (7 + 7 + 7 + 7)) - 1))

        self.assertEqual(5 * 8, bitsizeof_varsize(1 << (7 + 7 + 7 + 7)))
        self.assertEqual(5 * 8,
                         bitsizeof_varsize((1 << (2 + 7 + 7 + 7 + 8)) - 1))

        with self.assertRaises(PythonRuntimeException):
            bitsizeof_varsize(-1)  # below the lower bound

        with self.assertRaises(PythonRuntimeException):
            bitsizeof_varsize(1 <<
                              (2 + 7 + 7 + 7 + 8))  # above the upper bound