diff options
Diffstat (limited to 'tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp | 66 |
1 files changed, 30 insertions, 36 deletions
diff --git a/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp b/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp index 60f3fe9e3c..99fc7c5772 100644 --- a/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp +++ b/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 Intel Corporation. -** 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) 2021 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -134,8 +109,13 @@ void tst_QHashSeed::reseeding() void tst_QHashSeed::quality() { - constexpr int Iterations = 16; + // this "bad seed" is used internally in qhash.cpp and should never leak! + constexpr size_t BadSeed = size_t(Q_UINT64_C(0x5555'5555'5555'5555)); + + constexpr int Iterations = 24; // nicely divisible by 3 int oneThird = 0; + int badSeeds = 0; + int seedsToMinus1 = 0; size_t ored = 0; for (int i = 0; i < Iterations; ++i) { @@ -146,21 +126,35 @@ void tst_QHashSeed::quality() if (bits >= std::numeric_limits<size_t>::digits / 3) ++oneThird; + if (seed == BadSeed) + ++badSeeds; + if (ored != size_t(-1)) + ++seedsToMinus1; + + QHashSeed::resetRandomGlobalSeed(); } // report out + qInfo() << "Number of seeds until all bits became set:" << seedsToMinus1 << '/' << Iterations; qInfo() << "Number of seeds with at least one third of the bits set:" << oneThird << '/' << Iterations; - qInfo() << "Number of bits in OR'ed value:" << qPopulationCount(quintptr(ored)) - << '/' << std::numeric_limits<size_t>::digits; - if (std::numeric_limits<size_t>::digits > 32) { - quint32 upper = quint64(ored) >> 32; - qInfo() << "Number of bits in the upper half:" << qPopulationCount(upper) << "/ 32"; - QVERIFY(qPopulationCount(upper) > (32/3)); - } + + // we must have set all bits after all the iterations + QCOMPARE(ored, size_t(-1)); // at least one third of the seeds must have one third of all the bits set - QVERIFY(oneThird > (16/3)); + QVERIFY(oneThird > (Iterations/3)); + + // at most one seed can be the bad seed, if 32-bit, none on 64-bit + if (std::numeric_limits<size_t>::digits > 32) + QCOMPARE(badSeeds, 0); + else + QVERIFY2(badSeeds <= 1, "badSeeds = " + QByteArray::number(badSeeds)); + + // we must have taken at most two thirds of the iterations to have set each + // bit at least once + QVERIFY2(seedsToMinus1 < 2*Iterations/3, + "seedsToMinus1 = " + QByteArray::number(seedsToMinus1)); } #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |