blob: 930bd43d11b27874e4efe885009cbaec6a54d4ce [file] [log] [blame]
Mandeep Singh Grang872f6892017-08-24 23:02:48 +00001//===- 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
Mandeep Singh Grang872f6892017-08-24 23:02:48 +000014#include "llvm/Support/ReverseIteration.h"
Davide Italianof8874062017-09-05 21:27:23 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/DenseMapInfo.h"
Mandeep Singh Grang872f6892017-08-24 23:02:48 +000017#include "gtest/gtest.h"
18
19using namespace llvm;
20
21TEST(ReverseIterationTest, DenseMapTest1) {
22 static_assert(detail::IsPointerLike<int *>::value,
23 "int * is pointer-like");
24 static_assert(detail::IsPointerLike<uintptr_t>::value,
25 "uintptr_t is pointer-like");
Mandeep Singh Grang12bd3292017-08-25 01:11:28 +000026 static_assert(!detail::IsPointerLike<int>::value,
27 "int is not pointer-like");
28 static_assert(detail::IsPointerLike<void *>::value,
29 "void * is pointer-like");
Mandeep Singh Grang872f6892017-08-24 23:02:48 +000030 struct IncompleteType;
31 static_assert(detail::IsPointerLike<IncompleteType *>::value,
32 "incomplete * is pointer-like");
33
Mandeep Singh Grang872f6892017-08-24 23:02:48 +000034 // For a DenseMap with non-pointer-like keys, forward iteration equals
35 // reverse iteration.
36 DenseMap<int, int> Map;
37 int Keys[] = { 1, 2, 3, 4 };
38
39 // Insert keys into the DenseMap.
40 for (auto Key: Keys)
41 Map[Key] = 0;
42
43 // Note: This is the observed order of keys in the DenseMap.
44 // If there is any change in the behavior of the DenseMap, this order
45 // would need to be adjusted accordingly.
46 int IterKeys[] = { 2, 4, 1, 3 };
47
48 // Check that the DenseMap is iterated in the expected order.
49 for (const auto &Tuple : zip(Map, IterKeys))
50 ASSERT_EQ(std::get<0>(Tuple).first, std::get<1>(Tuple));
51
52 // Check operator++ (post-increment).
53 int i = 0;
54 for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i)
55 ASSERT_EQ(iter->first, IterKeys[i]);
56}
Mandeep Singh Grang9837e992017-09-05 20:39:01 +000057
58// Define a pointer-like int.
59struct PtrLikeInt { int value; };
60
Davide Italianof8874062017-09-05 21:27:23 +000061namespace llvm {
62
Mandeep Singh Grang9837e992017-09-05 20:39:01 +000063template<> struct DenseMapInfo<PtrLikeInt *> {
64 static PtrLikeInt *getEmptyKey() {
65 static PtrLikeInt EmptyKey;
66 return &EmptyKey;
67 }
68
69 static PtrLikeInt *getTombstoneKey() {
70 static PtrLikeInt TombstoneKey;
71 return &TombstoneKey;
72 }
73
74 static int getHashValue(const PtrLikeInt *P) {
75 return P->value;
76 }
77
78 static bool isEqual(const PtrLikeInt *LHS, const PtrLikeInt *RHS) {
79 return LHS == RHS;
80 }
81};
82
Davide Italianof8874062017-09-05 21:27:23 +000083} // end namespace llvm
84
Mandeep Singh Grang9837e992017-09-05 20:39:01 +000085TEST(ReverseIterationTest, DenseMapTest2) {
86 static_assert(detail::IsPointerLike<PtrLikeInt *>::value,
87 "PtrLikeInt * is pointer-like");
88
89 PtrLikeInt a = {4}, b = {8}, c = {12}, d = {16};
90 PtrLikeInt *Keys[] = { &a, &b, &c, &d };
91
92 // Insert keys into the DenseMap.
93 DenseMap<PtrLikeInt *, int> Map;
94 for (auto *Key : Keys)
95 Map[Key] = Key->value;
96
97 // Note: If there is any change in the behavior of the DenseMap,
98 // the observed order of keys would need to be adjusted accordingly.
99 if (shouldReverseIterate<PtrLikeInt *>())
100 std::reverse(&Keys[0], &Keys[4]);
101
102 // Check that the DenseMap is iterated in the expected order.
103 for (const auto &Tuple : zip(Map, Keys))
104 ASSERT_EQ(std::get<0>(Tuple).second, std::get<1>(Tuple)->value);
105
106 // Check operator++ (post-increment).
107 int i = 0;
108 for (auto iter = Map.begin(), end = Map.end(); iter != end; iter++, ++i)
109 ASSERT_EQ(iter->second, Keys[i]->value);
110}