From 93ffb81df6e2cdc57ac8f70c6578f132024d7117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Fri, 12 Jul 2013 19:17:33 +0200 Subject: QHash/QSet - fix QHash::erase when the hash is shared Before a call to erase on a shared instance would imply that the item was removed from the shared data (i.e all instances) This patch improves the behavior to detach and erase the item specified by the iterator (i.e same behavior as QVector) Since QSet uses QHash it improves QSet the same way. Change-Id: I850b1efcf7bdfc85ceddb23128b048af95f75063 Reviewed-by: Thiago Macieira --- tests/auto/corelib/tools/qhash/tst_qhash.cpp | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index 71428310b8..af1c7aed15 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -78,6 +78,7 @@ private slots: void qthash_data(); void qthash(); + void eraseValidIteratorOnSharedHash(); }; struct Foo { @@ -1352,5 +1353,34 @@ void tst_QHash::qthash() QTEST(result, "hash"); } +void tst_QHash::eraseValidIteratorOnSharedHash() +{ + QHash a, b; + a.insert(10, 10); + a.insertMulti(10, 25); + a.insertMulti(10, 30); + a.insert(20, 20); + a.insert(40, 40); + + QHash::iterator i = a.begin(); + while (i.value() != 25) + ++i; + + b = a; + a.erase(i); + + QCOMPARE(b.size(), 5); + QCOMPARE(a.size(), 4); + + for (i = a.begin(); i != a.end(); ++i) + QVERIFY(i.value() != 25); + + int itemsWith10 = 0; + for (i = b.begin(); i != b.end(); ++i) + itemsWith10 += (i.key() == 10); + + QCOMPARE(itemsWith10, 3); +} + QTEST_APPLESS_MAIN(tst_QHash) #include "tst_qhash.moc" -- cgit v1.2.3