summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMårten Nordheim <marten.nordheim@qt.io>2021-12-09 09:33:30 +0100
committerMårten Nordheim <marten.nordheim@qt.io>2021-12-13 18:58:36 +0100
commite508c4c094230ad0de66120caf3a50688b829755 (patch)
tree6d677930d34a1fc5f1932f17a6c69a2729a81048
parent20d869f4c54ed1b2ae472af24c66b3ed779fa207 (diff)
QCache: Adapt to upcoming QHash changes
QHash changes some of its preconditions, so we must not call findNode without verifying !isEmpty() Task-number: QTBUG-91739 Task-number: QTBUG-98436 Change-Id: I2701b9a01187530f541a7c9a12db56c92f856d87 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r--src/corelib/tools/qcache.h8
-rw-r--r--tests/auto/corelib/tools/qcache/tst_qcache.cpp16
2 files changed, 23 insertions, 1 deletions
diff --git a/src/corelib/tools/qcache.h b/src/corelib/tools/qcache.h
index 8a341c8555..32d822e892 100644
--- a/src/corelib/tools/qcache.h
+++ b/src/corelib/tools/qcache.h
@@ -160,6 +160,8 @@ class QCache
}
T *relink(const Key &key) const noexcept
{
+ if (isEmpty())
+ return nullptr;
Node *n = d.findNode(key);
if (!n)
return nullptr;
@@ -269,11 +271,13 @@ public:
}
inline bool contains(const Key &key) const noexcept
{
- return d.findNode(key) != nullptr;
+ return !isEmpty() && d.findNode(key) != nullptr;
}
bool remove(const Key &key) noexcept(std::is_nothrow_destructible_v<Node>)
{
+ if (isEmpty())
+ return false;
Node *n = d.findNode(key);
if (!n) {
return false;
@@ -285,6 +289,8 @@ public:
T *take(const Key &key) noexcept(std::is_nothrow_destructible_v<Key>)
{
+ if (isEmpty())
+ return nullptr;
Node *n = d.findNode(key);
if (!n)
return nullptr;
diff --git a/tests/auto/corelib/tools/qcache/tst_qcache.cpp b/tests/auto/corelib/tools/qcache/tst_qcache.cpp
index eb024e8f9e..58b8d6e4c6 100644
--- a/tests/auto/corelib/tools/qcache/tst_qcache.cpp
+++ b/tests/auto/corelib/tools/qcache/tst_qcache.cpp
@@ -37,6 +37,7 @@ public slots:
void initTestCase();
void cleanupTestCase();
private slots:
+ void empty();
void maxCost();
void setMaxCost();
void totalCost();
@@ -75,6 +76,21 @@ void tst_QCache::cleanupTestCase()
QCOMPARE(Foo::count, 0);
}
+void tst_QCache::empty()
+{
+ QCache<int, int> cache;
+ QCOMPARE(cache.size(), 0);
+ QCOMPARE(cache.count(), 0);
+ QVERIFY(cache.isEmpty());
+ QVERIFY(!cache.contains(1));
+ QCOMPARE(cache.keys().size(), 0);
+ QCOMPARE(cache.take(1), nullptr);
+ QVERIFY(!cache.remove(1));
+ QCOMPARE(cache.object(1), nullptr);
+ QCOMPARE(cache[1], nullptr);
+ QCOMPARE(cache.totalCost(), 0);
+}
+
void tst_QCache::maxCost()
{
QCache<QString, int> cache1, cache2(100), cache3(200), cache4(-50);