Mandeep Singh Grang | 872f689 | 2017-08-24 23:02:48 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/ReverseIterationTest.cpp ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Reverse Iteration unit tests. |
| 11 | // |
| 12 | //===---------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ADT/DenseMap.h" |
Mandeep Singh Grang | 872f689 | 2017-08-24 23:02:48 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ReverseIteration.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | TEST(ReverseIterationTest, DenseMapTest1) { |
| 21 | static_assert(detail::IsPointerLike<int *>::value, |
| 22 | "int * is pointer-like"); |
| 23 | static_assert(detail::IsPointerLike<uintptr_t>::value, |
| 24 | "uintptr_t is pointer-like"); |
Mandeep Singh Grang | 12bd329 | 2017-08-25 01:11:28 +0000 | [diff] [blame] | 25 | static_assert(!detail::IsPointerLike<int>::value, |
| 26 | "int is not pointer-like"); |
| 27 | static_assert(detail::IsPointerLike<void *>::value, |
| 28 | "void * is pointer-like"); |
Mandeep Singh Grang | 872f689 | 2017-08-24 23:02:48 +0000 | [diff] [blame] | 29 | struct IncompleteType; |
| 30 | static_assert(detail::IsPointerLike<IncompleteType *>::value, |
| 31 | "incomplete * is pointer-like"); |
| 32 | |
Mandeep Singh Grang | 872f689 | 2017-08-24 23:02:48 +0000 | [diff] [blame] | 33 | // For a DenseMap with non-pointer-like keys, forward iteration equals |
| 34 | // reverse iteration. |
| 35 | DenseMap<int, int> Map; |
| 36 | int Keys[] = { 1, 2, 3, 4 }; |
| 37 | |
| 38 | // Insert keys into the DenseMap. |
| 39 | for (auto Key: Keys) |
| 40 | Map[Key] = 0; |
| 41 | |
| 42 | // Note: This is the observed order of keys in the DenseMap. |
| 43 | // If there is any change in the behavior of the DenseMap, this order |
| 44 | // would need to be adjusted accordingly. |
| 45 | int IterKeys[] = { 2, 4, 1, 3 }; |
| 46 | |
| 47 | // Check that the DenseMap is iterated in the expected order. |
| 48 | for (const auto &Tuple : zip(Map, IterKeys)) |
| 49 | ASSERT_EQ(std::get<0>(Tuple).first, std::get<1>(Tuple)); |
| 50 | |
| 51 | // Check operator++ (post-increment). |
| 52 | int i = 0; |
| 53 | for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i) |
| 54 | ASSERT_EQ(iter->first, IterKeys[i]); |
| 55 | } |
Mandeep Singh Grang | 9837e99 | 2017-09-05 20:39:01 +0000 | [diff] [blame^] | 56 | |
| 57 | // Define a pointer-like int. |
| 58 | struct PtrLikeInt { int value; }; |
| 59 | |
| 60 | template<> struct DenseMapInfo<PtrLikeInt *> { |
| 61 | static PtrLikeInt *getEmptyKey() { |
| 62 | static PtrLikeInt EmptyKey; |
| 63 | return &EmptyKey; |
| 64 | } |
| 65 | |
| 66 | static PtrLikeInt *getTombstoneKey() { |
| 67 | static PtrLikeInt TombstoneKey; |
| 68 | return &TombstoneKey; |
| 69 | } |
| 70 | |
| 71 | static int getHashValue(const PtrLikeInt *P) { |
| 72 | return P->value; |
| 73 | } |
| 74 | |
| 75 | static bool isEqual(const PtrLikeInt *LHS, const PtrLikeInt *RHS) { |
| 76 | return LHS == RHS; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | TEST(ReverseIterationTest, DenseMapTest2) { |
| 81 | static_assert(detail::IsPointerLike<PtrLikeInt *>::value, |
| 82 | "PtrLikeInt * is pointer-like"); |
| 83 | |
| 84 | PtrLikeInt a = {4}, b = {8}, c = {12}, d = {16}; |
| 85 | PtrLikeInt *Keys[] = { &a, &b, &c, &d }; |
| 86 | |
| 87 | // Insert keys into the DenseMap. |
| 88 | DenseMap<PtrLikeInt *, int> Map; |
| 89 | for (auto *Key : Keys) |
| 90 | Map[Key] = Key->value; |
| 91 | |
| 92 | // Note: If there is any change in the behavior of the DenseMap, |
| 93 | // the observed order of keys would need to be adjusted accordingly. |
| 94 | if (shouldReverseIterate<PtrLikeInt *>()) |
| 95 | std::reverse(&Keys[0], &Keys[4]); |
| 96 | |
| 97 | // Check that the DenseMap is iterated in the expected order. |
| 98 | for (const auto &Tuple : zip(Map, Keys)) |
| 99 | ASSERT_EQ(std::get<0>(Tuple).second, std::get<1>(Tuple)->value); |
| 100 | |
| 101 | // Check operator++ (post-increment). |
| 102 | int i = 0; |
| 103 | for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i) |
| 104 | ASSERT_EQ(iter->second, Keys[i]->value); |
| 105 | } |