From a5c7a9032e9919bd495c1607bb2e6a668170442f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Sun, 16 Jun 2013 21:43:44 +0200 Subject: QMap - fix erase with iterator when the map 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) Change-Id: Ia44db84fc1388d92308bf0d2b32539ac4d53850b Reviewed-by: Thiago Macieira --- tests/auto/corelib/tools/qmap/tst_qmap.cpp | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/tools/qmap/tst_qmap.cpp b/tests/auto/corelib/tools/qmap/tst_qmap.cpp index 0742f19a5e..de9fa41feb 100644 --- a/tests/auto/corelib/tools/qmap/tst_qmap.cpp +++ b/tests/auto/corelib/tools/qmap/tst_qmap.cpp @@ -87,6 +87,7 @@ private slots: void initializerList(); void testInsertWithHint(); void testInsertMultiWithHint(); + void eraseValidIteratorOnSharedMap(); }; typedef QMap StringMap; @@ -1289,6 +1290,62 @@ void tst_QMap::testInsertMultiWithHint() QCOMPARE(map.size(), 14); } +void tst_QMap::eraseValidIteratorOnSharedMap() +{ + QMap a, b; + a.insert(10, 10); + a.insertMulti(10, 40); + a.insertMulti(10, 25); + a.insertMulti(10, 30); + a.insert(20, 20); + + QMap::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, 4); + + // Border cases + QMap ms1, ms2, ms3; + ms1.insert("foo", "bar"); + ms1.insertMulti("foo", "quux"); + ms1.insertMulti("foo", "bar"); + + QMap ::iterator si = ms1.begin(); + ms2 = ms1; + ms1.erase(si); + si = ms1.begin(); + QCOMPARE(si.value(), QString("quux")); + ++si; + QCOMPARE(si.value(), QString("bar")); + + si = ms2.begin(); + ++si; + ++si; + ms3 = ms2; + ms2.erase(si); + si = ms2.begin(); + QCOMPARE(si.value(), QString("bar")); + ++si; + QCOMPARE(si.value(), QString("quux")); + + QCOMPARE(ms1.size(), 2); + QCOMPARE(ms2.size(), 2); + QCOMPARE(ms3.size(), 3); +} QTEST_APPLESS_MAIN(tst_QMap) #include "tst_qmap.moc" -- cgit v1.2.3