Remove my bogus MapVector::erase() with a narrower ::pop_back(), and add a unit test.
authorDouglas Gregor <dgregor@apple.com>
Tue, 19 Feb 2013 18:26:07 +0000 (18:26 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 19 Feb 2013 18:26:07 +0000 (18:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175538 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/MapVector.h
unittests/ADT/MapVectorTest.cpp

index 405fc43978cfbea86fa499e35729bb722694b28f..f6fcb0888de32595b699f0a5074b80721bfd1bbe 100644 (file)
@@ -119,14 +119,11 @@ public:
                             (Vector.begin() + Pos->second);
   }
 
                             (Vector.begin() + Pos->second);
   }
 
-  /// \brief Erase entry with the given key.
-  void erase(const KeyT &key) {
-    typename MapType::iterator Pos = Map.find(key);
-    if (Pos == Map.end())
-      return;
-
-    Vector.erase(Vector.begin() + Pos->second);
+  /// \brief Remove the last element from the vector.
+  void pop_back() {
+    typename MapType::iterator Pos = Map.find(Vector.back().first);
     Map.erase(Pos);
     Map.erase(Pos);
+    Vector.pop_back();
   }
 };
 
   }
 };
 
index 9f613697d57409a90aacb52fa8c177e8292e9a6b..11178bc15e84ef3a05ff7daa3537e63b2e37dff4 100644 (file)
@@ -13,7 +13,7 @@
 
 using namespace llvm;
 
 
 using namespace llvm;
 
-TEST(MapVectorTest, insert) {
+TEST(MapVectorTest, insert_pop) {
   MapVector<int, int> MV;
   std::pair<MapVector<int, int>::iterator, bool> R;
 
   MapVector<int, int> MV;
   std::pair<MapVector<int, int>::iterator, bool> R;
 
@@ -38,4 +38,18 @@ TEST(MapVectorTest, insert) {
   EXPECT_EQ(MV.size(), 2u);
   EXPECT_EQ(MV[1], 2);
   EXPECT_EQ(MV[4], 5);
   EXPECT_EQ(MV.size(), 2u);
   EXPECT_EQ(MV[1], 2);
   EXPECT_EQ(MV[4], 5);
+
+  MV.pop_back();
+  EXPECT_EQ(MV.size(), 1u);
+  EXPECT_EQ(MV[1], 2);
+
+  R = MV.insert(std::make_pair(4, 7));
+  ASSERT_NE(R.first, MV.end());
+  EXPECT_EQ(R.first->first, 4);
+  EXPECT_EQ(R.first->second, 7);
+  EXPECT_TRUE(R.second);  
+
+  EXPECT_EQ(MV.size(), 2u);
+  EXPECT_EQ(MV[1], 2);
+  EXPECT_EQ(MV[4], 7);
 }
 }