Пример #1
0
 def _test_from_reader(self, array_traits, array_values):
     array = Array(array_traits, array_values)
     writer = BitStreamWriter()
     array.write(writer)
     reader = BitStreamReader(writer.byte_array)
     read_array = Array.from_reader(array_traits, reader, len(array_values))
     self.assertEqual(array, read_array)
Пример #2
0
    def _test_array_auto(self, array_traits, array_values, expected_bitsize):
        for i in range(8):
            array = Array(array_traits, array_values, is_auto=True)

            bitsize = array.bitsizeof(i)
            self.assertEqual(expected_bitsize, bitsize, i)
            self.assertEqual(i + bitsize, array.initialize_offsets(i), i)

            writer = BitStreamWriter()
            if i > 0:
                writer.write_bits(0, i)
            array.write(writer)
            self.assertEqual(i + bitsize, writer.bitposition, i)

            from_reader = BitStreamReader(writer.byte_array,
                                          writer.bitposition)
            self.assertEqual(0, from_reader.read_bits(i))
            read_array_from_reader = Array.from_reader(array_traits,
                                                       from_reader,
                                                       is_auto=True)
            self.assertEqual(array, read_array_from_reader, i)

            reader = BitStreamReader(writer.byte_array, writer.bitposition)
            if i > 0:
                self.assertEqual(0, reader.read_bits(i))
            read_array = Array(array_traits, is_auto=True)
            read_array.read(reader)
            self.assertEqual(array, read_array, i)
Пример #3
0
    def _test_array_implicit(self, array_traits, array_values,
                             expected_bitsize):
        for i in range(8):
            array = Array(array_traits, array_values, is_implicit=True)

            bitsize = array.bitsizeof(i)
            self.assertEqual(expected_bitsize, bitsize, i)
            self.assertEqual(i + bitsize, array.initialize_offsets(i), i)

            writer = BitStreamWriter()
            if i > 0:
                writer.write_bits(0, i)
            array.write(writer)
            self.assertEqual(i + bitsize, writer.bitposition, i)

            from_reader = BitStreamReader(writer.byte_array,
                                          writer.bitposition)
            if i > 0:
                self.assertEqual(0, from_reader.read_bits(i))
            if array_traits.HAS_BITSIZEOF_CONSTANT:
                read_array_from_reader = Array.from_reader(array_traits,
                                                           from_reader,
                                                           is_implicit=True)
                self.assertEqual(array, read_array_from_reader, i)
            else:
                with self.assertRaises(PythonRuntimeException):
                    Array.from_reader(array_traits,
                                      from_reader,
                                      is_implicit=True)

            reader = BitStreamReader(writer.byte_array, writer.bitposition)
            if i > 0:
                self.assertEqual(0, reader.read_bits(i))
            read_array = Array(array_traits, is_implicit=True)
            if array_traits.HAS_BITSIZEOF_CONSTANT:
                read_array.read(reader)
                self.assertEqual(array, read_array, i)
            else:
                with self.assertRaises(PythonRuntimeException):
                    read_array.read(reader)
Пример #4
0
    def _test_array_aligned(self, array_traits, array_values,
                            expected_bitsize):
        for i in range(8):
            array = Array(array_traits,
                          array_values,
                          set_offset_method=ArrayTest._set_offset_method,
                          check_offset_method=ArrayTest._check_offset_method)

            bitsize = array.bitsizeof(i)
            self.assertEqual(alignto(8, i) - i + expected_bitsize, bitsize, i)
            self.assertEqual(i + bitsize, array.initialize_offsets(i), i)

            writer = BitStreamWriter()
            if i > 0:
                writer.write_bits(0, i)
            array.write(writer)
            self.assertEqual(i + bitsize, writer.bitposition, i)

            from_reader = BitStreamReader(writer.byte_array,
                                          writer.bitposition)
            self.assertEqual(0, from_reader.read_bits(i))
            read_array_from_reader = Array.from_reader(
                array_traits,
                from_reader,
                len(array_values),
                set_offset_method=ArrayTest._set_offset_method,
                check_offset_method=ArrayTest._check_offset_method)
            self.assertEqual(array, read_array_from_reader, i)

            reader = BitStreamReader(writer.byte_array, writer.bitposition)
            if i > 0:
                self.assertEqual(0, reader.read_bits(i))
            read_array = Array(
                array_traits,
                set_offset_method=ArrayTest._set_offset_method,
                check_offset_method=ArrayTest._check_offset_method)
            read_array.read(reader, len(array_values))
            self.assertEqual(array, read_array, i)