diff options
Diffstat (limited to 'sources/pyside6/tests/QtCore/qbytearray_test.py')
-rw-r--r-- | sources/pyside6/tests/QtCore/qbytearray_test.py | 82 |
1 files changed, 46 insertions, 36 deletions
diff --git a/sources/pyside6/tests/QtCore/qbytearray_test.py b/sources/pyside6/tests/QtCore/qbytearray_test.py index 69e608d98..cb8f9a431 100644 --- a/sources/pyside6/tests/QtCore/qbytearray_test.py +++ b/sources/pyside6/tests/QtCore/qbytearray_test.py @@ -1,39 +1,12 @@ -# -*- 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$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 '''Unit tests for QByteArray''' import ctypes import os import pickle +import struct import sys import unittest @@ -43,7 +16,9 @@ from init_paths import init_test_paths init_test_paths(False) -from PySide6.QtCore import QByteArray, QSettings, QObject, QDataStream, QIODevice +from PySide6.QtCore import (QByteArray, QSettings, QObject, QDataStream, + QIODevice, qCompress, qUncompress) + class QByteArrayTestToNumber(unittest.TestCase): def testToNumberInt(self): @@ -95,10 +70,11 @@ class QByteArraySplit(unittest.TestCase): '''Test case for QByteArray.split''' def testPathSeparator(self): - #QByteArray.split('/') + # QByteArray.split('/') obj = QByteArray(bytes(unittest.__file__, "UTF8")) self.assertEqual(obj.split('/'), unittest.__file__.split('/')) + class QByteArrayData(unittest.TestCase): '''Test case for QByteArray.data''' @@ -114,6 +90,7 @@ class QByteArrayData(unittest.TestCase): self.assertEqual(s1, s2) self.assertEqual(s1, ba) + class QByteArrayOperatorAtSetter(unittest.TestCase): '''Test case for operator QByteArray[] - __setitem__''' @@ -123,6 +100,7 @@ class QByteArrayOperatorAtSetter(unittest.TestCase): obj[1] = bytes('0', "UTF8") self.assertEqual(obj, QByteArray(bytes('103456', "UTF8"))) + class QByteArrayOnQDataStream(unittest.TestCase): ''' Bug PYSIDE-232 @@ -134,6 +112,7 @@ class QByteArrayOnQDataStream(unittest.TestCase): # The __repr__ not suppose to crash anymore self.assertNotEqual(repr(b), None) + class TestBug664(unittest.TestCase): ''' QByteArray.data() should return correct data @@ -148,6 +127,7 @@ class QByteArrayOnQVariant(unittest.TestCase): a = QSettings().value("some_prop", QByteArray()) self.assertEqual(type(a), QByteArray) + class TestBug567(unittest.TestCase): ''' QByteArray should support slices @@ -171,12 +151,14 @@ class TestPickler(unittest.TestCase): ba2 = pickle.loads(output) self.assertEqual(str(ba), str(ba2)) + class QByteArrayBug720(unittest.TestCase): def testIt(self): ba = QByteArray(bytes("32\"1\x00123", "UTF8")) self.assertEqual(str(ba), str(bytes("32\"1\x00123", "UTF-8"))) self.assertEqual(repr(ba), "PySide6.QtCore.QByteArray(b'32\"1\\x00123')") + class QByteArrayImplicitConvert(unittest.TestCase): def testString(self): # No implicit conversions from QByteArray to python string @@ -225,7 +207,7 @@ class QByteArraySliceAssignment(unittest.TestCase): # shrink b[2:8] = QByteArray(bytes('aaa', "UTF8")) self.assertEqual(b, bytes('01aaa89', "UTF8")) - # expanse + # expand b[2:5] = QByteArray(bytes('uvwxyz', "UTF8")) self.assertEqual(b, bytes('01uvwxyz89', "UTF8")) # Delete behavior @@ -233,7 +215,7 @@ class QByteArraySliceAssignment(unittest.TestCase): self.assertEqual(b, bytes('0189', "UTF8")) b = QByteArray(bytes('0123456789', "UTF8")) - # reverse assginment + # reverse assignment b[5:2:-1] = QByteArray(bytes('ABC', "UTF8")) self.assertEqual(b, bytes('012CBA6789', "UTF8")) # step is not 1 @@ -251,7 +233,7 @@ class QByteArraySliceAssignment(unittest.TestCase): # shrink b[2:8] = bytearray(bytes('aaa', "UTF8")) self.assertEqual(b, bytes('01aaa89', "UTF8")) - # expanse + # expand b[2:5] = bytearray(bytes('uvwxyz', "UTF8")) self.assertEqual(b, bytes('01uvwxyz89', "UTF8")) # Delete behavior @@ -259,7 +241,7 @@ class QByteArraySliceAssignment(unittest.TestCase): self.assertEqual(b, bytes('0189', "UTF8")) b = QByteArray(bytes('0123456789', "UTF8")) - # reverse assginment + # reverse assignment b[5:2:-1] = bytearray(bytes('ABC', "UTF8")) self.assertEqual(b, bytes('012CBA6789', "UTF8")) # step is not 1 @@ -275,6 +257,34 @@ class QByteArraySliceAssignment(unittest.TestCase): actual_bytes = bytes(byte_array) self.assertEqual(orig_bytes, actual_bytes) + def testUnpack(self): + b = QByteArray(b'\x19\x00\x00\x00\xc4\t\x00\x00') + t = struct.unpack('<ii', b) + self.assertEqual(len(t), 2) + self.assertEqual(t[0], 25) + self.assertEqual(t[1], 2500) + + +class QCompressTest(unittest.TestCase): + def testQByteArrayCompression(self): + """Compress/uncompress a QByteArray.""" + data = bytes(10 * 'long redundant sentence bla bla', "UTF8") + ba = QByteArray(data) + compressed = qCompress(ba) + self.assertTrue(len(compressed) < len(data)) + uncompressed = qUncompress(compressed) + self.assertEqual(uncompressed, data) + + def testBufferCompression(self): + """Compress/uncompress portions of bytes without converting to + QByteArray.""" + data = bytes(10 * 'long redundant sentence bla bla', "UTF8") + used_len = int(len(data) / 2) + compressed = qCompress(data, used_len, -1) + self.assertTrue(len(compressed) < used_len) + uncompressed = qUncompress(compressed.data(), len(compressed)) + self.assertEqual(uncompressed, data[:used_len]) + if __name__ == '__main__': unittest.main() |