diff options
author | Thorbjørn Martsum <tmartsum@gmail.com> | 2013-02-13 17:48:49 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-03-07 06:12:59 +0100 |
commit | 510660080de6fab87e117de2663c33eff5ae451b (patch) | |
tree | 20edeb768f3dbfb2b4e85c86d4d3ede6a04e41b0 /tests/auto/corelib/tools/qvector/tst_qvector.cpp | |
parent | d1308232d31cb0b414e844fb18c8a02b7cba62c1 (diff) |
QVector - add remove functions
This patch adds the functions removeFirst() and removeLast().
Functions that QList has.
Beside making these functions, pop_back and pop_front are
redirected to these rather than calling erase.
Change-Id: Ifc5f8a78e33f436f06f21095a920ec5d4311fd6f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/tools/qvector/tst_qvector.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qvector/tst_qvector.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index 7738a2c797..3acd5ddb10 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -236,6 +236,7 @@ private slots: void removeInt() const; void removeMovable() const; void removeCustom() const; + void removeFirstLast() const; void resizePOD_data() const; void resizePOD() const; void resizeComplexMovable_data() const; @@ -1393,6 +1394,81 @@ void tst_QVector::removeCustom() const QCOMPARE(instancesCount, Custom::counter.loadAcquire()); } +struct RemoveLastTestClass +{ + RemoveLastTestClass() { other = 0; deleted = false; } + RemoveLastTestClass *other; + bool deleted; + ~RemoveLastTestClass() + { + deleted = true; + if (other) + other->other = 0; + } +}; + +void tst_QVector::removeFirstLast() const +{ + // pop_pack - pop_front + QVector<int> t; + t.append(1); + t.append(2); + t.append(3); + t.append(4); + t.pop_front(); + QCOMPARE(t.size(), 3); + QCOMPARE(t.at(0), 2); + t.pop_back(); + QCOMPARE(t.size(), 2); + QCOMPARE(t.at(0), 2); + QCOMPARE(t.at(1), 3); + + // remove first + QVector<int> x, y; + x.append(1); + x.append(2); + y = x; + x.removeFirst(); + QCOMPARE(x.size(), 1); + QCOMPARE(y.size(), 2); + QCOMPARE(x.at(0), 2); + + // remove Last + QVector<RemoveLastTestClass> v; + v.resize(2); + v[0].other = &(v[1]); + v[1].other = &(v[0]); + // Check dtor - complex type + QVERIFY(v.at(0).other != 0); + v.removeLast(); + QVERIFY(v.at(0).other == 0); + QCOMPARE(v.at(0).deleted, false); + // check iterator + int count = 0; + for (QVector<RemoveLastTestClass>::const_iterator i = v.constBegin(); i != v.constEnd(); ++i) { + ++count; + QVERIFY(i->other == 0); + QCOMPARE(i->deleted, false); + } + // Check size + QCOMPARE(count, 1); + QCOMPARE(v.size(), 1); + v.removeLast(); + QCOMPARE(v.size(), 0); + // Check if we do correct realloc + QVector<int> v2, v3; + v2.append(1); + v2.append(2); + v3 = v2; // shared + v2.removeLast(); + QCOMPARE(v2.size(), 1); + QCOMPARE(v3.size(), 2); + QCOMPARE(v2.at(0), 1); + QCOMPARE(v3.at(0), 1); + QCOMPARE(v3.at(1), 2); +} + + void tst_QVector::resizePOD_data() const { QTest::addColumn<QVector<int> >("vector"); |