Commit cd2ec8c3 authored by zerny@chromium.org's avatar zerny@chromium.org

Fix for LinkedHashSet::swap.

R=erik.corry@gmail.com
BUG=

Review URL: https://codereview.chromium.org/474943002

git-svn-id: svn://svn.chromium.org/blink/trunk@180257 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 4108074a
...@@ -499,7 +499,7 @@ template<typename T, typename U, typename V, typename W> ...@@ -499,7 +499,7 @@ template<typename T, typename U, typename V, typename W>
inline void LinkedHashSet<T, U, V, W>::swap(LinkedHashSet& other) inline void LinkedHashSet<T, U, V, W>::swap(LinkedHashSet& other)
{ {
m_impl.swap(other.m_impl); m_impl.swap(other.m_impl);
swap(m_anchor, other.m_anchor); swapAnchor(m_anchor, other.m_anchor);
} }
template<typename T, typename U, typename V, typename Allocator> template<typename T, typename U, typename V, typename Allocator>
...@@ -667,8 +667,32 @@ inline void LinkedHashSet<T, U, V, W>::remove(ValuePeekInType value) ...@@ -667,8 +667,32 @@ inline void LinkedHashSet<T, U, V, W>::remove(ValuePeekInType value)
remove(find(value)); remove(find(value));
} }
inline void swapAnchor(LinkedHashSetNodeBase& a, LinkedHashSetNodeBase& b)
{
ASSERT(a.m_prev && a.m_next && b.m_prev && b.m_next);
swap(a.m_prev, b.m_prev);
swap(a.m_next, b.m_next);
if (b.m_next == &a) {
ASSERT(b.m_prev == &a);
b.m_next = &b;
b.m_prev = &b;
} else {
b.m_next->m_prev = &b;
b.m_prev->m_next = &b;
}
if (a.m_next == &b) {
ASSERT(a.m_prev == &b);
a.m_next = &a;
a.m_prev = &a;
} else {
a.m_next->m_prev = &a;
a.m_prev->m_next = &a;
}
}
inline void swap(LinkedHashSetNodeBase& a, LinkedHashSetNodeBase& b) inline void swap(LinkedHashSetNodeBase& a, LinkedHashSetNodeBase& b)
{ {
ASSERT(a.m_next != &a && b.m_next != &b);
swap(a.m_prev, b.m_prev); swap(a.m_prev, b.m_prev);
swap(a.m_next, b.m_next); swap(a.m_next, b.m_next);
if (b.m_next) { if (b.m_next) {
......
...@@ -656,4 +656,66 @@ TEST(ListHashSetTest, WithOwnPtr) ...@@ -656,4 +656,66 @@ TEST(ListHashSetTest, WithOwnPtr)
EXPECT_EQ(ptr2, ownPtr2); EXPECT_EQ(ptr2, ownPtr2);
} }
template<typename Set>
void swapTestHelper()
{
int num = 10;
Set set0;
Set set1;
Set set2;
for (int i = 0; i < num; ++i) {
set1.add(i + 1);
set2.add(num - i);
}
typename Set::iterator it1 = set1.begin();
typename Set::iterator it2 = set2.begin();
for (int i = 0; i < num; ++i, ++it1, ++it2) {
EXPECT_EQ(*it1, i + 1);
EXPECT_EQ(*it2, num - i);
}
EXPECT_EQ(set0.begin(), set0.end());
EXPECT_EQ(it1, set1.end());
EXPECT_EQ(it2, set2.end());
// Shift sets: 2->1, 1->0, 0->2
set1.swap(set2); // Swap with non-empty sets.
set0.swap(set2); // Swap with an empty set.
it1 = set0.begin();
it2 = set1.begin();
for (int i = 0; i < num; ++i, ++it1, ++it2) {
EXPECT_EQ(*it1, i + 1);
EXPECT_EQ(*it2, num - i);
}
EXPECT_EQ(it1, set0.end());
EXPECT_EQ(it2, set1.end());
EXPECT_EQ(set2.begin(), set2.end());
int removedIndex = num >> 1;
set0.remove(removedIndex + 1);
set1.remove(num - removedIndex);
it1 = set0.begin();
it2 = set1.begin();
for (int i = 0; i < num; ++i, ++it1, ++it2) {
if (i == removedIndex)
++i;
EXPECT_EQ(*it1, i + 1);
EXPECT_EQ(*it2, num - i);
}
EXPECT_EQ(it1, set0.end());
EXPECT_EQ(it2, set1.end());
}
TEST(ListHashSetTest, Swap)
{
swapTestHelper<ListHashSet<int> >();
}
TEST(LinkedHashSetTest, Swap)
{
swapTestHelper<LinkedHashSet<int> >();
}
} // namespace } // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment