1 //===- llvm/unittest/ADT/FoldingSetTest.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 // FoldingSet unit tests.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ADT/FoldingSet.h"
15 #include "gtest/gtest.h"
16 #include <string>
17
18 using namespace llvm;
19
20 namespace {
21
22 // Unaligned string test.
TEST(FoldingSetTest,UnalignedStringTest)23 TEST(FoldingSetTest, UnalignedStringTest) {
24 SCOPED_TRACE("UnalignedStringTest");
25
26 FoldingSetNodeID a, b;
27 // An aligned string.
28 std::string str1= "a test string";
29 a.AddString(str1);
30
31 // An unaligned string.
32 std::string str2 = ">" + str1;
33 b.AddString(str2.c_str() + 1);
34
35 EXPECT_EQ(a.ComputeHash(), b.ComputeHash());
36 }
37
TEST(FoldingSetTest,LongLongComparison)38 TEST(FoldingSetTest, LongLongComparison) {
39 struct LongLongContainer : FoldingSetNode {
40 unsigned long long A, B;
41 LongLongContainer(unsigned long long A, unsigned long long B)
42 : A(A), B(B) {}
43 void Profile(FoldingSetNodeID &ID) const {
44 ID.AddInteger(A);
45 ID.AddInteger(B);
46 }
47 };
48
49 LongLongContainer C1((1ULL << 32) + 1, 1ULL);
50 LongLongContainer C2(1ULL, (1ULL << 32) + 1);
51
52 FoldingSet<LongLongContainer> Set;
53
54 EXPECT_EQ(&C1, Set.GetOrInsertNode(&C1));
55 EXPECT_EQ(&C2, Set.GetOrInsertNode(&C2));
56 EXPECT_EQ(2U, Set.size());
57 }
58
59 struct TrivialPair : public FoldingSetNode {
60 unsigned Key = 0;
61 unsigned Value = 0;
TrivialPair__anona5c009a80111::TrivialPair62 TrivialPair(unsigned K, unsigned V) : FoldingSetNode(), Key(K), Value(V) {}
63
Profile__anona5c009a80111::TrivialPair64 void Profile(FoldingSetNodeID &ID) const {
65 ID.AddInteger(Key);
66 ID.AddInteger(Value);
67 }
68 };
69
TEST(FoldingSetTest,IDComparison)70 TEST(FoldingSetTest, IDComparison) {
71 FoldingSet<TrivialPair> Trivial;
72
73 TrivialPair T(99, 42);
74 Trivial.InsertNode(&T);
75
76 void *InsertPos = nullptr;
77 FoldingSetNodeID ID;
78 T.Profile(ID);
79 TrivialPair *N = Trivial.FindNodeOrInsertPos(ID, InsertPos);
80 EXPECT_EQ(&T, N);
81 EXPECT_EQ(nullptr, InsertPos);
82 }
83
TEST(FoldingSetTest,MissedIDComparison)84 TEST(FoldingSetTest, MissedIDComparison) {
85 FoldingSet<TrivialPair> Trivial;
86
87 TrivialPair S(100, 42);
88 TrivialPair T(99, 42);
89 Trivial.InsertNode(&T);
90
91 void *InsertPos = nullptr;
92 FoldingSetNodeID ID;
93 S.Profile(ID);
94 TrivialPair *N = Trivial.FindNodeOrInsertPos(ID, InsertPos);
95 EXPECT_EQ(nullptr, N);
96 EXPECT_NE(nullptr, InsertPos);
97 }
98
TEST(FoldingSetTest,RemoveNodeThatIsPresent)99 TEST(FoldingSetTest, RemoveNodeThatIsPresent) {
100 FoldingSet<TrivialPair> Trivial;
101
102 TrivialPair T(99, 42);
103 Trivial.InsertNode(&T);
104 EXPECT_EQ(Trivial.size(), 1U);
105
106 bool WasThere = Trivial.RemoveNode(&T);
107 EXPECT_TRUE(WasThere);
108 EXPECT_EQ(0U, Trivial.size());
109 }
110
TEST(FoldingSetTest,RemoveNodeThatIsAbsent)111 TEST(FoldingSetTest, RemoveNodeThatIsAbsent) {
112 FoldingSet<TrivialPair> Trivial;
113
114 TrivialPair T(99, 42);
115 bool WasThere = Trivial.RemoveNode(&T);
116 EXPECT_FALSE(WasThere);
117 EXPECT_EQ(0U, Trivial.size());
118 }
119
TEST(FoldingSetTest,GetOrInsertInserting)120 TEST(FoldingSetTest, GetOrInsertInserting) {
121 FoldingSet<TrivialPair> Trivial;
122
123 TrivialPair T(99, 42);
124 TrivialPair *N = Trivial.GetOrInsertNode(&T);
125 EXPECT_EQ(&T, N);
126 }
127
TEST(FoldingSetTest,GetOrInsertGetting)128 TEST(FoldingSetTest, GetOrInsertGetting) {
129 FoldingSet<TrivialPair> Trivial;
130
131 TrivialPair T(99, 42);
132 TrivialPair T2(99, 42);
133 Trivial.InsertNode(&T);
134 TrivialPair *N = Trivial.GetOrInsertNode(&T2);
135 EXPECT_EQ(&T, N);
136 }
137
TEST(FoldingSetTest,InsertAtPos)138 TEST(FoldingSetTest, InsertAtPos) {
139 FoldingSet<TrivialPair> Trivial;
140
141 void *InsertPos = nullptr;
142 TrivialPair Finder(99, 42);
143 FoldingSetNodeID ID;
144 Finder.Profile(ID);
145 Trivial.FindNodeOrInsertPos(ID, InsertPos);
146
147 TrivialPair T(99, 42);
148 Trivial.InsertNode(&T, InsertPos);
149 EXPECT_EQ(1U, Trivial.size());
150 }
151
TEST(FoldingSetTest,EmptyIsTrue)152 TEST(FoldingSetTest, EmptyIsTrue) {
153 FoldingSet<TrivialPair> Trivial;
154 EXPECT_TRUE(Trivial.empty());
155 }
156
TEST(FoldingSetTest,EmptyIsFalse)157 TEST(FoldingSetTest, EmptyIsFalse) {
158 FoldingSet<TrivialPair> Trivial;
159 TrivialPair T(99, 42);
160 Trivial.InsertNode(&T);
161 EXPECT_FALSE(Trivial.empty());
162 }
163
TEST(FoldingSetTest,ClearOnEmpty)164 TEST(FoldingSetTest, ClearOnEmpty) {
165 FoldingSet<TrivialPair> Trivial;
166 Trivial.clear();
167 EXPECT_TRUE(Trivial.empty());
168 }
169
TEST(FoldingSetTest,ClearOnNonEmpty)170 TEST(FoldingSetTest, ClearOnNonEmpty) {
171 FoldingSet<TrivialPair> Trivial;
172 TrivialPair T(99, 42);
173 Trivial.InsertNode(&T);
174 Trivial.clear();
175 EXPECT_TRUE(Trivial.empty());
176 }
177
TEST(FoldingSetTest,CapacityLargerThanReserve)178 TEST(FoldingSetTest, CapacityLargerThanReserve) {
179 FoldingSet<TrivialPair> Trivial;
180 auto OldCapacity = Trivial.capacity();
181 Trivial.reserve(OldCapacity + 1);
182 EXPECT_GE(Trivial.capacity(), OldCapacity + 1);
183 }
184
TEST(FoldingSetTest,SmallReserveChangesNothing)185 TEST(FoldingSetTest, SmallReserveChangesNothing) {
186 FoldingSet<TrivialPair> Trivial;
187 auto OldCapacity = Trivial.capacity();
188 Trivial.reserve(OldCapacity - 1);
189 EXPECT_EQ(Trivial.capacity(), OldCapacity);
190 }
191
192 }
193
194