diff options
Diffstat (limited to 'tests/auto/corelib/tools/qalgorithms')
-rw-r--r-- | tests/auto/corelib/tools/qalgorithms/CMakeLists.txt | 11 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qalgorithms/qalgorithms.pro | 4 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp | 119 |
3 files changed, 63 insertions, 71 deletions
diff --git a/tests/auto/corelib/tools/qalgorithms/CMakeLists.txt b/tests/auto/corelib/tools/qalgorithms/CMakeLists.txt index 2ddc47b5c0..9e87144a4c 100644 --- a/tests/auto/corelib/tools/qalgorithms/CMakeLists.txt +++ b/tests/auto/corelib/tools/qalgorithms/CMakeLists.txt @@ -1,10 +1,17 @@ -# Generated from qalgorithms.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qalgorithms Test: ##################################################################### -qt_add_test(tst_qalgorithms +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qalgorithms LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qalgorithms SOURCES tst_qalgorithms.cpp ) diff --git a/tests/auto/corelib/tools/qalgorithms/qalgorithms.pro b/tests/auto/corelib/tools/qalgorithms/qalgorithms.pro deleted file mode 100644 index 0e6e830185..0000000000 --- a/tests/auto/corelib/tools/qalgorithms/qalgorithms.pro +++ /dev/null @@ -1,4 +0,0 @@ -CONFIG += testcase -TARGET = tst_qalgorithms -QT = core testlib -SOURCES = tst_qalgorithms.cpp diff --git a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp index e0062b9118..8d68a7a270 100644 --- a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp +++ b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp @@ -1,33 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2020 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) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include "../../../../../src/corelib/tools/qalgorithms.h" -#include <QtTest/QtTest> +#include <QTest> QT_WARNING_DISABLE_DEPRECATED @@ -95,6 +70,18 @@ private: void countLeading_impl(); }; +template <typename T> struct PrintIfFailed +{ + T value; + PrintIfFailed(T v) : value(v) {} + ~PrintIfFailed() + { + if (!QTest::currentTestFailed()) + return; + qWarning() << "Original value was" << Qt::hex << Qt::showbase << T(value); + } +}; + void tst_QAlgorithms::swap() { { @@ -167,42 +154,42 @@ void tst_QAlgorithms::swap() } { - void *a = 0, *b = 0; + void *a = nullptr, *b = nullptr; qSwap(a, b); } { - const void *a = 0, *b = 0; + const void *a = nullptr, *b = nullptr; qSwap(a, b); } { - QString *a = 0, *b = 0; + QString *a = nullptr, *b = nullptr; qSwap(a, b); } { - const QString *a = 0, *b = 0; + const QString *a = nullptr, *b = nullptr; qSwap(a, b); } { - QString **a = 0, **b = 0; + QString **a = nullptr, **b = nullptr; qSwap(a, b); } { - const QString **a = 0, **b = 0; + const QString **a = nullptr, **b = nullptr; qSwap(a, b); } { - QString * const *a = 0, * const *b = 0; + QString * const *a = nullptr, * const *b = nullptr; qSwap(a, b); } { - const QString * const *a = 0, * const *b = 0; + const QString * const *a = nullptr, * const *b = nullptr; qSwap(a, b); } } @@ -274,23 +261,26 @@ void tst_QAlgorithms::popCount_data_impl(size_t sizeof_T_Int) const uint bits = bitsSetInByte(byte); const quint64 value = static_cast<quint64>(byte); const quint64 input = value << ((i % sizeof_T_Int) * 8U); - QTest::addRow("0x%016llx", input) << input << bits; + QTest::addRow("%u-bits", i) << input << bits; } // and some random ones: - if (sizeof_T_Int >= 8) + if (sizeof_T_Int >= 8) { for (size_t i = 0; i < 1000; ++i) { const quint64 input = QRandomGenerator::global()->generate64(); - QTest::addRow("0x%016llx", input) << input << bitsSetInInt64(input); + QTest::addRow("random-%zu", i) << input << bitsSetInInt64(input); } - else if (sizeof_T_Int >= 2) - for (size_t i = 0; i < 1000 ; ++i) { - const quint32 input = QRandomGenerator::global()->generate(); - if (sizeof_T_Int >= 4) - QTest::addRow("0x%08x", input) << quint64(input) << bitsSetInInt(input); - else - QTest::addRow("0x%04x", quint16(input & 0xFFFF)) << quint64(input & 0xFFFF) << bitsSetInShort(input & 0xFFFF); + } else if (sizeof_T_Int >= 2) { + for (size_t i = 0; i < 1000 ; ++i) { + const quint32 input = QRandomGenerator::global()->generate(); + if (sizeof_T_Int >= 4) { + QTest::addRow("random-%zu", i) << quint64(input) << bitsSetInInt(input); + } else { + QTest::addRow("random-%zu", i) + << quint64(input & 0xFFFF) << bitsSetInShort(input & 0xFFFF); } + } + } } template <typename T_Int> @@ -300,22 +290,23 @@ void tst_QAlgorithms::popCount_impl() QFETCH(uint, expected); const T_Int value = static_cast<T_Int>(input); - + PrintIfFailed pf(value); QCOMPARE(qPopulationCount(value), expected); } +// Number of test-cases per offset into each size (arbitrary): +static constexpr int casesPerOffset = 3; + void tst_QAlgorithms::countTrailing_data_impl(size_t sizeof_T_Int) { using namespace QTest; addColumn<quint64>("input"); addColumn<uint>("expected"); - int nibs = sizeof_T_Int*2; - - newRow(("0x"+QByteArray::number(0,16).rightJustified(nibs,'0')).constData()) << Q_UINT64_C(0) << uint(sizeof_T_Int*8); + addRow("0") << Q_UINT64_C(0) << uint(sizeof_T_Int*8); for (uint i = 0; i < sizeof_T_Int*8; ++i) { const quint64 input = Q_UINT64_C(1) << i; - newRow(("0x"+QByteArray::number(input,16).rightJustified(nibs,'0')).constData()) << input << i; + addRow("bit-%u", i) << input << i; } quint64 type_mask; @@ -326,12 +317,12 @@ void tst_QAlgorithms::countTrailing_data_impl(size_t sizeof_T_Int) // and some random ones: for (uint i = 0; i < sizeof_T_Int*8; ++i) { - for (uint j = 0; j < sizeof_T_Int*3; ++j) { // 3 is arbitrary + const quint64 b = Q_UINT64_C(1) << i; + const quint64 mask = ((~(b - 1)) ^ b) & type_mask; + for (uint j = 0; j < sizeof_T_Int * casesPerOffset; ++j) { const quint64 r = QRandomGenerator::global()->generate64(); - const quint64 b = Q_UINT64_C(1) << i; - const quint64 mask = ((~(b-1)) ^ b) & type_mask; const quint64 input = (r&mask) | b; - newRow(("0x"+QByteArray::number(input,16).rightJustified(nibs,'0')).constData()) << input << i; + addRow("%u-bits-random-%u", i, j) << input << i; } } } @@ -343,7 +334,7 @@ void tst_QAlgorithms::countTrailing_impl() QFETCH(uint, expected); const T_Int value = static_cast<T_Int>(input); - + PrintIfFailed pf(value); QCOMPARE(qCountTrailingZeroBits(value), expected); } @@ -353,22 +344,20 @@ void tst_QAlgorithms::countLeading_data_impl(size_t sizeof_T_Int) addColumn<quint64>("input"); addColumn<uint>("expected"); - int nibs = sizeof_T_Int*2; - - newRow(("0x"+QByteArray::number(0,16).rightJustified(nibs,'0')).constData()) << Q_UINT64_C(0) << uint(sizeof_T_Int*8); + addRow("0") << Q_UINT64_C(0) << uint(sizeof_T_Int*8); for (uint i = 0; i < sizeof_T_Int*8; ++i) { const quint64 input = Q_UINT64_C(1) << i; - newRow(("0x"+QByteArray::number(input,16).rightJustified(nibs,'0')).constData()) << input << uint(sizeof_T_Int*8-i-1); + addRow("bit-%u", i) << input << uint(sizeof_T_Int*8-i-1); } // and some random ones: for (uint i = 0; i < sizeof_T_Int*8; ++i) { - for (uint j = 0; j < sizeof_T_Int*3; ++j) { // 3 is arbitrary + const quint64 b = Q_UINT64_C(1) << i; + const quint64 mask = b - 1; + for (uint j = 0; j < sizeof_T_Int * casesPerOffset; ++j) { const quint64 r = QRandomGenerator::global()->generate64(); - const quint64 b = Q_UINT64_C(1) << i; - const quint64 mask = b-1; const quint64 input = (r&mask) | b; - newRow(("0x"+QByteArray::number(input,16).rightJustified(nibs,'0')).constData()) << input << uint(sizeof_T_Int*8-i-1); + addRow("%u-bits-random-%u", i, j) << input << uint(sizeof_T_Int*8-i-1); } } } @@ -380,7 +369,7 @@ void tst_QAlgorithms::countLeading_impl() QFETCH(uint, expected); const T_Int value = static_cast<T_Int>(input); - + PrintIfFailed pf(value); QCOMPARE(qCountLeadingZeroBits(value), expected); } |