diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 624d647c72d..25c00ce26fc 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -850,7 +850,11 @@ public: else d = Data::detached(d, size_t(size)); } - inline void squeeze() { reserve(0); } + inline void squeeze() + { + if (capacity()) + reserve(0); + } inline void detach() { if (!d || d->ref.isShared()) d = Data::detached(d); } inline bool isDetached() const noexcept { return d && !d->ref.isShared(); } diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index 0ca37b5f7aa..3c873d093ed 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -2587,6 +2587,8 @@ void tst_QHash::fineTuningInEmptyHash() { QHash hash; QCOMPARE(hash.capacity(), 0); + hash.squeeze(); + QCOMPARE(hash.capacity(), 0); QVERIFY(qFuzzyIsNull(hash.load_factor())); QVERIFY(!hash.isDetached()); diff --git a/tests/auto/corelib/tools/qset/tst_qset.cpp b/tests/auto/corelib/tools/qset/tst_qset.cpp index 2911f822e00..490e8e97fd4 100644 --- a/tests/auto/corelib/tools/qset/tst_qset.cpp +++ b/tests/auto/corelib/tools/qset/tst_qset.cpp @@ -244,12 +244,12 @@ void tst_QSet::reserve() void tst_QSet::squeeze() { QSet set; - int n = set.capacity(); - QVERIFY(n == 0); + QCOMPARE(set.capacity(), 0); set.squeeze(); - n = set.capacity(); - QVERIFY(n > 0 && n < 100); + QCOMPARE(set.capacity(), 0); + + QVERIFY(!set.isDetached()); set.reserve(1000); QVERIFY(set.capacity() >= 1000);