diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2017-03-07 21:56:32 +0000 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2017-03-07 21:56:32 +0000 |
commit | 14ab3e6885ba8e703889fc5e49fbe059d57b1eb6 (patch) | |
tree | 88e239284f2d480d9a91b49ab5839ff47021038e /llvm/unittests/ADT/SmallPtrSetTest.cpp | |
parent | 48d257d76cac5bfc3f0e1f15125d21577a2299da (diff) | |
download | bcm5719-llvm-14ab3e6885ba8e703889fc5e49fbe059d57b1eb6.tar.gz bcm5719-llvm-14ab3e6885ba8e703889fc5e49fbe059d57b1eb6.zip |
ADT: Fix SmallPtrSet iterators in reverse mode
Fix SmallPtrSet::iterator behaviour and creation ReverseIterate is true.
- Any function that creates an iterator now uses
SmallPtrSet::makeIterator, which creates an iterator that
dereferences to the given pointer.
- In reverse-iterate mode, initialze iterator::End with "CurArray"
instead of EndPointer.
- In reverse-iterate mode, the current node is iterator::Buffer[-1].
iterator::operator* and SmallPtrSet::makeIterator are the only ones
that need to know.
- Fix the assertions for reverse-iterate mode.
This fixes the tests Danny B added in r297182, and adds a couple of
others to confirm that dereferencing does the right thing, regardless of
how the iterator was found, and that iteration works correctly from each
return from find.
llvm-svn: 297234
Diffstat (limited to 'llvm/unittests/ADT/SmallPtrSetTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/SmallPtrSetTest.cpp | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/llvm/unittests/ADT/SmallPtrSetTest.cpp b/llvm/unittests/ADT/SmallPtrSetTest.cpp index bb9ee67b7eb..fc14c684d67 100644 --- a/llvm/unittests/ADT/SmallPtrSetTest.cpp +++ b/llvm/unittests/ADT/SmallPtrSetTest.cpp @@ -282,6 +282,28 @@ TEST(SmallPtrSetTest, EraseTest) { checkEraseAndIterators(A); } +// Verify that dereferencing and iteration work. +TEST(SmallPtrSetTest, dereferenceAndIterate) { + int Ints[] = {0, 1, 2, 3, 4, 5, 6, 7}; + SmallPtrSet<const int *, 4> S; + for (int &I : Ints) { + EXPECT_EQ(&I, *S.insert(&I).first); + EXPECT_EQ(&I, *S.find(&I)); + } + + // Iterate from each and count how many times each element is found. + int Found[sizeof(Ints)/sizeof(int)] = {0}; + for (int &I : Ints) + for (auto F = S.find(&I), E = S.end(); F != E; ++F) + ++Found[*F - Ints]; + + // Sort. We should hit the first element just once and the final element N + // times. + std::sort(std::begin(Found), std::end(Found)); + for (auto F = std::begin(Found), E = std::end(Found); F != E; ++F) + EXPECT_EQ(F - Found + 1, *F); +} + // Verify that const pointers work for count and find even when the underlying // SmallPtrSet is not for a const pointer type. TEST(SmallPtrSetTest, ConstTest) { @@ -292,10 +314,8 @@ TEST(SmallPtrSetTest, ConstTest) { IntSet.insert(B); EXPECT_EQ(IntSet.count(B), 1u); EXPECT_EQ(IntSet.count(C), 1u); - // FIXME: We can't unit test find right now because ABI_BREAKING_CHECKS breaks - // find(). - // EXPECT_NE(IntSet.find(B), IntSet.end()); - // EXPECT_NE(IntSet.find(C), IntSet.end()); + EXPECT_NE(IntSet.find(B), IntSet.end()); + EXPECT_NE(IntSet.find(C), IntSet.end()); } // Verify that we automatically get the const version of PointerLikeTypeTraits @@ -308,7 +328,5 @@ TEST(SmallPtrSetTest, ConstNonPtrTest) { TestPair Pair(&A[0], 1); IntSet.insert(Pair); EXPECT_EQ(IntSet.count(Pair), 1u); - // FIXME: We can't unit test find right now because ABI_BREAKING_CHECKS breaks - // find(). - // EXPECT_NE(IntSet.find(Pair), IntSet.end()); + EXPECT_NE(IntSet.find(Pair), IntSet.end()); } |