aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ADT/SmallPtrSetTest.cpp
blob: 4ea7403b65abc15411468270e27ea6fd6615d0a7 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//===- llvm/unittest/ADT/SmallPtrSetTest.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
//
//===----------------------------------------------------------------------===//
//
// SmallPtrSet unit tests.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace llvm;
using testing::UnorderedElementsAre;

TEST(SmallPtrSetTest, Assignment) {
  int buf[8];
  for (int i = 0; i < 8; ++i)
    buf[i] = 0;

  SmallPtrSet<int *, 4> s1 = {&buf[0], &buf[1]};
  SmallPtrSet<int *, 4> s2;
  (s2 = s1).insert(&buf[2]);

  // Self assign as well.
  (s2 = static_cast<SmallPtrSet<int *, 4> &>(s2)).insert(&buf[3]);

  s1 = s2;
  EXPECT_EQ(4U, s1.size());
  for (int i = 0; i < 8; ++i)
    if (i < 4)
      EXPECT_TRUE(s1.count(&buf[i]));
    else
      EXPECT_FALSE(s1.count(&buf[i]));

  // Assign and insert with initializer lists, and ones that contain both
  // duplicates and out-of-order elements.
  (s2 = {&buf[6], &buf[7], &buf[6]}).insert({&buf[5], &buf[4]});
  for (int i = 0; i < 8; ++i)
    if (i < 4)
      EXPECT_FALSE(s2.count(&buf[i]));
    else
      EXPECT_TRUE(s2.count(&buf[i]));
}

TEST(SmallPtrSetTest, GrowthTest) {
  int i;
  int buf[8];
  for(i=0; i<8; ++i) buf[i]=0;


  SmallPtrSet<int *, 4> s;
  typedef SmallPtrSet<int *, 4>::iterator iter;

  s.insert(&buf[0]);
  s.insert(&buf[1]);
  s.insert(&buf[2]);
  s.insert(&buf[3]);
  EXPECT_EQ(4U, s.size());

  i = 0;
  for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
      (**I)++;
  EXPECT_EQ(4, i);
  for(i=0; i<8; ++i)
      EXPECT_EQ(i<4?1:0,buf[i]);

  s.insert(&buf[4]);
  s.insert(&buf[5]);
  s.insert(&buf[6]);
  s.insert(&buf[7]);

  i = 0;
  for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
      (**I)++;
  EXPECT_EQ(8, i);
  s.erase(&buf[4]);
  s.erase(&buf[5]);
  s.erase(&buf[6]);
  s.erase(&buf[7]);
  EXPECT_EQ(4U, s.size());

  i = 0;
  for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
      (**I)++;
  EXPECT_EQ(4, i);
  for(i=0; i<8; ++i)
      EXPECT_EQ(i<4?3:1,buf[i]);

  s.clear();
  for(i=0; i<8; ++i) buf[i]=0;
  for(i=0; i<128; ++i) s.insert(&buf[i%8]); // test repeated entires
  EXPECT_EQ(8U, s.size());
  for(iter I=s.begin(), E=s.end(); I!=E; ++I, ++i)
      (**I)++;
  for(i=0; i<8; ++i)
      EXPECT_EQ(1,buf[i]);
}

TEST(SmallPtrSetTest, CopyAndMoveTest) {
  int buf[8];
  for (int i = 0; i < 8; ++i)
    buf[i] = 0;

  SmallPtrSet<int *, 4> s1;
  s1.insert(&buf[0]);
  s1.insert(&buf[1]);
  s1.insert(&buf[2]);
  s1.insert(&buf[3]);
  EXPECT_EQ(4U, s1.size());
  for (int i = 0; i < 8; ++i)
    if (i < 4)
      EXPECT_TRUE(s1.count(&buf[i]));
    else
      EXPECT_FALSE(s1.count(&buf[i]));

  SmallPtrSet<int *, 4> s2(s1);
  EXPECT_EQ(4U, s2.size());
  for (int i = 0; i < 8; ++i)
    if (i < 4)
      EXPECT_TRUE(s2.count(&buf[i]));
    else
      EXPECT_FALSE(s2.count(&buf[i]));

  s1 = s2;
  EXPECT_EQ(4U, s1.size());
  EXPECT_EQ(4U, s2.size());
  for (int i = 0; i < 8; ++i)
    if (i < 4)
      EXPECT_TRUE(s1.count(&buf[i]));
    else
      EXPECT_FALSE(s1.count(&buf[i]));

  SmallPtrSet<int *, 4> s3(std::move(s1));
  EXPECT_EQ(4U, s3.size());
  EXPECT_TRUE(s1.empty());
  for (int i = 0; i < 8; ++i)
    if (i < 4)
      EXPECT_TRUE(s3.count(&buf[i]));
    else
      EXPECT_FALSE(s3.count(&buf[i]));

  // Move assign into the moved-from object. Also test move of a non-small
  // container.
  s3.insert(&buf[4]);
  s3.insert(&buf[5]);
  s3.insert(&buf[6]);
  s3.insert(&buf[7]);
  s1 = std::move(s3);
  EXPECT_EQ(8U, s1.size());
  EXPECT_TRUE(s3.empty());
  for (int i = 0; i < 8; ++i)
    EXPECT_TRUE(s1.count(&buf[i]));

  // Copy assign into a moved-from object.
  s3 = s1;
  EXPECT_EQ(8U, s3.size());
  EXPECT_EQ(8U, s1.size());
  for (int i = 0; i < 8; ++i)
    EXPECT_TRUE(s3.count(&buf[i]));
}

TEST(SmallPtrSetTest, SwapTest) {
  int buf[10];

  SmallPtrSet<int *, 2> a;
  SmallPtrSet<int *, 2> b;

  a.insert(&buf[0]);
  a.insert(&buf[1]);
  b.insert(&buf[2]);

  EXPECT_EQ(2U, a.size());
  EXPECT_EQ(1U, b.size());
  EXPECT_TRUE(a.count(&buf[0]));
  EXPECT_TRUE(a.count(&buf[1]));
  EXPECT_FALSE(a.count(&buf[2]));
  EXPECT_FALSE(a.count(&buf[3]));
  EXPECT_FALSE(b.count(&buf[0]));
  EXPECT_FALSE(b.count(&buf[1]));
  EXPECT_TRUE(b.count(&buf[2]));
  EXPECT_FALSE(b.count(&buf[3]));

  std::swap(a, b);

  EXPECT_EQ(1U, a.size());
  EXPECT_EQ(2U, b.size());
  EXPECT_FALSE(a.count(&buf[0]));
  EXPECT_FALSE(a.count(&buf[1]));
  EXPECT_TRUE(a.count(&buf[2]));
  EXPECT_FALSE(a.count(&buf[3]));
  EXPECT_TRUE(b.count(&buf[0]));
  EXPECT_TRUE(b.count(&buf[1]));
  EXPECT_FALSE(b.count(&buf[2]));
  EXPECT_FALSE(b.count(&buf[3]));

  b.insert(&buf[3]);
  std::swap(a, b);

  EXPECT_EQ(3U, a.size());
  EXPECT_EQ(1U, b.size());
  EXPECT_TRUE(a.count(&buf[0]));
  EXPECT_TRUE(a.count(&buf[1]));
  EXPECT_FALSE(a.count(&buf[2]));
  EXPECT_TRUE(a.count(&buf[3]));
  EXPECT_FALSE(b.count(&buf[0]));
  EXPECT_FALSE(b.count(&buf[1]));
  EXPECT_TRUE(b.count(&buf[2]));
  EXPECT_FALSE(b.count(&buf[3]));

  std::swap(a, b);

  EXPECT_EQ(1U, a.size());
  EXPECT_EQ(3U, b.size());
  EXPECT_FALSE(a.count(&buf[0]));
  EXPECT_FALSE(a.count(&buf[1]));
  EXPECT_TRUE(a.count(&buf[2]));
  EXPECT_FALSE(a.count(&buf[3]));
  EXPECT_TRUE(b.count(&buf[0]));
  EXPECT_TRUE(b.count(&buf[1]));
  EXPECT_FALSE(b.count(&buf[2]));
  EXPECT_TRUE(b.count(&buf[3]));

  a.insert(&buf[4]);
  a.insert(&buf[5]);
  a.insert(&buf[6]);

  std::swap(b, a);

  EXPECT_EQ(3U, a.size());
  EXPECT_EQ(4U, b.size());
  EXPECT_TRUE(b.count(&buf[2]));
  EXPECT_TRUE(b.count(&buf[4]));
  EXPECT_TRUE(b.count(&buf[5]));
  EXPECT_TRUE(b.count(&buf[6]));
  EXPECT_TRUE(a.count(&buf[0]));
  EXPECT_TRUE(a.count(&buf[1]));
  EXPECT_TRUE(a.count(&buf[3]));
}

// Verify that dereferencing and iteration work.
TEST(SmallPtrSetTest, dereferenceAndIterate) {
  int Ints[] = {0, 1, 2, 3, 4, 5, 6, 7};
  SmallPtrSet<const int *, 4> S;
  for (int &I : Ints) {
    EXPECT_EQ(&I, *S.insert(&I).first);
    EXPECT_EQ(&I, *S.find(&I));
  }

  // Iterate from each and count how many times each element is found.
  int Found[sizeof(Ints)/sizeof(int)] = {0};
  for (int &I : Ints)
    for (auto F = S.find(&I), E = S.end(); F != E; ++F)
      ++Found[*F - Ints];

  // Sort.  We should hit the first element just once and the final element N
  // times.
  llvm::sort(Found);
  for (auto F = std::begin(Found), E = std::end(Found); F != E; ++F)
    EXPECT_EQ(F - Found + 1, *F);
}

// Verify that const pointers work for count and find even when the underlying
// SmallPtrSet is not for a const pointer type.
TEST(SmallPtrSetTest, ConstTest) {
  SmallPtrSet<int *, 8> IntSet;
  int A;
  int *B = &A;
  const int *C = &A;
  IntSet.insert(B);
  EXPECT_EQ(IntSet.count(B), 1u);
  EXPECT_EQ(IntSet.count(C), 1u);
  EXPECT_TRUE(IntSet.contains(B));
  EXPECT_TRUE(IntSet.contains(C));
}

// Verify that we automatically get the const version of PointerLikeTypeTraits
// filled in for us, even for a non-pointer type
using TestPair = PointerIntPair<int *, 1>;

TEST(SmallPtrSetTest, ConstNonPtrTest) {
  SmallPtrSet<TestPair, 8> IntSet;
  int A[1];
  TestPair Pair(&A[0], 1);
  IntSet.insert(Pair);
  EXPECT_EQ(IntSet.count(Pair), 1u);
  EXPECT_TRUE(IntSet.contains(Pair));
}

// Test equality comparison.
TEST(SmallPtrSetTest, EqualityComparison) {
  int buf[3];
  for (int i = 0; i < 3; ++i)
    buf[i] = 0;

  SmallPtrSet<int *, 1> a;
  a.insert(&buf[0]);
  a.insert(&buf[1]);

  SmallPtrSet<int *, 2> b;
  b.insert(&buf[1]);
  b.insert(&buf[0]);

  SmallPtrSet<int *, 3> c;
  c.insert(&buf[1]);
  c.insert(&buf[2]);

  SmallPtrSet<int *, 4> d;
  d.insert(&buf[0]);

  SmallPtrSet<int *, 5> e;
  e.insert(&buf[0]);
  e.insert(&buf[1]);
  e.insert(&buf[2]);

  EXPECT_EQ(a, b);
  EXPECT_EQ(b, a);
  EXPECT_NE(b, c);
  EXPECT_NE(c, a);
  EXPECT_NE(d, a);
  EXPECT_NE(a, d);
  EXPECT_NE(a, e);
  EXPECT_NE(e, a);
  EXPECT_NE(c, e);
  EXPECT_NE(e, d);
}

TEST(SmallPtrSetTest, Contains) {
  SmallPtrSet<int *, 2> Set;
  int buf[4] = {0, 11, 22, 11};
  EXPECT_FALSE(Set.contains(&buf[0]));
  EXPECT_FALSE(Set.contains(&buf[1]));

  Set.insert(&buf[0]);
  Set.insert(&buf[1]);
  EXPECT_TRUE(Set.contains(&buf[0]));
  EXPECT_TRUE(Set.contains(&buf[1]));
  EXPECT_FALSE(Set.contains(&buf[3]));

  Set.insert(&buf[1]);
  EXPECT_TRUE(Set.contains(&buf[0]));
  EXPECT_TRUE(Set.contains(&buf[1]));
  EXPECT_FALSE(Set.contains(&buf[3]));

  Set.erase(&buf[1]);
  EXPECT_TRUE(Set.contains(&buf[0]));
  EXPECT_FALSE(Set.contains(&buf[1]));

  Set.insert(&buf[1]);
  Set.insert(&buf[2]);
  EXPECT_TRUE(Set.contains(&buf[0]));
  EXPECT_TRUE(Set.contains(&buf[1]));
  EXPECT_TRUE(Set.contains(&buf[2]));
}

TEST(SmallPtrSetTest, InsertIterator) {
  SmallPtrSet<int *, 5> Set;
  int Vals[5] = {11, 22, 33, 44, 55};
  int *Buf[5] = {&Vals[0], &Vals[1], &Vals[2], &Vals[3], &Vals[4]};

  for (int *Ptr : Buf)
    Set.insert(Set.begin(), Ptr);

  // Ensure that all of the values were copied into the set.
  for (const auto *Ptr : Buf)
    EXPECT_TRUE(Set.contains(Ptr));
}

TEST(SmallPtrSetTest, RemoveIf) {
  SmallPtrSet<int *, 5> Set;
  int Vals[6] = {0, 1, 2, 3, 4, 5};

  // Stay in small regime.
  Set.insert(&Vals[0]);
  Set.insert(&Vals[1]);
  Set.insert(&Vals[2]);
  Set.insert(&Vals[3]);
  Set.erase(&Vals[0]); // Leave a tombstone.

  // Remove odd elements.
  bool Removed = Set.remove_if([](int *Ptr) { return *Ptr % 2 != 0; });
  // We should only have element 2 left now.
  EXPECT_TRUE(Removed);
  EXPECT_EQ(Set.size(), 1u);
  EXPECT_TRUE(Set.contains(&Vals[2]));

  // Switch to big regime.
  Set.insert(&Vals[0]);
  Set.insert(&Vals[1]);
  Set.insert(&Vals[3]);
  Set.insert(&Vals[4]);
  Set.insert(&Vals[5]);
  Set.erase(&Vals[0]); // Leave a tombstone.

  // Remove odd elements.
  Removed = Set.remove_if([](int *Ptr) { return *Ptr % 2 != 0; });
  // We should only have elements 2 and 4 left now.
  EXPECT_TRUE(Removed);
  EXPECT_EQ(Set.size(), 2u);
  EXPECT_TRUE(Set.contains(&Vals[2]));
  EXPECT_TRUE(Set.contains(&Vals[4]));

  Removed = Set.remove_if([](int *Ptr) { return false; });
  EXPECT_FALSE(Removed);
}

TEST(SmallPtrSetTest, CtorRange) {
  int V0 = 0;
  int V1 = 1;
  int V2 = 2;
  int *Args[] = {&V2, &V0, &V1};
  SmallPtrSet<int *, 4> Set(llvm::from_range, Args);
  EXPECT_THAT(Set, UnorderedElementsAre(&V0, &V1, &V2));
}

TEST(SmallPtrSetTest, InsertRange) {
  int V0 = 0;
  int V1 = 1;
  int V2 = 2;
  SmallPtrSet<int *, 4> Set;
  int *Args[] = {&V2, &V0, &V1};
  Set.insert_range(Args);
  EXPECT_THAT(Set, UnorderedElementsAre(&V0, &V1, &V2));
}

TEST(SmallPtrSetTest, Reserve) {
  // Check that we don't do anything silly when using reserve().
  SmallPtrSet<int *, 4> Set;
  int Vals[8] = {0, 1, 2, 3, 4, 5, 6, 7};

  Set.insert(&Vals[0]);

  // We shouldn't reallocate when this happens.
  Set.reserve(4);
  EXPECT_EQ(Set.capacity(), 4u);

  Set.insert(&Vals[1]);
  Set.insert(&Vals[2]);
  Set.insert(&Vals[3]);

  // We shouldn't reallocate this time either.
  Set.reserve(4);
  EXPECT_EQ(Set.capacity(), 4u);
  EXPECT_EQ(Set.size(), 4u);
  EXPECT_THAT(Set,
              UnorderedElementsAre(&Vals[0], &Vals[1], &Vals[2], &Vals[3]));

  // Reserving further should lead to a reallocation. And matching the existing
  // insertion approach, we immediately allocate up to 128 elements.
  Set.reserve(5);
  EXPECT_EQ(Set.capacity(), 128u);
  EXPECT_EQ(Set.size(), 4u);
  EXPECT_THAT(Set,
              UnorderedElementsAre(&Vals[0], &Vals[1], &Vals[2], &Vals[3]));

  // And we should be able to insert another two or three elements without
  // reallocating.
  Set.insert(&Vals[4]);
  Set.insert(&Vals[5]);

  // Calling a smaller reserve size should have no effect.
  Set.reserve(1);
  EXPECT_EQ(Set.capacity(), 128u);
  EXPECT_EQ(Set.size(), 6u);

  // Reserving zero should have no effect either.
  Set.reserve(0);
  EXPECT_EQ(Set.capacity(), 128u);
  EXPECT_EQ(Set.size(), 6u);
  EXPECT_THAT(Set, UnorderedElementsAre(&Vals[0], &Vals[1], &Vals[2], &Vals[3], &Vals[4], &Vals[5]));
}