[llvm] r259151 - SmallPtrSetTest: Check that iterators are still valid after erase()

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 28 19:34:34 PST 2016


Author: matze
Date: Thu Jan 28 21:34:34 2016
New Revision: 259151

URL: http://llvm.org/viewvc/llvm-project?rev=259151&view=rev
Log:
SmallPtrSetTest: Check that iterators are still valid after erase()

Modified:
    llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp

Modified: llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp?rev=259151&r1=259150&r2=259151&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp (original)
+++ llvm/trunk/unittests/ADT/SmallPtrSetTest.cpp Thu Jan 28 21:34:34 2016
@@ -210,3 +210,42 @@ TEST(SmallPtrSetTest, SwapTest) {
   EXPECT_TRUE(a.count(&buf[1]));
   EXPECT_TRUE(a.count(&buf[3]));
 }
+
+void checkEraseAndIterators(SmallPtrSetImpl<int*> &S) {
+  int buf[3];
+
+  S.insert(&buf[0]);
+  S.insert(&buf[1]);
+  S.insert(&buf[2]);
+
+  // Iterators must still be valid after erase() calls;
+  auto B = S.begin();
+  auto M = std::next(B);
+  auto E = S.end();
+  EXPECT_TRUE(*B == &buf[0] || *B == &buf[1] || *B == &buf[2]);
+  EXPECT_TRUE(*M == &buf[0] || *M == &buf[1] || *M == &buf[2]);
+  EXPECT_TRUE(*B != *M);
+  int *Removable = *std::next(M);
+  // No iterator points to Removable now.
+  EXPECT_TRUE(Removable == &buf[0] || Removable == &buf[1] ||
+              Removable == &buf[2]);
+  EXPECT_TRUE(Removable != *B && Removable != *M);
+
+  S.erase(Removable);
+
+  // B,M,E iterators should still be valid
+  EXPECT_EQ(B, S.begin());
+  EXPECT_EQ(M, std::next(B));
+  EXPECT_EQ(E, S.end());
+  EXPECT_EQ(std::next(M), E);
+}
+
+TEST(SmallPtrSetTest, EraseTest) {
+  // Test when set stays small.
+  SmallPtrSet<int *, 8> B;
+  checkEraseAndIterators(B);
+
+  // Test when set grows big.
+  SmallPtrSet<int *, 2> A;
+  checkEraseAndIterators(A);
+}




More information about the llvm-commits mailing list