From b057e32dc455d81f9378d6bd0c58888a7eddd155 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Fri, 19 Aug 2022 11:57:32 +0200 Subject: [PATCH] Port tests away from using q{Set}GlobalQHashSeed These functions are marked as deprecated in future Qt releases. Task-number: QTBUG-104858 Change-Id: I25d2932455d8c9e3e2d722b1c48fc2cfa2d1e679 Reviewed-by: Thiago Macieira --- tests/auto/corelib/tools/qhash/tst_qhash.cpp | 23 +++++++++++++++---- .../qhashfunctions/tst_qhashfunctions.cpp | 6 +++++ tests/auto/corelib/tools/qset/tst_qset.cpp | 6 ++--- .../qdbusxmlparser/tst_qdbusxmlparser.cpp | 4 ++-- .../painting/tst_baseline_painting.cpp | 3 ++- .../stylesheet/tst_baseline_stylesheet.cpp | 3 ++- tests/baseline/text/tst_baseline_text.cpp | 3 ++- .../baseline/widgets/tst_baseline_widgets.cpp | 3 ++- 8 files changed, 38 insertions(+), 13 deletions(-) diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index 5b51d6888d..5c0fbab406 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -904,16 +904,31 @@ class QGlobalQHashSeedResetter int oldSeed; public: // not entirely correct (may lost changes made by another thread between the query - // of the old and the setting of the new seed), but qSetGlobalQHashSeed doesn't + // of the old and the setting of the new seed), but setHashSeed() can't // return the old value, so this is the best we can do: explicit QGlobalQHashSeedResetter(int newSeed) - : oldSeed(qGlobalQHashSeed()) + : oldSeed(getHashSeed()) { - qSetGlobalQHashSeed(newSeed); + setHashSeed(newSeed); } ~QGlobalQHashSeedResetter() { - qSetGlobalQHashSeed(oldSeed); + setHashSeed(oldSeed); + } + +private: + // The functions are implemented to replace the deprecated + // qGlobalQHashSeed() and qSetGlobalQHashSeed() + static int getHashSeed() + { + return int(QHashSeed::globalSeed() & INT_MAX); + } + static void setHashSeed(int seed) + { + if (seed == 0) + QHashSeed::setDeterministicGlobalSeed(); + else + QHashSeed::resetRandomGlobalSeed(); } }; diff --git a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp index d060f50609..2d04579253 100644 --- a/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp +++ b/tests/auto/corelib/tools/qhashfunctions/tst_qhashfunctions.cpp @@ -53,7 +53,9 @@ private Q_SLOTS: void stdPair_string_pairIntInt() { stdPair_template(QString("Hello"), std::make_pair(42, -47)); } // QTBUG-92910 void stdPair_int_pairIntPairIntInt() { stdPair_template(1, std::make_pair(2, std::make_pair(3, 4))); } +#if QT_DEPRECATED_SINCE(6, 6) void setGlobalQHashSeed(); +#endif }; void tst_QHashFunctions::consistent() @@ -370,8 +372,10 @@ void tst_QHashFunctions::stdPair_template(const T1 &t1, const T2 &t2) QCOMPARE(qHash(vpair, seed), qHash(vpair, seed)); } +#if QT_DEPRECATED_SINCE(6, 6) void tst_QHashFunctions::setGlobalQHashSeed() { +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED // Setter works as advertised qSetGlobalQHashSeed(0); QCOMPARE(qGlobalQHashSeed(), 0); @@ -384,7 +388,9 @@ void tst_QHashFunctions::setGlobalQHashSeed() // Reset works as advertised qSetGlobalQHashSeed(-1); QVERIFY(qGlobalQHashSeed() > 0); +QT_WARNING_POP } +#endif // QT_DEPRECATED_SINCE(6, 6) QTEST_APPLESS_MAIN(tst_QHashFunctions) #include "tst_qhashfunctions.moc" diff --git a/tests/auto/corelib/tools/qset/tst_qset.cpp b/tests/auto/corelib/tools/qset/tst_qset.cpp index e72bf34581..bd88c19bd0 100644 --- a/tests/auto/corelib/tools/qset/tst_qset.cpp +++ b/tests/auto/corelib/tools/qset/tst_qset.cpp @@ -1039,14 +1039,14 @@ void tst_QSet::qhash() // { // create some deterministic initial state: - qSetGlobalQHashSeed(0); + QHashSeed::setDeterministicGlobalSeed(); QSet s1; s1.reserve(4); s1 << 400 << 300 << 200 << 100; // also change the seed: - qSetGlobalQHashSeed(0x10101010); + QHashSeed::resetRandomGlobalSeed(); QSet s2; s2.reserve(100); // provoke different bucket counts @@ -1087,7 +1087,7 @@ void tst_QSet::intersects() s1 << 200; QVERIFY(s1.intersects(s2)); - qSetGlobalQHashSeed(0x10101010); + QHashSeed::resetRandomGlobalSeed(); QSet s3; s3 << 500; QVERIFY(!s1.intersects(s3)); diff --git a/tests/auto/dbus/qdbusxmlparser/tst_qdbusxmlparser.cpp b/tests/auto/dbus/qdbusxmlparser/tst_qdbusxmlparser.cpp index 3e01d18ffd..e25199c87f 100644 --- a/tests/auto/dbus/qdbusxmlparser/tst_qdbusxmlparser.cpp +++ b/tests/auto/dbus/qdbusxmlparser/tst_qdbusxmlparser.cpp @@ -34,8 +34,8 @@ private slots: void tst_QDBusXmlParser::initTestCase() { - // Always initialize the hash seed to 0 to get reliable test results - qSetGlobalQHashSeed(0); + // Always initialize the hash seed with a known value to get reliable test results + QHashSeed::setDeterministicGlobalSeed(); } void tst_QDBusXmlParser::parsing_data() diff --git a/tests/baseline/painting/tst_baseline_painting.cpp b/tests/baseline/painting/tst_baseline_painting.cpp index fe340e80e6..f9738b61e8 100644 --- a/tests/baseline/painting/tst_baseline_painting.cpp +++ b/tests/baseline/painting/tst_baseline_painting.cpp @@ -469,7 +469,8 @@ QTEST_MAIN(tst_Lancelot) int main(int argc, char *argv[]) { - qSetGlobalQHashSeed(0); // Avoid rendering variations caused by QHash randomization + // Avoid rendering variations caused by QHash randomization + QHashSeed::setDeterministicGlobalSeed(); QBaselineTest::handleCmdLineArgs(&argc, &argv); return _realmain(argc, argv); diff --git a/tests/baseline/stylesheet/tst_baseline_stylesheet.cpp b/tests/baseline/stylesheet/tst_baseline_stylesheet.cpp index 19bfed5e33..11c8221443 100644 --- a/tests/baseline/stylesheet/tst_baseline_stylesheet.cpp +++ b/tests/baseline/stylesheet/tst_baseline_stylesheet.cpp @@ -198,7 +198,8 @@ QTEST_MAIN(tst_Stylesheet) int main(int argc, char *argv[]) { - qSetGlobalQHashSeed(0); // Avoid rendering variations caused by QHash randomization + // Avoid rendering variations caused by QHash randomization + QHashSeed::setDeterministicGlobalSeed(); QBaselineTest::handleCmdLineArgs(&argc, &argv); return _realmain(argc, argv); diff --git a/tests/baseline/text/tst_baseline_text.cpp b/tests/baseline/text/tst_baseline_text.cpp index b3a0cf60fa..0d2c2d47f5 100644 --- a/tests/baseline/text/tst_baseline_text.cpp +++ b/tests/baseline/text/tst_baseline_text.cpp @@ -88,7 +88,8 @@ QTEST_MAIN(tst_Text) int main(int argc, char *argv[]) { - qSetGlobalQHashSeed(0); // Avoid rendering variations caused by QHash randomization + // Avoid rendering variations caused by QHash randomization + QHashSeed::setDeterministicGlobalSeed(); QBaselineTest::handleCmdLineArgs(&argc, &argv); return _realmain(argc, argv); diff --git a/tests/baseline/widgets/tst_baseline_widgets.cpp b/tests/baseline/widgets/tst_baseline_widgets.cpp index 51803f0dec..64b8ea4aba 100644 --- a/tests/baseline/widgets/tst_baseline_widgets.cpp +++ b/tests/baseline/widgets/tst_baseline_widgets.cpp @@ -1095,7 +1095,8 @@ QTEST_MAIN(tst_Widgets) int main(int argc, char *argv[]) { - qSetGlobalQHashSeed(0); // Avoid rendering variations caused by QHash randomization + // Avoid rendering variations caused by QHash randomization + QHashSeed::setDeterministicGlobalSeed(); QBaselineTest::handleCmdLineArgs(&argc, &argv); return _realmain(argc, argv);