1 //===- llvm/unittest/ADT/SetVector.cpp ------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // SetVector unit tests.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/ADT/SetVector.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "gtest/gtest.h"
16
17 using namespace llvm;
18
TEST(SetVector,EraseTest)19 TEST(SetVector, EraseTest) {
20 SetVector<int> S;
21 S.insert(0);
22 S.insert(1);
23 S.insert(2);
24
25 auto I = S.erase(std::next(S.begin()));
26
27 // Test that the returned iterator is the expected one-after-erase
28 // and the size/contents is the expected sequence {0, 2}.
29 EXPECT_EQ(std::next(S.begin()), I);
30 EXPECT_EQ(2u, S.size());
31 EXPECT_EQ(0, *S.begin());
32 EXPECT_EQ(2, *std::next(S.begin()));
33 }
34
TEST(SetVector,ContainsTest)35 TEST(SetVector, ContainsTest) {
36 SetVector<int> S;
37 S.insert(0);
38 S.insert(1);
39 S.insert(2);
40
41 EXPECT_TRUE(S.contains(0));
42 EXPECT_TRUE(S.contains(1));
43 EXPECT_TRUE(S.contains(2));
44 EXPECT_FALSE(S.contains(-1));
45
46 S.insert(2);
47 EXPECT_TRUE(S.contains(2));
48
49 S.remove(2);
50 EXPECT_FALSE(S.contains(2));
51 }
52
TEST(SetVector,ConstPtrKeyTest)53 TEST(SetVector, ConstPtrKeyTest) {
54 SetVector<int *, SmallVector<int *, 8>, SmallPtrSet<const int *, 8>> S, T;
55 int i, j, k, m, n;
56
57 S.insert(&i);
58 S.insert(&j);
59 S.insert(&k);
60
61 EXPECT_TRUE(S.contains(&i));
62 EXPECT_TRUE(S.contains(&j));
63 EXPECT_TRUE(S.contains(&k));
64
65 EXPECT_TRUE(S.contains((const int *)&i));
66 EXPECT_TRUE(S.contains((const int *)&j));
67 EXPECT_TRUE(S.contains((const int *)&k));
68
69 EXPECT_TRUE(S.contains(S[0]));
70 EXPECT_TRUE(S.contains(S[1]));
71 EXPECT_TRUE(S.contains(S[2]));
72
73 S.remove(&k);
74 EXPECT_FALSE(S.contains(&k));
75 EXPECT_FALSE(S.contains((const int *)&k));
76
77 T.insert(&j);
78 T.insert(&m);
79 T.insert(&n);
80
81 EXPECT_TRUE(S.set_union(T));
82 EXPECT_TRUE(S.contains(&m));
83 EXPECT_TRUE(S.contains((const int *)&m));
84
85 S.set_subtract(T);
86 EXPECT_FALSE(S.contains(&j));
87 EXPECT_FALSE(S.contains((const int *)&j));
88 }
89