summaryrefslogtreecommitdiffstats
path: root/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2022-07-11 16:51:26 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2022-08-12 20:32:30 +0200
commit54bcefb25c8050b8a3ce9e5afb9fd70d9dff5bca (patch)
tree94cd0c93ca083dfd9157d6ca41eca757295ac350 /tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp
parentf5840692b064ddf15107c7dffbbe7f5d735ec63c (diff)
Test QTRY_COMPARE() and expand testing of QTRY_VERIFY*()
In the process, simplify the latter while adding some actual time-variation for the QTRY_* loop to navigate round - based on the extendedcompare test's ClassWithDeferredSetter. Testing remains primitive, but is at least a bit more thorough. Pick-to: 6.4 Change-Id: I40be8fb485f3f18f0a4f4bc62ad36cccac691979 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Mitch Curtis <mitch.curtis@qt.io> Reviewed-by: Jason McDonald <macadder1@gmail.com>
Diffstat (limited to 'tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp')
-rw-r--r--tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp85
1 files changed, 79 insertions, 6 deletions
diff --git a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp
index 94c04157f2..072324880e 100644
--- a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp
+++ b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp
@@ -1,9 +1,9 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
-
-#include <QtCore/QCoreApplication>
#include <QTest>
+#include <QtCore/QCoreApplication>
+#include <QtCore/QTimer>
#ifdef QT_GUI_LIB
#include <QtGui/QColor>
#include <QtGui/QImage>
@@ -139,6 +139,7 @@ private slots:
void compareQVector3D();
void compareQVector4D();
#endif
+ void tryCompare();
void verify();
void verify2();
void tryVerify();
@@ -649,16 +650,88 @@ void tst_Cmptest::verify2()
QVERIFY2(opaqueFunc() < 2, QByteArray::number(opaqueFunc()).constData());
}
+class DeferredFlag : public QObject // Can't be const.
+{
+ Q_OBJECT
+ bool m_flag;
+public:
+ // A boolean that either starts out true or decays to true after 50 ms.
+ // However, that decay will only happen when the event loop is run.
+ explicit DeferredFlag(bool initial = false) : m_flag(initial)
+ {
+ if (!initial)
+ QTimer::singleShot(50, this, &DeferredFlag::onTimeOut);
+ }
+ explicit operator bool() const { return m_flag; }
+ bool operator!() const { return !m_flag; }
+ friend bool operator==(const DeferredFlag &a, const DeferredFlag &b)
+ {
+ return bool(a) == bool(b);
+ }
+public slots:
+ void onTimeOut() { m_flag = true; }
+};
+
+char *toString(const DeferredFlag &val)
+{
+ return qstrdup(bool(val) ? "DeferredFlag(true)" : "DeferredFlag(false)");
+}
+
+void tst_Cmptest::tryCompare()
+{
+ /* Note that expected values given as DeferredFlag() shall be re-evaluated
+ each time the comparison is checked, hence supply a fresh false instance,
+ that'll be discarded before it has a chance to decay, hence only compare
+ equal to a false instance. Do not replace them with a local variable
+ initialized to false, as it would (of course) decay.
+ */
+ DeferredFlag trueAlready(true);
+ {
+ DeferredFlag c;
+ // QTRY should check before looping, so be equal to the fresh false immediately.
+ QTRY_COMPARE(c, DeferredFlag());
+ // Given time, it'll end up equal to a true one.
+ QTRY_COMPARE(c, trueAlready);
+ }
+ {
+ DeferredFlag c;
+ QTRY_COMPARE_WITH_TIMEOUT(c, DeferredFlag(), 300);
+ QVERIFY(!c); // Instantly equal, so succeeded without delay.
+ QTRY_COMPARE_WITH_TIMEOUT(c, trueAlready, 200);
+ qInfo("Should now time out and fail");
+ QTRY_COMPARE_WITH_TIMEOUT(c, DeferredFlag(), 200);
+ }
+}
+
void tst_Cmptest::tryVerify()
{
- QTRY_VERIFY(opaqueFunc() > 2);
- QTRY_VERIFY_WITH_TIMEOUT(opaqueFunc() < 2, 1);
+ {
+ DeferredFlag c;
+ QTRY_VERIFY(!c);
+ QTRY_VERIFY(c);
+ }
+ {
+ DeferredFlag c;
+ QTRY_VERIFY_WITH_TIMEOUT(!c, 300);
+ QTRY_VERIFY_WITH_TIMEOUT(c, 200);
+ qInfo("Should now time out and fail");
+ QTRY_VERIFY_WITH_TIMEOUT(!c, 200);
+ }
}
void tst_Cmptest::tryVerify2()
{
- QTRY_VERIFY2(opaqueFunc() > 2, QByteArray::number(opaqueFunc()).constData());
- QTRY_VERIFY2_WITH_TIMEOUT(opaqueFunc() < 2, QByteArray::number(opaqueFunc()).constData(), 1);
+ {
+ DeferredFlag c;
+ QTRY_VERIFY2(!c, "Failed to check before looping");
+ QTRY_VERIFY2(c, "Failed to trigger single-shot");
+ }
+ {
+ DeferredFlag c;
+ QTRY_VERIFY2_WITH_TIMEOUT(!c, "Failed to check before looping", 300);
+ QTRY_VERIFY2_WITH_TIMEOUT(c, "Failed to trigger single-shot", 200);
+ QTRY_VERIFY2_WITH_TIMEOUT(!c, "Should time out and fail", 200);
+ }
}
void tst_Cmptest::verifyExplicitOperatorBool()