blob: 5a72bdd0bf4591b4ebae49b8f4216b8926a97425 [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
14#include "llvm/ADT/DenseMap.h"
Mandeep Singh Grang872f6892017-08-24 23:02:48 +000015#include "llvm/Support/ReverseIteration.h"
16#include "gtest/gtest.h"
17
18using namespace llvm;
19
20TEST(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 Grang12bd3292017-08-25 01:11:28 +000025 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 Grang872f6892017-08-24 23:02:48 +000029 struct IncompleteType;
30 static_assert(detail::IsPointerLike<IncompleteType *>::value,
31 "incomplete * is pointer-like");
32
Mandeep Singh Grang872f6892017-08-24 23:02:48 +000033 // 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 Grang9837e992017-09-05 20:39:01 +000056
57// Define a pointer-like int.
58struct PtrLikeInt { int value; };
59
60template<> 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
80TEST(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}