[PATCH] D37241: [unittests] Add reverse iteration unit tests for pointer-like keys

Mandeep Singh Grang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 3 21:19:15 PDT 2017


mgrang updated this revision to Diff 113719.
mgrang added a comment.

NFC. Made the static_assert the first statement in the test.


Repository:
  rL LLVM

https://reviews.llvm.org/D37241

Files:
  unittests/Support/ReverseIterationTest.cpp


Index: unittests/Support/ReverseIterationTest.cpp
===================================================================
--- unittests/Support/ReverseIterationTest.cpp
+++ unittests/Support/ReverseIterationTest.cpp
@@ -53,3 +53,53 @@
   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 inline PtrLikeInt *getEmptyKey() {
+    return reinterpret_cast<PtrLikeInt *>(0x7fffffff);
+  }
+
+  static inline PtrLikeInt *getTombstoneKey() {
+    return reinterpret_cast<PtrLikeInt *>(-0x7fffffff - 1);
+  }
+
+  static unsigned getHashValue(const PtrLikeInt *PtrVal) {
+    return (unsigned)(PtrVal->value * 37U);
+  }
+
+  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: 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.
+  PtrLikeInt *IterKeys[] = { &d, &a, &b, &c };
+  if (shouldReverseIterate<PtrLikeInt *>())
+    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)->value);
+
+  // 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]->value);
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37241.113719.patch
Type: text/x-patch
Size: 1991 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170904/e5345acc/attachment.bin>


More information about the llvm-commits mailing list