QLinkedList - fix insert with iterator when the list is shared.

Before a call to erase on a shared instance would imply that the
item was inserted into the shared data (i.e all instances)

Change-Id: I655ccf04b1ad9bf82e6bfade58929538fa7df000
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Thorbjørn Martsum 2013-06-17 11:35:46 +02:00 committed by The Qt Project
parent 97f867212f
commit c35b55f076
2 changed files with 22 additions and 0 deletions

View File

@ -453,6 +453,9 @@ int QLinkedList<T>::count(const T &t) const
template <typename T>
typename QLinkedList<T>::iterator QLinkedList<T>::insert(iterator before, const T &t)
{
if (d->ref.isShared())
before = detach_helper2(before);
Node *i = before.i;
Node *m = new Node(t);
m->n = i;

View File

@ -47,6 +47,7 @@ class tst_QLinkedList : public QObject
Q_OBJECT
private slots:
void eraseValidIteratorsOnSharedList() const;
void insertWithIteratorsOnSharedList() const;
};
void tst_QLinkedList::eraseValidIteratorsOnSharedList() const
@ -73,5 +74,23 @@ void tst_QLinkedList::eraseValidIteratorsOnSharedList() const
QCOMPARE(*r, 10); // Ensure that number 2 instance was removed;
}
void tst_QLinkedList::insertWithIteratorsOnSharedList() const
{
QLinkedList<int> a, b;
a.append(5);
a.append(10);
a.append(20);
QLinkedList<int>::iterator i = a.begin();
++i;
++i;
b = a;
QLinkedList<int>::iterator i2 = a.insert(i, 15);
QCOMPARE(b.size(), 3);
QCOMPARE(a.size(), 4);
--i2;
QCOMPARE(*i2, 10);
}
QTEST_MAIN(tst_QLinkedList)
#include "tst_qlinkedlist.moc"