1 //===- llvm/unittest/ADT/SmallSetTest.cpp ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // SmallSet unit tests.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/SmallSet.h"
15 #include "gtest/gtest.h"
20 TEST(SmallSetTest
, Insert
) {
24 for (int i
= 0; i
< 4; i
++)
27 for (int i
= 0; i
< 4; i
++)
30 EXPECT_EQ(4u, s1
.size());
32 for (int i
= 0; i
< 4; i
++)
33 EXPECT_EQ(1u, s1
.count(i
));
35 EXPECT_EQ(0u, s1
.count(4));
38 TEST(SmallSetTest
, Grow
) {
41 for (int i
= 0; i
< 8; i
++)
44 EXPECT_EQ(8u, s1
.size());
46 for (int i
= 0; i
< 8; i
++)
47 EXPECT_EQ(1u, s1
.count(i
));
49 EXPECT_EQ(0u, s1
.count(8));
52 TEST(SmallSetTest
, Erase
) {
55 for (int i
= 0; i
< 8; i
++)
58 EXPECT_EQ(8u, s1
.size());
60 // Remove elements one by one and check if all other elements are still there.
61 for (int i
= 0; i
< 8; i
++) {
62 EXPECT_EQ(1u, s1
.count(i
));
63 EXPECT_TRUE(s1
.erase(i
));
64 EXPECT_EQ(0u, s1
.count(i
));
65 EXPECT_EQ(8u - i
- 1, s1
.size());
66 for (int j
= i
+ 1; j
< 8; j
++)
67 EXPECT_EQ(1u, s1
.count(j
));
70 EXPECT_EQ(0u, s1
.count(8));
73 TEST(SmallSetTest
, IteratorInt
) {
76 // Test the 'small' case.
77 for (int i
= 0; i
< 3; i
++)
80 std::vector
<int> V(s1
.begin(), s1
.end());
81 // Make sure the elements are in the expected order.
82 std::sort(V
.begin(), V
.end());
83 for (int i
= 0; i
< 3; i
++)
86 // Test the 'big' case by adding a few more elements to switch to std::set
88 for (int i
= 3; i
< 6; i
++)
91 V
.assign(s1
.begin(), s1
.end());
92 // Make sure the elements are in the expected order.
93 std::sort(V
.begin(), V
.end());
94 for (int i
= 0; i
< 6; i
++)
98 TEST(SmallSetTest
, IteratorString
) {
99 // Test SmallSetIterator for SmallSet with a type with non-trivial
101 SmallSet
<std::string
, 2> s1
;
107 std::vector
<std::string
> V(s1
.begin(), s1
.end());
108 std::sort(V
.begin(), V
.end());
109 EXPECT_EQ(2u, s1
.size());
110 EXPECT_EQ("str 1", V
[0]);
111 EXPECT_EQ("str 2", V
[1]);
117 V
.assign(s1
.begin(), s1
.end());
118 // Make sure the elements are in the expected order.
119 std::sort(V
.begin(), V
.end());
120 EXPECT_EQ(4u, s1
.size());
121 EXPECT_EQ("str 0", V
[0]);
122 EXPECT_EQ("str 1", V
[1]);
123 EXPECT_EQ("str 2", V
[2]);
124 EXPECT_EQ("str 4", V
[3]);
127 TEST(SmallSetTest
, IteratorIncMoveCopy
) {
128 // Test SmallSetIterator for SmallSet with a type with non-trivial
130 SmallSet
<std::string
, 2> s1
;
135 auto Iter
= s1
.begin();
136 EXPECT_EQ("str 1", *Iter
);
138 EXPECT_EQ("str 2", *Iter
);
142 auto Iter2
= s1
.begin();
143 Iter
= std::move(Iter2
);
144 EXPECT_EQ("str 0", *Iter
);
146 auto Iter3
= s1
.end();
148 EXPECT_EQ(Iter3
, Iter2
);