1 //===- llvm/unittest/ADT/SparseBitVectorTest.cpp - SparseBitVector tests --===//
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 #include "llvm/ADT/SparseBitVector.h"
10 #include "gtest/gtest.h"
16 TEST(SparseBitVectorTest
, TrivialOperation
) {
17 SparseBitVector
<> Vec
;
18 EXPECT_EQ(0U, Vec
.count());
19 EXPECT_FALSE(Vec
.test(17));
21 EXPECT_TRUE(Vec
.test(5));
22 EXPECT_FALSE(Vec
.test(17));
24 EXPECT_TRUE(Vec
.test(5));
25 EXPECT_FALSE(Vec
.test(6));
27 EXPECT_FALSE(Vec
.test(5));
28 EXPECT_TRUE(Vec
.test_and_set(17));
29 EXPECT_FALSE(Vec
.test_and_set(17));
30 EXPECT_TRUE(Vec
.test(17));
32 EXPECT_FALSE(Vec
.test(17));
35 const SparseBitVector
<> ConstVec
= Vec
;
36 EXPECT_TRUE(ConstVec
.test(5));
37 EXPECT_FALSE(ConstVec
.test(17));
40 EXPECT_TRUE(Vec
.test(1337));
42 EXPECT_FALSE(Vec
.test(1337));
45 EXPECT_FALSE(Vec
.empty());
46 SparseBitVector
<> MovedVec(std::move(Vec
));
47 EXPECT_TRUE(Vec
.empty());
48 EXPECT_TRUE(MovedVec
.test(5));
49 EXPECT_TRUE(MovedVec
.test(1337));
51 Vec
= std::move(MovedVec
);
52 EXPECT_TRUE(MovedVec
.empty());
53 EXPECT_FALSE(Vec
.empty());
56 TEST(SparseBitVectorTest
, IntersectWith
) {
57 SparseBitVector
<> Vec
, Other
;
61 EXPECT_FALSE(Vec
&= Other
);
62 EXPECT_TRUE(Vec
.test(1));
68 EXPECT_TRUE(Vec
&= Other
);
69 EXPECT_TRUE(Vec
.empty());
75 EXPECT_TRUE(Vec
&= Other
);
76 EXPECT_TRUE(Vec
.empty());
82 EXPECT_TRUE(Vec
&= Other
);
83 EXPECT_TRUE(Vec
.empty());
86 TEST(SparseBitVectorTest
, SelfAssignment
) {
87 SparseBitVector
<> Vec
, Other
;
91 Vec
= static_cast<SparseBitVector
<> &>(Vec
);
92 EXPECT_TRUE(Vec
.test(23));
93 EXPECT_TRUE(Vec
.test(234));
98 EXPECT_FALSE(Vec
|= Vec
);
99 EXPECT_TRUE(Vec
.test(17));
100 EXPECT_TRUE(Vec
.test(256));
105 EXPECT_FALSE(Vec
&= Vec
);
106 EXPECT_TRUE(Vec
.test(56));
107 EXPECT_TRUE(Vec
.test(517));
112 EXPECT_TRUE(Vec
.intersectWithComplement(Vec
));
113 EXPECT_TRUE(Vec
.empty());
114 EXPECT_FALSE(Vec
.intersectWithComplement(Vec
));
119 Vec
.intersectWithComplement(Vec
, Vec
);
120 EXPECT_TRUE(Vec
.empty());
127 Vec
.intersectWithComplement(Vec
, Other
);
128 EXPECT_TRUE(Vec
.test(42));
129 EXPECT_FALSE(Vec
.test(567));
137 Vec
.intersectWithComplement(Other
, Vec
);
138 EXPECT_FALSE(Vec
.test(19));
139 EXPECT_TRUE(Vec
.test(31));
146 Vec
.intersectWithComplement(Other
, Other
);
147 EXPECT_TRUE(Vec
.empty());
150 TEST(SparseBitVectorTest
, Find
) {
151 SparseBitVector
<> Vec
;
153 EXPECT_EQ(1, Vec
.find_first());
154 EXPECT_EQ(1, Vec
.find_last());
157 EXPECT_EQ(1, Vec
.find_first());
158 EXPECT_EQ(2, Vec
.find_last());
162 EXPECT_EQ(0, Vec
.find_first());
163 EXPECT_EQ(3, Vec
.find_last());
168 EXPECT_EQ(2, Vec
.find_first());
169 EXPECT_EQ(2, Vec
.find_last());
171 // Set some large bits to ensure we are pulling bits from more than just a
178 EXPECT_EQ(500, Vec
.find_first());
179 EXPECT_EQ(4000, Vec
.find_last());
184 EXPECT_EQ(2000, Vec
.find_first());
185 EXPECT_EQ(2000, Vec
.find_last());