diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2017-11-23 17:39:52 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-01-20 09:27:14 +0000 |
commit | dd58ddd5d97f0663d5fafb7e81bff4fc7db13ba7 (patch) | |
tree | 4e275f3ded24dde28d6a751efe86784f2e8745a6 /tests/auto/corelib/tools/qvector | |
parent | 0d23b7f074fde36a164ed2535957114a52a28ad8 (diff) |
Add rvalue overload of insert/prepend to QVarLengthArray and QVector
Improves performance and STL compatibility by adding rvalue versions
of prepend and insert.
[ChangeLog][QtCore][QVarLengthArray] Added rvalue overloads of
prepend and insert.
[ChangeLog][QtCore][QVector] Added rvalue overloads of prepend
and insert.
[ChangeLog][QtCore][QVarLengthArray] Can now contain movable but
non-copyable types, such as std::unique_ptr.
Change-Id: I6c946acc5b67502c91c52ac5dea67cedb1af93a5
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/tools/qvector')
-rw-r--r-- | tests/auto/corelib/tools/qvector/tst_qvector.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index 713109a214..ef10357b6d 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -35,17 +35,28 @@ struct Movable { Movable(char input = 'j') : i(input) + , that(this) , state(Constructed) { counter.fetchAndAddRelaxed(1); } Movable(const Movable &other) : i(other.i) + , that(this) , state(Constructed) { check(other.state, Constructed); counter.fetchAndAddRelaxed(1); } + Movable(Movable &&other) + : i(other.i) + , that(other.that) + , state(Constructed) + { + check(other.state, Constructed); + counter.fetchAndAddRelaxed(1); + other.that = nullptr; + } ~Movable() { @@ -67,11 +78,27 @@ struct Movable { check(state, Constructed); check(other.state, Constructed); i = other.i; + that = this; + return *this; + } + Movable &operator=(Movable &&other) + { + check(state, Constructed); + check(other.state, Constructed); + i = other.i; + that = other.that; + other.that = nullptr; return *this; } + bool wasConstructedAt(const Movable *other) const + { + return that == other; + } char i; static QAtomicInt counter; private: + Movable *that; // used to check if an instance was moved + enum State { Constructed = 106, Destructed = 110 }; State state; @@ -297,6 +324,8 @@ private slots: void detachThreadSafetyMovable() const; void detachThreadSafetyCustom() const; + void insertMove() const; + private: template<typename T> void copyConstructor() const; template<typename T> void add() const; @@ -2861,6 +2890,34 @@ void tst_QVector::detachThreadSafetyCustom() const } } +void tst_QVector::insertMove() const +{ + const int instancesCount = Movable::counter.loadAcquire(); + { + QVector<Movable> vec; + Movable m1; + Movable m2; + Movable m3; + Movable m4; + + vec.append(std::move(m3)); + QVERIFY(vec.at(0).wasConstructedAt(&m3)); + vec.push_back(std::move(m4)); + QVERIFY(vec.at(0).wasConstructedAt(&m3)); + QVERIFY(vec.at(1).wasConstructedAt(&m4)); + vec.prepend(std::move(m1)); + QVERIFY(vec.at(0).wasConstructedAt(&m1)); + QVERIFY(vec.at(1).wasConstructedAt(&m3)); + QVERIFY(vec.at(2).wasConstructedAt(&m4)); + vec.insert(1, std::move(m2)); + QVERIFY(vec.at(0).wasConstructedAt(&m1)); + QVERIFY(vec.at(1).wasConstructedAt(&m2)); + QVERIFY(vec.at(2).wasConstructedAt(&m3)); + + QCOMPARE(Movable::counter.loadAcquire(), instancesCount + 8); + } + QCOMPARE(Movable::counter.loadAcquire(), instancesCount); +} QTEST_MAIN(tst_QVector) #include "tst_qvector.moc" |