Skip to content

Commit

Permalink
Fix ScopedPtrMap with custom Compare.
Browse files Browse the repository at this point in the history
Copy operator, assignment operator and swap ignored the Compare template argument.

BUG=529201

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

Cr-Commit-Position: refs/heads/master@{#348005}
  • Loading branch information
pneubeck authored and Commit bot committed Sep 9, 2015
1 parent 6013930 commit 759762f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
6 changes: 3 additions & 3 deletions base/containers/scoped_ptr_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class ScopedPtrMap {

ScopedPtrMap() {}
~ScopedPtrMap() { clear(); }
ScopedPtrMap(ScopedPtrMap<Key, ScopedPtr>&& other) { swap(other); }
ScopedPtrMap(ScopedPtrMap&& other) { swap(other); }

ScopedPtrMap& operator=(ScopedPtrMap<Key, ScopedPtr>&& rhs) {
ScopedPtrMap& operator=(ScopedPtrMap&& rhs) {
swap(rhs);
return *this;
}
Expand All @@ -61,7 +61,7 @@ class ScopedPtrMap {
const_iterator begin() const { return data_.begin(); }
const_iterator end() const { return data_.end(); }

void swap(ScopedPtrMap<Key, ScopedPtr>& other) { data_.swap(other.data_); }
void swap(ScopedPtrMap& other) { data_.swap(other.data_); }

void clear() { STLDeleteValues(&data_); }

Expand Down
25 changes: 20 additions & 5 deletions base/containers/scoped_ptr_map_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,30 @@ TEST(ScopedPtrMapTest, Clear) {

TEST(ScopedPtrMapTest, Compare) {
// Construct a ScopedPtrMap with a custom comparison function.
bool destroyed = false;
ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>, std::greater<int>> scoped_map;
scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed)));
scoped_map.insert(1, make_scoped_ptr(new ScopedDestroyer(&destroyed)));
ScopedPtrMap<int, scoped_ptr<int>, std::greater<int>> scoped_map1;
scoped_map1.insert(0, make_scoped_ptr(new int(0)));
scoped_map1.insert(1, make_scoped_ptr(new int(0)));

auto it = scoped_map.begin();
auto it = scoped_map1.begin();
EXPECT_EQ(1, it->first);
++it;
EXPECT_EQ(0, it->first);

// Test the move constructor.
ScopedPtrMap<int, scoped_ptr<int>, std::greater<int>> scoped_map2(
scoped_map1.Pass());
EXPECT_EQ(2u, scoped_map2.size());
EXPECT_TRUE(scoped_map1.empty());

// Test move assignment.
scoped_map1 = scoped_map2.Pass();
EXPECT_EQ(2u, scoped_map1.size());
EXPECT_TRUE(scoped_map2.empty());

// Test swap.
scoped_map2.swap(scoped_map1);
EXPECT_EQ(2u, scoped_map2.size());
EXPECT_TRUE(scoped_map1.empty());
}

TEST(ScopedPtrMapTest, Scope) {
Expand Down

0 comments on commit 759762f

Please sign in to comment.