1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
#!/usr/bin/python
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
'''Tests if QBitArray class is iterable and also '~' (__invert__) and bitwise operators'''
import gc
import os
import sys
import unittest
from pathlib import Path
sys.path.append(os.fspath(Path(__file__).resolve().parents[1]))
from init_paths import init_test_paths
init_test_paths(False)
from PySide6.QtCore import QBitArray
def bool_list_from_qbitarray(qbitarray):
'''This function is used instead of a list comprehension because
the QBitArray is being tested also to check if it is providing
the iterable protocol.
'''
qbitarray_values = []
for i in range(qbitarray.size()):
qbitarray_values.append(qbitarray.at(i))
return qbitarray_values
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
# PYSIDE-535: Need to collect garbage in PyPy to trigger deletion
gc.collect()
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)
class QBitArrayGetItemTest(unittest.TestCase):
'''Test case for []/__getitem__ operator'''
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
def testSequenceProtocol(self):
'''QBitArray sequence protocol'''
data = [True, False, True]
obj = self.create_bitarray(data)
for reference, value in zip(data, obj):
self.assertEqual(reference, value)
if __name__ == '__main__':
unittest.main()
|