diff options
Diffstat (limited to 'tests/auto/corelib/tools/qbitarray')
-rw-r--r-- | tests/auto/corelib/tools/qbitarray/CMakeLists.txt | 11 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp | 367 |
2 files changed, 322 insertions, 56 deletions
diff --git a/tests/auto/corelib/tools/qbitarray/CMakeLists.txt b/tests/auto/corelib/tools/qbitarray/CMakeLists.txt index 37e7a873e8..ac3bd24bd5 100644 --- a/tests/auto/corelib/tools/qbitarray/CMakeLists.txt +++ b/tests/auto/corelib/tools/qbitarray/CMakeLists.txt @@ -1,10 +1,19 @@ -# Generated from qbitarray.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qbitarray Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qbitarray LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qbitarray SOURCES tst_qbitarray.cpp + LIBRARIES + Qt::TestPrivate ) diff --git a/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp b/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp index 263083972c..f52a368aa9 100644 --- a/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp +++ b/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp @@ -1,47 +1,26 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $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) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> +#include <QtTest/private/qcomparisontesthelper_p.h> #include <QtCore/QBuffer> #include <QtCore/QDataStream> #include "qbitarray.h" +#include <QtCore/qelapsedtimer.h> +#include <QtCore/qscopeguard.h> + /** * Helper function to initialize a bitarray from a string */ static QBitArray QStringToQBitArray(const QString &str) { QBitArray ba; - ba.resize(str.length()); + ba.resize(str.size()); int i; QChar tru('1'); - for (i = 0; i < str.length(); i++) + for (i = 0; i < str.size(); i++) { if (str.at(i) == tru) { @@ -51,10 +30,18 @@ static QBitArray QStringToQBitArray(const QString &str) return ba; } +static QBitArray detached(QBitArray a) +{ + a.detach(); + return a; +} + class tst_QBitArray : public QObject { Q_OBJECT private slots: + void compareCompiles(); + void canHandleIntMaxBits(); void size_data(); void size(); void countBits_data(); @@ -68,12 +55,21 @@ private slots: // operator &= void operator_andeq_data(); void operator_andeq(); + // operator & + void operator_and_data() { operator_andeq_data(); } + void operator_and(); // operator |= void operator_oreq_data(); void operator_oreq(); + // operator | + void operator_or_data() { operator_oreq_data(); } + void operator_or(); // operator ^= void operator_xoreq_data(); void operator_xoreq(); + // operator ^ + void operator_xor_data() { operator_xoreq_data(); } + void operator_xor(); // operator ~ void operator_neg_data(); void operator_neg(); @@ -91,6 +87,59 @@ private slots: void toUInt32(); }; +void tst_QBitArray::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile<QBitArray>(); +} + +void tst_QBitArray::canHandleIntMaxBits() +{ + QElapsedTimer timer; + timer.start(); + const auto print = qScopeGuard([&] { + qDebug("Function took %lldms", qlonglong(timer.elapsed())); + }); + + try { + constexpr qsizetype Size1 = sizeof(void*) > sizeof(int) ? qsizetype(INT_MAX) + 2 : + INT_MAX - 2; + constexpr qsizetype Size2 = Size1 + 2; + + QBitArray ba(Size1, true); + QCOMPARE(ba.size(), Size1); + QCOMPARE(ba.at(Size1 - 1), true); + + ba.resize(Size2); + QCOMPARE(ba.size(), Size2); + QCOMPARE(ba.at(Size1 - 1), true); + QCOMPARE(ba.at(Size1), false); + QCOMPARE(ba.at(Size2 - 1), false); + + QByteArray serialized; + if constexpr (sizeof(void*) > sizeof(int)) { + QDataStream ds(&serialized, QIODevice::WriteOnly); + ds.setVersion(QDataStream::Qt_5_15); + ds << ba; + QCOMPARE(ds.status(), QDataStream::Status::SizeLimitExceeded); + serialized.clear(); + } + { + QDataStream ds(&serialized, QIODevice::WriteOnly); + ds << ba; + QCOMPARE(ds.status(), QDataStream::Status::Ok); + } + { + QDataStream ds(serialized); + QBitArray ba2; + ds >> ba2; + QCOMPARE(ds.status(), QDataStream::Status::Ok); + QT_TEST_EQUALITY_OPS(ba, ba2, true); + } + } catch (const std::bad_alloc &) { + QSKIP("Failed to allocate sufficient memory"); + } +} + void tst_QBitArray::size_data() { //create the testtable instance and define the elements @@ -150,7 +199,6 @@ void tst_QBitArray::countBits_data() QTest::newRow("11111111111111111111111111111111") << QString("11111111111111111111111111111111") << 32 << 32; QTest::newRow("11111111111111111111111111111111111111111111111111111111") << QString("11111111111111111111111111111111111111111111111111111111") << 56 << 56; - QTest::newRow("00000000000000000000000000000000000") << QString("00000000000000000000000000000000000") << 35 << 0; QTest::newRow("00000000000000000000000000000000") << QString("00000000000000000000000000000000") << 32 << 0; QTest::newRow("00000000000000000000000000000000000000000000000000000000") << QString("00000000000000000000000000000000000000000000000000000000") << 56 << 0; @@ -168,6 +216,8 @@ void tst_QBitArray::countBits() bits.setBit(i); } + QCOMPARE(bits.size(), numBits); + // NOLINTNEXTLINE(qt-port-to-std-compatible-api): We want to test count() and size() QCOMPARE(bits.count(), numBits); QCOMPARE(bits.count(true), onBits); QCOMPARE(bits.count(false), numBits - onBits); @@ -223,14 +273,20 @@ void tst_QBitArray::isEmpty() QVERIFY(!a1.isEmpty()); QVERIFY(!a1.isNull()); QVERIFY(a1.size() == 2); + + QT_TEST_EQUALITY_OPS(a1, a2, false); + QT_TEST_EQUALITY_OPS(a2, a3, false); + QT_TEST_EQUALITY_OPS(QBitArray(), QBitArray(), true); + a3 = a2; + QT_TEST_EQUALITY_OPS(a2, a3, true); } void tst_QBitArray::swap() { QBitArray b1 = QStringToQBitArray("1"), b2 = QStringToQBitArray("10"); b1.swap(b2); - QCOMPARE(b1,QStringToQBitArray("10")); - QCOMPARE(b2,QStringToQBitArray("1")); + QT_TEST_EQUALITY_OPS(b1,QStringToQBitArray("10"), true); + QT_TEST_EQUALITY_OPS(b2,QStringToQBitArray("1"), true); } void tst_QBitArray::fill() @@ -280,7 +336,7 @@ void tst_QBitArray::toggleBit() input.toggleBit(index); - QCOMPARE(input, res); + QT_TEST_EQUALITY_OPS(input, res, true); } void tst_QBitArray::operator_andeq_data() @@ -325,9 +381,64 @@ void tst_QBitArray::operator_andeq() QFETCH(QBitArray, input2); QFETCH(QBitArray, res); - input1&=input2; + QBitArray result = input1; + result &= input2; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1; + result &= std::move(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1; + result &= detached(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + + // operation is commutative + result = input2; + result &= input1; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2; + result &= std::move(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2; + result &= detached(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + + // operation is idempotent + result &= result; + QT_TEST_EQUALITY_OPS(result, res, true); + result &= std::move(result); + QT_TEST_EQUALITY_OPS(result, res, true); + result &= detached(result); + QT_TEST_EQUALITY_OPS(result, res, true); +} - QCOMPARE(input1, res); +void tst_QBitArray::operator_and() +{ + QFETCH(QBitArray, input1); + QFETCH(QBitArray, input2); + QFETCH(QBitArray, res); + + QBitArray result = input1 & input2; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1 & QBitArray(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1 & detached(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + + // operation is commutative + result = input2 & input1; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2 & QBitArray(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2 & detached(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + + // operation is idempotent + result = result & result; + QT_TEST_EQUALITY_OPS(result, res, true); + result = result & QBitArray(result); + QT_TEST_EQUALITY_OPS(result, res, true); + result = result & detached(result); + QT_TEST_EQUALITY_OPS(result, res, true); } void tst_QBitArray::operator_oreq_data() @@ -376,9 +487,64 @@ void tst_QBitArray::operator_oreq() QFETCH(QBitArray, input2); QFETCH(QBitArray, res); - input1|=input2; + QBitArray result = input1; + result |= input2; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1; + result |= QBitArray(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1; + result |= detached(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + + // operation is commutative + result = input2; + result |= input1; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2; + result |= QBitArray(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2; + result |= detached(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + + // operation is idempotent + result |= result; + QT_TEST_EQUALITY_OPS(result, res, true); + result |= QBitArray(result); + QT_TEST_EQUALITY_OPS(result, res, true); + result |= detached(result); + QT_TEST_EQUALITY_OPS(result, res, true); +} + +void tst_QBitArray::operator_or() +{ + QFETCH(QBitArray, input1); + QFETCH(QBitArray, input2); + QFETCH(QBitArray, res); - QCOMPARE(input1, res); + QBitArray result = input1 | input2; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1 | QBitArray(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1 | detached(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + + // operation is commutative + result = input2 | input1; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2 | QBitArray(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2 | detached(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + + // operation is idempotent + result = result | result; + QT_TEST_EQUALITY_OPS(result, res, true); + result = result | QBitArray(result); + QT_TEST_EQUALITY_OPS(result, res, true); + result = result | detached(result); + QT_TEST_EQUALITY_OPS(result, res, true); } void tst_QBitArray::operator_xoreq_data() @@ -425,11 +591,102 @@ void tst_QBitArray::operator_xoreq() QFETCH(QBitArray, input2); QFETCH(QBitArray, res); - input1^=input2; - - QCOMPARE(input1, res); + QBitArray result = input1; + result ^= input2; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1; + result ^= QBitArray(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1; + result ^= detached(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + + // operation is commutative + result = input2; + result ^= input1; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2; + result ^= QBitArray(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2; + result ^= detached(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + + // XORing with oneself is nilpotent + result = input1; + result ^= input1; + QT_TEST_EQUALITY_OPS(result, QBitArray(input1.size()), true); + result = input1; + result ^= QBitArray(result); + QT_TEST_EQUALITY_OPS(result, QBitArray(input1.size()), true); + result = input1; + result ^= detached(result); + QT_TEST_EQUALITY_OPS(result, QBitArray(input1.size()), true); + + result = input2; + result ^= input2; + QT_TEST_EQUALITY_OPS(result, QBitArray(input2.size()), true); + result = input2; + result ^= QBitArray(input2); + QT_TEST_EQUALITY_OPS(result, QBitArray(input2.size()), true); + result = input2; + result ^= detached(input2); + QT_TEST_EQUALITY_OPS(result, QBitArray(input2.size()), true); + + result = res; + result ^= res; + QT_TEST_EQUALITY_OPS(result, QBitArray(res.size()), true); + result = res; + result ^= QBitArray(res); + QT_TEST_EQUALITY_OPS(result, QBitArray(res.size()), true); + result = res; + result ^= detached(res); + QT_TEST_EQUALITY_OPS(result, QBitArray(res.size()), true); } +void tst_QBitArray::operator_xor() +{ + QFETCH(QBitArray, input1); + QFETCH(QBitArray, input2); + QFETCH(QBitArray, res); + + QBitArray result = input1 ^ input2; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1 ^ QBitArray(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input1 ^ detached(input2); + QT_TEST_EQUALITY_OPS(result, res, true); + + // operation is commutative + result = input2 ^ input1; + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2 ^ QBitArray(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + result = input2 ^ detached(input1); + QT_TEST_EQUALITY_OPS(result, res, true); + + // XORing with oneself is nilpotent + result = input1 ^ input1; + QT_TEST_EQUALITY_OPS(result, QBitArray(input1.size()), true); + result = input1 ^ QBitArray(input1); + QT_TEST_EQUALITY_OPS(result, QBitArray(input1.size()), true); + result = input1 ^ detached(input1); + QT_TEST_EQUALITY_OPS(result, QBitArray(input1.size()), true); + + result = input2 ^ input2; + QT_TEST_EQUALITY_OPS(result, QBitArray(input2.size()), true); + result = input2 ^ QBitArray(input2); + QT_TEST_EQUALITY_OPS(result, QBitArray(input2.size()), true); + result = input2 ^ detached(input2); + QT_TEST_EQUALITY_OPS(result, QBitArray(input2.size()), true); + + result = res ^ res; + QT_TEST_EQUALITY_OPS(result, QBitArray(res.size()), true); + result = res ^ QBitArray(res); + QT_TEST_EQUALITY_OPS(result, QBitArray(res.size()), true); + result = res ^ detached(res); + QT_TEST_EQUALITY_OPS(result, QBitArray(res.size()), true); +} void tst_QBitArray::operator_neg_data() { @@ -477,7 +734,8 @@ void tst_QBitArray::operator_neg() input = ~input; - QCOMPARE(input, res); + QT_TEST_EQUALITY_OPS(input, res, true); + QT_TEST_EQUALITY_OPS(~~input, res, true); // performs two in-place negations } void tst_QBitArray::datastream_data() @@ -497,7 +755,6 @@ void tst_QBitArray::datastream_data() QTest::newRow("11111111111111111111111111111111") << QString("11111111111111111111111111111111") << 32 << 32; QTest::newRow("11111111111111111111111111111111111111111111111111111111") << QString("11111111111111111111111111111111111111111111111111111111") << 56 << 56; - QTest::newRow("00000000000000000000000000000000000") << QString("00000000000000000000000000000000000") << 35 << 0; QTest::newRow("00000000000000000000000000000000") << QString("00000000000000000000000000000000") << 32 << 0; QTest::newRow("00000000000000000000000000000000000000000000000000000000") << QString("00000000000000000000000000000000000000000000000000000000") << 56 << 0; @@ -519,7 +776,7 @@ void tst_QBitArray::datastream() bits.setBit(i); } - QCOMPARE(bits.count(), numBits); + QCOMPARE(bits.size(), numBits); QCOMPARE(bits.count(true), onBits); QCOMPARE(bits.count(false), numBits - onBits); @@ -534,19 +791,19 @@ void tst_QBitArray::datastream() QBitArray array1, array2, array3; stream2 >> array1 >> array2 >> array3; - QCOMPARE(array1.count(), numBits); + QCOMPARE(array1.size(), numBits); QCOMPARE(array1.count(true), onBits); QCOMPARE(array1.count(false), numBits - onBits); - QCOMPARE(array1, bits); - QCOMPARE(array2, bits); - QCOMPARE(array3, bits); + QT_TEST_EQUALITY_OPS(array1, bits, true); + QT_TEST_EQUALITY_OPS(array2, bits, true); + QT_TEST_EQUALITY_OPS(array3, bits, true); } void tst_QBitArray::invertOnNull() const { QBitArray a; - QCOMPARE(a = ~a, QBitArray()); + QT_TEST_EQUALITY_OPS(a = ~a, QBitArray(), true); } void tst_QBitArray::operator_noteq_data() @@ -587,7 +844,7 @@ void tst_QBitArray::operator_noteq() QFETCH(bool, res); bool b = input1 != input2; - QCOMPARE(b, res); + QT_TEST_EQUALITY_OPS(b, res, true); } void tst_QBitArray::resize() @@ -596,22 +853,22 @@ void tst_QBitArray::resize() QBitArray a = QStringToQBitArray(QString("11")); a.resize(10); QVERIFY(a.size() == 10); - QCOMPARE( a, QStringToQBitArray(QString("1100000000")) ); + QT_TEST_EQUALITY_OPS( a, QStringToQBitArray(QString("1100000000")), true); a.setBit(9); a.resize(9); // now the bit in a should have been gone: - QCOMPARE( a, QStringToQBitArray(QString("110000000")) ); + QT_TEST_EQUALITY_OPS( a, QStringToQBitArray(QString("110000000")), true); // grow the array back and check the new bit a.resize(10); - QCOMPARE( a, QStringToQBitArray(QString("1100000000")) ); + QT_TEST_EQUALITY_OPS( a, QStringToQBitArray(QString("1100000000")), true); // other test with and a.resize(9); QBitArray b = QStringToQBitArray(QString("1111111111")); b &= a; - QCOMPARE( b, QStringToQBitArray(QString("1100000000")) ); + QT_TEST_EQUALITY_OPS( b, QStringToQBitArray(QString("1100000000")), true); } @@ -665,9 +922,9 @@ void tst_QBitArray::fromBits() QFETCH(QBitArray, expected); QBitArray fromBits = QBitArray::fromBits(data, size); - QCOMPARE(fromBits, expected); + QT_TEST_EQUALITY_OPS(fromBits, expected, true); - QCOMPARE(QBitArray::fromBits(fromBits.bits(), fromBits.size()), expected); + QT_TEST_EQUALITY_OPS(QBitArray::fromBits(fromBits.bits(), fromBits.size()), expected, true); } void tst_QBitArray::toUInt32_data() |