summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp')
-rw-r--r--tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp72
1 files changed, 35 insertions, 37 deletions
diff --git a/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp b/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp
index 1e3a7572d0..99fc7c5772 100644
--- a/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp
+++ b/tests/auto/corelib/tools/qhashseed/tst_qhashseed.cpp
@@ -1,35 +1,12 @@
-/****************************************************************************
-**
-** 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>
#include <qhashfunctions.h>
+#if QT_CONFIG(process)
#include <qprocess.h>
+#endif
class tst_QHashSeed : public QObject
{
@@ -64,7 +41,7 @@ void tst_QHashSeed::initTestCase()
void tst_QHashSeed::environmentVariable_data()
{
-#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
+#ifdef Q_OS_ANDROID
QSKIP("This test needs a helper binary, so is excluded from this platform.");
#endif
@@ -77,6 +54,7 @@ void tst_QHashSeed::environmentVariable_data()
void tst_QHashSeed::environmentVariable()
{
+ #if QT_CONFIG(process)
QFETCH(QByteArray, envVar);
QFETCH(bool, isZero);
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
@@ -97,6 +75,7 @@ void tst_QHashSeed::environmentVariable()
QByteArray line2 = helper.readLine().trimmed();
QCOMPARE(line2, line1);
QCOMPARE(line1 == "0", isZero);
+#endif
}
void tst_QHashSeed::deterministicSeed()
@@ -130,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) {
@@ -142,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)