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)
def _testFromReader(self, arrayTraits, arrayValues): array = Array(arrayTraits, arrayValues) writer = BitStreamWriter() array.write(writer) reader = BitStreamReader(writer.getByteArray()) readArray = Array.fromReader(arrayTraits, reader, len(arrayValues)) self.assertEqual(array, readArray)
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)
def _testRead(self, arrayTraits, arrayValues): def _checkOffsetMethod(_index, _bitOffset): pass array = Array(arrayTraits, arrayValues) writer = BitStreamWriter() array.write(writer) reader = BitStreamReader(writer.getByteArray()) emptyArray = Array(arrayTraits) emptyArray.read(reader, len(array.getRawArray())) self.assertEqual(array, emptyArray) autoArray = Array(arrayTraits, arrayValues, isAuto=True) writer = BitStreamWriter() autoArray.write(writer) reader = BitStreamReader(writer.getByteArray()) emptyAutoArray = Array(arrayTraits, isAuto=True) emptyAutoArray.read(reader, len(autoArray.getRawArray())) self.assertEqual(autoArray, emptyAutoArray) alignedArray = Array(arrayTraits, arrayValues, checkOffsetMethod=_checkOffsetMethod) writer = BitStreamWriter() alignedArray.write(writer) reader = BitStreamReader(writer.getByteArray()) emptyAlignedArray = Array(arrayTraits, checkOffsetMethod=_checkOffsetMethod) emptyAlignedArray.read(reader, len(alignedArray.getRawArray())) self.assertEqual(alignedArray, emptyAlignedArray) implicitArray = Array(arrayTraits, arrayValues, isImplicit=True) writer = BitStreamWriter() implicitArray.write(writer) reader = BitStreamReader(writer.getByteArray()) emptyImplicitArray = Array(arrayTraits, isImplicit=True) emptyImplicitArray.read(reader) # read implicit array can be bigger (for example, BoolArray) implicitRawArray = implicitArray.getRawArray() emptyImplicitRawArray = emptyImplicitArray.getRawArray() for index, implicitRawArrayElement in enumerate(implicitRawArray): self.assertEqual(implicitRawArrayElement, emptyImplicitRawArray[index])
def _test_read(self, array_traits, array_values): array = Array(array_traits, array_values) writer = BitStreamWriter() array.write(writer) reader = BitStreamReader(writer.byte_array) read_array = Array(array_traits) read_array.read(reader, len(array.raw_array)) self.assertEqual(array, read_array) auto_array = Array(array_traits, array_values, is_auto=True) writer = BitStreamWriter() auto_array.write(writer) reader = BitStreamReader(writer.byte_array) read_auto_array = Array(array_traits, is_auto=True) read_auto_array.read(reader, len(auto_array.raw_array)) self.assertEqual(auto_array, read_auto_array) aligned_array = Array( array_traits, array_values, check_offset_method=ArrayTest._check_offset_method) writer = BitStreamWriter() aligned_array.write(writer) reader = BitStreamReader(writer.byte_array) read_aligned_array = Array( array_traits, check_offset_method=ArrayTest._check_offset_method) read_aligned_array.read(reader, len(aligned_array.raw_array)) self.assertEqual(aligned_array, read_aligned_array) if array_traits.HAS_BITSIZEOF_CONSTANT and array_traits.bitsizeof( ) % 8 == 0: implicit_array = Array(array_traits, array_values, is_implicit=True) writer = BitStreamWriter() implicit_array.write(writer) reader = BitStreamReader(writer.byte_array) read_implicit_array = Array(array_traits, is_implicit=True) read_implicit_array.read(reader) self.assertEqual(implicit_array, read_implicit_array) elif not array_traits.HAS_BITSIZEOF_CONSTANT: with self.assertRaises(PythonRuntimeException): Array(array_traits, is_implicit=True).read(reader)
def _testRead(self, arrayTraits, arrayValues): def _checkOffsetMethod(_index, _bitOffset): pass array = Array(arrayTraits, arrayValues) writer = BitStreamWriter() array.write(writer) reader = BitStreamReader(writer.getByteArray()) readArray = Array(arrayTraits) readArray.read(reader, len(array.getRawArray())) self.assertEqual(array, readArray) autoArray = Array(arrayTraits, arrayValues, isAuto=True) writer = BitStreamWriter() autoArray.write(writer) reader = BitStreamReader(writer.getByteArray()) readAutoArray = Array(arrayTraits, isAuto=True) readAutoArray.read(reader, len(autoArray.getRawArray())) self.assertEqual(autoArray, readAutoArray) alignedArray = Array(arrayTraits, arrayValues, checkOffsetMethod=_checkOffsetMethod) writer = BitStreamWriter() alignedArray.write(writer) reader = BitStreamReader(writer.getByteArray()) readAlignedArray = Array(arrayTraits, checkOffsetMethod=_checkOffsetMethod) readAlignedArray.read(reader, len(alignedArray.getRawArray())) self.assertEqual(alignedArray, readAlignedArray) if arrayTraits.HAS_BITSIZEOF_CONSTANT and arrayTraits.bitSizeOf( ) % 8 == 0: implicitArray = Array(arrayTraits, arrayValues, isImplicit=True) writer = BitStreamWriter() implicitArray.write(writer) reader = BitStreamReader(writer.getByteArray()) readImplicitArray = Array(arrayTraits, isImplicit=True) readImplicitArray.read(reader) self.assertEqual(implicitArray, readImplicitArray) elif not arrayTraits.HAS_BITSIZEOF_CONSTANT: with self.assertRaises(PythonRuntimeException): Array(arrayTraits, isImplicit=True).read(reader)
def _test_write(self, array_traits, array_values, expected_bitsize, expected_aligned_bitsize): array = Array(array_traits, array_values) writer = BitStreamWriter() array.write(writer) self.assertEqual(expected_bitsize, writer.bitposition) auto_array = Array(array_traits, array_values, is_auto=True) writer = BitStreamWriter() auto_array.write(writer) self.assertEqual( bitsizeof_varuint64(len(array_values)) + expected_bitsize, writer.bitposition) aligned_array = Array( array_traits, array_values, check_offset_method=ArrayTest._check_offset_method) writer = BitStreamWriter() writer.write_bool(False) aligned_array.write(writer) self.assertEqual(1 + 7 + expected_aligned_bitsize, writer.bitposition)
def _testWrite(self, arrayTraits, arrayValues, expectedBitSize, expectedAlignedBitSize): def _checkOffsetMethod(_index, _bitOffset): pass array = Array(arrayTraits, arrayValues) writer = BitStreamWriter() array.write(writer) self.assertEqual(expectedBitSize, writer.getBitPosition()) autoArray = Array(arrayTraits, arrayValues, isAuto=True) writer = BitStreamWriter() autoArray.write(writer) self.assertEqual( getBitSizeOfVarUInt64(len(arrayValues)) + expectedBitSize, writer.getBitPosition()) alignedArray = Array(arrayTraits, arrayValues, checkOffsetMethod=_checkOffsetMethod) writer = BitStreamWriter() alignedArray.write(writer) self.assertEqual(expectedAlignedBitSize, writer.getBitPosition())