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 "gtest/gtest.h"
19 TEST(SmallSetTest
, Insert
) {
23 for (int i
= 0; i
< 4; i
++)
26 for (int i
= 0; i
< 4; i
++)
29 EXPECT_EQ(4u, s1
.size());
31 for (int i
= 0; i
< 4; i
++)
32 EXPECT_EQ(1u, s1
.count(i
));
34 EXPECT_EQ(0u, s1
.count(4));
37 TEST(SmallSetTest
, Grow
) {
40 for (int i
= 0; i
< 8; i
++)
43 EXPECT_EQ(8u, s1
.size());
45 for (int i
= 0; i
< 8; i
++)
46 EXPECT_EQ(1u, s1
.count(i
));
48 EXPECT_EQ(0u, s1
.count(8));
51 TEST(SmallSetTest
, Erase
) {
54 for (int i
= 0; i
< 8; i
++)
57 EXPECT_EQ(8u, s1
.size());
59 // Remove elements one by one and check if all other elements are still there.
60 for (int i
= 0; i
< 8; i
++) {
61 EXPECT_EQ(1u, s1
.count(i
));
62 EXPECT_TRUE(s1
.erase(i
));
63 EXPECT_EQ(0u, s1
.count(i
));
64 EXPECT_EQ(8u - i
- 1, s1
.size());
65 for (int j
= i
+ 1; j
< 8; j
++)
66 EXPECT_EQ(1u, s1
.count(j
));
69 EXPECT_EQ(0u, s1
.count(8));
72 TEST(SmallSetTest
, IteratorInt
) {
75 // Test the 'small' case.
76 for (int i
= 0; i
< 3; i
++)
79 std::vector
<int> V(s1
.begin(), s1
.end());
80 // Make sure the elements are in the expected order.
81 std::sort(V
.begin(), V
.end());
82 for (int i
= 0; i
< 3; i
++)
85 // Test the 'big' case by adding a few more elements to switch to std::set
87 for (int i
= 3; i
< 6; i
++)
90 V
.assign(s1
.begin(), s1
.end());
91 // Make sure the elements are in the expected order.
92 std::sort(V
.begin(), V
.end());
93 for (int i
= 0; i
< 6; i
++)
97 TEST(SmallSetTest
, IteratorString
) {
98 // Test SmallSetIterator for SmallSet with a type with non-trivial
100 SmallSet
<std::string
, 2> s1
;
106 std::vector
<std::string
> V(s1
.begin(), s1
.end());
107 std::sort(V
.begin(), V
.end());
108 EXPECT_EQ(2u, s1
.size());
109 EXPECT_EQ("str 1", V
[0]);
110 EXPECT_EQ("str 2", V
[1]);
116 V
.assign(s1
.begin(), s1
.end());
117 // Make sure the elements are in the expected order.
118 std::sort(V
.begin(), V
.end());
119 EXPECT_EQ(4u, s1
.size());
120 EXPECT_EQ("str 0", V
[0]);
121 EXPECT_EQ("str 1", V
[1]);
122 EXPECT_EQ("str 2", V
[2]);
123 EXPECT_EQ("str 4", V
[3]);
126 TEST(SmallSetTest
, IteratorIncMoveCopy
) {
127 // Test SmallSetIterator for SmallSet with a type with non-trivial
129 SmallSet
<std::string
, 2> s1
;
134 auto Iter
= s1
.begin();
135 EXPECT_EQ("str 1", *Iter
);
137 EXPECT_EQ("str 2", *Iter
);
141 auto Iter2
= s1
.begin();
142 Iter
= std::move(Iter2
);
143 EXPECT_EQ("str 0", *Iter
);