aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/QtCore/qbytearray_test.py
diff options
context:
space:
mode:
Diffstat (limited to 'sources/pyside6/tests/QtCore/qbytearray_test.py')
-rw-r--r--sources/pyside6/tests/QtCore/qbytearray_test.py72
1 files changed, 37 insertions, 35 deletions
diff --git a/sources/pyside6/tests/QtCore/qbytearray_test.py b/sources/pyside6/tests/QtCore/qbytearray_test.py
index c347a6e4d..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,8 @@ 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):
@@ -233,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
@@ -241,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
@@ -259,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
@@ -267,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
@@ -283,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()