ReverseIterationTest.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //===- llvm/unittest/Support/ReverseIterationTest.cpp ---------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===---------------------------------------------------------------------===//
  8. //
  9. // Reverse Iteration unit tests.
  10. //
  11. //===---------------------------------------------------------------------===//
  12. #include "llvm/Support/ReverseIteration.h"
  13. #include "llvm/ADT/DenseMap.h"
  14. #include "llvm/ADT/DenseMapInfo.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. TEST(ReverseIterationTest, DenseMapTest1) {
  18. static_assert(detail::IsPointerLike<int *>::value,
  19. "int * is pointer-like");
  20. static_assert(detail::IsPointerLike<uintptr_t>::value,
  21. "uintptr_t is pointer-like");
  22. static_assert(!detail::IsPointerLike<int>::value,
  23. "int is not pointer-like");
  24. static_assert(detail::IsPointerLike<void *>::value,
  25. "void * is pointer-like");
  26. struct IncompleteType;
  27. static_assert(detail::IsPointerLike<IncompleteType *>::value,
  28. "incomplete * is pointer-like");
  29. // For a DenseMap with non-pointer-like keys, forward iteration equals
  30. // reverse iteration.
  31. DenseMap<int, int> Map;
  32. int Keys[] = { 1, 2, 3, 4 };
  33. // Insert keys into the DenseMap.
  34. for (auto Key: Keys)
  35. Map[Key] = 0;
  36. // Note: This is the observed order of keys in the DenseMap.
  37. // If there is any change in the behavior of the DenseMap, this order
  38. // would need to be adjusted accordingly.
  39. int IterKeys[] = { 2, 4, 1, 3 };
  40. // Check that the DenseMap is iterated in the expected order.
  41. for (const auto &Tuple : zip(Map, IterKeys))
  42. ASSERT_EQ(std::get<0>(Tuple).first, std::get<1>(Tuple));
  43. // Check operator++ (post-increment).
  44. int i = 0;
  45. for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i)
  46. ASSERT_EQ(iter->first, IterKeys[i]);
  47. }
  48. // Define a pointer-like int.
  49. struct PtrLikeInt { int value; };
  50. namespace llvm {
  51. template<> struct DenseMapInfo<PtrLikeInt *> {
  52. static PtrLikeInt *getEmptyKey() {
  53. static PtrLikeInt EmptyKey;
  54. return &EmptyKey;
  55. }
  56. static PtrLikeInt *getTombstoneKey() {
  57. static PtrLikeInt TombstoneKey;
  58. return &TombstoneKey;
  59. }
  60. static int getHashValue(const PtrLikeInt *P) {
  61. return P->value;
  62. }
  63. static bool isEqual(const PtrLikeInt *LHS, const PtrLikeInt *RHS) {
  64. return LHS == RHS;
  65. }
  66. };
  67. } // end namespace llvm
  68. TEST(ReverseIterationTest, DenseMapTest2) {
  69. static_assert(detail::IsPointerLike<PtrLikeInt *>::value,
  70. "PtrLikeInt * is pointer-like");
  71. PtrLikeInt a = {4}, b = {8}, c = {12}, d = {16};
  72. PtrLikeInt *Keys[] = { &a, &b, &c, &d };
  73. // Insert keys into the DenseMap.
  74. DenseMap<PtrLikeInt *, int> Map;
  75. for (auto *Key : Keys)
  76. Map[Key] = Key->value;
  77. // Note: If there is any change in the behavior of the DenseMap,
  78. // the observed order of keys would need to be adjusted accordingly.
  79. if (shouldReverseIterate<PtrLikeInt *>())
  80. std::reverse(&Keys[0], &Keys[4]);
  81. // Check that the DenseMap is iterated in the expected order.
  82. for (const auto &Tuple : zip(Map, Keys))
  83. ASSERT_EQ(std::get<0>(Tuple).second, std::get<1>(Tuple)->value);
  84. // Check operator++ (post-increment).
  85. int i = 0;
  86. for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i)
  87. ASSERT_EQ(iter->second, Keys[i]->value);
  88. }