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 "gmock/gmock.h"
16 #include "gtest/gtest.h"
21 TEST(SmallSetTest
, ConstructorIteratorPair
) {
22 std::initializer_list
<int> L
= {1, 2, 3, 4, 5};
23 SmallSet
<int, 4> S(std::begin(L
), std::end(L
));
24 EXPECT_THAT(S
, testing::UnorderedElementsAreArray(L
));
27 TEST(SmallSet
, ConstructorRange
) {
28 std::initializer_list
<int> L
= {1, 2, 3, 4, 5};
30 SmallSet
<int, 4> S(llvm::make_range(std::begin(L
), std::end(L
)));
31 EXPECT_THAT(S
, testing::UnorderedElementsAreArray(L
));
34 TEST(SmallSet
, ConstructorInitializerList
) {
35 std::initializer_list
<int> L
= {1, 2, 3, 4, 5};
36 SmallSet
<int, 4> S
= {1, 2, 3, 4, 5};
37 EXPECT_THAT(S
, testing::UnorderedElementsAreArray(L
));
40 TEST(SmallSet
, CopyConstructor
) {
41 SmallSet
<int, 4> S
= {1, 2, 3};
42 SmallSet
<int, 4> T
= S
;
44 EXPECT_THAT(S
, testing::ContainerEq(T
));
47 TEST(SmallSet
, MoveConstructor
) {
48 std::initializer_list
<int> L
= {1, 2, 3};
49 SmallSet
<int, 4> S
= L
;
50 SmallSet
<int, 4> T
= std::move(S
);
52 EXPECT_THAT(T
, testing::UnorderedElementsAreArray(L
));
55 TEST(SmallSet
, CopyAssignment
) {
56 SmallSet
<int, 4> S
= {1, 2, 3};
60 EXPECT_THAT(S
, testing::ContainerEq(T
));
63 TEST(SmallSet
, MoveAssignment
) {
64 std::initializer_list
<int> L
= {1, 2, 3};
65 SmallSet
<int, 4> S
= L
;
69 EXPECT_THAT(T
, testing::UnorderedElementsAreArray(L
));
72 TEST(SmallSetTest
, Insert
) {
76 for (int i
= 0; i
< 4; i
++) {
77 auto InsertResult
= s1
.insert(i
);
78 EXPECT_EQ(*InsertResult
.first
, i
);
79 EXPECT_EQ(InsertResult
.second
, true);
82 for (int i
= 0; i
< 4; i
++) {
83 auto InsertResult
= s1
.insert(i
);
84 EXPECT_EQ(*InsertResult
.first
, i
);
85 EXPECT_EQ(InsertResult
.second
, false);
88 EXPECT_EQ(4u, s1
.size());
90 for (int i
= 0; i
< 4; i
++)
91 EXPECT_EQ(1u, s1
.count(i
));
93 EXPECT_EQ(0u, s1
.count(4));
96 TEST(SmallSetTest
, InsertPerfectFwd
) {
101 Value(int Key
) : Key(Key
), Moved(false) {}
102 Value(const Value
&) = default;
103 Value(Value
&&Other
) : Key(Other
.Key
), Moved(false) { Other
.Moved
= true; }
104 bool operator==(const Value
&Other
) const { return Key
== Other
.Key
; }
105 bool operator<(const Value
&Other
) const { return Key
< Other
.Key
; }
109 SmallSet
<Value
, 4> S
;
113 EXPECT_EQ(V1
.Moved
, false);
115 S
.insert(std::move(V2
));
116 EXPECT_EQ(V2
.Moved
, true);
119 SmallSet
<Value
, 1> S
;
123 EXPECT_EQ(V1
.Moved
, false);
125 S
.insert(std::move(V2
));
126 EXPECT_EQ(V2
.Moved
, true);
130 TEST(SmallSetTest
, Grow
) {
133 for (int i
= 0; i
< 8; i
++) {
134 auto InsertResult
= s1
.insert(i
);
135 EXPECT_EQ(*InsertResult
.first
, i
);
136 EXPECT_EQ(InsertResult
.second
, true);
139 for (int i
= 0; i
< 8; i
++) {
140 auto InsertResult
= s1
.insert(i
);
141 EXPECT_EQ(*InsertResult
.first
, i
);
142 EXPECT_EQ(InsertResult
.second
, false);
145 EXPECT_EQ(8u, s1
.size());
147 for (int i
= 0; i
< 8; i
++)
148 EXPECT_EQ(1u, s1
.count(i
));
150 EXPECT_EQ(0u, s1
.count(8));
153 TEST(SmallSetTest
, Erase
) {
156 for (int i
= 0; i
< 8; i
++)
159 EXPECT_EQ(8u, s1
.size());
161 // Remove elements one by one and check if all other elements are still there.
162 for (int i
= 0; i
< 8; i
++) {
163 EXPECT_EQ(1u, s1
.count(i
));
164 EXPECT_TRUE(s1
.erase(i
));
165 EXPECT_EQ(0u, s1
.count(i
));
166 EXPECT_EQ(8u - i
- 1, s1
.size());
167 for (int j
= i
+ 1; j
< 8; j
++)
168 EXPECT_EQ(1u, s1
.count(j
));
171 EXPECT_EQ(0u, s1
.count(8));
174 TEST(SmallSetTest
, IteratorInt
) {
177 // Test the 'small' case.
178 for (int i
= 0; i
< 3; i
++)
181 std::vector
<int> V(s1
.begin(), s1
.end());
182 // Make sure the elements are in the expected order.
184 for (int i
= 0; i
< 3; i
++)
187 // Test the 'big' case by adding a few more elements to switch to std::set
189 for (int i
= 3; i
< 6; i
++)
192 V
.assign(s1
.begin(), s1
.end());
193 // Make sure the elements are in the expected order.
195 for (int i
= 0; i
< 6; i
++)
199 TEST(SmallSetTest
, IteratorString
) {
200 // Test SmallSetIterator for SmallSet with a type with non-trivial
202 SmallSet
<std::string
, 2> s1
;
208 std::vector
<std::string
> V(s1
.begin(), s1
.end());
210 EXPECT_EQ(2u, s1
.size());
211 EXPECT_EQ("str 1", V
[0]);
212 EXPECT_EQ("str 2", V
[1]);
218 V
.assign(s1
.begin(), s1
.end());
219 // Make sure the elements are in the expected order.
221 EXPECT_EQ(4u, s1
.size());
222 EXPECT_EQ("str 0", V
[0]);
223 EXPECT_EQ("str 1", V
[1]);
224 EXPECT_EQ("str 2", V
[2]);
225 EXPECT_EQ("str 4", V
[3]);
228 TEST(SmallSetTest
, IteratorIncMoveCopy
) {
229 // Test SmallSetIterator for SmallSet with a type with non-trivial
231 SmallSet
<std::string
, 2> s1
;
236 auto Iter
= s1
.begin();
237 EXPECT_EQ("str 1", *Iter
);
239 EXPECT_EQ("str 2", *Iter
);
243 auto Iter2
= s1
.begin();
244 Iter
= std::move(Iter2
);
245 EXPECT_EQ("str 0", *Iter
);
248 TEST(SmallSetTest
, EqualityComparisonTest
) {
249 SmallSet
<int, 8> s1small
;
250 SmallSet
<int, 10> s2small
;
251 SmallSet
<int, 3> s3large
;
252 SmallSet
<int, 8> s4large
;
254 for (int i
= 1; i
< 5; i
++) {
256 s2small
.insert(5 - i
);
259 for (int i
= 1; i
< 11; i
++)
262 EXPECT_EQ(s1small
, s1small
);
263 EXPECT_EQ(s3large
, s3large
);
265 EXPECT_EQ(s1small
, s2small
);
266 EXPECT_EQ(s1small
, s3large
);
267 EXPECT_EQ(s2small
, s3large
);
269 EXPECT_NE(s1small
, s4large
);
270 EXPECT_NE(s4large
, s3large
);
273 TEST(SmallSetTest
, Contains
) {
274 SmallSet
<int, 2> Set
;
275 EXPECT_FALSE(Set
.contains(0));
276 EXPECT_FALSE(Set
.contains(1));
280 EXPECT_TRUE(Set
.contains(0));
281 EXPECT_TRUE(Set
.contains(1));
284 EXPECT_TRUE(Set
.contains(0));
285 EXPECT_TRUE(Set
.contains(1));
288 EXPECT_TRUE(Set
.contains(0));
289 EXPECT_FALSE(Set
.contains(1));
293 EXPECT_TRUE(Set
.contains(0));
294 EXPECT_TRUE(Set
.contains(1));
295 EXPECT_TRUE(Set
.contains(2));