diff options
Diffstat (limited to 'tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp index 5443cf120b..0174885cf3 100644 --- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp +++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp @@ -29,6 +29,7 @@ #include <QtTest/QTest> #include <qvarlengtharray.h> #include <qvariant.h> +#include <qscopedvaluerollback.h> #include <memory> @@ -36,10 +37,13 @@ class tst_QVarLengthArray : public QObject { Q_OBJECT private slots: + void defaultConstructor_int() { defaultConstructor<int>(); } + void defaultConstructor_QString() { defaultConstructor<QString>(); } void append(); void removeLast(); void oldTests(); void appendCausingRealloc(); + void appendIsStronglyExceptionSafe(); void resize(); void realloc(); void reverseIterators(); @@ -61,6 +65,8 @@ private slots: void implicitDefaultCtor(); private: + template <typename T> + void defaultConstructor(); template<typename T> void initializeList(); }; @@ -80,6 +86,23 @@ struct Tracker int Tracker::count = 0; +template <typename T> +void tst_QVarLengthArray::defaultConstructor() +{ + { + QVarLengthArray<T, 123> vla; + QCOMPARE(vla.size(), 0); + QVERIFY(vla.empty()); + QVERIFY(vla.isEmpty()); + QCOMPARE(vla.begin(), vla.end()); + QCOMPARE(vla.capacity(), 123); + } + { + QVarLengthArray<T> vla; + QCOMPARE(vla.capacity(), 256); // notice, should we change the default + } +} + void tst_QVarLengthArray::append() { QVarLengthArray<QString, 2> v; @@ -245,6 +268,49 @@ void tst_QVarLengthArray::appendCausingRealloc() d.append(i); } +void tst_QVarLengthArray::appendIsStronglyExceptionSafe() +{ +#ifdef QT_NO_EXCEPTIONS + QSKIP("This test requires exception support enabled in the compiler."); +#else + static bool throwOnCopyNow = false; + static bool throwOnMoveNow = false; + struct Thrower { + Thrower() = default; + Thrower(const Thrower &) + { + if (throwOnCopyNow) + throw 1; + } + Thrower &operator=(const Thrower &) = default; + Thrower(Thrower &&) + { + if (throwOnMoveNow) + throw 1; + } + Thrower &operator=(Thrower &&) = default; + ~Thrower() = default; + }; + + { + // ### TODO: QVLA isn't exception-safe when throwing during reallocation, + // ### so check with size() < capacity() for now + QVarLengthArray<Thrower, 2> vla(1); + { + Thrower t; + const QScopedValueRollback<bool> rb(throwOnCopyNow, true); + QVERIFY_EXCEPTION_THROWN(vla.push_back(t), int); + QCOMPARE(vla.size(), 1); + } + { + const QScopedValueRollback<bool> rb(throwOnMoveNow, true); + QVERIFY_EXCEPTION_THROWN(vla.push_back({}), int); + QCOMPARE(vla.size(), 1); + } + } +#endif +} + void tst_QVarLengthArray::resize() { //MOVABLE |