diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2022-10-17 08:51:57 -0700 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-10-18 14:15:51 +0000 |
commit | d9280bb63b0c5f0557385832cddc6097c50926c9 (patch) | |
tree | b7724d10a90d2d7e54747e96aeb53af2e34a7025 /tests/auto/corelib | |
parent | 6ba003f73295b896aa6dc1fba099daadb4760209 (diff) |
tst_QSet: fix flakiness after we removed the fixed, non-zero seed
The "qhash" test relied on the fact that those four elements would
produce a different order with a zero and a non-zero seed. But since
commit b057e32dc455d81f9378d6bd0c58888a7eddd155 removed the setting of a
deterministic non-zero seed, this test had a 1 in 4! chance of failing.
Since 4! = 24, 128 retries should be more than enough to ensure we do
find at least hash seed that provokes a different order.
Fixes: QTBUG-107725
Change-Id: I3c79b7e08fa346988dfefffd171ee61b79ca5489
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r-- | tests/auto/corelib/tools/qset/tst_qset.cpp | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/tests/auto/corelib/tools/qset/tst_qset.cpp b/tests/auto/corelib/tools/qset/tst_qset.cpp index 433568be63..734d1d491b 100644 --- a/tests/auto/corelib/tools/qset/tst_qset.cpp +++ b/tests/auto/corelib/tools/qset/tst_qset.cpp @@ -1045,17 +1045,27 @@ void tst_QSet::qhash() s1.reserve(4); s1 << 400 << 300 << 200 << 100; - // also change the seed: - QHashSeed::resetRandomGlobalSeed(); - - QSet<int> s2; - s2.reserve(100); // provoke different bucket counts - s2 << 100 << 200 << 300 << 400; // and insert elements in different order, too - - QVERIFY(s1.capacity() != s2.capacity()); - QCOMPARE(s1, s2); - QVERIFY(!std::equal(s1.cbegin(), s1.cend(), s2.cbegin())); // verify that the order _is_ different - QCOMPARE(qHash(s1), qHash(s2)); + int retries = 128; + while (--retries >= 0) { + // reset the global seed to something different + QHashSeed::resetRandomGlobalSeed(); + + QSet<int> s2; + s2.reserve(100); // provoke different bucket counts + s2 << 100 << 200 << 300 << 400; // and insert elements in different order, too + QVERIFY(s1.capacity() != s2.capacity()); + + // see if we got a _different_ order + if (std::equal(s1.cbegin(), s1.cend(), s2.cbegin())) + continue; + + // check if the two QHashes still compare equal and produce the + // same hash, despite containing elements in different orders + QCOMPARE(s1, s2); + QCOMPARE(qHash(s1), qHash(s2)); + } + QVERIFY2(retries != 0, "Could not find a QSet with a different order of elements even " + "after a lot of retries. This is unlikely, but possible."); } // |