llvm.org GIT mirror llvm / 22e4b97
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@312574 91177308-0d34-0410-b5e6-96231b3b80d8 Mandeep Singh Grang 2 years ago
1 changed file(s) with 50 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
5252 for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i)
5353 ASSERT_EQ(iter->first, IterKeys[i]);
5454 }
55
56 // Define a pointer-like int.
57 struct PtrLikeInt { int value; };
58
59 template<> struct DenseMapInfo {
60 static PtrLikeInt *getEmptyKey() {
61 static PtrLikeInt EmptyKey;
62 return &EmptyKey;
63 }
64
65 static PtrLikeInt *getTombstoneKey() {
66 static PtrLikeInt TombstoneKey;
67 return &TombstoneKey;
68 }
69
70 static int getHashValue(const PtrLikeInt *P) {
71 return P->value;
72 }
73
74 static bool isEqual(const PtrLikeInt *LHS, const PtrLikeInt *RHS) {
75 return LHS == RHS;
76 }
77 };
78
79 TEST(ReverseIterationTest, DenseMapTest2) {
80 static_assert(detail::IsPointerLike::value,
81 "PtrLikeInt * is pointer-like");
82
83 PtrLikeInt a = {4}, b = {8}, c = {12}, d = {16};
84 PtrLikeInt *Keys[] = { &a, &b, &c, &d };
85
86 // Insert keys into the DenseMap.
87 DenseMap Map;
88 for (auto *Key : Keys)
89 Map[Key] = Key->value;
90
91 // Note: If there is any change in the behavior of the DenseMap,
92 // the observed order of keys would need to be adjusted accordingly.
93 if (shouldReverseIterate())
94 std::reverse(&Keys[0], &Keys[4]);
95
96 // Check that the DenseMap is iterated in the expected order.
97 for (const auto &Tuple : zip(Map, Keys))
98 ASSERT_EQ(std::get<0>(Tuple).second, std::get<1>(Tuple)->value);
99
100 // Check operator++ (post-increment).
101 int i = 0;
102 for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i)
103 ASSERT_EQ(iter->second, Keys[i]->value);
104 }