diff options
author | Mandeep Singh Grang <mgrang@codeaurora.org> | 2017-09-05 20:39:01 +0000 |
---|---|---|
committer | Mandeep Singh Grang <mgrang@codeaurora.org> | 2017-09-05 20:39:01 +0000 |
commit | 9837e9945f83db7d1ffff75ef00d94a42f9fcf88 (patch) | |
tree | e396bfb34a39abc961e883616737d6c735355044 /llvm/unittests/Support/ReverseIterationTest.cpp | |
parent | d53c39ba46ef103b2c6aa8f2ec516491bdb6acfb (diff) | |
download | bcm5719-llvm-9837e9945f83db7d1ffff75ef00d94a42f9fcf88.tar.gz bcm5719-llvm-9837e9945f83db7d1ffff75ef00d94a42f9fcf88.zip |
[unittests] Add reverse iteration unit test for pointer-like keys
Reviewers: dblaikie, efriedma, mehdi_amini
Reviewed By: dblaikie
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D37241
llvm-svn: 312574
Diffstat (limited to 'llvm/unittests/Support/ReverseIterationTest.cpp')
-rw-r--r-- | llvm/unittests/Support/ReverseIterationTest.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/llvm/unittests/Support/ReverseIterationTest.cpp b/llvm/unittests/Support/ReverseIterationTest.cpp index 41205958f41..5a72bdd0bf4 100644 --- a/llvm/unittests/Support/ReverseIterationTest.cpp +++ b/llvm/unittests/Support/ReverseIterationTest.cpp @@ -53,3 +53,53 @@ TEST(ReverseIterationTest, DenseMapTest1) { for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i) ASSERT_EQ(iter->first, IterKeys[i]); } + +// Define a pointer-like int. +struct PtrLikeInt { int value; }; + +template<> struct DenseMapInfo<PtrLikeInt *> { + static PtrLikeInt *getEmptyKey() { + static PtrLikeInt EmptyKey; + return &EmptyKey; + } + + static PtrLikeInt *getTombstoneKey() { + static PtrLikeInt TombstoneKey; + return &TombstoneKey; + } + + static int getHashValue(const PtrLikeInt *P) { + return P->value; + } + + static bool isEqual(const PtrLikeInt *LHS, const PtrLikeInt *RHS) { + return LHS == RHS; + } +}; + +TEST(ReverseIterationTest, DenseMapTest2) { + static_assert(detail::IsPointerLike<PtrLikeInt *>::value, + "PtrLikeInt * is pointer-like"); + + PtrLikeInt a = {4}, b = {8}, c = {12}, d = {16}; + PtrLikeInt *Keys[] = { &a, &b, &c, &d }; + + // Insert keys into the DenseMap. + DenseMap<PtrLikeInt *, int> Map; + for (auto *Key : Keys) + Map[Key] = Key->value; + + // Note: If there is any change in the behavior of the DenseMap, + // the observed order of keys would need to be adjusted accordingly. + if (shouldReverseIterate<PtrLikeInt *>()) + std::reverse(&Keys[0], &Keys[4]); + + // Check that the DenseMap is iterated in the expected order. + for (const auto &Tuple : zip(Map, Keys)) + ASSERT_EQ(std::get<0>(Tuple).second, std::get<1>(Tuple)->value); + + // Check operator++ (post-increment). + int i = 0; + for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i) + ASSERT_EQ(iter->second, Keys[i]->value); +} |