1 //===- llvm/unittest/ADT/SmallSetTest.cpp ------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // SmallSet unit tests.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/SmallSet.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "gtest/gtest.h"
20 TEST(SmallSetTest
, Insert
) {
24 for (int i
= 0; i
< 4; i
++) {
25 auto InsertResult
= s1
.insert(i
);
26 EXPECT_EQ(*InsertResult
.first
, i
);
27 EXPECT_EQ(InsertResult
.second
, true);
30 for (int i
= 0; i
< 4; i
++) {
31 auto InsertResult
= s1
.insert(i
);
32 EXPECT_EQ(*InsertResult
.first
, i
);
33 EXPECT_EQ(InsertResult
.second
, false);
36 EXPECT_EQ(4u, s1
.size());
38 for (int i
= 0; i
< 4; i
++)
39 EXPECT_EQ(1u, s1
.count(i
));
41 EXPECT_EQ(0u, s1
.count(4));
44 TEST(SmallSetTest
, Grow
) {
47 for (int i
= 0; i
< 8; i
++) {
48 auto InsertResult
= s1
.insert(i
);
49 EXPECT_EQ(*InsertResult
.first
, i
);
50 EXPECT_EQ(InsertResult
.second
, true);
53 for (int i
= 0; i
< 8; i
++) {
54 auto InsertResult
= s1
.insert(i
);
55 EXPECT_EQ(*InsertResult
.first
, i
);
56 EXPECT_EQ(InsertResult
.second
, false);
59 EXPECT_EQ(8u, s1
.size());
61 for (int i
= 0; i
< 8; i
++)
62 EXPECT_EQ(1u, s1
.count(i
));
64 EXPECT_EQ(0u, s1
.count(8));
67 TEST(SmallSetTest
, Erase
) {
70 for (int i
= 0; i
< 8; i
++)
73 EXPECT_EQ(8u, s1
.size());
75 // Remove elements one by one and check if all other elements are still there.
76 for (int i
= 0; i
< 8; i
++) {
77 EXPECT_EQ(1u, s1
.count(i
));
78 EXPECT_TRUE(s1
.erase(i
));
79 EXPECT_EQ(0u, s1
.count(i
));
80 EXPECT_EQ(8u - i
- 1, s1
.size());
81 for (int j
= i
+ 1; j
< 8; j
++)
82 EXPECT_EQ(1u, s1
.count(j
));
85 EXPECT_EQ(0u, s1
.count(8));
88 TEST(SmallSetTest
, IteratorInt
) {
91 // Test the 'small' case.
92 for (int i
= 0; i
< 3; i
++)
95 std::vector
<int> V(s1
.begin(), s1
.end());
96 // Make sure the elements are in the expected order.
98 for (int i
= 0; i
< 3; i
++)
101 // Test the 'big' case by adding a few more elements to switch to std::set
103 for (int i
= 3; i
< 6; i
++)
106 V
.assign(s1
.begin(), s1
.end());
107 // Make sure the elements are in the expected order.
109 for (int i
= 0; i
< 6; i
++)
113 TEST(SmallSetTest
, IteratorString
) {
114 // Test SmallSetIterator for SmallSet with a type with non-trivial
116 SmallSet
<std::string
, 2> s1
;
122 std::vector
<std::string
> V(s1
.begin(), s1
.end());
124 EXPECT_EQ(2u, s1
.size());
125 EXPECT_EQ("str 1", V
[0]);
126 EXPECT_EQ("str 2", V
[1]);
132 V
.assign(s1
.begin(), s1
.end());
133 // Make sure the elements are in the expected order.
135 EXPECT_EQ(4u, s1
.size());
136 EXPECT_EQ("str 0", V
[0]);
137 EXPECT_EQ("str 1", V
[1]);
138 EXPECT_EQ("str 2", V
[2]);
139 EXPECT_EQ("str 4", V
[3]);
142 TEST(SmallSetTest
, IteratorIncMoveCopy
) {
143 // Test SmallSetIterator for SmallSet with a type with non-trivial
145 SmallSet
<std::string
, 2> s1
;
150 auto Iter
= s1
.begin();
151 EXPECT_EQ("str 1", *Iter
);
153 EXPECT_EQ("str 2", *Iter
);
157 auto Iter2
= s1
.begin();
158 Iter
= std::move(Iter2
);
159 EXPECT_EQ("str 0", *Iter
);
162 TEST(SmallSetTest
, EqualityComparisonTest
) {
163 SmallSet
<int, 8> s1small
;
164 SmallSet
<int, 10> s2small
;
165 SmallSet
<int, 3> s3large
;
166 SmallSet
<int, 8> s4large
;
168 for (int i
= 1; i
< 5; i
++) {
170 s2small
.insert(5 - i
);
173 for (int i
= 1; i
< 11; i
++)
176 EXPECT_EQ(s1small
, s1small
);
177 EXPECT_EQ(s3large
, s3large
);
179 EXPECT_EQ(s1small
, s2small
);
180 EXPECT_EQ(s1small
, s3large
);
181 EXPECT_EQ(s2small
, s3large
);
183 EXPECT_NE(s1small
, s4large
);
184 EXPECT_NE(s4large
, s3large
);
187 TEST(SmallSetTest
, Contains
) {
188 SmallSet
<int, 2> Set
;
189 EXPECT_FALSE(Set
.contains(0));
190 EXPECT_FALSE(Set
.contains(1));
194 EXPECT_TRUE(Set
.contains(0));
195 EXPECT_TRUE(Set
.contains(1));
198 EXPECT_TRUE(Set
.contains(0));
199 EXPECT_TRUE(Set
.contains(1));
202 EXPECT_TRUE(Set
.contains(0));
203 EXPECT_FALSE(Set
.contains(1));
207 EXPECT_TRUE(Set
.contains(0));
208 EXPECT_TRUE(Set
.contains(1));
209 EXPECT_TRUE(Set
.contains(2));