summaryrefslogtreecommitdiffstats
path: root/base/id_map_unittest.cc
diff options
context:
space:
mode:
authorjsbell@chromium.org <jsbell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-29 21:36:22 +0000
committerjsbell@chromium.org <jsbell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-29 21:36:22 +0000
commitfea0b9617466710b4961623de0e7a34846e88cbe (patch)
treeb8dcfb9e8f20bb20fe96ae1460841d8239278e93 /base/id_map_unittest.cc
parent8560e31c3608c1c02fb7177cd1a7fc5197eff5a8 (diff)
downloadchromium_src-fea0b9617466710b4961623de0e7a34846e88cbe.zip
chromium_src-fea0b9617466710b4961623de0e7a34846e88cbe.tar.gz
chromium_src-fea0b9617466710b4961623de0e7a34846e88cbe.tar.bz2
Add IDMap::Clear() which removes all items.
Includes unit tests. R=brettw@chromium.org Review URL: https://chromiumcodereview.appspot.com/11266017 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164742 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/id_map_unittest.cc')
-rw-r--r--base/id_map_unittest.cc73
1 files changed, 73 insertions, 0 deletions
diff --git a/base/id_map_unittest.cc b/base/id_map_unittest.cc
index c4838ee..80c4c66 100644
--- a/base/id_map_unittest.cc
+++ b/base/id_map_unittest.cc
@@ -191,6 +191,42 @@ TEST_F(IDMapTest, AssignIterator) {
EXPECT_EQ(0, map.iteration_depth());
}
+TEST_F(IDMapTest, IteratorRemainsValidWhenClearing) {
+ IDMap<TestObject> map;
+
+ const int kCount = 5;
+ TestObject obj[kCount];
+ int32 ids[kCount];
+
+ for (int i = 0; i < kCount; i++)
+ ids[i] = map.Add(&obj[i]);
+
+ int counter = 0;
+ for (IDMap<TestObject>::const_iterator iter(&map);
+ !iter.IsAtEnd(); iter.Advance()) {
+ switch (counter) {
+ case 0:
+ EXPECT_EQ(ids[0], iter.GetCurrentKey());
+ EXPECT_EQ(&obj[0], iter.GetCurrentValue());
+ break;
+ case 1:
+ EXPECT_EQ(ids[1], iter.GetCurrentKey());
+ EXPECT_EQ(&obj[1], iter.GetCurrentValue());
+ map.Clear();
+ EXPECT_TRUE(map.IsEmpty());
+ EXPECT_EQ(0U, map.size());
+ break;
+ default:
+ FAIL() << "should not have that many elements";
+ break;
+ }
+ counter++;
+ }
+
+ EXPECT_TRUE(map.IsEmpty());
+ EXPECT_EQ(0U, map.size());
+}
+
TEST_F(IDMapTest, OwningPointersDeletesThemOnRemove) {
const int kCount = 3;
@@ -229,6 +265,43 @@ TEST_F(IDMapTest, OwningPointersDeletesThemOnRemove) {
EXPECT_EQ(owned_del_count, kCount);
}
+TEST_F(IDMapTest, OwningPointersDeletesThemOnClear) {
+ const int kCount = 3;
+
+ int external_del_count = 0;
+ DestructorCounter* external_obj[kCount];
+
+ int owned_del_count = 0;
+ DestructorCounter* owned_obj[kCount];
+
+ IDMap<DestructorCounter> map_external;
+ IDMap<DestructorCounter, IDMapOwnPointer> map_owned;
+
+ for (int i = 0; i < kCount; ++i) {
+ external_obj[i] = new DestructorCounter(&external_del_count);
+ map_external.Add(external_obj[i]);
+
+ owned_obj[i] = new DestructorCounter(&owned_del_count);
+ map_owned.Add(owned_obj[i]);
+ }
+
+ EXPECT_EQ(external_del_count, 0);
+ EXPECT_EQ(owned_del_count, 0);
+
+ map_external.Clear();
+ map_owned.Clear();
+
+ EXPECT_EQ(external_del_count, 0);
+ EXPECT_EQ(owned_del_count, kCount);
+
+ for (int i = 0; i < kCount; ++i) {
+ delete external_obj[i];
+ }
+
+ EXPECT_EQ(external_del_count, kCount);
+ EXPECT_EQ(owned_del_count, kCount);
+}
+
TEST_F(IDMapTest, OwningPointersDeletesThemOnDestruct) {
const int kCount = 3;