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