def _testEq(self, arrayTraits, array1Values, array2Values): array1 = Array(arrayTraits, array1Values) array2 = Array(arrayTraits, array2Values) array3 = Array(arrayTraits, array1Values) self.assertNotEqual(array1, None) self.assertNotEqual(array1, array2) self.assertEqual(array1, array3)
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_eq(self, array_traits, array1_values, array2_values): array1 = Array(array_traits, array1_values) array2 = Array(array_traits, array2_values) array3 = Array(array_traits, array1_values) self.assertNotEqual(array1, None) self.assertNotEqual(array1, array2) self.assertEqual(array1, array3)
def _testSetItem(self, arrayTraits, arrayValues): array = Array(arrayTraits, arrayValues) rawArray = array.getRawArray() self.assertTrue(len(array) > 1) firstValue = array[0] secondValue = array[1] array[0] = secondValue self.assertEqual(array[0], rawArray[0]) rawArray[0] = firstValue # return the original value for other tests self.assertEqual(array[0], rawArray[0])
def _testBitSizeOf(self, arrayTraits, arrayValues, expectedBitSize, expectedAlignedBitSize): array = Array(arrayTraits, arrayValues) self.assertEqual(expectedBitSize, array.bitSizeOf(0)) self.assertEqual(expectedBitSize, array.bitSizeOf(7)) autoArray = Array(arrayTraits, arrayValues, isAuto=True) self.assertEqual( getBitSizeOfVarUInt64(len(arrayValues)) + expectedBitSize, autoArray.bitSizeOf(0)) self.assertEqual( getBitSizeOfVarUInt64(len(arrayValues)) + expectedBitSize, autoArray.bitSizeOf(7)) alignedArray = Array(arrayTraits, arrayValues, setOffsetMethod=not None) self.assertEqual(expectedAlignedBitSize, alignedArray.bitSizeOf(0))
def _test_set_item(self, array_traits, array_values): array = Array(array_traits, array_values) raw_array = array.raw_array self.assertTrue(len(array) > 1) first_value = array[0] second_value = array[1] array[0] = second_value self.assertEqual(array[0], raw_array[0]) raw_array[0] = first_value # return the original value for other tests self.assertEqual(array[0], raw_array[0])
def _testInitializeOffsets(self, arrayTraits, arrayValues, expectedBitSize, expectedAlignedBitSize): def _setOffsetMethod(_index, _bitOffset): pass array = Array(arrayTraits, arrayValues) self.assertEqual(0 + expectedBitSize, array.initializeOffsets(0)) self.assertEqual(7 + expectedBitSize, array.initializeOffsets(7)) autoArray = Array(arrayTraits, arrayValues, isAuto=True) self.assertEqual( 0 + getBitSizeOfVarUInt64(len(arrayValues)) + expectedBitSize, autoArray.initializeOffsets(0)) self.assertEqual( 7 + getBitSizeOfVarUInt64(len(arrayValues)) + expectedBitSize, autoArray.initializeOffsets(7)) alignedArray = Array(arrayTraits, arrayValues, setOffsetMethod=_setOffsetMethod) self.assertEqual(0 + expectedAlignedBitSize, alignedArray.initializeOffsets(0))
def _test_packed_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_packed(i) if expected_bitsize is not None: self.assertEqual(expected_bitsize, bitsize) self.assertEqual(i + bitsize, array.initialize_offsets_packed(i), i) writer = BitStreamWriter() if i > 0: writer.write_bits(0, i) array.write_packed(writer) self.assertEqual(i + bitsize, writer.bitposition, i) 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) with self.assertRaises(PythonRuntimeException): read_array.read_packed(reader)
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_get_item(self, array_traits, array_values): array = Array(array_traits, array_values) raw_array = array.raw_array for value, raw_value in zip(array, raw_array): self.assertEqual(value, raw_value)
def _test_len(self, array_traits, array_values): array = Array(array_traits, array_values) raw_array = array.raw_array self.assertEqual(len(raw_array), len(array))
def _test_hashcode(self, array_traits, array1_values, array2_values): array1 = Array(array_traits, array1_values) array2 = Array(array_traits, array2_values) array3 = Array(array_traits, array1_values) self.assertNotEqual(hash(array1), hash(array2)) self.assertEqual(hash(array1), hash(array3))
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_packed_array_normal(self, array_traits, array_values, expected_bitsize): for i in range(8): array = Array(array_traits, array_values) bitsize = array.bitsizeof_packed(i) if expected_bitsize is not None: self.assertEqual(expected_bitsize, bitsize) self.assertEqual(i + bitsize, array.initialize_offsets_packed(i), i) writer = BitStreamWriter() if i > 0: writer.write_bits(0, i) array.write_packed(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_packed( array_traits, from_reader, len(array_values)) 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) read_array.read_packed(reader, len(array_values)) self.assertEqual(array, read_array, i)
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())
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_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 _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 _test_raw_array(self, array_traits, array1_values, array2_values): array1 = Array(array_traits, array1_values) array2 = Array(array_traits, array2_values) array3 = Array(array_traits, array1_values) self.assertNotEqual(array1.raw_array, array2.raw_array) self.assertEqual(array1.raw_array, array3.raw_array)
def _testGetItem(self, arrayTraits, arrayValues): array = Array(arrayTraits, arrayValues) rawArray = array.getRawArray() for value, rawValue in zip(array, rawArray): self.assertEqual(value, rawValue)
def _testLen(self, arrayTraits, arrayValues): array = Array(arrayTraits, arrayValues) rawArray = array.getRawArray() self.assertEqual(len(rawArray), len(array))
def _testHashCode(self, arrayTraits, array1Values, array2Values): array1 = Array(arrayTraits, array1Values) array2 = Array(arrayTraits, array2Values) array3 = Array(arrayTraits, array1Values) self.assertNotEqual(hash(array1), hash(array2)) self.assertEqual(hash(array1), hash(array3))
def _test_packed_array_aligned_auto(self, array_traits, array_values, expected_bitsize): for i in range(8): array = Array(array_traits, array_values, is_auto=True, set_offset_method=ArrayTest._set_offset_method, check_offset_method=ArrayTest._check_offset_method) bitsize = array.bitsizeof_packed(i) if expected_bitsize is not None and i == 0: self.assertEqual(expected_bitsize, bitsize) self.assertEqual(i + bitsize, array.initialize_offsets_packed(i), i) writer = BitStreamWriter() if i > 0: writer.write_bits(0, i) array.write_packed(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_packed( array_traits, from_reader, is_auto=True, 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, is_auto=True, set_offset_method=ArrayTest._set_offset_method, check_offset_method=ArrayTest._check_offset_method) read_array.read_packed(reader) self.assertEqual(array, read_array, i)
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 _testGetRawArray(self, arrayTraits, array1Values, array2Values): array1 = Array(arrayTraits, array1Values) array2 = Array(arrayTraits, array2Values) array3 = Array(arrayTraits, array1Values) self.assertNotEqual(array1.getRawArray(), array2.getRawArray()) self.assertEqual(array1.getRawArray(), array3.getRawArray())
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 _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)