summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/tools/qhash/tst_qhash.cpp36
-rw-r--r--tests/auto/corelib/tools/qset/tst_qset.cpp28
2 files changed, 56 insertions, 8 deletions
diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
index ed21dc71b5..50b1248d91 100644
--- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp
+++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
@@ -76,6 +76,8 @@ private slots:
void reserveShared();
void reserveLessThanCurrentAmount();
+ void reserveKeepCapacity_data();
+ void reserveKeepCapacity();
void QTBUG98265();
@@ -2693,6 +2695,40 @@ void tst_QHash::reserveLessThanCurrentAmount()
}
}
+void tst_QHash::reserveKeepCapacity_data()
+{
+ QTest::addColumn<qsizetype>("requested");
+ auto addRow = [](qsizetype requested) {
+ QTest::addRow("%td", ptrdiff_t(requested)) << requested;
+ };
+
+ QHash<int, int> testHash = {{1, 1}};
+ qsizetype minCapacity = testHash.capacity();
+ addRow(minCapacity - 1);
+ addRow(minCapacity + 0);
+ addRow(minCapacity + 1);
+ addRow(2 * minCapacity - 1);
+ addRow(2 * minCapacity + 0);
+ addRow(2 * minCapacity + 1);
+}
+
+void tst_QHash::reserveKeepCapacity()
+{
+ QFETCH(qsizetype, requested);
+
+ QHash<qsizetype, qsizetype> hash;
+ hash.reserve(requested);
+ qsizetype initialCapacity = hash.capacity();
+ QCOMPARE_GE(initialCapacity, requested);
+
+ // insert this many elements into the hash
+ for (qsizetype i = 0; i < requested; ++i)
+ hash.insert(i, i);
+
+ // it mustn't have increased capacity after inserting the elements
+ QCOMPARE(hash.capacity(), initialCapacity);
+}
+
void tst_QHash::QTBUG98265()
{
QMultiHash<QUuid, QByteArray> a;
diff --git a/tests/auto/corelib/tools/qset/tst_qset.cpp b/tests/auto/corelib/tools/qset/tst_qset.cpp
index 734d1d491b..391e00485c 100644
--- a/tests/auto/corelib/tools/qset/tst_qset.cpp
+++ b/tests/auto/corelib/tools/qset/tst_qset.cpp
@@ -232,27 +232,39 @@ void tst_QSet::squeeze()
set.squeeze();
QVERIFY(set.capacity() < 100);
- for (int i = 0; i < 512; ++i)
+ for (int i = 0; i < 500; ++i)
set.insert(i);
- QVERIFY(set.capacity() == 512);
+ QCOMPARE(set.size(), 500);
+
+ // squeezed capacity for 500 elements
+ qsizetype capacity = set.capacity(); // current implementation: 512
+ QCOMPARE_GE(capacity, set.size());
set.reserve(50000);
- QVERIFY(set.capacity() >= 50000);
+ QVERIFY(set.capacity() >= 50000); // current implementation: 65536
set.squeeze();
- QVERIFY(set.capacity() == 512);
+ QCOMPARE(set.capacity(), capacity);
+ // removing elements does not shed capacity
set.remove(499);
- QVERIFY(set.capacity() == 512);
+ QCOMPARE(set.capacity(), capacity);
set.insert(499);
- QVERIFY(set.capacity() == 512);
+ QCOMPARE(set.capacity(), capacity);
- set.insert(1000);
- QVERIFY(set.capacity() == 1024);
+ // grow it beyond the current capacity
+ for (int i = set.size(); i <= capacity; ++i)
+ set.insert(i);
+ QCOMPARE(set.size(), capacity + 1);
+ QCOMPARE_GT(set.capacity(), capacity + 1);// current implementation: 2 * capacity (1024)
for (int i = 0; i < 500; ++i)
set.remove(i);
+
+ // removing elements does not shed capacity
+ QCOMPARE_GT(set.capacity(), capacity + 1);
+
set.squeeze();
QVERIFY(set.capacity() < 100);
}