def _test_initialize_offsets(self, array_traits, array_values, expected_bitsize, expected_aligned_bitsize): array = Array(array_traits, array_values) self.assertEqual(0 + expected_bitsize, array.initialize_offsets(0)) self.assertEqual(7 + expected_bitsize, array.initialize_offsets(7)) auto_array = Array(array_traits, array_values, is_auto=True) self.assertEqual( 0 + bitsizeof_varuint64(len(array_values)) + expected_bitsize, auto_array.initialize_offsets(0)) self.assertEqual( 7 + bitsizeof_varuint64(len(array_values)) + expected_bitsize, auto_array.initialize_offsets(7)) aligned_array = Array(array_traits, array_values, set_offset_method=ArrayTest._set_offset_method) self.assertEqual(0 + expected_aligned_bitsize, aligned_array.initialize_offsets(0)) self.assertEqual(1 + 7 + expected_aligned_bitsize, aligned_array.initialize_offsets(1)) self.assertEqual(3 + 5 + expected_aligned_bitsize, aligned_array.initialize_offsets(3)) self.assertEqual(5 + 3 + expected_aligned_bitsize, aligned_array.initialize_offsets(5)) self.assertEqual(7 + 1 + expected_aligned_bitsize, aligned_array.initialize_offsets(7))
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)
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)
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)