Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: base/containers/scoped_ptr_map_unittest.cc

Issue 1328203002: Fix ScopedPtrMap with custom Compare. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit tests. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/containers/scoped_ptr_map.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/containers/scoped_ptr_map.h" 5 #include "base/containers/scoped_ptr_map.h"
6 6
7 #include <functional> 7 #include <functional>
8 #include <map> 8 #include <map>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; 140 ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map;
141 scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed))); 141 scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed)));
142 EXPECT_FALSE(destroyed); 142 EXPECT_FALSE(destroyed);
143 scoped_map.clear(); 143 scoped_map.clear();
144 EXPECT_TRUE(destroyed); 144 EXPECT_TRUE(destroyed);
145 EXPECT_TRUE(scoped_map.empty()); 145 EXPECT_TRUE(scoped_map.empty());
146 } 146 }
147 147
148 TEST(ScopedPtrMapTest, Compare) { 148 TEST(ScopedPtrMapTest, Compare) {
149 // Construct a ScopedPtrMap with a custom comparison function. 149 // Construct a ScopedPtrMap with a custom comparison function.
150 bool destroyed = false; 150 ScopedPtrMap<int, scoped_ptr<int>, std::greater<int>> scoped_map1;
151 ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>, std::greater<int>> scoped_map; 151 scoped_map1.insert(0, make_scoped_ptr(new int(0)));
152 scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed))); 152 scoped_map1.insert(1, make_scoped_ptr(new int(0)));
153 scoped_map.insert(1, make_scoped_ptr(new ScopedDestroyer(&destroyed)));
154 153
155 auto it = scoped_map.begin(); 154 auto it = scoped_map1.begin();
156 EXPECT_EQ(1, it->first); 155 EXPECT_EQ(1, it->first);
157 ++it; 156 ++it;
158 EXPECT_EQ(0, it->first); 157 EXPECT_EQ(0, it->first);
158
159 // Test the move constructor.
160 ScopedPtrMap<int, scoped_ptr<int>, std::greater<int>> scoped_map2(
161 scoped_map1.Pass());
162 EXPECT_EQ(2u, scoped_map2.size());
163 EXPECT_TRUE(scoped_map1.empty());
164
165 // Test move assignment.
166 scoped_map1 = scoped_map2.Pass();
167 EXPECT_EQ(2u, scoped_map1.size());
168 EXPECT_TRUE(scoped_map2.empty());
169
170 // Test swap.
171 scoped_map2.swap(scoped_map1);
172 EXPECT_EQ(2u, scoped_map2.size());
173 EXPECT_TRUE(scoped_map1.empty());
159 } 174 }
160 175
161 TEST(ScopedPtrMapTest, Scope) { 176 TEST(ScopedPtrMapTest, Scope) {
162 bool destroyed = false; 177 bool destroyed = false;
163 { 178 {
164 ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; 179 ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map;
165 scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed))); 180 scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed)));
166 EXPECT_FALSE(destroyed); 181 EXPECT_FALSE(destroyed);
167 } 182 }
168 EXPECT_TRUE(destroyed); 183 EXPECT_TRUE(destroyed);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 EXPECT_TRUE(scoped_map.empty()); 246 EXPECT_TRUE(scoped_map.empty());
232 EXPECT_EQ(elem, result.find(0)->second); 247 EXPECT_EQ(elem, result.find(0)->second);
233 EXPECT_FALSE(destroyed); 248 EXPECT_FALSE(destroyed);
234 249
235 result.clear(); 250 result.clear();
236 EXPECT_TRUE(destroyed); 251 EXPECT_TRUE(destroyed);
237 }; 252 };
238 253
239 } // namespace 254 } // namespace
240 } // namespace base 255 } // namespace base
OLDNEW
« no previous file with comments | « base/containers/scoped_ptr_map.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698