1 //===- llvm/unittest/ADT/SmallBitVectorTest.cpp - SmallBitVector tests ----===//
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/SmallBitVector.h"
11 #include "gtest/gtest.h"
17 TEST(SmallBitVectorTest
, TrivialOperation
) {
19 EXPECT_EQ(0U, Vec
.count());
20 EXPECT_EQ(0U, Vec
.size());
21 EXPECT_FALSE(Vec
.any());
22 EXPECT_TRUE(Vec
.all());
23 EXPECT_TRUE(Vec
.none());
24 EXPECT_TRUE(Vec
.empty());
27 EXPECT_EQ(5U, Vec
.count());
28 EXPECT_EQ(5U, Vec
.size());
29 EXPECT_TRUE(Vec
.any());
30 EXPECT_TRUE(Vec
.all());
31 EXPECT_FALSE(Vec
.none());
32 EXPECT_FALSE(Vec
.empty());
35 EXPECT_EQ(5U, Vec
.count());
36 EXPECT_EQ(11U, Vec
.size());
37 EXPECT_TRUE(Vec
.any());
38 EXPECT_FALSE(Vec
.all());
39 EXPECT_FALSE(Vec
.none());
40 EXPECT_FALSE(Vec
.empty());
42 SmallBitVector Inv
= ~Vec
;
43 EXPECT_EQ(6U, Inv
.count());
44 EXPECT_EQ(11U, Inv
.size());
45 EXPECT_TRUE(Inv
.any());
46 EXPECT_FALSE(Inv
.all());
47 EXPECT_FALSE(Inv
.none());
48 EXPECT_FALSE(Inv
.empty());
50 EXPECT_FALSE(Inv
== Vec
);
51 EXPECT_TRUE(Inv
!= Vec
);
53 EXPECT_TRUE(Inv
== Vec
);
54 EXPECT_FALSE(Inv
!= Vec
);
56 // Add some "interesting" data to Vec.
58 Vec
.resize(25, false);
60 Vec
.resize(29, false);
62 Vec
.resize(57, false);
64 for (unsigned i
= Vec
.find_first(); i
!= -1u; i
= Vec
.find_next(i
)) {
67 EXPECT_TRUE(Vec
.test(i
));
69 EXPECT_EQ(Count
, Vec
.count());
70 EXPECT_EQ(Count
, 23u);
73 EXPECT_FALSE(Vec
[56]);
74 Vec
.resize(61, false);
76 SmallBitVector Copy
= Vec
;
77 SmallBitVector
Alt(3, false);
80 EXPECT_TRUE(Copy
== Alt
);
81 EXPECT_TRUE(Vec
.size() == 6);
82 EXPECT_TRUE(Vec
.count() == 3);
83 EXPECT_TRUE(Vec
.find_first() == 3);
86 // Add some more "interesting" data.
88 Vec
.resize(78, false);
90 Vec
.resize(90, false);
92 Vec
.resize(130, false);
94 for (unsigned i
= Vec
.find_first(); i
!= -1u; i
= Vec
.find_next(i
)) {
97 EXPECT_TRUE(Vec
.test(i
));
99 EXPECT_EQ(Count
, Vec
.count());
100 EXPECT_EQ(Count
, 42u);
101 EXPECT_FALSE(Vec
[0]);
102 EXPECT_TRUE(Vec
[32]);
103 EXPECT_FALSE(Vec
[60]);
104 EXPECT_FALSE(Vec
[129]);
107 EXPECT_TRUE(Vec
[60]);
108 EXPECT_EQ(Count
+ 1, Vec
.count());
110 EXPECT_FALSE(Vec
[60]);
111 EXPECT_EQ(Count
, Vec
.count());
114 EXPECT_FALSE(Vec
[32]);
115 EXPECT_EQ(Count
- 1, Vec
.count());
117 EXPECT_TRUE(Vec
[32]);
118 EXPECT_EQ(Count
, Vec
.count());
121 EXPECT_EQ(Vec
.size() - Count
, Vec
.count());
124 EXPECT_EQ(0U, Vec
.count());
125 EXPECT_EQ(130U, Vec
.size());
126 EXPECT_FALSE(Vec
.any());
127 EXPECT_FALSE(Vec
.all());
128 EXPECT_TRUE(Vec
.none());
129 EXPECT_FALSE(Vec
.empty());
131 Inv
= ~SmallBitVector();
132 EXPECT_EQ(0U, Inv
.count());
133 EXPECT_EQ(0U, Inv
.size());
134 EXPECT_FALSE(Inv
.any());
135 EXPECT_TRUE(Inv
.all());
136 EXPECT_TRUE(Inv
.none());
137 EXPECT_TRUE(Inv
.empty());
140 EXPECT_EQ(0U, Vec
.count());
141 EXPECT_EQ(0U, Vec
.size());
142 EXPECT_FALSE(Vec
.any());
143 EXPECT_TRUE(Vec
.all());
144 EXPECT_TRUE(Vec
.none());
145 EXPECT_TRUE(Vec
.empty());
148 TEST(SmallBitVectorTest
, CompoundAssignment
) {
160 EXPECT_TRUE(A
.test(4));
161 EXPECT_TRUE(A
.test(5));
162 EXPECT_TRUE(A
.test(7));
163 EXPECT_TRUE(A
.test(18));
164 EXPECT_EQ(4U, A
.count());
165 EXPECT_EQ(50U, A
.size());
172 EXPECT_FALSE(A
.test(2));
173 EXPECT_FALSE(A
.test(7));
174 EXPECT_EQ(2U, A
.count());
175 EXPECT_EQ(50U, A
.size());
181 EXPECT_TRUE(A
.test(2));
182 EXPECT_TRUE(A
.test(7));
183 EXPECT_EQ(98U, A
.count());
184 EXPECT_EQ(100U, A
.size());
187 TEST(SmallBitVectorTest
, ProxyIndex
) {
188 SmallBitVector
Vec(3);
189 EXPECT_TRUE(Vec
.none());
190 Vec
[0] = Vec
[1] = Vec
[2] = true;
191 EXPECT_EQ(Vec
.size(), Vec
.count());
192 Vec
[2] = Vec
[1] = Vec
[0] = false;
193 EXPECT_TRUE(Vec
.none());