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 | |
19 | TEST(SetVector, EraseTest) { |
20 | SetVector<int> S; |
21 | S.insert(X: 0); |
22 | S.insert(X: 1); |
23 | S.insert(X: 2); |
24 | |
25 | auto I = S.erase(I: std::next(x: 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 | |
35 | TEST(SetVector, ContainsTest) { |
36 | SetVector<int> S; |
37 | S.insert(X: 0); |
38 | S.insert(X: 1); |
39 | S.insert(X: 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(X: 2); |
47 | EXPECT_TRUE(S.contains(2)); |
48 | |
49 | S.remove(X: 2); |
50 | EXPECT_FALSE(S.contains(2)); |
51 | } |
52 | |
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(X: &i); |
58 | S.insert(X: &j); |
59 | S.insert(X: &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(X: &k); |
74 | EXPECT_FALSE(S.contains(&k)); |
75 | EXPECT_FALSE(S.contains((const int *)&k)); |
76 | |
77 | T.insert(X: &j); |
78 | T.insert(X: &m); |
79 | T.insert(X: &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 | |