diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2016-02-16 15:24:38 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2016-02-29 07:57:47 +0000 |
commit | a7885c9756d423042bd0670d82d78d8dffe9be54 (patch) | |
tree | 913f0db5b4ef655c6d708781e7de5a352e5c5043 /tests/auto/corelib | |
parent | 85c2a128ef8d1b5fbddf551691bccc0770e558ba (diff) |
QVector: preserve capacity in clear()
This is what std::vector implementations usually do,
because it minimizes memory fragmentation and useless
allocations since no user will call clear() unless
she intends to append new data afterwards.
Fix calls to resize(0) that show how existing code
tried to work around the issue.
Adjust test. Port from QVERIFY(==) to QCOMPARE as a
drive-by.
[ChangeLog][QtCore][QVector] clear() now preserves
capacity. To shed capacity, call squeeze() or swap
with a default-constructed QVector object, see the
documentation for an example.
Change-Id: I9cebe611a97e027a89e821e64408a4741b31f1f6
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r-- | tests/auto/corelib/tools/qvector/tst_qvector.cpp | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index 8fad672f42..eb37d6b8e6 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -734,10 +734,11 @@ void tst_QVector::clear() const QVector<T> myvec; myvec << SimpleValue<T>::at(0) << SimpleValue<T>::at(1) << SimpleValue<T>::at(2); - QVERIFY(myvec.size() == 3); + const auto oldCapacity = myvec.capacity(); + QCOMPARE(myvec.size(), 3); myvec.clear(); - QVERIFY(myvec.size() == 0); - QVERIFY(myvec.capacity() == 0); + QCOMPARE(myvec.size(), 0); + QCOMPARE(myvec.capacity(), oldCapacity); } void tst_QVector::clearInt() const @@ -1945,7 +1946,7 @@ void tst_QVector::resizePOD() const const int capacity = vector.capacity(); - vector.resize(0); + vector.clear(); QCOMPARE(vector.size(), 0); QVERIFY(vector.capacity() <= capacity); } |