From 715468df40e4ce97da04f327b6e34d535ff9b97d Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 27 Feb 2020 09:53:49 +0100 Subject: Fix flaky QElapsedTimer::elapsed test case Much of this test case was testing that the machine it runs on didn't take more than an expected amount of time, which is an assumption that won't hold in a virtual environment where the hypervisor might decide to not allocate any CPU time to the machine at certain times. Instead, take the samples that we want to compare with once, then use them as reference for further comparisons. Also, split the test in two, with the comparison operators and msecsTo test moved into a separate test function. Change-Id: I7db12b8e02552f4d63af933c1b0fee9d62b591eb Fixes: QTBUG-58713 Reviewed-by: Lars Knoll --- .../kernel/qelapsedtimer/tst_qelapsedtimer.cpp | 47 ++++++++++++++-------- 1 file changed, 30 insertions(+), 17 deletions(-) (limited to 'tests/auto/corelib') diff --git a/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp b/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp index 4ee3ca361f..bfc4f2ca36 100644 --- a/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp +++ b/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp @@ -48,6 +48,7 @@ private Q_SLOTS: void validity(); void basics(); void elapsed(); + void msecsTo(); }; void tst_QElapsedTimer::statics() @@ -108,30 +109,42 @@ void tst_QElapsedTimer::elapsed() t1.start(); QTest::qSleep(2*minResolution); - QElapsedTimer t2; - t2.start(); - - QVERIFY(t1 != t2); - QVERIFY(!(t1 == t2)); - QVERIFY(t1 < t2); - QVERIFY(t1.msecsTo(t2) > 0); - QVERIFY(t1.nsecsElapsed() > 0); - QVERIFY(t1.elapsed() > 0); + auto nsecs = t1.nsecsElapsed(); + auto msecs = t1.elapsed(); + QVERIFY(nsecs > 0); + QVERIFY(msecs > 0); // the number of elapsed nanoseconds and milliseconds should match - QVERIFY(t1.nsecsElapsed() - t1.elapsed() * 1000000 < 1000000); + QVERIFY(nsecs - msecs * 1000000 < 1000000); + + if (msecs > 8 * minResolution) + QSKIP("Sampling timer took too long, aborting test"); + QVERIFY(t1.hasExpired(minResolution)); QVERIFY(!t1.hasExpired(8*minResolution)); - QVERIFY(!t2.hasExpired(minResolution)); - QVERIFY(!t1.hasExpired(-1)); - QVERIFY(!t2.hasExpired(-1)); qint64 elapsed = t1.restart(); - QVERIFY(elapsed > minResolution); - QVERIFY(elapsed < 3*minResolution); - qint64 diff = t2.msecsTo(t1); - QVERIFY(diff < minResolution); + QVERIFY(elapsed >= msecs); + QVERIFY(elapsed < msecs + 3*minResolution); +} + +void tst_QElapsedTimer::msecsTo() +{ + QElapsedTimer t1; + t1.start(); + QTest::qSleep(minResolution); + QElapsedTimer t2; + t2.start(); + + QVERIFY(t1 != t2); + QVERIFY(!(t1 == t2)); + QVERIFY(t1 < t2); + + auto diff = t1.msecsTo(t2); + QVERIFY2(diff > 0, QString("difference t1 and t2 is %1").arg(diff).toLatin1()); + diff = t2.msecsTo(t1); + QVERIFY2(diff < 0, QString("difference t2 and t1 is %1").arg(diff).toLatin1()); } QTEST_MAIN(tst_QElapsedTimer); -- cgit v1.2.3 From 4a1de178c9cc891560f38d64d89074799b0fa0e1 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Fri, 28 Feb 2020 12:33:35 +0100 Subject: Try again to make QDeadlineTimer test robust against context switches Instead of comparing to absolute values, compare the result from QDeadlineTimer with the reference clock types from std::chrono. Pass the test as long as we are within 10% of that reference. In addition, handle the case where QTest::qSleep sleeps for more than 10% longer or shorter than what is requested, and if so, abort the test. Change-Id: If8b77aea55a8c5c53e96427b2fff2f78281d0f82 Reviewed-by: Lars Knoll --- .../kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp | 50 +++++++++++++++------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'tests/auto/corelib') diff --git a/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp b/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp index 35c5e7cb75..db53c3f20c 100644 --- a/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp +++ b/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp @@ -573,12 +573,32 @@ void tst_QDeadlineTimer::stdchrono() QCOMPARE(deadline.remainingTimeAsDuration(), nanoseconds::zero()); + /* + Call QTest::qSleep, and return true if the time actually slept is + within \a deviationPercent percent of the requested sleep time. + Otherwise, return false, in which case the test should to abort. + */ + auto sleepHelper = [](int ms, int deviationPercent = 10) -> bool { + auto before = steady_clock::now(); + QTest::qSleep(ms); + auto after = steady_clock::now(); + auto diff = duration_cast(after - before).count(); + bool inRange = qAbs(diff - ms) < ms * deviationPercent/100.0; + if (!inRange) + qWarning() << "sleeping" << diff << "instead of" << ms << inRange; + return inRange; + }; + auto steady_before = steady_clock::now(); auto system_before = system_clock::now(); - QTest::qSleep(minResolution); + if (!sleepHelper(minResolution)) + QSKIP("Slept too long"); auto now = QDeadlineTimer::current(timerType); - QTest::qSleep(minResolution); + auto steady_reference = steady_clock::now(); + auto system_reference = system_clock::now(); + if (!sleepHelper(minResolution)) + QSKIP("Slept too long"); auto sampling_start = steady_clock::now(); auto steady_deadline = now.deadline(); @@ -599,35 +619,33 @@ void tst_QDeadlineTimer::stdchrono() } { - auto diff = duration_cast(steady_after - steady_deadline); - QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count()))); - QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count()))); + auto reference = duration_cast(steady_after - steady_reference).count(); + auto diff = duration_cast(steady_after - steady_deadline).count(); + QVERIFY2(diff > reference * 0.9 && diff < reference*1.1, QByteArray::number(qint64(diff))); QDeadlineTimer dt_after(steady_after, timerType); QVERIFY2(now < dt_after, ("now = " + QLocale().toString(now.deadlineNSecs()) + "; after = " + QLocale().toString(dt_after.deadlineNSecs())).toLatin1()); - diff = duration_cast(steady_deadline - steady_before); - QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count()))); - QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count()))); + reference = duration_cast(steady_reference - steady_before).count(); + diff = duration_cast(steady_deadline - steady_before).count(); + QVERIFY2(diff > reference * 0.9 && diff < reference*1.1, QByteArray::number(qint64(diff))); QDeadlineTimer dt_before(steady_before, timerType); QVERIFY2(now > dt_before, ("now = " + QLocale().toString(now.deadlineNSecs()) + "; before = " + QLocale().toString(dt_before.deadlineNSecs())).toLatin1()); } { - auto diff = duration_cast(system_after - system_deadline); - QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count()))); - QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count()))); - QDeadlineTimer dt_after(system_after, timerType); + auto reference = duration_cast(system_after - system_reference).count(); + auto diff = duration_cast(system_after - system_deadline).count(); + QVERIFY2(diff > reference * 0.9 && diff < reference*1.1, QByteArray::number(qint64(diff))); QDeadlineTimer dt_after(system_after, timerType); QVERIFY2(now < dt_after, ("now = " + QLocale().toString(now.deadlineNSecs()) + "; after = " + QLocale().toString(dt_after.deadlineNSecs())).toLatin1()); - diff = duration_cast(system_deadline - system_before); - QVERIFY2(diff.count() > minResolution/2, QByteArray::number(qint64(diff.count()))); - QVERIFY2(diff.count() < 3*minResolution/2, QByteArray::number(qint64(diff.count()))); - QDeadlineTimer dt_before(system_before, timerType); + reference = duration_cast(system_reference - system_before).count(); + diff = duration_cast(steady_deadline - steady_before).count(); + QVERIFY2(diff > reference * 0.9 && diff < reference*1.1, QByteArray::number(qint64(diff))); QDeadlineTimer dt_before(system_before, timerType); QVERIFY2(now > dt_before, ("now = " + QLocale().toString(now.deadlineNSecs()) + "; before = " + QLocale().toString(dt_before.deadlineNSecs())).toLatin1()); -- cgit v1.2.3 From 466d32160a23e6848fe0a42e74085c79071d7dec Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 3 Mar 2020 08:01:05 +0100 Subject: Suppress warnings where QString and its tests use SplitBehavior This is a follow-up to commit 895939c7f91d0c8424a0638c42d05cb42293a142 to fix deprecation warnings it added. Change-Id: I3d86655ec2c84c1bdcac9c70436075fc78f2f781 Reviewed-by: Volker Hilsheimer --- tests/auto/corelib/text/qstring/tst_qstring.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests/auto/corelib') diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 7072cb9516..bc2b19125a 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -5824,6 +5824,8 @@ void tst_QString::split(const QString &string, const QString &sep, QStringList r QVERIFY(list == result); } +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED list = str.split(sep, QString::KeepEmptyParts); QVERIFY(list == result); list = str.split(rx, QString::KeepEmptyParts); @@ -5846,6 +5848,7 @@ void tst_QString::split(const QString &string, const QString &sep, QStringList r list = str.split(sep.at(0), QString::SkipEmptyParts); QVERIFY(list == result); } +QT_WARNING_POP } void tst_QString::split() -- cgit v1.2.3 From 14420b359b2d14d202df8df841af5e88f16352a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=C4=8Cuki=C4=87?= Date: Fri, 10 Jan 2020 18:13:41 +0100 Subject: Add operator-> to the key-value iterator for QHash and QMap This patch adds the arrow operator to the stl-like key-value iterator (QKeyValueIterator) for QMap and QHash. This allows using normal member access syntax it->first and it->second instead of having to use (*it).first and (*it).second. [ChangeLog][QtCore][Containers] Added operator-> to the key-value iterator for QHash/QMap. Change-Id: I9cfa6480784ebce147fcfbf37fec5ad0080e2899 Reviewed-by: Vitaly Fanaskov --- tests/auto/corelib/tools/qhash/tst_qhash.cpp | 6 ++++++ tests/auto/corelib/tools/qmap/tst_qmap.cpp | 6 ++++++ 2 files changed, 12 insertions(+) (limited to 'tests/auto/corelib') diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index b98ac38288..134b3ff4c0 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -1144,6 +1144,12 @@ void tst_QHash::keyValueIterator() entry_type pair(it.key(), it.value()); QCOMPARE(*key_value_it, pair); + QCOMPARE(key_value_it->first, pair.first); + QCOMPARE(key_value_it->second, pair.second); + QCOMPARE(&(*key_value_it).first, &it.key()); + QCOMPARE(&key_value_it->first, &it.key()); + QCOMPARE(&(*key_value_it).second, &it.value()); + QCOMPARE(&key_value_it->second, &it.value()); ++key_value_it; ++it; } diff --git a/tests/auto/corelib/tools/qmap/tst_qmap.cpp b/tests/auto/corelib/tools/qmap/tst_qmap.cpp index c3a8a88f0c..ba4b190f06 100644 --- a/tests/auto/corelib/tools/qmap/tst_qmap.cpp +++ b/tests/auto/corelib/tools/qmap/tst_qmap.cpp @@ -882,6 +882,12 @@ void tst_QMap::keyValueIterator() entry_type pair(it.key(), it.value()); QCOMPARE(*key_value_it, pair); + QCOMPARE(key_value_it->first, pair.first); + QCOMPARE(key_value_it->second, pair.second); + QCOMPARE(&(*key_value_it).first, &it.key()); + QCOMPARE(&key_value_it->first, &it.key()); + QCOMPARE(&(*key_value_it).second, &it.value()); + QCOMPARE(&key_value_it->second, &it.value()); ++key_value_it; ++it; } -- cgit v1.2.3