Index: unittests/Support/ReverseIterationTest.cpp =================================================================== --- unittests/Support/ReverseIterationTest.cpp +++ unittests/Support/ReverseIterationTest.cpp @@ -12,6 +12,7 @@ //===---------------------------------------------------------------------===// #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallPtrSet.h" #include "llvm/Support/ReverseIteration.h" #include "gtest/gtest.h" @@ -53,3 +54,57 @@ for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i) ASSERT_EQ(iter->first, IterKeys[i]); } + +TEST(ReverseIterationTest, DenseMapTest2) { + struct PtrLike { int a, b, c, d; } P = { 4, 8, 12, 16 }; + static_assert(detail::IsPointerLike::value, + "ptrlike * is pointer-like"); + int *Keys[] = { &P.a, &P.b, &P.c, &P.d }; + + // Insert keys into the DenseMap. + DenseMap Map; + for (auto *Key : Keys) + Map[Key] = *Key; + + // Note: This is the observed order of keys in the DenseMap. + // If there is any change in the behavior of the DenseMap, this order + // would need to be adjusted accordingly. + int IterKeys[] = { P.a, P.b, P.c, P.d }; + if (shouldReverseIterate()) + std::reverse(&IterKeys[0], &IterKeys[4]); + + // Check that the DenseMap is iterated in the expected order. + for (const auto &Tuple : zip(Map, IterKeys)) + ASSERT_EQ(std::get<0>(Tuple).second, std::get<1>(Tuple)); + + // Check operator++ (post-increment). + int i = 0; + for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i) + ASSERT_EQ(iter->second, IterKeys[i]); +} + +TEST(ReverseIterationTest, SmallPtrSetTest) { + struct PtrLike { int a, b, c, d; } P = { 4, 8, 12, 16 }; + int *Keys[] = { &P.a, &P.b, &P.c, &P.d }; + + // Insert keys into the SmallPtrSet. + SmallPtrSet Set; + for (auto *Key: Keys) + Set.insert(Key); + + // Note: This is the observed order of keys in the SmallPtrSet. + // If there is any change in the behavior of the SmallPtrSet, this + // order would need to be adjusted accordingly. + int IterKeys[] = { P.a, P.b, P.c, P.d }; + if (shouldReverseIterate()) + std::reverse(&IterKeys[0], &IterKeys[4]); + + // Check that the SmallPtrSet is iterated in the expected order. + for (const auto &Tuple : zip(Set, IterKeys)) + ASSERT_EQ(*std::get<0>(Tuple), std::get<1>(Tuple)); + + // Check operator++ (post-increment). + int i = 0; + for (auto iter = Set.begin(), end = Set.end(); iter != end; iter++, ++i) + ASSERT_EQ(**iter, IterKeys[i]); +}