1 //===- llvm/unittest/ADT/BitVectorTest.cpp - BitVector 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 // Some of these tests fail on PowerPC for unknown reasons.
13 #include "llvm/ADT/BitVector.h"
14 #include "gtest/gtest.h"
20 TEST(BitVectorTest
, TrivialOperation
) {
22 EXPECT_EQ(0U, Vec
.count());
23 EXPECT_EQ(0U, Vec
.size());
24 EXPECT_FALSE(Vec
.any());
25 EXPECT_TRUE(Vec
.all());
26 EXPECT_TRUE(Vec
.none());
27 EXPECT_TRUE(Vec
.empty());
30 EXPECT_EQ(5U, Vec
.count());
31 EXPECT_EQ(5U, Vec
.size());
32 EXPECT_TRUE(Vec
.any());
33 EXPECT_TRUE(Vec
.all());
34 EXPECT_FALSE(Vec
.none());
35 EXPECT_FALSE(Vec
.empty());
38 EXPECT_EQ(5U, Vec
.count());
39 EXPECT_EQ(11U, Vec
.size());
40 EXPECT_TRUE(Vec
.any());
41 EXPECT_FALSE(Vec
.all());
42 EXPECT_FALSE(Vec
.none());
43 EXPECT_FALSE(Vec
.empty());
46 EXPECT_EQ(6U, Inv
.count());
47 EXPECT_EQ(11U, Inv
.size());
48 EXPECT_TRUE(Inv
.any());
49 EXPECT_FALSE(Inv
.all());
50 EXPECT_FALSE(Inv
.none());
51 EXPECT_FALSE(Inv
.empty());
53 EXPECT_FALSE(Inv
== Vec
);
54 EXPECT_TRUE(Inv
!= Vec
);
56 EXPECT_TRUE(Inv
== Vec
);
57 EXPECT_FALSE(Inv
!= Vec
);
59 // Add some "interesting" data to Vec.
61 Vec
.resize(25, false);
63 Vec
.resize(29, false);
65 Vec
.resize(57, false);
67 for (unsigned i
= Vec
.find_first(); i
!= -1u; i
= Vec
.find_next(i
)) {
70 EXPECT_TRUE(Vec
.test(i
));
72 EXPECT_EQ(Count
, Vec
.count());
73 EXPECT_EQ(Count
, 23u);
76 EXPECT_FALSE(Vec
[56]);
77 Vec
.resize(61, false);
80 BitVector
Alt(3, false);
83 EXPECT_TRUE(Copy
== Alt
);
84 EXPECT_TRUE(Vec
.size() == 6);
85 EXPECT_TRUE(Vec
.count() == 3);
86 EXPECT_TRUE(Vec
.find_first() == 3);
89 // Add some more "interesting" data.
91 Vec
.resize(78, false);
93 Vec
.resize(90, false);
95 Vec
.resize(130, false);
97 for (unsigned i
= Vec
.find_first(); i
!= -1u; i
= Vec
.find_next(i
)) {
100 EXPECT_TRUE(Vec
.test(i
));
102 EXPECT_EQ(Count
, Vec
.count());
103 EXPECT_EQ(Count
, 42u);
104 EXPECT_FALSE(Vec
[0]);
105 EXPECT_TRUE(Vec
[32]);
106 EXPECT_FALSE(Vec
[60]);
107 EXPECT_FALSE(Vec
[129]);
110 EXPECT_TRUE(Vec
[60]);
111 EXPECT_EQ(Count
+ 1, Vec
.count());
113 EXPECT_FALSE(Vec
[60]);
114 EXPECT_EQ(Count
, Vec
.count());
117 EXPECT_FALSE(Vec
[32]);
118 EXPECT_EQ(Count
- 1, Vec
.count());
120 EXPECT_TRUE(Vec
[32]);
121 EXPECT_EQ(Count
, Vec
.count());
124 EXPECT_EQ(Vec
.size() - Count
, Vec
.count());
127 EXPECT_EQ(0U, Vec
.count());
128 EXPECT_EQ(130U, Vec
.size());
129 EXPECT_FALSE(Vec
.any());
130 EXPECT_FALSE(Vec
.all());
131 EXPECT_TRUE(Vec
.none());
132 EXPECT_FALSE(Vec
.empty());
135 EXPECT_EQ(0U, Inv
.count());
136 EXPECT_EQ(0U, Inv
.size());
137 EXPECT_FALSE(Inv
.any());
138 EXPECT_TRUE(Inv
.all());
139 EXPECT_TRUE(Inv
.none());
140 EXPECT_TRUE(Inv
.empty());
143 EXPECT_EQ(0U, Vec
.count());
144 EXPECT_EQ(0U, Vec
.size());
145 EXPECT_FALSE(Vec
.any());
146 EXPECT_TRUE(Vec
.all());
147 EXPECT_TRUE(Vec
.none());
148 EXPECT_TRUE(Vec
.empty());
151 TEST(BitVectorTest
, CompoundAssignment
) {
163 EXPECT_TRUE(A
.test(4));
164 EXPECT_TRUE(A
.test(5));
165 EXPECT_TRUE(A
.test(7));
166 EXPECT_TRUE(A
.test(18));
167 EXPECT_EQ(4U, A
.count());
168 EXPECT_EQ(50U, A
.size());
175 EXPECT_FALSE(A
.test(2));
176 EXPECT_FALSE(A
.test(7));
177 EXPECT_EQ(2U, A
.count());
178 EXPECT_EQ(50U, A
.size());
184 EXPECT_TRUE(A
.test(2));
185 EXPECT_TRUE(A
.test(7));
186 EXPECT_EQ(98U, A
.count());
187 EXPECT_EQ(100U, A
.size());
190 TEST(BitVectorTest
, ProxyIndex
) {
192 EXPECT_TRUE(Vec
.none());
193 Vec
[0] = Vec
[1] = Vec
[2] = true;
194 EXPECT_EQ(Vec
.size(), Vec
.count());
195 Vec
[2] = Vec
[1] = Vec
[0] = false;
196 EXPECT_TRUE(Vec
.none());