def testQBitArrayNull(self): '''QDataStream <<>> QBitArray - null''' self.stream << QBitArray() res = QBitArray() self.read_stream >> res self.assertEqual(res, QBitArray())
def _check_bitarray(self, data_set): '''Check the >> operator for the given data set''' for data, expectedStatus, expectedString in data_set: stream = QDataStream(data, QIODevice.ReadOnly) string = QBitArray() stream >> string self.assertEqual(stream.status(), expectedStatus) self.assertEqual(string.size(), expectedString.size()) self.assertEqual(string, expectedString)
def testPastEnd(self): '''QDataStream >> QBitArray reading past the end of the data''' data = [] data.append((QByteArray(), QDataStream.ReadPastEnd, QBitArray())) data.append( (QByteArray(py3k.b('\x00')), QDataStream.ReadPastEnd, QBitArray())) data.append((QByteArray(py3k.b('\x00\x00')), QDataStream.ReadPastEnd, QBitArray())) data.append( (QByteArray(py3k.b('\x00\x00\x00')), QDataStream.ReadPastEnd, QBitArray())) data.append( (QByteArray(py3k.b('\x00\x00\x00\x01')), QDataStream.ReadPastEnd, QBitArray())) data.append( (QByteArray(py3k.b('\x00\x00\x00\x02')), QDataStream.ReadPastEnd, QBitArray())) data.append( (QByteArray(py3k.b('\x00\x00\x00\x03')), QDataStream.ReadPastEnd, QBitArray())) data.append( (QByteArray(py3k.b('\x00\x00\x00\x04')), QDataStream.ReadPastEnd, QBitArray())) self._check_bitarray(data)
def setUp(self): #Acquire resources self.qbitarray = QBitArray(3) self.qbitarray_values = [True, False, False] # WARNING: do not pythonify the following loop for i in range(len(self.qbitarray_values)): self.qbitarray.setBit(i, self.qbitarray_values[i]) self.inverted_qbitarray_values = [not bit for bit in self.qbitarray_values] self.other_qbitarray = QBitArray(3) self.other_qbitarray_values = [True, True, False] # WARNING: do not pythonify the following loop for i in range(len(self.other_qbitarray_values)): self.other_qbitarray.setBit(i, self.other_qbitarray_values[i])
def testOk(self): '''QDataStream with valid QBitArray''' data = [] data.append( (QByteArray('\x00\x00\x00\x00'), QDataStream.Ok, QBitArray())) data.append((QByteArray('\x00\x00\x00\x01\x00'), QDataStream.Ok, create_bitarray('0'))) data.append((QByteArray('\x00\x00\x00\x01\x01'), QDataStream.Ok, create_bitarray('1'))) data.append((QByteArray('\x00\x00\x00\x02\x03'), QDataStream.Ok, create_bitarray('11'))) data.append((QByteArray('\x00\x00\x00\x03\x07'), QDataStream.Ok, create_bitarray('111'))) data.append((QByteArray('\x00\x00\x00\x04\x0f'), QDataStream.Ok, create_bitarray('1111'))) data.append((QByteArray('\x00\x00\x00\x05\x1f'), QDataStream.Ok, create_bitarray('11111'))) data.append((QByteArray('\x00\x00\x00\x06\x3f'), QDataStream.Ok, create_bitarray('111111'))) data.append((QByteArray('\x00\x00\x00\x07\x7f'), QDataStream.Ok, create_bitarray('1111111'))) data.append((QByteArray('\x00\x00\x00\x07\x7e'), QDataStream.Ok, create_bitarray('0111111'))) data.append((QByteArray('\x00\x00\x00\x07\x00'), QDataStream.Ok, create_bitarray('0000000'))) data.append((QByteArray('\x00\x00\x00\x07\x39'), QDataStream.Ok, create_bitarray('1001110'))) self._check_bitarray(data)
def testQBitArrayValid(self): '''QDataStream <<>> QBitArray - valid''' self.stream << create_bitarray('01010101') res = QBitArray() self.read_stream >> res self.assertEqual(res, create_bitarray('01010101'))
def testCorruptData(self): '''QDataStream reading corrupt data''' data = [] data.append((QByteArray(py3k.b('\x00\x00\x00\x01\x02')), QDataStream.ReadCorruptData, QBitArray())) self._check_bitarray(data)
def create_bitarray(string): array = QBitArray(len(string)) for i, char in enumerate(string): array.setBit(i, char != '0') return array
class QBitArrayIsIterableTest(unittest.TestCase): '''Tests if QBitArray class is iterable and also '~' (__invert__) and bitwise operators''' def setUp(self): #Acquire resources self.qbitarray = QBitArray(3) self.qbitarray_values = [True, False, False] # WARNING: do not pythonify the following loop for i in range(len(self.qbitarray_values)): self.qbitarray.setBit(i, self.qbitarray_values[i]) self.inverted_qbitarray_values = [ not bit for bit in self.qbitarray_values ] self.other_qbitarray = QBitArray(3) self.other_qbitarray_values = [True, True, False] # WARNING: do not pythonify the following loop for i in range(len(self.other_qbitarray_values)): self.other_qbitarray.setBit(i, self.other_qbitarray_values[i]) def tearDown(self): #Release resources del self.qbitarray del self.other_qbitarray del self.qbitarray_values del self.other_qbitarray_values del self.inverted_qbitarray_values def testQBitArrayIsIterable(self): #Tests if QBitArray class is iterable qbitarray_is_iterable = True try: bitarray = [bit for bit in self.qbitarray] except: qbitarray_is_iterable = False self.assertTrue(qbitarray_is_iterable) def testQBitArrayInvertOperator(self): #Tests QBitArray '~' (__invert__) operator inverted_qbitarray = ~self.qbitarray # WARNING: do not pythonify the following loop, the # iterability of QBitArray class is tested in another place inverted_qbitarray_values = bool_list_from_qbitarray( inverted_qbitarray) self.assertEqual(self.inverted_qbitarray_values, inverted_qbitarray_values) def testQBitArrayOrBitwiseOperator(self): #Tests QBitArray '|' (or) operator has_or_bitwise_operator = True ored_qbitarray, ored_bool_list = None, None try: ored_qbitarray = self.qbitarray | self.other_qbitarray ored_bool_list = [ b1 | b2 for b1, b2 in zip(self.qbitarray_values, self.other_qbitarray_values) ] except: has_or_bitwise_operator = False self.assertTrue(has_or_bitwise_operator) self.assertEqual(bool_list_from_qbitarray(ored_qbitarray), ored_bool_list) def testQBitArrayAndBitwiseOperator(self): #Tests QBitArray '&' (and) operator has_and_bitwise_operator = True anded_qbitarray, anded_bool_list = None, None try: anded_qbitarray = self.qbitarray | self.other_qbitarray anded_bool_list = [ b1 | b2 for b1, b2 in zip(self.qbitarray_values, self.other_qbitarray_values) ] except: has_and_bitwise_operator = False self.assertTrue(has_and_bitwise_operator) self.assertEqual(bool_list_from_qbitarray(anded_qbitarray), anded_bool_list) def testQBitArrayXorBitwiseOperator(self): #Tests QBitArray '^' (xor) operator has_xor_bitwise_operator = True xored_qbitarray, xored_bool_list = None, None try: xored_qbitarray = self.qbitarray | self.other_qbitarray xored_bool_list = [ b1 | b2 for b1, b2 in zip(self.qbitarray_values, self.other_qbitarray_values) ] except: has_xor_bitwise_operator = False self.assertTrue(has_xor_bitwise_operator) self.assertEqual(bool_list_from_qbitarray(xored_qbitarray), xored_bool_list)
def create_bitarray(self, values): '''helper function to create a bit array''' obj = QBitArray(len(values)) for i, value in enumerate(values): obj.setBit(i, value) return obj
class QBitArrayIsIterableTest(unittest.TestCase): '''Tests if QBitArray class is iterable and also '~' (__invert__) and bitwise operators''' def setUp(self): #Acquire resources self.qbitarray = QBitArray(3) self.qbitarray_values = [True, False, False] # WARNING: do not pythonify the following loop for i in range(len(self.qbitarray_values)): self.qbitarray.setBit(i, self.qbitarray_values[i]) self.inverted_qbitarray_values = [not bit for bit in self.qbitarray_values] self.other_qbitarray = QBitArray(3) self.other_qbitarray_values = [True, True, False] # WARNING: do not pythonify the following loop for i in range(len(self.other_qbitarray_values)): self.other_qbitarray.setBit(i, self.other_qbitarray_values[i]) def tearDown(self): #Release resources del self.qbitarray del self.other_qbitarray del self.qbitarray_values del self.other_qbitarray_values del self.inverted_qbitarray_values def testQBitArrayIsIterable(self): #Tests if QBitArray class is iterable qbitarray_is_iterable = True try: bitarray = [bit for bit in self.qbitarray] except: qbitarray_is_iterable = False self.assertTrue(qbitarray_is_iterable) def testQBitArrayInvertOperator(self): #Tests QBitArray '~' (__invert__) operator inverted_qbitarray = ~self.qbitarray # WARNING: do not pythonify the following loop, the # iterability of QBitArray class is tested in another place inverted_qbitarray_values = bool_list_from_qbitarray(inverted_qbitarray) self.assertEqual(self.inverted_qbitarray_values, inverted_qbitarray_values) def testQBitArrayOrBitwiseOperator(self): #Tests QBitArray '|' (or) operator has_or_bitwise_operator = True ored_qbitarray, ored_bool_list = None, None try: ored_qbitarray = self.qbitarray | self.other_qbitarray ored_bool_list = [b1 | b2 for b1, b2 in zip(self.qbitarray_values, self.other_qbitarray_values)] except: has_or_bitwise_operator = False self.assertTrue(has_or_bitwise_operator) self.assertEqual(bool_list_from_qbitarray(ored_qbitarray), ored_bool_list) def testQBitArrayAndBitwiseOperator(self): #Tests QBitArray '&' (and) operator has_and_bitwise_operator = True anded_qbitarray, anded_bool_list = None, None try: anded_qbitarray = self.qbitarray | self.other_qbitarray anded_bool_list = [b1 | b2 for b1, b2 in zip(self.qbitarray_values, self.other_qbitarray_values)] except: has_and_bitwise_operator = False self.assertTrue(has_and_bitwise_operator) self.assertEqual(bool_list_from_qbitarray(anded_qbitarray), anded_bool_list) def testQBitArrayXorBitwiseOperator(self): #Tests QBitArray '^' (xor) operator has_xor_bitwise_operator = True xored_qbitarray, xored_bool_list = None, None try: xored_qbitarray = self.qbitarray | self.other_qbitarray xored_bool_list = [b1 | b2 for b1, b2 in zip(self.qbitarray_values, self.other_qbitarray_values)] except: has_xor_bitwise_operator = False self.assertTrue(has_xor_bitwise_operator) self.assertEqual(bool_list_from_qbitarray(xored_qbitarray), xored_bool_list)