blob: 76f9cf7fb9afd3ba99923f09b8ec2a55a2f06c02 [file] [log] [blame]
Benjamin Kramer2945a322012-03-06 20:40:02 +00001//===- llvm/unittest/ADT/SmallPtrSetTest.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// SmallPtrSet unit tests.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2945a322012-03-06 20:40:02 +000014#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth3c0d6072017-06-06 11:06:56 +000015#include "llvm/ADT/PointerIntPair.h"
Daniel Berlin05382af2017-03-07 18:47:52 +000016#include "llvm/Support/PointerLikeTypeTraits.h"
Chandler Carruth3c0d6072017-06-06 11:06:56 +000017#include "gtest/gtest.h"
Benjamin Kramer2945a322012-03-06 20:40:02 +000018
19using namespace llvm;
20
Chandler Carruthaa8e5352013-11-20 18:21:25 +000021TEST(SmallPtrSetTest, Assignment) {
22 int buf[8];
23 for (int i = 0; i < 8; ++i)
24 buf[i] = 0;
25
Chandler Carruth76884e52016-11-22 03:27:43 +000026 SmallPtrSet<int *, 4> s1 = {&buf[0], &buf[1]};
Chandler Carruthaa8e5352013-11-20 18:21:25 +000027 SmallPtrSet<int *, 4> s2;
28 (s2 = s1).insert(&buf[2]);
29
30 // Self assign as well.
Roman Lebedev6bc5d612018-04-07 10:37:18 +000031 (s2 = static_cast<SmallPtrSet<int *, 4> &>(s2)).insert(&buf[3]);
Chandler Carruthaa8e5352013-11-20 18:21:25 +000032
33 s1 = s2;
34 EXPECT_EQ(4U, s1.size());
35 for (int i = 0; i < 8; ++i)
36 if (i < 4)
37 EXPECT_TRUE(s1.count(&buf[i]));
38 else
39 EXPECT_FALSE(s1.count(&buf[i]));
Chandler Carruth76884e52016-11-22 03:27:43 +000040
41 // Assign and insert with initializer lists, and ones that contain both
42 // duplicates and out-of-order elements.
43 (s2 = {&buf[6], &buf[7], &buf[6]}).insert({&buf[5], &buf[4]});
44 for (int i = 0; i < 8; ++i)
45 if (i < 4)
46 EXPECT_FALSE(s2.count(&buf[i]));
47 else
48 EXPECT_TRUE(s2.count(&buf[i]));
Chandler Carruthaa8e5352013-11-20 18:21:25 +000049}
50
Jean-Luc Duprate1e93662013-03-29 22:07:12 +000051TEST(SmallPtrSetTest, GrowthTest) {
52 int i;
53 int buf[8];
54 for(i=0; i<8; ++i) buf[i]=0;
55
56
57 SmallPtrSet<int *, 4> s;
58 typedef SmallPtrSet<int *, 4>::iterator iter;
Roman Lebedev6bc5d612018-04-07 10:37:18 +000059
Jean-Luc Duprate1e93662013-03-29 22:07:12 +000060 s.insert(&buf[0]);
61 s.insert(&buf[1]);
62 s.insert(&buf[2]);
63 s.insert(&buf[3]);
64 EXPECT_EQ(4U, s.size());
65
66 i = 0;
67 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
68 (**I)++;
69 EXPECT_EQ(4, i);
70 for(i=0; i<8; ++i)
71 EXPECT_EQ(i<4?1:0,buf[i]);
72
73 s.insert(&buf[4]);
74 s.insert(&buf[5]);
75 s.insert(&buf[6]);
76 s.insert(&buf[7]);
77
78 i = 0;
79 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
80 (**I)++;
81 EXPECT_EQ(8, i);
82 s.erase(&buf[4]);
83 s.erase(&buf[5]);
84 s.erase(&buf[6]);
85 s.erase(&buf[7]);
86 EXPECT_EQ(4U, s.size());
87
88 i = 0;
89 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
90 (**I)++;
91 EXPECT_EQ(4, i);
92 for(i=0; i<8; ++i)
93 EXPECT_EQ(i<4?3:1,buf[i]);
94
95 s.clear();
96 for(i=0; i<8; ++i) buf[i]=0;
97 for(i=0; i<128; ++i) s.insert(&buf[i%8]); // test repeated entires
98 EXPECT_EQ(8U, s.size());
99 for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
100 (**I)++;
101 for(i=0; i<8; ++i)
102 EXPECT_EQ(1,buf[i]);
103}
104
Chandler Carruth46198162013-11-20 11:14:33 +0000105TEST(SmallPtrSetTest, CopyAndMoveTest) {
106 int buf[8];
107 for (int i = 0; i < 8; ++i)
108 buf[i] = 0;
109
110 SmallPtrSet<int *, 4> s1;
111 s1.insert(&buf[0]);
112 s1.insert(&buf[1]);
113 s1.insert(&buf[2]);
114 s1.insert(&buf[3]);
115 EXPECT_EQ(4U, s1.size());
116 for (int i = 0; i < 8; ++i)
117 if (i < 4)
118 EXPECT_TRUE(s1.count(&buf[i]));
119 else
120 EXPECT_FALSE(s1.count(&buf[i]));
121
122 SmallPtrSet<int *, 4> s2(s1);
123 EXPECT_EQ(4U, s2.size());
124 for (int i = 0; i < 8; ++i)
125 if (i < 4)
126 EXPECT_TRUE(s2.count(&buf[i]));
127 else
128 EXPECT_FALSE(s2.count(&buf[i]));
129
130 s1 = s2;
131 EXPECT_EQ(4U, s1.size());
Chandler Carruthfd033572013-11-20 18:29:56 +0000132 EXPECT_EQ(4U, s2.size());
Chandler Carruth46198162013-11-20 11:14:33 +0000133 for (int i = 0; i < 8; ++i)
134 if (i < 4)
135 EXPECT_TRUE(s1.count(&buf[i]));
136 else
137 EXPECT_FALSE(s1.count(&buf[i]));
138
Chandler Carruthfd033572013-11-20 18:29:56 +0000139 SmallPtrSet<int *, 4> s3(std::move(s1));
Chandler Carruth46198162013-11-20 11:14:33 +0000140 EXPECT_EQ(4U, s3.size());
Chandler Carruthfd033572013-11-20 18:29:56 +0000141 EXPECT_TRUE(s1.empty());
Chandler Carruth46198162013-11-20 11:14:33 +0000142 for (int i = 0; i < 8; ++i)
143 if (i < 4)
144 EXPECT_TRUE(s3.count(&buf[i]));
145 else
146 EXPECT_FALSE(s3.count(&buf[i]));
147
Chandler Carruthfd033572013-11-20 18:29:56 +0000148 // Move assign into the moved-from object. Also test move of a non-small
149 // container.
150 s3.insert(&buf[4]);
151 s3.insert(&buf[5]);
152 s3.insert(&buf[6]);
153 s3.insert(&buf[7]);
Chandler Carruth0a3eef52014-03-02 04:08:41 +0000154 s1 = std::move(s3);
Chandler Carruthfd033572013-11-20 18:29:56 +0000155 EXPECT_EQ(8U, s1.size());
156 EXPECT_TRUE(s3.empty());
Chandler Carruth46198162013-11-20 11:14:33 +0000157 for (int i = 0; i < 8; ++i)
Chandler Carruthfd033572013-11-20 18:29:56 +0000158 EXPECT_TRUE(s1.count(&buf[i]));
159
160 // Copy assign into a moved-from object.
161 s3 = s1;
162 EXPECT_EQ(8U, s3.size());
163 EXPECT_EQ(8U, s1.size());
164 for (int i = 0; i < 8; ++i)
165 EXPECT_TRUE(s3.count(&buf[i]));
Chandler Carruth46198162013-11-20 11:14:33 +0000166}
Jean-Luc Duprate1e93662013-03-29 22:07:12 +0000167
Benjamin Kramer2945a322012-03-06 20:40:02 +0000168TEST(SmallPtrSetTest, SwapTest) {
169 int buf[10];
170
171 SmallPtrSet<int *, 2> a;
172 SmallPtrSet<int *, 2> b;
173
174 a.insert(&buf[0]);
175 a.insert(&buf[1]);
176 b.insert(&buf[2]);
177
Matthias Braun13a05162016-01-29 03:34:36 +0000178 EXPECT_EQ(2U, a.size());
179 EXPECT_EQ(1U, b.size());
180 EXPECT_TRUE(a.count(&buf[0]));
181 EXPECT_TRUE(a.count(&buf[1]));
182 EXPECT_FALSE(a.count(&buf[2]));
183 EXPECT_FALSE(a.count(&buf[3]));
184 EXPECT_FALSE(b.count(&buf[0]));
185 EXPECT_FALSE(b.count(&buf[1]));
186 EXPECT_TRUE(b.count(&buf[2]));
187 EXPECT_FALSE(b.count(&buf[3]));
188
Benjamin Kramer2945a322012-03-06 20:40:02 +0000189 std::swap(a, b);
190
191 EXPECT_EQ(1U, a.size());
192 EXPECT_EQ(2U, b.size());
Matthias Braun13a05162016-01-29 03:34:36 +0000193 EXPECT_FALSE(a.count(&buf[0]));
194 EXPECT_FALSE(a.count(&buf[1]));
Benjamin Kramer2945a322012-03-06 20:40:02 +0000195 EXPECT_TRUE(a.count(&buf[2]));
Matthias Braun13a05162016-01-29 03:34:36 +0000196 EXPECT_FALSE(a.count(&buf[3]));
Benjamin Kramer2945a322012-03-06 20:40:02 +0000197 EXPECT_TRUE(b.count(&buf[0]));
198 EXPECT_TRUE(b.count(&buf[1]));
Matthias Braun13a05162016-01-29 03:34:36 +0000199 EXPECT_FALSE(b.count(&buf[2]));
200 EXPECT_FALSE(b.count(&buf[3]));
Benjamin Kramer2945a322012-03-06 20:40:02 +0000201
202 b.insert(&buf[3]);
203 std::swap(a, b);
204
205 EXPECT_EQ(3U, a.size());
206 EXPECT_EQ(1U, b.size());
207 EXPECT_TRUE(a.count(&buf[0]));
208 EXPECT_TRUE(a.count(&buf[1]));
Matthias Braun13a05162016-01-29 03:34:36 +0000209 EXPECT_FALSE(a.count(&buf[2]));
Benjamin Kramer2945a322012-03-06 20:40:02 +0000210 EXPECT_TRUE(a.count(&buf[3]));
Matthias Braun13a05162016-01-29 03:34:36 +0000211 EXPECT_FALSE(b.count(&buf[0]));
212 EXPECT_FALSE(b.count(&buf[1]));
Benjamin Kramer2945a322012-03-06 20:40:02 +0000213 EXPECT_TRUE(b.count(&buf[2]));
Matthias Braun13a05162016-01-29 03:34:36 +0000214 EXPECT_FALSE(b.count(&buf[3]));
Benjamin Kramer2945a322012-03-06 20:40:02 +0000215
216 std::swap(a, b);
217
218 EXPECT_EQ(1U, a.size());
219 EXPECT_EQ(3U, b.size());
Matthias Braun13a05162016-01-29 03:34:36 +0000220 EXPECT_FALSE(a.count(&buf[0]));
221 EXPECT_FALSE(a.count(&buf[1]));
Benjamin Kramer2945a322012-03-06 20:40:02 +0000222 EXPECT_TRUE(a.count(&buf[2]));
Matthias Braun13a05162016-01-29 03:34:36 +0000223 EXPECT_FALSE(a.count(&buf[3]));
Benjamin Kramer2945a322012-03-06 20:40:02 +0000224 EXPECT_TRUE(b.count(&buf[0]));
225 EXPECT_TRUE(b.count(&buf[1]));
Matthias Braun13a05162016-01-29 03:34:36 +0000226 EXPECT_FALSE(b.count(&buf[2]));
Benjamin Kramer2945a322012-03-06 20:40:02 +0000227 EXPECT_TRUE(b.count(&buf[3]));
228
229 a.insert(&buf[4]);
230 a.insert(&buf[5]);
231 a.insert(&buf[6]);
232
233 std::swap(b, a);
234
235 EXPECT_EQ(3U, a.size());
236 EXPECT_EQ(4U, b.size());
237 EXPECT_TRUE(b.count(&buf[2]));
238 EXPECT_TRUE(b.count(&buf[4]));
239 EXPECT_TRUE(b.count(&buf[5]));
240 EXPECT_TRUE(b.count(&buf[6]));
241 EXPECT_TRUE(a.count(&buf[0]));
242 EXPECT_TRUE(a.count(&buf[1]));
243 EXPECT_TRUE(a.count(&buf[3]));
244}
Matthias Braun2f0c7872016-01-29 03:34:34 +0000245
246void checkEraseAndIterators(SmallPtrSetImpl<int*> &S) {
247 int buf[3];
248
249 S.insert(&buf[0]);
250 S.insert(&buf[1]);
251 S.insert(&buf[2]);
252
253 // Iterators must still be valid after erase() calls;
254 auto B = S.begin();
255 auto M = std::next(B);
256 auto E = S.end();
257 EXPECT_TRUE(*B == &buf[0] || *B == &buf[1] || *B == &buf[2]);
258 EXPECT_TRUE(*M == &buf[0] || *M == &buf[1] || *M == &buf[2]);
259 EXPECT_TRUE(*B != *M);
260 int *Removable = *std::next(M);
261 // No iterator points to Removable now.
262 EXPECT_TRUE(Removable == &buf[0] || Removable == &buf[1] ||
263 Removable == &buf[2]);
264 EXPECT_TRUE(Removable != *B && Removable != *M);
265
266 S.erase(Removable);
267
268 // B,M,E iterators should still be valid
269 EXPECT_EQ(B, S.begin());
270 EXPECT_EQ(M, std::next(B));
271 EXPECT_EQ(E, S.end());
272 EXPECT_EQ(std::next(M), E);
273}
274
275TEST(SmallPtrSetTest, EraseTest) {
276 // Test when set stays small.
277 SmallPtrSet<int *, 8> B;
278 checkEraseAndIterators(B);
279
280 // Test when set grows big.
281 SmallPtrSet<int *, 2> A;
282 checkEraseAndIterators(A);
283}
Daniel Berlin05382af2017-03-07 18:47:52 +0000284
Duncan P. N. Exon Smithcb8ffd22017-03-07 21:56:32 +0000285// Verify that dereferencing and iteration work.
286TEST(SmallPtrSetTest, dereferenceAndIterate) {
287 int Ints[] = {0, 1, 2, 3, 4, 5, 6, 7};
288 SmallPtrSet<const int *, 4> S;
289 for (int &I : Ints) {
290 EXPECT_EQ(&I, *S.insert(&I).first);
291 EXPECT_EQ(&I, *S.find(&I));
292 }
293
294 // Iterate from each and count how many times each element is found.
295 int Found[sizeof(Ints)/sizeof(int)] = {0};
296 for (int &I : Ints)
297 for (auto F = S.find(&I), E = S.end(); F != E; ++F)
298 ++Found[*F - Ints];
299
300 // Sort. We should hit the first element just once and the final element N
301 // times.
Mandeep Singh Grang77a1dcd2018-04-07 01:29:45 +0000302 llvm::sort(std::begin(Found), std::end(Found));
Duncan P. N. Exon Smithcb8ffd22017-03-07 21:56:32 +0000303 for (auto F = std::begin(Found), E = std::end(Found); F != E; ++F)
304 EXPECT_EQ(F - Found + 1, *F);
305}
306
Daniel Berlin05382af2017-03-07 18:47:52 +0000307// Verify that const pointers work for count and find even when the underlying
308// SmallPtrSet is not for a const pointer type.
309TEST(SmallPtrSetTest, ConstTest) {
310 SmallPtrSet<int *, 8> IntSet;
311 int A;
312 int *B = &A;
313 const int *C = &A;
314 IntSet.insert(B);
315 EXPECT_EQ(IntSet.count(B), 1u);
316 EXPECT_EQ(IntSet.count(C), 1u);
Duncan P. N. Exon Smithcb8ffd22017-03-07 21:56:32 +0000317 EXPECT_NE(IntSet.find(B), IntSet.end());
318 EXPECT_NE(IntSet.find(C), IntSet.end());
Daniel Berlin05382af2017-03-07 18:47:52 +0000319}
320
321// Verify that we automatically get the const version of PointerLikeTypeTraits
322// filled in for us, even for a non-pointer type
323using TestPair = PointerIntPair<int *, 1>;
324
325TEST(SmallPtrSetTest, ConstNonPtrTest) {
326 SmallPtrSet<TestPair, 8> IntSet;
327 int A[1];
328 TestPair Pair(&A[0], 1);
329 IntSet.insert(Pair);
330 EXPECT_EQ(IntSet.count(Pair), 1u);
Duncan P. N. Exon Smithcb8ffd22017-03-07 21:56:32 +0000331 EXPECT_NE(IntSet.find(Pair), IntSet.end());
Daniel Berlin05382af2017-03-07 18:47:52 +0000332}