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 | } |