1 //===- llvm/unittest/ADT/FoldingSetTest.cpp -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // FoldingSet unit tests.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/FoldingSet.h"
15 #include "gtest/gtest.h"
22 // Unaligned string test.
23 TEST(FoldingSetTest, UnalignedStringTest) {
24 SCOPED_TRACE("UnalignedStringTest");
26 FoldingSetNodeID a, b;
28 std::string str1= "a test string";
31 // An unaligned string.
32 std::string str2 = ">" + str1;
33 b.AddString(str2.c_str() + 1);
35 EXPECT_EQ(a.ComputeHash(), b.ComputeHash());
38 TEST(FoldingSetTest, LongLongComparison) {
39 struct LongLongContainer : FoldingSetNode {
40 unsigned long long A, B;
41 LongLongContainer(unsigned long long A, unsigned long long B)
43 void Profile(FoldingSetNodeID &ID) const {
49 LongLongContainer C1((1ULL << 32) + 1, 1ULL);
50 LongLongContainer C2(1ULL, (1ULL << 32) + 1);
52 FoldingSet<LongLongContainer> Set;
54 EXPECT_EQ(&C1, Set.GetOrInsertNode(&C1));
55 EXPECT_EQ(&C2, Set.GetOrInsertNode(&C2));
56 EXPECT_EQ(2U, Set.size());
59 struct TrivialPair : public FoldingSetNode {
62 TrivialPair(unsigned K, unsigned V) : FoldingSetNode(), Key(K), Value(V) {}
64 void Profile(FoldingSetNodeID &ID) const {
70 TEST(FoldingSetTest, IDComparison) {
71 FoldingSet<TrivialPair> Trivial;
73 TrivialPair T(99, 42);
74 Trivial.InsertNode(&T);
76 void *InsertPos = nullptr;
79 TrivialPair *N = Trivial.FindNodeOrInsertPos(ID, InsertPos);
81 EXPECT_EQ(nullptr, InsertPos);
84 TEST(FoldingSetTest, MissedIDComparison) {
85 FoldingSet<TrivialPair> Trivial;
87 TrivialPair S(100, 42);
88 TrivialPair T(99, 42);
89 Trivial.InsertNode(&T);
91 void *InsertPos = nullptr;
94 TrivialPair *N = Trivial.FindNodeOrInsertPos(ID, InsertPos);
95 EXPECT_EQ(nullptr, N);
96 EXPECT_NE(nullptr, InsertPos);
99 TEST(FoldingSetTest, RemoveNodeThatIsPresent) {
100 FoldingSet<TrivialPair> Trivial;
102 TrivialPair T(99, 42);
103 Trivial.InsertNode(&T);
104 EXPECT_EQ(Trivial.size(), 1U);
106 bool WasThere = Trivial.RemoveNode(&T);
107 EXPECT_TRUE(WasThere);
108 EXPECT_EQ(0U, Trivial.size());
111 TEST(FoldingSetTest, RemoveNodeThatIsAbsent) {
112 FoldingSet<TrivialPair> Trivial;
114 TrivialPair T(99, 42);
115 bool WasThere = Trivial.RemoveNode(&T);
116 EXPECT_FALSE(WasThere);
117 EXPECT_EQ(0U, Trivial.size());
120 TEST(FoldingSetTest, GetOrInsertInserting) {
121 FoldingSet<TrivialPair> Trivial;
123 TrivialPair T(99, 42);
124 TrivialPair *N = Trivial.GetOrInsertNode(&T);
128 TEST(FoldingSetTest, GetOrInsertGetting) {
129 FoldingSet<TrivialPair> Trivial;
131 TrivialPair T(99, 42);
132 TrivialPair T2(99, 42);
133 Trivial.InsertNode(&T);
134 TrivialPair *N = Trivial.GetOrInsertNode(&T2);
138 TEST(FoldingSetTest, InsertAtPos) {
139 FoldingSet<TrivialPair> Trivial;
141 void *InsertPos = nullptr;
142 TrivialPair Finder(99, 42);
145 Trivial.FindNodeOrInsertPos(ID, InsertPos);
147 TrivialPair T(99, 42);
148 Trivial.InsertNode(&T, InsertPos);
149 EXPECT_EQ(1U, Trivial.size());
152 TEST(FoldingSetTest, EmptyIsTrue) {
153 FoldingSet<TrivialPair> Trivial;
154 EXPECT_TRUE(Trivial.empty());
157 TEST(FoldingSetTest, EmptyIsFalse) {
158 FoldingSet<TrivialPair> Trivial;
159 TrivialPair T(99, 42);
160 Trivial.InsertNode(&T);
161 EXPECT_FALSE(Trivial.empty());
164 TEST(FoldingSetTest, ClearOnEmpty) {
165 FoldingSet<TrivialPair> Trivial;
167 EXPECT_TRUE(Trivial.empty());
170 TEST(FoldingSetTest, ClearOnNonEmpty) {
171 FoldingSet<TrivialPair> Trivial;
172 TrivialPair T(99, 42);
173 Trivial.InsertNode(&T);
175 EXPECT_TRUE(Trivial.empty());
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);
185 TEST(FoldingSetTest, SmallReserveChangesNothing) {
186 FoldingSet<TrivialPair> Trivial;
187 auto OldCapacity = Trivial.capacity();
188 Trivial.reserve(OldCapacity - 1);
189 EXPECT_EQ(Trivial.capacity(), OldCapacity);