From fc172c43132a15e17bd435db90e85722c7100361 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 6 Nov 2020 15:57:03 +0100 Subject: Simplify the code for QList::emplace() Unify it with the code in QArrayDataOps and only have one emplace method there that handles it all. Adjust autotests to API changes in QArrayDataOps and fix a wrong test case (that just happened to pass by chance before). Change-Id: Ia08cadebe2f74b82c31f856b1ff8a3d8dc400a3c Reviewed-by: Andrei Golubev Reviewed-by: Thiago Macieira --- tests/auto/corelib/tools/qarraydata/simplevector.h | 11 +++-------- tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp | 16 ++++++++-------- 2 files changed, 11 insertions(+), 16 deletions(-) (limited to 'tests/auto/corelib') diff --git a/tests/auto/corelib/tools/qarraydata/simplevector.h b/tests/auto/corelib/tools/qarraydata/simplevector.h index 152bf02ce3..1dc9be765c 100644 --- a/tests/auto/corelib/tools/qarraydata/simplevector.h +++ b/tests/auto/corelib/tools/qarraydata/simplevector.h @@ -216,15 +216,10 @@ public: auto requiredSize = qsizetype(last - first); if (d->needsDetach() || d.freeSpaceAtEnd() < requiredSize) { - SimpleVector detached(DataPointer::allocateGrow(d, requiredSize, QArrayData::GrowsAtEnd)); - - if (d->size) { - const T *const begin = constBegin(); - detached.d->copyAppend(begin, begin + d->size); - } - detached.d->copyAppend(first, last); - detached.swap(*this); + DataPointer oldData; + d.reallocateAndGrow(QArrayData::GrowsAtEnd, requiredSize, &oldData); + d->copyAppend(first, last); return; } diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 68e8a26cb2..e5acee6971 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -1632,7 +1632,7 @@ void tst_QArrayData::arrayOpsExtra() const size_t originalSize = dataPointer.size; auto copy = cloneArrayDataPointer(dataPointer, dataPointer.size); - dataPointer->emplace(dataPointer.begin() + pos, value); + dataPointer->emplace(pos, value); QCOMPARE(size_t(dataPointer.size), originalSize + 1); size_t i = 0; for (; i < pos; ++i) @@ -2136,7 +2136,7 @@ struct MyQStringWrapper : public QString bool movedFrom = false; MyQStringWrapper() = default; MyQStringWrapper(QChar c) : QString(c) { } - MyQStringWrapper(MyQStringWrapper &&other) : QString(std::move(static_cast(other))) + MyQStringWrapper(MyQStringWrapper &&other) : QString(std::move(static_cast(other))) { movedTo = true; movedFrom = other.movedFrom; @@ -2144,7 +2144,7 @@ struct MyQStringWrapper : public QString } MyQStringWrapper &operator=(MyQStringWrapper &&other) { - QString::operator=(std::move(static_cast(other))); + QString::operator=(std::move(static_cast(other))); movedTo = true; movedFrom = other.movedFrom; other.movedFrom = true; @@ -2216,13 +2216,13 @@ void tst_QArrayData::selfEmplaceBackwards() QVERIFY(!adp.freeSpaceAtEnd()); QVERIFY(adp.freeSpaceAtBegin()); - adp->emplace(adp.end(), adp.data()[0]); + adp->emplace(adp.size, adp.data()[0]); for (qsizetype i = 0; i < adp.size - 1; ++i) { QCOMPARE(adp.data()[i], initValues[i]); } QCOMPARE(adp.data()[adp.size - 1], initValues[0]); - adp->emplace(adp.end(), std::move(adp.data()[0])); + adp->emplace(adp.size, std::move(adp.data()[0])); for (qsizetype i = 1; i < adp.size - 2; ++i) { QCOMPARE(adp.data()[i], initValues[i]); } @@ -2259,14 +2259,14 @@ void tst_QArrayData::selfEmplaceForward() QVERIFY(!adp.freeSpaceAtBegin()); QVERIFY(adp.freeSpaceAtEnd()); - adp->emplace(adp.begin(), adp.data()[adp.size - 1]); + adp->emplace(0, adp.data()[adp.size - 1]); for (qsizetype i = 1; i < adp.size; ++i) { QCOMPARE(adp.data()[i], initValues[i - 1]); } QCOMPARE(adp.data()[0], initValues[spaceAtBegin - 1]); - adp->emplace(adp.begin(), std::move(adp.data()[adp.size - 1])); - for (qsizetype i = 2; i < adp.size; ++i) { + adp->emplace(0, std::move(adp.data()[adp.size - 1])); + for (qsizetype i = 2; i < adp.size - 1; ++i) { QCOMPARE(adp.data()[i], initValues[i - 2]); } QCOMPARE(adp.data()[1], initValues[spaceAtBegin - 1]); -- cgit v1.2.3