summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@qt.io>2020-02-13 09:14:09 +0100
committerAlexandru Croitor <alexandru.croitor@qt.io>2020-02-13 18:31:40 +0100
commit6b2535ea15cdbdb2355416b604f072fc13ff36b2 (patch)
tree4bf1560bab77c8b315850c5337ba31a0ea87b5f0 /tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
parent54c2cebabdda0280b8443c6947b6fee02445e138 (diff)
parent67491e2df5357706dbf88ddaf1f030ff095b4528 (diff)
Merge remote-tracking branch 'origin/5.15' into dev
Conflicts: examples/widgets/graphicsview/boxes/scene.h src/corelib/Qt5CoreMacros.cmake src/corelib/Qt6CoreMacros.cmake src/network/ssl/qsslsocket.cpp src/network/ssl/qsslsocket.h src/platformsupport/fontdatabases/windows/qwindowsfontenginedirectwrite.cpp src/testlib/CMakeLists.txt src/testlib/.prev_CMakeLists.txt tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp Disabled building manual tests with CMake for now, because qmake doesn't do it, and it confuses people. Done-With: Alexandru Croitor <alexandru.croitor@qt.io> Done-With: Volker Hilsheimer <volker.hilsheimer@qt.io> Change-Id: I865ae347bd01f4e59f16d007b66d175a52f1f152
Diffstat (limited to 'tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp')
-rw-r--r--tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp119
1 files changed, 113 insertions, 6 deletions
diff --git a/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp b/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
index 01181ce20e..4bb4113845 100644
--- a/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
+++ b/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sérgio Martins <sergio.martins@kdab.com>
+** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
@@ -30,24 +31,130 @@
#include <QtCore/QScopeGuard>
/*!
- \class tst_QScopedGuard
+ \class tst_QScopeGuard
\internal
\since 5.11
- \brief Tests class QScopedCleanup and function qScopeGuard
+ \brief Tests class QScopeGuard and function qScopeGuard
*/
-class tst_QScopedGuard : public QObject
+class tst_QScopeGuard : public QObject
{
Q_OBJECT
private Q_SLOTS:
+ void construction();
+ void constructionFromLvalue();
+ void constructionFromRvalue();
void leavingScope();
void exceptions();
};
+void func()
+{
+}
+
+int intFunc()
+{
+ return 0;
+}
+
+Q_REQUIRED_RESULT int noDiscardFunc()
+{
+ return 0;
+}
+
+struct Callable
+{
+ Callable() { }
+ Callable(const Callable &other)
+ {
+ Q_UNUSED(other);
+ ++copied;
+ }
+ Callable(Callable &&other)
+ {
+ Q_UNUSED(other);
+ ++moved;
+ }
+ void operator()() { }
+
+ static int copied;
+ static int moved;
+ static void resetCounts()
+ {
+ copied = 0;
+ moved = 0;
+ }
+};
+
+int Callable::copied = 0;
+int Callable::moved = 0;
+
static int s_globalState = 0;
-void tst_QScopedGuard::leavingScope()
+void tst_QScopeGuard::construction()
+{
+#ifdef __cpp_deduction_guides
+ QScopeGuard fromLambda([] { });
+ QScopeGuard fromFunction(func);
+ QScopeGuard fromFunctionPointer(&func);
+ QScopeGuard fromNonVoidFunction(intFunc);
+ QScopeGuard fromNoDiscardFunction(noDiscardFunc);
+#ifndef __apple_build_version__
+ QScopeGuard fromStdFunction{std::function(func)};
+ std::function stdFunction(func);
+ QScopeGuard fromNamedStdFunction(stdFunction);
+#endif
+#else
+ QSKIP("This test requires C++17 Class Template Argument Deduction support enabled in the compiler.");
+#endif
+}
+
+void tst_QScopeGuard::constructionFromLvalue()
+{
+#ifdef __cpp_deduction_guides
+ Callable::resetCounts();
+ {
+ Callable callable;
+ QScopeGuard guard(callable);
+ }
+ QCOMPARE(Callable::copied, 1);
+ QCOMPARE(Callable::moved, 0);
+ Callable::resetCounts();
+ {
+ Callable callable;
+ auto guard = qScopeGuard(callable);
+ }
+ QCOMPARE(Callable::copied, 1);
+ QCOMPARE(Callable::moved, 0);
+#else
+ QSKIP("This test requires C++17 Class Template Argument Deduction support enabled in the compiler.");
+#endif
+}
+
+void tst_QScopeGuard::constructionFromRvalue()
+{
+#ifdef __cpp_deduction_guides
+ Callable::resetCounts();
+ {
+ Callable callable;
+ QScopeGuard guard(std::move(callable));
+ }
+ QCOMPARE(Callable::copied, 0);
+ QCOMPARE(Callable::moved, 1);
+ Callable::resetCounts();
+ {
+ Callable callable;
+ auto guard = qScopeGuard(std::move(callable));
+ }
+ QCOMPARE(Callable::copied, 0);
+ QCOMPARE(Callable::moved, 1);
+#else
+ QSKIP("This test requires C++17 Class Template Argument Deduction support enabled in the compiler.");
+#endif
+}
+
+void tst_QScopeGuard::leavingScope()
{
auto cleanup = qScopeGuard([] { s_globalState++; QCOMPARE(s_globalState, 3); });
QCOMPARE(s_globalState, 0);
@@ -61,7 +168,7 @@ void tst_QScopedGuard::leavingScope()
s_globalState++;
}
-void tst_QScopedGuard::exceptions()
+void tst_QScopeGuard::exceptions()
{
s_globalState = 0;
bool caught = false;
@@ -81,5 +188,5 @@ void tst_QScopedGuard::exceptions()
}
-QTEST_MAIN(tst_QScopedGuard)
+QTEST_MAIN(tst_QScopeGuard)
#include "tst_qscopeguard.moc"