summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools
diff options
context:
space:
mode:
authorDennis Oberst <dennis.oberst@qt.io>2023-04-03 17:16:18 +0200
committerMarc Mutz <marc.mutz@qt.io>2023-05-16 20:09:39 +0200
commitbbbe5f45c4d354ef977d4e09459ef5ae4d6db0da (patch)
tree510a79687d3b5201465b65a1129d4238147f9775 /tests/auto/corelib/tools
parentd8bdb66e82b0a248d4bbc9f3898f3d8b61620dbd (diff)
QList: add STL-style assign()
Implemented assign() methods for QList to align with the criteria of std::vector, addressing the previously missing functionality. Reference: https://en.cppreference.com/w/cpp/container/vector/assign [ChangeLog][QtCore][QList] Added assign(). Fixes: QTBUG-106196 Change-Id: I5df8689c020dafde68d2cd7d09c769744fa8f137 Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'tests/auto/corelib/tools')
-rw-r--r--tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp5
-rw-r--r--tests/auto/corelib/tools/qlist/tst_qlist.cpp64
2 files changed, 69 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp
index f3909cd4e8..24f5dba17e 100644
--- a/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp
+++ b/tests/auto/corelib/tools/containerapisymmetry/tst_containerapisymmetry.cpp
@@ -338,6 +338,7 @@ private:
private Q_SLOTS:
void assign_std_vector() { assign_impl<std::vector<int>>(); };
void assign_QVarLengthArray() { assign_impl<QVarLengthArray<int, 4>>(); };
+ void assign_QList() { assign_impl<QList<int>>(); }
private:
template <typename Container>
@@ -802,6 +803,10 @@ void tst_ContainerApiSymmetry::assign_impl() const
iter.assign(8, tData);
c.assign(iter.begin(), iter.end());
CHECK(c, tData, c.size(), S(8), c.capacity(), std::max(oldCapacity, S(8)));
+
+ c.assign(iter.begin(), iter.begin());
+ QCOMPARE_EQ(c.size(), S(0));
+ QCOMPARE_EQ(c.capacity(), std::max(oldCapacity, S(8)));
}
{
// range version for input iterator
diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp
index 482079b0fe..050bb80ab1 100644
--- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp
+++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp
@@ -231,6 +231,9 @@ private slots:
void appendCustom() const { append<Custom>(); }
void appendRvalue() const;
void appendList() const;
+ void assignInt() const { assign<int>(); }
+ void assignMovable() const { assign<Movable>(); }
+ void assignCustom() const { assign<Custom>(); }
void at() const;
void capacityInt() const { capacity<int>(); }
void capacityMovable() const { capacity<Movable>(); }
@@ -396,6 +399,7 @@ private:
template<typename T> void testAssignment() const;
template<typename T> void add() const;
template<typename T> void append() const;
+ template<typename T> void assign() const;
template<typename T> void assignFromInitializerList() const;
template<typename T> void capacity() const;
template<typename T> void clear() const;
@@ -750,6 +754,66 @@ void tst_QList::append() const
}
}
+template <typename T>
+void tst_QList::assign() const
+{
+ TST_QLIST_CHECK_LEAKS(T)
+ {
+ QList<T> myvec;
+ myvec.assign(2, T_FOO);
+ QVERIFY(myvec.isDetached());
+ QCOMPARE(myvec, QList<T>() << T_FOO << T_FOO);
+
+ QList<T> myvecCopy = myvec;
+ QVERIFY(!myvec.isDetached());
+ QVERIFY(!myvecCopy.isDetached());
+ QVERIFY(myvec.isSharedWith(myvecCopy));
+ QVERIFY(myvecCopy.isSharedWith(myvec));
+
+ myvec.assign(3, T_BAR);
+ QCOMPARE(myvec, QList<T>() << T_BAR << T_BAR << T_BAR);
+ QVERIFY(myvec.isDetached());
+ QVERIFY(myvecCopy.isDetached());
+ QVERIFY(!myvec.isSharedWith(myvecCopy));
+ QVERIFY(!myvecCopy.isSharedWith(myvec));
+ }
+ {
+ QList<T> myvec;
+ myvec.assign(4, T_FOO);
+ QVERIFY(myvec.isDetached());
+ QCOMPARE(myvec, QList<T>() << T_FOO << T_FOO << T_FOO << T_FOO);
+
+ QList<T> myvecCopy = myvec;
+ QVERIFY(!myvec.isDetached());
+ QVERIFY(!myvecCopy.isDetached());
+ QVERIFY(myvec.isSharedWith(myvecCopy));
+ QVERIFY(myvecCopy.isSharedWith(myvec));
+
+ myvecCopy.assign(myvec.begin(), myvec.begin() + 2);
+ QVERIFY(myvec.isDetached());
+ QVERIFY(myvecCopy.isDetached());
+ QVERIFY(!myvec.isSharedWith(myvecCopy));
+ QVERIFY(!myvecCopy.isSharedWith(myvec));
+ QCOMPARE(myvecCopy, QList<T>() << T_FOO << T_FOO);
+ }
+ {
+ // Test the prepend optimization.
+ QList<T> withFreeSpaceAtBegin(16, T_FOO);
+ // try at most 100 times to create freeSpaceAtBegin():
+ for (int i = 0; i < 100 && !withFreeSpaceAtBegin.d.freeSpaceAtBegin(); ++i)
+ withFreeSpaceAtBegin.prepend(T_FOO);
+ QCOMPARE_GT(withFreeSpaceAtBegin.d.freeSpaceAtBegin(), 0);
+ const auto oldData = withFreeSpaceAtBegin.constData();
+ std::vector<T> v(withFreeSpaceAtBegin.capacity(), T_BAR);
+ withFreeSpaceAtBegin.assign(v.begin(), v.end());
+ QCOMPARE_EQ(withFreeSpaceAtBegin.d.freeSpaceAtBegin(), 0);
+ // TODO: Check for equality after the prepend optimization
+ // the following test checks that we didn't reallocate, but re-used the prepend buffer
+ QEXPECT_FAIL("","Use of freeSpaceAtBegin() isn't, yet, implemented", Continue);
+ QVERIFY(QtPrivate::q_points_into_range(oldData, withFreeSpaceAtBegin));
+ }
+}
+
void tst_QList::appendRvalue() const
{
QList<QString> v;