From f77356c6e99a89c2d198b054ad9161b0fb36a52f Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 27 Feb 2023 14:30:21 +0100 Subject: [PATCH] tst_qvarlengtharray: add test for QVLA(n) ctor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also add one for types that are neither copy- nor move-constructible. In contrast to resize(n), the QVLA(n) ctor worked for such types, so make sure it stays that way. Change-Id: If54fbc9dd6a4808175c4bcb0ffb492b33c879746 Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit e7c792ba7179c4c81fb2e26f66031ec81234b0d7) Reviewed-by: Qt Cherry-pick Bot --- .../qvarlengtharray/tst_qvarlengtharray.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp index 75ae23d5788..1ae18ca1717 100644 --- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp +++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp @@ -42,12 +42,29 @@ public: { return !operator==(lhs, rhs); } }; +class NonCopyable +{ + Q_DISABLE_COPY(NonCopyable) + int n; +public: + NonCopyable() : n(0) {} + explicit NonCopyable(int n) : n(n) {} + + friend bool operator==(const NonCopyable &lhs, const NonCopyable &rhs) noexcept + { return lhs.n == rhs.n; } + friend bool operator!=(const NonCopyable &lhs, const NonCopyable &rhs) noexcept + { return !operator==(lhs, rhs); } +}; + class tst_QVarLengthArray : public QObject { Q_OBJECT private slots: void defaultConstructor_int() { defaultConstructor(); } void defaultConstructor_QString() { defaultConstructor(); } + void sizeConstructor_int() { sizeConstructor(); } + void sizeConstructor_QString() { sizeConstructor(); } + void sizeConstructor_NonCopyable() { sizeConstructor(); } void append(); #if QT_DEPRECATED_SINCE(6, 3) void prepend(); @@ -99,6 +116,8 @@ private slots: private: template void defaultConstructor(); + template + void sizeConstructor(); template void move(T t1, T t2); template @@ -128,6 +147,31 @@ void tst_QVarLengthArray::defaultConstructor() } } +template +void tst_QVarLengthArray::sizeConstructor() +{ + { + QVarLengthArray vla(0); + QCOMPARE(vla.size(), 0); + QVERIFY(vla.empty()); + QVERIFY(vla.isEmpty()); + QCOMPARE(vla.begin(), vla.end()); + QCOMPARE(vla.capacity(), 123); + } + { + QVarLengthArray vla(124); + QCOMPARE(vla.size(), 124); + QVERIFY(!vla.empty()); + QCOMPARE(vla.capacity(), 124); + } + { + QVarLengthArray vla(125); + QCOMPARE(vla.size(), 125); + QVERIFY(!vla.empty()); + QCOMPARE_GE(vla.capacity(), 125); + } +} + void tst_QVarLengthArray::append() { QVarLengthArray v;