aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside2/tests/QtCore/qbytearray_test.py
blob: dba9ecfeaf6c85bf00e74cc7903f2458a139c39b (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding:utf-8 -*-
# !/usr/bin/python

#############################################################################
##
## Copyright (C) 2016 The Qt Company Ltd.
## Contact: https://www.qt.io/licensing/
##
## This file is part of the test suite of Qt for Python.
##
## $QT_BEGIN_LICENSE:GPL-EXCEPT$
## Commercial License Usage
## Licensees holding valid commercial Qt licenses may use this file in
## accordance with the commercial license agreement provided with the
## Software or, alternatively, in accordance with the terms contained in
## a written agreement between you and The Qt Company. For licensing terms
## and conditions see https://www.qt.io/terms-conditions. For further
## information use the contact form at https://www.qt.io/contact-us.
##
## GNU General Public License Usage
## Alternatively, this file may be used under the terms of the GNU
## General Public License version 3 as published by the Free Software
## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
## included in the packaging of this file. Please review the following
## information to ensure the GNU General Public License requirements will
## be met: https://www.gnu.org/licenses/gpl-3.0.html.
##
## $QT_END_LICENSE$
##
#############################################################################

'''Unit tests for QByteArray'''

import unittest
import ctypes
import pickle
import py3kcompat as py3k

from PySide2.QtCore import QByteArray, QSettings, QObject, QDataStream, QIODevice

class QByteArrayTestToNumber(unittest.TestCase):
    def testToNumberInt(self):
        obj = QByteArray(py3k.b('37'))
        self.assertEqual((37, True), obj.toInt())

    def testToNumberUShort(self):
        obj = QByteArray(py3k.b('37'))
        self.assertEqual((37, True), obj.toUShort())

    def testToNumberFloat(self):
        obj = QByteArray(py3k.b('37.109'))
        self.assertEqual((ctypes.c_float(37.109).value, True),
                         obj.toFloat())

    def testToNumberDouble(self):
        obj = QByteArray(py3k.b('37.109'))
        self.assertEqual((ctypes.c_double(37.109).value, True),
                         obj.toDouble())

    def testSetNum(self):
        b = QByteArray()
        b.setNum(py3k.long(-124124))
        self.assertEqual(b, "-124124")
        b = QByteArray()
        b.setNum(-124124)
        self.assertEqual(b, "-124124")
        b = QByteArray()
        b.setNum(-0.5)
        self.assertEqual(b, "-0.5")

    def testAppend(self):
        b = QByteArray()
        b.append(py3k.b("A"))
        self.assertEqual(b.size(), 1)
        b.append(py3k.b("AB"))
        self.assertEqual(b.size(), 3)


class QByteArraySplit(unittest.TestCase):
    '''Test case for QByteArray.split'''

    def testPathSeparator(self):
        #QByteArray.split('/')
        obj = QByteArray(py3k.b(unittest.__file__))
        self.assertEqual(obj.split('/'), unittest.__file__.split('/'))

class QByteArrayData(unittest.TestCase):

    '''Test case for QByteArray.data'''

    def testData(self):
        url = QByteArray(py3k.b("http://pyside.org"))
        self.assertEqual(url.data(), py3k.b("http://pyside.org"))

    def testDataWithZeros(self):
        s1 = py3k.b("123\000321")
        ba = QByteArray(s1)
        s2 = ba.data()
        self.assertEqual(py3k.b(s1), s2)
        self.assertEqual(s1, ba)

class QByteArrayOperatorAtSetter(unittest.TestCase):
    '''Test case for operator QByteArray[] - __setitem__'''

    def testSetterString(self):
        '''QByteArray[x] = pythonstring'''
        obj = QByteArray(py3k.b('123456'))
        obj[1] = py3k.b('0')
        self.assertEqual(obj, QByteArray(py3k.b('103456')))

class QByteArrayOnQDataStream(unittest.TestCase):
    '''
    Bug PYSIDE-232
    '''
    def testIt(self):
        a = QByteArray()
        b = QDataStream(a, QIODevice.WriteOnly)
        b.writeUInt16(5000)
        # The __repr__ not suppose to crash anymore
        self.assertNotEqual(repr(b), None)

class TestBug664(unittest.TestCase):
    '''
    QByteArray.data() should return correct data
    '''
    def testIt(self):
        a = QByteArray(py3k.unicode_('hi 猫').encode('utf-8'))
        if py3k.IS_PY3K:
            self.assertEqual(repr(a), "PySide2.QtCore.QByteArray(b'hi \\xe7\\x8c\\xab')")
        else:
            self.assertEqual(repr(a), "PySide2.QtCore.QByteArray('hi \\xe7\\x8c\\xab')")

class QByteArrayOnQVariant(unittest.TestCase):
    def testQByteArrayOnQVariant(self):
        a = QSettings().value("some_prop", QByteArray())
        self.assertEqual(type(a), QByteArray)

class TestBug567(unittest.TestCase):
    '''
    QByteArray should support slices
    '''
    def testIt(self):
        ba = QByteArray(py3k.b('1234567890'))
        self.assertEqual(ba[2:4], '34')
        self.assertEqual(ba[:4], '1234')
        self.assertEqual(ba[4:], '567890')
        self.assertEqual(len(ba[4:1]), 0)
        self.assertEqual(ba[::-1], '0987654321')
        self.assertEqual(ba[::2], '13579')
        self.assertEqual(ba[::-2], '08642')
        self.assertEqual(ba[2:8:3], '36')

class QByteArrayBug514(unittest.TestCase):
    def testIt(self):
        data = py3k.b("foobar")
        a = QByteArray.fromRawData(data)
        self.assertEqual(type(a), QByteArray)
        self.assertEqual(a.data(), data)

class TestPickler(unittest.TestCase):
    def testIt(self):
        ba = QByteArray(py3k.b("321\x00123"))
        output = pickle.dumps(str(ba))
        ba2 = pickle.loads(output)
        self.assertEqual(str(ba), str(ba2))

class QByteArrayBug720(unittest.TestCase):
    def testIt(self):
        ba = QByteArray(py3k.b("32\"1\x00123"))
        self.assertEqual(str(ba), str(py3k.b("32\"1\x00123")))
        if py3k.IS_PY3K:
            self.assertEqual(repr(ba), "PySide2.QtCore.QByteArray(b'32\"1\\x00123')")
        else:
            self.assertEqual(repr(ba), "PySide2.QtCore.QByteArray('32\"1\\x00123')")

class QByteArrayImplicitConvert(unittest.TestCase):
    def testString(self):
        # No implicit conversions from QByteArray to python string
        ba = QByteArray(py3k.b("object name"))
        obj = QObject()
        self.assertRaises(TypeError, obj.setObjectName, ba)


class QByteArraySliceAssignment(unittest.TestCase):
    def testIndexAssignment(self):
        a = QByteArray(py3k.b('abc'))
        a[0] = py3k.b('x')
        self.assertEqual(a[0], py3k.b('x'))

        def test_1():
            a[0] = py3k.b('xy')
        self.assertRaises(ValueError, test_1)

    def testSliceAssignmentBytes(self):
        b = QByteArray(py3k.b('0123456789'))
        b[2:8] = py3k.b('abcdef')
        self.assertEqual(b[2:8], py3k.b('abcdef'))
        # Delete behavior
        b[2:8] = None
        self.assertEqual(b, py3k.b('0189'))

        # number of slots and number of values doesn't match
        def test_2():
            b[2:8:2] = py3k.b('')
        self.assertRaises(ValueError, test_2)
        b = QByteArray(py3k.b('0123456789'))
        # reverse slice
        b[5:2:-1] = py3k.b('ABC')
        self.assertEqual(b, py3k.b('012CBA6789'))
        # step is not 1
        b[2:9:3] = py3k.b('XYZ')
        self.assertEqual(b, py3k.b('01XCBY67Z9'))
        b = QByteArray(py3k.b('0123456789'))
        b[9:2:-3] = py3k.b('XYZ')
        self.assertEqual(b, py3k.b('012Z45Y78X'))

    def testSliceAssignmentQByteArray(self):
        b = QByteArray(py3k.b('0123456789'))
        b[2:8] = QByteArray(py3k.b('abcdef'))
        self.assertEqual(b[2:8], py3k.b('abcdef'))
        # shrink
        b[2:8] = QByteArray(py3k.b('aaa'))
        self.assertEqual(b, py3k.b('01aaa89'))
        # expanse
        b[2:5] = QByteArray(py3k.b('uvwxyz'))
        self.assertEqual(b, py3k.b('01uvwxyz89'))
        # Delete behavior
        b[2:8] = QByteArray()
        self.assertEqual(b, py3k.b('0189'))

        b = QByteArray(py3k.b('0123456789'))
        # reverse assginment
        b[5:2:-1] = QByteArray(py3k.b('ABC'))
        self.assertEqual(b, py3k.b('012CBA6789'))
        # step is not 1
        b[2:9:3] = QByteArray(py3k.b('XYZ'))
        self.assertEqual(b, py3k.b('01XCBY67Z9'))
        b = QByteArray(py3k.b('0123456789'))
        b[9:2:-3] = QByteArray(py3k.b('XYZ'))
        self.assertEqual(b, py3k.b('012Z45Y78X'))

    def testSliceAssignmentByteArray(self):
        b = QByteArray(py3k.b('0123456789'))
        # replace
        b[2:8] = bytearray(py3k.b('abcdef'))
        self.assertEqual(b[2:8], py3k.b('abcdef'))
        # shrink
        b[2:8] = bytearray(py3k.b('aaa'))
        self.assertEqual(b, py3k.b('01aaa89'))
        # expanse
        b[2:5] = bytearray(py3k.b('uvwxyz'))
        self.assertEqual(b, py3k.b('01uvwxyz89'))
        # Delete behavior
        b[2:8] = bytearray(py3k.b(''))
        self.assertEqual(b, py3k.b('0189'))

        b = QByteArray(py3k.b('0123456789'))
        # reverse assginment
        b[5:2:-1] = bytearray(py3k.b('ABC'))
        self.assertEqual(b, py3k.b('012CBA6789'))
        # step is not 1
        b[2:9:3] = bytearray(py3k.b('XYZ'))
        self.assertEqual(b, py3k.b('01XCBY67Z9'))
        b = QByteArray(py3k.b('0123456789'))
        b[9:2:-3] = bytearray(py3k.b('XYZ'))
        self.assertEqual(b, py3k.b('012Z45Y78X'))


if __name__ == '__main__':
    unittest.main()