1 //===------ ADT/SparseSetTest.cpp - SparseSet unit tests - -----*- C++ -*-===//
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 #include "llvm/ADT/SparseMultiSet.h"
11 #include "gtest/gtest.h"
17 typedef SparseMultiSet
<unsigned> USet
;
20 TEST(SparseMultiSetTest
, EmptySet
) {
22 EXPECT_TRUE(Set
.empty());
23 EXPECT_EQ(0u, Set
.size());
27 // Lookups on empty set.
28 EXPECT_TRUE(Set
.find(0) == Set
.end());
29 EXPECT_TRUE(Set
.find(9) == Set
.end());
31 // Same thing on a const reference.
32 const USet
&CSet
= Set
;
33 EXPECT_TRUE(CSet
.empty());
34 EXPECT_EQ(0u, CSet
.size());
35 EXPECT_TRUE(CSet
.find(0) == CSet
.end());
36 USet::const_iterator I
= CSet
.find(5);
37 EXPECT_TRUE(I
== CSet
.end());
40 // Single entry set tests.
41 TEST(SparseMultiSetTest
, SingleEntrySet
) {
44 USet::iterator I
= Set
.insert(5);
45 EXPECT_TRUE(I
!= Set
.end());
48 EXPECT_FALSE(Set
.empty());
49 EXPECT_EQ(1u, Set
.size());
51 EXPECT_TRUE(Set
.find(0) == Set
.end());
52 EXPECT_TRUE(Set
.find(9) == Set
.end());
54 EXPECT_FALSE(Set
.contains(0));
55 EXPECT_TRUE(Set
.contains(5));
59 EXPECT_TRUE(I
!= Set
.end());
60 EXPECT_TRUE(I
== ++Set
.find(5));
62 EXPECT_TRUE(I
== Set
.find(5));
64 // Erase non-existent element.
66 EXPECT_TRUE(I
== Set
.end());
67 EXPECT_EQ(2u, Set
.size());
68 EXPECT_EQ(5u, *Set
.find(5));
72 EXPECT_TRUE(I
!= Set
.end());
74 EXPECT_TRUE(I
!= Set
.end());
76 EXPECT_TRUE(I
== Set
.end());
77 EXPECT_TRUE(Set
.empty());
80 // Multiple entry set tests.
81 TEST(SparseMultiSetTest
, MultipleEntrySet
) {
92 EXPECT_EQ(7u, Set
.size());
94 // Erase last element by key.
95 EXPECT_TRUE(Set
.erase(Set
.find(4)) == Set
.end());
96 EXPECT_EQ(6u, Set
.size());
97 EXPECT_FALSE(Set
.contains(4));
98 EXPECT_TRUE(Set
.find(4) == Set
.end());
100 // Erase first element by key.
101 EXPECT_EQ(3u, Set
.count(5));
102 EXPECT_TRUE(Set
.find(5) != Set
.end());
103 EXPECT_TRUE(Set
.erase(Set
.find(5)) != Set
.end());
104 EXPECT_EQ(5u, Set
.size());
105 EXPECT_EQ(2u, Set
.count(5));
109 EXPECT_EQ(7u, Set
.size());
111 // Erase tail by iterator.
112 EXPECT_TRUE(Set
.getTail(6) == Set
.getHead(6));
113 USet::iterator I
= Set
.erase(Set
.find(6));
114 EXPECT_TRUE(I
== Set
.end());
115 EXPECT_EQ(6u, Set
.size());
117 // Erase tails by iterator.
118 EXPECT_EQ(2u, Set
.count(5));
121 EXPECT_TRUE(I
== Set
.end());
123 EXPECT_EQ(1u, Set
.count(5));
126 EXPECT_TRUE(I
== Set
.end());
127 EXPECT_EQ(0u, Set
.count(5));
136 EXPECT_EQ(5, std::distance(Set
.getHead(8), Set
.end()));
138 EXPECT_EQ(0, std::distance(Set
.getHead(8), Set
.end()));
140 // Clear and resize the universe.
142 EXPECT_EQ(0u, Set
.size());
143 EXPECT_FALSE(Set
.contains(3));
144 Set
.setUniverse(1000);
146 // Add more than 256 elements.
147 for (unsigned i
= 100; i
!= 800; ++i
)
150 for (unsigned i
= 0; i
!= 10; ++i
)
153 for (unsigned i
= 100; i
!= 800; ++i
)
154 EXPECT_EQ(1u, Set
.count(i
));
156 EXPECT_FALSE(Set
.contains(99));
157 EXPECT_FALSE(Set
.contains(800));
158 EXPECT_EQ(700u, Set
.size());
161 // Test out iterators
162 TEST(SparseMultiSetTest
, Iterators
) {
164 Set
.setUniverse(100);
173 USet::RangePair RangePair
= Set
.equal_range(0);
174 USet::iterator B
= RangePair
.first
;
175 USet::iterator E
= RangePair
.second
;
177 // Move the iterators around, going to end and coming back.
178 EXPECT_EQ(3, std::distance(B
, E
));
179 EXPECT_EQ(B
, --(--(--E
)));
180 EXPECT_EQ(++(++(++E
)), Set
.end());
181 EXPECT_EQ(B
, --(--(--E
)));
182 EXPECT_EQ(++(++(++E
)), Set
.end());
184 // Insert into the tail, and move around again
186 EXPECT_EQ(B
, --(--(--(--E
))));
187 EXPECT_EQ(++(++(++(++E
))), Set
.end());
188 EXPECT_EQ(B
, --(--(--(--E
))));
189 EXPECT_EQ(++(++(++(++E
))), Set
.end());
191 // Erase a tail, and move around again
192 USet::iterator Erased
= Set
.erase(Set
.getTail(0));
193 EXPECT_EQ(Erased
, E
);
194 EXPECT_EQ(B
, --(--(--E
)));
197 Set2
.setUniverse(11);
199 EXPECT_TRUE(!Set2
.contains(0));
200 EXPECT_TRUE(!Set
.contains(3));
202 EXPECT_EQ(Set2
.getHead(3), Set2
.getTail(3));
203 EXPECT_EQ(Set2
.getHead(0), Set2
.getTail(0));
205 EXPECT_EQ(Set2
.find(3), --(++B
));
210 explicit Alt(unsigned x
) : Value(x
) {}
211 unsigned getSparseSetIndex() const { return Value
- 1000; }
214 TEST(SparseMultiSetTest
, AltStructSet
) {
215 typedef SparseMultiSet
<Alt
> ASet
;
218 Set
.insert(Alt(1005));
220 ASet::iterator I
= Set
.find(5);
221 ASSERT_TRUE(I
!= Set
.end());
222 EXPECT_EQ(1005u, I
->Value
);
224 Set
.insert(Alt(1006));
225 Set
.insert(Alt(1006));
226 I
= Set
.erase(Set
.find(6));
227 ASSERT_TRUE(I
!= Set
.end());
228 EXPECT_EQ(1006u, I
->Value
);
229 I
= Set
.erase(Set
.find(6));
230 ASSERT_TRUE(I
== Set
.end());
232 EXPECT_TRUE(Set
.contains(5));
233 EXPECT_FALSE(Set
.contains(6));