diff options
Diffstat (limited to 'tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp | 157 |
1 files changed, 100 insertions, 57 deletions
diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp index e97848fb1c..187b73eeec 100644 --- a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp @@ -79,6 +79,7 @@ private slots: void sharedPointerFromQObjectWithWeak(); void weakQObjectFromSharedPointer(); void objectCast(); + void objectCastStdSharedPtr(); void differentPointers(); void virtualBaseDifferentPointers(); #ifndef QTEST_NO_RTTI @@ -90,9 +91,7 @@ private slots: #endif void constCorrectness(); void customDeleter(); -#ifdef Q_COMPILER_LAMBDA void lambdaCustomDeleter(); -#endif void creating(); void creatingCvQualified(); void creatingVariadic(); @@ -227,7 +226,6 @@ struct NoDefaultConstructorConstRef2 NoDefaultConstructorConstRef2(const QByteArray &ba, int i = 42) : str(QString::fromLatin1(ba)), i(i) {} }; -#ifdef Q_COMPILER_RVALUE_REFS struct NoDefaultConstructorRRef1 { int &i; @@ -239,7 +237,6 @@ struct NoDefaultConstructorRRef2 std::unique_ptr<int> i; NoDefaultConstructorRRef2(std::unique_ptr<int> &&i) : i(std::move(i)) {} }; -#endif void tst_QSharedPointer::basics_data() { @@ -297,8 +294,8 @@ void tst_QSharedPointer::basics() QVERIFY(! (ptr == otherData)); QVERIFY(! (otherData == ptr)); } - QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1); - QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.load() == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.loadRelaxed() == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.loadRelaxed() == 1); { // create another object: @@ -310,8 +307,8 @@ void tst_QSharedPointer::basics() // otherData is deleted here } - QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1); - QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.load() == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.loadRelaxed() == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.loadRelaxed() == 1); { // create a copy: @@ -328,8 +325,8 @@ void tst_QSharedPointer::basics() QCOMPARE(copy.get(), aData); QVERIFY(copy == aData); } - QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1); - QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.load() == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.loadRelaxed() == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.loadRelaxed() == 1); { // create a weak reference: @@ -361,8 +358,8 @@ void tst_QSharedPointer::basics() QCOMPARE(strong.data(), aData); QCOMPARE(strong.get(), aData); } - QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.load() == 1); - QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.load() == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->weakref.loadRelaxed() == 1); + QVERIFY(!refCountData(ptr) || refCountData(ptr)->strongref.loadRelaxed() == 1); // aData is deleted here } @@ -508,7 +505,6 @@ void tst_QSharedPointer::swap() void tst_QSharedPointer::moveSemantics() { -#ifdef Q_COMPILER_RVALUE_REFS QSharedPointer<int> p1, p2(new int(42)), control = p2; QVERIFY(p1 != control); QVERIFY(p1.isNull()); @@ -561,9 +557,6 @@ void tst_QSharedPointer::moveSemantics() QVERIFY(w1.isNull()); QVERIFY(w2.isNull()); QVERIFY(w3.isNull()); -#else - QSKIP("This test requires C++11 rvalue/move semantics support in the compiler."); -#endif } void tst_QSharedPointer::useOfForwardDeclared() @@ -580,10 +573,10 @@ void tst_QSharedPointer::useOfForwardDeclared() // move assignment: QSharedPointer<ForwardDeclared> sp4; - sp4 = qMove(sp); + sp4 = std::move(sp); // and move constuction: - QSharedPointer<ForwardDeclared> sp5 = qMove(sp2); + QSharedPointer<ForwardDeclared> sp5 = std::move(sp2); // swapping: sp4.swap(sp3); @@ -850,15 +843,15 @@ void tst_QSharedPointer::upCast() QVERIFY(baseptr == derivedptr); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { QWeakPointer<DerivedData> derivedptr = qWeakPointerCast<DerivedData>(baseptr); QVERIFY(baseptr == derivedptr); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { QWeakPointer<Data> weakptr = baseptr; @@ -866,16 +859,16 @@ void tst_QSharedPointer::upCast() QVERIFY(baseptr == derivedptr); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { QSharedPointer<DerivedData> derivedptr = baseptr.staticCast<DerivedData>(); QVERIFY(baseptr == derivedptr); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); } class OtherObject: public QObject @@ -1122,6 +1115,60 @@ void tst_QSharedPointer::objectCast() safetyCheck(); } + +void tst_QSharedPointer::objectCastStdSharedPtr() +{ + { + OtherObject *data = new OtherObject; + std::shared_ptr<QObject> baseptr = std::shared_ptr<QObject>(data); + QVERIFY(baseptr.get() == data); + + // perform successful object cast + std::shared_ptr<OtherObject> ptr = qobject_pointer_cast<OtherObject>(baseptr); + QVERIFY(ptr.get()); + QVERIFY(ptr.get() == data); + + QVERIFY(baseptr.get() == data); + } + + { + OtherObject *data = new OtherObject; + std::shared_ptr<QObject> baseptr = std::shared_ptr<QObject>(data); + QVERIFY(baseptr.get() == data); + + // perform successful object cast + std::shared_ptr<OtherObject> ptr = qobject_pointer_cast<OtherObject>(std::move(baseptr)); + QVERIFY(ptr.get()); + QVERIFY(ptr.get() == data); + + QVERIFY(!baseptr.get()); + } + + { + QObject *data = new QObject; + std::shared_ptr<QObject> baseptr = std::shared_ptr<QObject>(data); + QVERIFY(baseptr.get() == data); + + // perform unsuccessful object cast + std::shared_ptr<OtherObject> ptr = qobject_pointer_cast<OtherObject>(baseptr); + QVERIFY(!ptr.get()); + + QVERIFY(baseptr.get() == data); + } + + { + QObject *data = new QObject; + std::shared_ptr<QObject> baseptr = std::shared_ptr<QObject>(data); + QVERIFY(baseptr.get() == data); + + // perform unsuccessful object cast + std::shared_ptr<OtherObject> ptr = qobject_pointer_cast<OtherObject>(std::move(baseptr)); + QVERIFY(!ptr.get()); + + QVERIFY(baseptr.get() == data); + } +} + void tst_QSharedPointer::differentPointers() { { @@ -1250,8 +1297,8 @@ void tst_QSharedPointer::dynamicCast() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { QWeakPointer<Data> weakptr = baseptr; @@ -1260,8 +1307,8 @@ void tst_QSharedPointer::dynamicCast() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { QSharedPointer<DerivedData> derivedptr = baseptr.dynamicCast<DerivedData>(); @@ -1269,8 +1316,8 @@ void tst_QSharedPointer::dynamicCast() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); } void tst_QSharedPointer::dynamicCastDifferentPointers() @@ -1285,8 +1332,8 @@ void tst_QSharedPointer::dynamicCastDifferentPointers() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { QWeakPointer<Data> weakptr = baseptr; @@ -1295,8 +1342,8 @@ void tst_QSharedPointer::dynamicCastDifferentPointers() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { QSharedPointer<DiffPtrDerivedData> derivedptr = baseptr.dynamicCast<DiffPtrDerivedData>(); @@ -1304,8 +1351,8 @@ void tst_QSharedPointer::dynamicCastDifferentPointers() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { Stuffing *nakedptr = dynamic_cast<Stuffing *>(baseptr.data()); @@ -1330,8 +1377,8 @@ void tst_QSharedPointer::dynamicCastVirtualBase() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { QWeakPointer<Data> weakptr = baseptr; @@ -1340,8 +1387,8 @@ void tst_QSharedPointer::dynamicCastVirtualBase() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { QSharedPointer<VirtualDerived> derivedptr = baseptr.dynamicCast<VirtualDerived>(); @@ -1349,8 +1396,8 @@ void tst_QSharedPointer::dynamicCastVirtualBase() QCOMPARE(derivedptr.data(), aData); QCOMPARE(static_cast<Data *>(derivedptr.data()), baseptr.data()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); } void tst_QSharedPointer::dynamicCastFailure() @@ -1362,15 +1409,15 @@ void tst_QSharedPointer::dynamicCastFailure() QSharedPointer<DerivedData> derivedptr = qSharedPointerDynamicCast<DerivedData>(baseptr); QVERIFY(derivedptr.isNull()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); { QSharedPointer<DerivedData> derivedptr = baseptr.dynamicCast<DerivedData>(); QVERIFY(derivedptr.isNull()); } - QCOMPARE(int(refCountData(baseptr)->weakref.load()), 1); - QCOMPARE(int(refCountData(baseptr)->strongref.load()), 1); + QCOMPARE(int(refCountData(baseptr)->weakref.loadRelaxed()), 1); + QCOMPARE(int(refCountData(baseptr)->strongref.loadRelaxed()), 1); } void tst_QSharedPointer::dynamicCastFailureNoLeak() @@ -1677,7 +1724,6 @@ void tst_QSharedPointer::customDeleter() safetyCheck(); } -#ifdef Q_COMPILER_LAMBDA // The compiler needs to be in C++11 mode and to support lambdas void tst_QSharedPointer::lambdaCustomDeleter() { @@ -1705,7 +1751,6 @@ void tst_QSharedPointer::lambdaCustomDeleter() } safetyCheck(); } -#endif void customQObjectDeleterFn(QObject *obj) { @@ -1741,8 +1786,8 @@ void tst_QSharedPointer::creating() QCOMPARE(Data::destructorCounter, 1); // valgrind will complain here if something happened to the pointer - QVERIFY(d->weakref.load() == 1); - QVERIFY(d->strongref.load() == 0); + QVERIFY(d->weakref.loadRelaxed() == 1); + QVERIFY(d->strongref.loadRelaxed() == 0); } safetyCheck(); @@ -1977,7 +2022,7 @@ void tst_QSharedPointer::threadStressTest() for (int r = 0; r < 5; ++r) { QVector<QThread*> allThreads(6 * qMax(strongThreadCount, weakThreadCount) + 3, 0); QSharedPointer<ThreadData> base = QSharedPointer<ThreadData>(new ThreadData(&counter)); - counter.store(0); + counter.storeRelaxed(0); // set the pointers for (int i = 0; i < strongThreadCount; ++i) { @@ -2011,8 +2056,8 @@ void tst_QSharedPointer::threadStressTest() // verify that the count is the right range int minValue = strongThreadCount; int maxValue = strongThreadCount + weakThreadCount; - QVERIFY(counter.load() >= minValue); - QVERIFY(counter.load() <= maxValue); + QVERIFY(counter.loadRelaxed() >= minValue); + QVERIFY(counter.loadRelaxed() <= maxValue); } } @@ -2245,11 +2290,9 @@ void tst_QSharedPointer::invalidConstructs_data() << &QTest::QExternalTest::tryCompileFail << "struct IncompatibleCustomDeleter { void operator()(int *); };\n" "QSharedPointer<Data> ptr(new Data, IncompatibleCustomDeleter());\n"; -#ifdef Q_COMPILER_LAMBDA QTest::newRow("incompatible-custom-lambda-deleter") << &QTest::QExternalTest::tryCompileFail << "QSharedPointer<Data> ptr(new Data, [](int *) {});\n"; -#endif } void tst_QSharedPointer::invalidConstructs() |