1 //===- llvm/unittest/ADT/BitVectorTest.cpp - BitVector 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/BitVector.h"
10 #include "llvm/ADT/SmallBitVector.h"
11 #include "gtest/gtest.h"
19 class BitVectorTest
: public ::testing::Test
{ };
21 // Test both BitVector and SmallBitVector with the same suite of tests.
22 typedef ::testing::Types
<BitVector
, SmallBitVector
> BitVectorTestTypes
;
23 TYPED_TEST_CASE(BitVectorTest
, BitVectorTestTypes
);
25 TYPED_TEST(BitVectorTest
, TrivialOperation
) {
27 EXPECT_EQ(0U, Vec
.count());
28 EXPECT_EQ(0U, Vec
.size());
29 EXPECT_FALSE(Vec
.any());
30 EXPECT_TRUE(Vec
.all());
31 EXPECT_TRUE(Vec
.none());
32 EXPECT_TRUE(Vec
.empty());
35 EXPECT_EQ(5U, Vec
.count());
36 EXPECT_EQ(5U, Vec
.size());
37 EXPECT_TRUE(Vec
.any());
38 EXPECT_TRUE(Vec
.all());
39 EXPECT_FALSE(Vec
.none());
40 EXPECT_FALSE(Vec
.empty());
43 EXPECT_EQ(5U, Vec
.count());
44 EXPECT_EQ(11U, Vec
.size());
45 EXPECT_TRUE(Vec
.any());
46 EXPECT_FALSE(Vec
.all());
47 EXPECT_FALSE(Vec
.none());
48 EXPECT_FALSE(Vec
.empty());
52 EXPECT_EQ(6U, Inv
.count());
53 EXPECT_EQ(11U, Inv
.size());
54 EXPECT_TRUE(Inv
.any());
55 EXPECT_FALSE(Inv
.all());
56 EXPECT_FALSE(Inv
.none());
57 EXPECT_FALSE(Inv
.empty());
59 EXPECT_FALSE(Inv
== Vec
);
60 EXPECT_TRUE(Inv
!= Vec
);
62 EXPECT_TRUE(Inv
== Vec
);
63 EXPECT_FALSE(Inv
!= Vec
);
65 // Add some "interesting" data to Vec.
67 Vec
.resize(25, false);
69 Vec
.resize(29, false);
71 Vec
.resize(57, false);
73 for (unsigned i
= Vec
.find_first(); i
!= -1u; i
= Vec
.find_next(i
)) {
76 EXPECT_TRUE(Vec
.test(i
));
78 EXPECT_EQ(Count
, Vec
.count());
79 EXPECT_EQ(Count
, 23u);
82 EXPECT_FALSE(Vec
[56]);
83 Vec
.resize(61, false);
86 TypeParam
Alt(3, false);
89 EXPECT_TRUE(Copy
== Alt
);
90 EXPECT_TRUE(Vec
.size() == 6);
91 EXPECT_TRUE(Vec
.count() == 3);
92 EXPECT_TRUE(Vec
.find_first() == 3);
95 // Add some more "interesting" data.
97 Vec
.resize(78, false);
99 Vec
.resize(90, false);
100 Vec
.resize(91, true);
101 Vec
.resize(130, false);
103 for (unsigned i
= Vec
.find_first(); i
!= -1u; i
= Vec
.find_next(i
)) {
106 EXPECT_TRUE(Vec
.test(i
));
108 EXPECT_EQ(Count
, Vec
.count());
109 EXPECT_EQ(Count
, 42u);
110 EXPECT_FALSE(Vec
[0]);
111 EXPECT_TRUE(Vec
[32]);
112 EXPECT_FALSE(Vec
[60]);
113 EXPECT_FALSE(Vec
[129]);
116 EXPECT_TRUE(Vec
[60]);
117 EXPECT_EQ(Count
+ 1, Vec
.count());
119 EXPECT_FALSE(Vec
[60]);
120 EXPECT_EQ(Count
, Vec
.count());
123 EXPECT_FALSE(Vec
[32]);
124 EXPECT_EQ(Count
- 1, Vec
.count());
126 EXPECT_TRUE(Vec
[32]);
127 EXPECT_EQ(Count
, Vec
.count());
130 EXPECT_EQ(Vec
.size() - Count
, Vec
.count());
133 EXPECT_EQ(0U, Vec
.count());
134 EXPECT_EQ(130U, Vec
.size());
135 EXPECT_FALSE(Vec
.any());
136 EXPECT_FALSE(Vec
.all());
137 EXPECT_TRUE(Vec
.none());
138 EXPECT_FALSE(Vec
.empty());
141 EXPECT_EQ(130U, Vec
.count());
142 EXPECT_EQ(130U, Vec
.size());
143 EXPECT_TRUE(Vec
.any());
144 EXPECT_TRUE(Vec
.all());
145 EXPECT_FALSE(Vec
.none());
146 EXPECT_FALSE(Vec
.empty());
149 EXPECT_EQ(64U, Vec
.count());
150 EXPECT_EQ(64U, Vec
.size());
151 EXPECT_TRUE(Vec
.any());
152 EXPECT_TRUE(Vec
.all());
153 EXPECT_FALSE(Vec
.none());
154 EXPECT_FALSE(Vec
.empty());
157 EXPECT_EQ(0U, Vec
.count());
158 EXPECT_EQ(64U, Vec
.size());
159 EXPECT_FALSE(Vec
.any());
160 EXPECT_FALSE(Vec
.all());
161 EXPECT_TRUE(Vec
.none());
162 EXPECT_FALSE(Vec
.empty());
164 Inv
= TypeParam().flip();
165 EXPECT_EQ(0U, Inv
.count());
166 EXPECT_EQ(0U, Inv
.size());
167 EXPECT_FALSE(Inv
.any());
168 EXPECT_TRUE(Inv
.all());
169 EXPECT_TRUE(Inv
.none());
170 EXPECT_TRUE(Inv
.empty());
173 EXPECT_EQ(0U, Vec
.count());
174 EXPECT_EQ(0U, Vec
.size());
175 EXPECT_FALSE(Vec
.any());
176 EXPECT_TRUE(Vec
.all());
177 EXPECT_TRUE(Vec
.none());
178 EXPECT_TRUE(Vec
.empty());
181 TYPED_TEST(BitVectorTest
, SimpleFindOpsMultiWord
) {
184 // Test finding next set and unset bits in a BitVector with multiple words
190 EXPECT_EQ(75, A
.find_last());
191 EXPECT_EQ(12, A
.find_first());
192 EXPECT_EQ(13, A
.find_next(12));
193 EXPECT_EQ(75, A
.find_next(13));
194 EXPECT_EQ(-1, A
.find_next(75));
196 EXPECT_EQ(-1, A
.find_prev(12));
197 EXPECT_EQ(12, A
.find_prev(13));
198 EXPECT_EQ(13, A
.find_prev(75));
199 EXPECT_EQ(75, A
.find_prev(90));
201 EXPECT_EQ(0, A
.find_first_unset());
202 EXPECT_EQ(99, A
.find_last_unset());
203 EXPECT_EQ(14, A
.find_next_unset(11));
204 EXPECT_EQ(14, A
.find_next_unset(12));
205 EXPECT_EQ(14, A
.find_next_unset(13));
206 EXPECT_EQ(16, A
.find_next_unset(15));
207 EXPECT_EQ(76, A
.find_next_unset(74));
208 EXPECT_EQ(76, A
.find_next_unset(75));
209 EXPECT_EQ(-1, A
.find_next_unset(99));
212 EXPECT_EQ(100U, A
.count());
213 EXPECT_EQ(0, A
.find_first());
214 EXPECT_EQ(-1, A
.find_first_unset());
215 EXPECT_EQ(-1, A
.find_last_unset());
216 EXPECT_EQ(99, A
.find_last());
217 EXPECT_EQ(99, A
.find_next(98));
220 EXPECT_EQ(0U, A
.count());
221 EXPECT_EQ(-1, A
.find_first());
222 EXPECT_EQ(-1, A
.find_last());
223 EXPECT_EQ(0, A
.find_first_unset());
224 EXPECT_EQ(99, A
.find_last_unset());
225 EXPECT_EQ(99, A
.find_next_unset(98));
228 // Test finding next set and unset bits in a BitVector/SmallBitVector within a
229 // uintptr_t - check both 32-bit (Multi) and 64-bit (Small) targets.
230 TYPED_TEST(BitVectorTest
, SimpleFindOps64Bit
) {
238 EXPECT_EQ(47, A
.find_last());
239 EXPECT_EQ(12, A
.find_first());
240 EXPECT_EQ(13, A
.find_next(12));
241 EXPECT_EQ(47, A
.find_next(13));
242 EXPECT_EQ(-1, A
.find_next(47));
244 EXPECT_EQ(-1, A
.find_prev(12));
245 EXPECT_EQ(12, A
.find_prev(13));
246 EXPECT_EQ(13, A
.find_prev(47));
247 EXPECT_EQ(47, A
.find_prev(56));
249 EXPECT_EQ(0, A
.find_first_unset());
250 EXPECT_EQ(56, A
.find_last_unset());
251 EXPECT_EQ(14, A
.find_next_unset(11));
252 EXPECT_EQ(14, A
.find_next_unset(12));
253 EXPECT_EQ(14, A
.find_next_unset(13));
254 EXPECT_EQ(16, A
.find_next_unset(15));
255 EXPECT_EQ(48, A
.find_next_unset(46));
256 EXPECT_EQ(48, A
.find_next_unset(47));
257 EXPECT_EQ(-1, A
.find_next_unset(56));
260 // Check if a SmallBitVector is in small mode. This check is used in tests
261 // that run for both SmallBitVector and BitVector. This check doesn't apply
262 // to BitVector so we provide an overload that returns true to get the tests
264 static bool SmallBitVectorIsSmallMode(const SmallBitVector
&bv
) {
267 static bool SmallBitVectorIsSmallMode(const BitVector
&) { return true; }
269 // These tests are intended to exercise the single-word case of BitVector
270 // and the small-mode case of SmallBitVector.
271 TYPED_TEST(BitVectorTest
, SimpleFindOpsSingleWord
) {
272 // Test finding in an empty BitVector.
274 ASSERT_TRUE(SmallBitVectorIsSmallMode(A
));
275 EXPECT_EQ(-1, A
.find_first());
276 EXPECT_EQ(-1, A
.find_last());
277 EXPECT_EQ(-1, A
.find_first_unset());
278 EXPECT_EQ(-1, A
.find_last_unset());
284 ASSERT_TRUE(SmallBitVectorIsSmallMode(A
));
285 EXPECT_EQ(16, A
.find_last());
286 EXPECT_EQ(3, A
.find_first());
287 EXPECT_EQ(3, A
.find_next(1));
288 EXPECT_EQ(4, A
.find_next(3));
289 EXPECT_EQ(16, A
.find_next(4));
290 EXPECT_EQ(-1, A
.find_next(16));
292 EXPECT_EQ(-1, A
.find_prev(3));
293 EXPECT_EQ(3, A
.find_prev(4));
294 EXPECT_EQ(4, A
.find_prev(16));
295 EXPECT_EQ(16, A
.find_prev(18));
297 EXPECT_EQ(0, A
.find_first_unset());
298 EXPECT_EQ(19, A
.find_last_unset());
299 EXPECT_EQ(5, A
.find_next_unset(3));
300 EXPECT_EQ(5, A
.find_next_unset(4));
301 EXPECT_EQ(13, A
.find_next_unset(12));
302 EXPECT_EQ(17, A
.find_next_unset(15));
305 TEST(BitVectorTest
, FindInRangeMultiWord
) {
317 EXPECT_EQ(-1, Vec
.find_first_in(0, 0));
318 EXPECT_EQ(-1, Vec
.find_first_in(24, 24));
319 EXPECT_EQ(-1, Vec
.find_first_in(7, 24));
321 EXPECT_EQ(3, Vec
.find_first_in(0, 10));
322 EXPECT_EQ(4, Vec
.find_first_in(4, 10));
323 EXPECT_EQ(150, Vec
.find_first_in(100, 200));
324 EXPECT_EQ(152, Vec
.find_first_in(151, 200));
325 EXPECT_EQ(154, Vec
.find_first_in(153, 200));
327 EXPECT_EQ(-1, Vec
.find_first_in(155, 200));
329 EXPECT_EQ(199, Vec
.find_first_in(199, 200));
333 EXPECT_EQ(-1, Vec
.find_last_in(0, 0));
334 EXPECT_EQ(-1, Vec
.find_last_in(24, 24));
335 EXPECT_EQ(-1, Vec
.find_last_in(7, 24));
337 EXPECT_EQ(6, Vec
.find_last_in(0, 10));
338 EXPECT_EQ(5, Vec
.find_last_in(0, 6));
339 EXPECT_EQ(154, Vec
.find_last_in(100, 155));
340 EXPECT_EQ(152, Vec
.find_last_in(100, 154));
341 EXPECT_EQ(150, Vec
.find_last_in(100, 152));
342 EXPECT_EQ(-1, Vec
.find_last_in(100, 150));
344 EXPECT_EQ(199, Vec
.find_last_in(199, 200));
348 EXPECT_EQ(-1, Vec
.find_first_unset_in(0, 0));
349 EXPECT_EQ(-1, Vec
.find_first_unset_in(23, 23));
350 EXPECT_EQ(-1, Vec
.find_first_unset_in(24, 35));
352 EXPECT_EQ(0, Vec
.find_first_unset_in(0, 10));
353 EXPECT_EQ(1, Vec
.find_first_unset_in(1, 10));
354 EXPECT_EQ(7, Vec
.find_first_unset_in(5, 25));
355 EXPECT_EQ(151, Vec
.find_first_unset_in(150, 200));
356 EXPECT_EQ(151, Vec
.find_first_unset_in(151, 200));
357 EXPECT_EQ(153, Vec
.find_first_unset_in(152, 200));
358 EXPECT_EQ(153, Vec
.find_first_unset_in(153, 200));
359 EXPECT_EQ(155, Vec
.find_first_unset_in(154, 200));
360 EXPECT_EQ(155, Vec
.find_first_unset_in(155, 200));
361 EXPECT_EQ(199, Vec
.find_first_unset_in(199, 200));
364 EXPECT_EQ(-1, Vec
.find_last_unset_in(0, 0));
365 EXPECT_EQ(-1, Vec
.find_last_unset_in(23, 23));
366 EXPECT_EQ(-1, Vec
.find_last_unset_in(24, 35));
368 EXPECT_EQ(9, Vec
.find_last_unset_in(0, 10));
369 EXPECT_EQ(8, Vec
.find_last_unset_in(0, 9));
370 EXPECT_EQ(2, Vec
.find_last_unset_in(0, 7));
371 EXPECT_EQ(149, Vec
.find_last_unset_in(100, 151));
372 EXPECT_EQ(151, Vec
.find_last_unset_in(100, 152));
373 EXPECT_EQ(151, Vec
.find_last_unset_in(100, 153));
374 EXPECT_EQ(153, Vec
.find_last_unset_in(100, 154));
375 EXPECT_EQ(153, Vec
.find_last_unset_in(100, 155));
376 EXPECT_EQ(155, Vec
.find_last_unset_in(100, 156));
377 EXPECT_EQ(199, Vec
.find_last_unset_in(199, 200));
380 TEST(BitVectorTest
, FindInRangeSingleWord
) {
381 // When the bit vector contains only a single word, this is slightly different
382 // than when the bit vector contains multiple words, because masks are applied
383 // to the front and back of the same word. So make sure this works.
395 EXPECT_EQ(-1, Vec
.find_first_in(0, 0));
396 EXPECT_EQ(-1, Vec
.find_first_in(24, 24));
397 EXPECT_EQ(-1, Vec
.find_first_in(9, 12));
399 EXPECT_EQ(2, Vec
.find_first_in(0, 10));
400 EXPECT_EQ(6, Vec
.find_first_in(4, 10));
401 EXPECT_EQ(19, Vec
.find_first_in(18, 25));
402 EXPECT_EQ(21, Vec
.find_first_in(20, 25));
403 EXPECT_EQ(23, Vec
.find_first_in(22, 25));
404 EXPECT_EQ(-1, Vec
.find_first_in(24, 25));
407 EXPECT_EQ(-1, Vec
.find_last_in(0, 0));
408 EXPECT_EQ(-1, Vec
.find_last_in(24, 24));
409 EXPECT_EQ(-1, Vec
.find_last_in(9, 12));
411 EXPECT_EQ(8, Vec
.find_last_in(0, 10));
412 EXPECT_EQ(3, Vec
.find_last_in(0, 6));
413 EXPECT_EQ(23, Vec
.find_last_in(18, 25));
414 EXPECT_EQ(21, Vec
.find_last_in(18, 23));
415 EXPECT_EQ(19, Vec
.find_last_in(18, 21));
416 EXPECT_EQ(-1, Vec
.find_last_in(18, 19));
419 EXPECT_EQ(-1, Vec
.find_first_unset_in(0, 0));
420 EXPECT_EQ(-1, Vec
.find_first_unset_in(23, 23));
421 EXPECT_EQ(-1, Vec
.find_first_unset_in(6, 9));
423 EXPECT_EQ(0, Vec
.find_first_unset_in(0, 6));
424 EXPECT_EQ(1, Vec
.find_first_unset_in(1, 6));
425 EXPECT_EQ(9, Vec
.find_first_unset_in(7, 13));
426 EXPECT_EQ(18, Vec
.find_first_unset_in(18, 25));
427 EXPECT_EQ(20, Vec
.find_first_unset_in(19, 25));
428 EXPECT_EQ(20, Vec
.find_first_unset_in(20, 25));
429 EXPECT_EQ(22, Vec
.find_first_unset_in(21, 25));
430 EXPECT_EQ(22, Vec
.find_first_unset_in(22, 25));
431 EXPECT_EQ(24, Vec
.find_first_unset_in(23, 25));
432 EXPECT_EQ(24, Vec
.find_first_unset_in(24, 25));
435 EXPECT_EQ(-1, Vec
.find_last_unset_in(0, 0));
436 EXPECT_EQ(-1, Vec
.find_last_unset_in(23, 23));
437 EXPECT_EQ(-1, Vec
.find_last_unset_in(6, 9));
439 EXPECT_EQ(5, Vec
.find_last_unset_in(0, 6));
440 EXPECT_EQ(4, Vec
.find_last_unset_in(0, 5));
441 EXPECT_EQ(1, Vec
.find_last_unset_in(0, 4));
442 EXPECT_EQ(11, Vec
.find_last_unset_in(7, 13));
443 EXPECT_EQ(24, Vec
.find_last_unset_in(18, 25));
444 EXPECT_EQ(22, Vec
.find_last_unset_in(18, 24));
445 EXPECT_EQ(22, Vec
.find_last_unset_in(18, 23));
446 EXPECT_EQ(20, Vec
.find_last_unset_in(18, 22));
447 EXPECT_EQ(20, Vec
.find_last_unset_in(18, 21));
448 EXPECT_EQ(18, Vec
.find_last_unset_in(18, 20));
449 EXPECT_EQ(18, Vec
.find_last_unset_in(18, 19));
452 TYPED_TEST(BitVectorTest
, CompoundAssignment
) {
464 EXPECT_TRUE(A
.test(4));
465 EXPECT_TRUE(A
.test(5));
466 EXPECT_TRUE(A
.test(7));
467 EXPECT_TRUE(A
.test(18));
468 EXPECT_EQ(4U, A
.count());
469 EXPECT_EQ(50U, A
.size());
476 EXPECT_FALSE(A
.test(2));
477 EXPECT_FALSE(A
.test(7));
478 EXPECT_TRUE(A
.test(4));
479 EXPECT_TRUE(A
.test(5));
480 EXPECT_EQ(2U, A
.count());
481 EXPECT_EQ(50U, A
.size());
487 EXPECT_TRUE(A
.test(2));
488 EXPECT_TRUE(A
.test(7));
489 EXPECT_EQ(98U, A
.count());
490 EXPECT_EQ(100U, A
.size());
493 // Test SmallBitVector operations with mixed big/small representations
494 TYPED_TEST(BitVectorTest
, MixedBigSmall
) {
510 EXPECT_TRUE(Small
.test(0));
511 EXPECT_EQ(1u, Small
.count());
512 // FIXME BitVector and SmallBitVector behave differently here.
513 // SmallBitVector resizes the LHS to max(LHS.size(), RHS.size())
514 // but BitVector does not.
515 // EXPECT_EQ(20u, Small.size());
533 EXPECT_TRUE(Big
.test(0));
534 EXPECT_EQ(1u, Big
.count());
535 // FIXME BitVector and SmallBitVector behave differently here.
536 // SmallBitVector resizes the LHS to max(LHS.size(), RHS.size())
537 // but BitVector does not.
538 // EXPECT_EQ(20u, Big.size());
556 EXPECT_TRUE(Small
.test(0));
557 EXPECT_TRUE(Small
.test(1));
558 EXPECT_TRUE(Small
.test(2));
559 EXPECT_TRUE(Small
.test(16));
560 EXPECT_EQ(4u, Small
.count());
561 EXPECT_EQ(20u, Small
.size());
579 EXPECT_TRUE(Big
.test(0));
580 EXPECT_TRUE(Big
.test(1));
581 EXPECT_TRUE(Big
.test(2));
582 EXPECT_TRUE(Big
.test(16));
583 EXPECT_EQ(4u, Big
.count());
584 EXPECT_EQ(20u, Big
.size());
602 EXPECT_TRUE(Small
.test(1));
603 EXPECT_TRUE(Small
.test(2));
604 EXPECT_TRUE(Small
.test(16));
605 EXPECT_EQ(3u, Small
.count());
606 EXPECT_EQ(20u, Small
.size());
624 EXPECT_TRUE(Big
.test(1));
625 EXPECT_TRUE(Big
.test(2));
626 EXPECT_TRUE(Big
.test(16));
627 EXPECT_EQ(3u, Big
.count());
628 EXPECT_EQ(20u, Big
.size());
646 EXPECT_TRUE(Small
.test(1));
647 EXPECT_EQ(1u, Small
.count());
648 EXPECT_EQ(10u, Small
.size());
666 EXPECT_TRUE(Big
.test(2));
667 EXPECT_TRUE(Big
.test(16));
668 EXPECT_EQ(2u, Big
.count());
669 EXPECT_EQ(20u, Big
.size());
684 EXPECT_FALSE(Big
== Small
);
685 EXPECT_FALSE(Small
== Big
);
687 EXPECT_TRUE(Big
== Small
);
688 EXPECT_TRUE(Small
== Big
);
702 EXPECT_FALSE(Small
.anyCommon(Big
));
703 EXPECT_FALSE(Big
.anyCommon(Small
));
705 EXPECT_TRUE(Small
.anyCommon(Big
));
706 EXPECT_TRUE(Big
.anyCommon(Small
));
721 EXPECT_TRUE(Small
.test(Big
));
722 EXPECT_FALSE(Big
.test(Small
));
724 EXPECT_FALSE(Small
.test(Big
));
725 EXPECT_FALSE(Big
.test(Small
));
729 TYPED_TEST(BitVectorTest
, ProxyIndex
) {
731 EXPECT_TRUE(Vec
.none());
732 Vec
[0] = Vec
[1] = Vec
[2] = true;
733 EXPECT_EQ(Vec
.size(), Vec
.count());
734 Vec
[2] = Vec
[1] = Vec
[0] = false;
735 EXPECT_TRUE(Vec
.none());
738 TYPED_TEST(BitVectorTest
, PortableBitMask
) {
740 const uint32_t Mask1
[] = { 0x80000000, 6, 5 };
743 A
.setBitsInMask(Mask1
, 1);
744 EXPECT_EQ(10u, A
.size());
745 EXPECT_FALSE(A
.test(0));
748 A
.setBitsInMask(Mask1
, 1);
749 EXPECT_FALSE(A
.test(0));
750 EXPECT_TRUE(A
.test(31));
751 EXPECT_EQ(1u, A
.count());
754 A
.setBitsInMask(Mask1
, 1);
755 EXPECT_EQ(1u, A
.count());
756 A
.setBitsInMask(Mask1
, 2);
757 EXPECT_EQ(1u, A
.count());
760 A
.setBitsInMask(Mask1
, 2);
761 EXPECT_EQ(2u, A
.count());
764 A
.setBitsInMask(Mask1
, 3);
765 EXPECT_EQ(4u, A
.count());
767 A
.setBitsNotInMask(Mask1
, 1);
768 EXPECT_EQ(32u+3u, A
.count());
770 A
.setBitsNotInMask(Mask1
, 3);
771 EXPECT_EQ(65u, A
.count());
774 EXPECT_EQ(65u, A
.count());
778 A
.setBitsNotInMask(Mask1
, 3);
779 EXPECT_EQ(96u-5u, A
.count());
781 A
.clearBitsNotInMask(Mask1
, 1);
782 EXPECT_EQ(64-4u, A
.count());
785 TYPED_TEST(BitVectorTest
, BinOps
) {
790 EXPECT_FALSE(A
.anyCommon(B
));
791 EXPECT_FALSE(B
.anyCommon(B
));
795 EXPECT_FALSE(A
.anyCommon(B
));
796 EXPECT_FALSE(B
.anyCommon(A
));
799 EXPECT_FALSE(A
.anyCommon(B
));
800 EXPECT_FALSE(B
.anyCommon(A
));
803 EXPECT_TRUE(A
.anyCommon(B
));
804 EXPECT_TRUE(B
.anyCommon(A
));
810 EXPECT_FALSE(A
.anyCommon(B
));
811 EXPECT_FALSE(B
.anyCommon(A
));
814 typedef std::vector
<std::pair
<int, int>> RangeList
;
816 template <typename VecType
>
817 static inline VecType
createBitVector(uint32_t Size
,
818 const RangeList
&setRanges
) {
821 for (auto &R
: setRanges
)
822 V
.set(R
.first
, R
.second
);
826 TYPED_TEST(BitVectorTest
, ShiftOpsSingleWord
) {
827 // Test that shift ops work when the desired shift amount is less
830 // 1. Case where the number of bits in the BitVector also fit into a single
832 TypeParam A
= createBitVector
<TypeParam
>(12, {{2, 4}, {8, 10}});
835 EXPECT_EQ(4U, A
.count());
836 EXPECT_TRUE(A
.test(2));
837 EXPECT_TRUE(A
.test(3));
838 EXPECT_TRUE(A
.test(8));
839 EXPECT_TRUE(A
.test(9));
842 EXPECT_EQ(createBitVector
<TypeParam
>(12, {{1, 3}, {7, 9}}), A
);
848 EXPECT_EQ(createBitVector
<TypeParam
>(12, {}), A
);
852 EXPECT_EQ(createBitVector
<TypeParam
>(12, {}), A
);
854 // 2. Case where the number of bits in the BitVector do not fit into a single
857 // 31----------------------------------------------------------------------0
858 // XXXXXXXX XXXXXXXX XXXXXXXX 00000111 | 11111110 00000000 00001111 11111111
859 A
= createBitVector
<TypeParam
>(40, {{0, 12}, {25, 35}});
860 EXPECT_EQ(40U, A
.size());
861 EXPECT_EQ(22U, A
.count());
863 // 2a. Make sure that left shifting some 1 bits out of the vector works.
864 // 31----------------------------------------------------------------------0
866 // XXXXXXXX XXXXXXXX XXXXXXXX 00000111 | 11111110 00000000 00001111 11111111
868 // XXXXXXXX XXXXXXXX XXXXXXXX 11111100 | 00000000 00011111 11111110 00000000
870 EXPECT_EQ(createBitVector
<TypeParam
>(40, {{9, 21}, {34, 40}}), A
);
872 // 2b. Make sure that keeping the number of one bits unchanged works.
873 // 31----------------------------------------------------------------------0
875 // XXXXXXXX XXXXXXXX XXXXXXXX 11111100 | 00000000 00011111 11111110 00000000
877 // XXXXXXXX XXXXXXXX XXXXXXXX 00000011 | 11110000 00000000 01111111 11111000
879 EXPECT_EQ(createBitVector
<TypeParam
>(40, {{3, 15}, {28, 34}}), A
);
881 // 2c. Make sure that right shifting some 1 bits out of the vector works.
882 // 31----------------------------------------------------------------------0
884 // XXXXXXXX XXXXXXXX XXXXXXXX 00000011 | 11110000 00000000 01111111 11111000
886 // XXXXXXXX XXXXXXXX XXXXXXXX 00000000 | 00000000 11111100 00000000 00011111
888 EXPECT_EQ(createBitVector
<TypeParam
>(40, {{0, 5}, {18, 24}}), A
);
891 A
= createBitVector
<TypeParam
>(300, {{1, 30}, {60, 95}, {200, 275}});
893 EXPECT_EQ(createBitVector
<TypeParam
>(
894 300, {{1 + 29, 30 + 29}, {60 + 29, 95 + 29}, {200 + 29, 300}}),
898 TYPED_TEST(BitVectorTest
, ShiftOpsMultiWord
) {
899 // Test that shift ops work when the desired shift amount is greater than or
900 // equal to the size of a single word.
901 auto A
= createBitVector
<TypeParam
>(300, {{1, 30}, {60, 95}, {200, 275}});
903 // Make a copy so we can re-use it later.
906 // 1. Shift left by an exact multiple of the word size. This should invoke
907 // only a memmove and no per-word bit operations.
909 auto Expected
= createBitVector
<TypeParam
>(
910 300, {{1 + 64, 30 + 64}, {60 + 64, 95 + 64}, {200 + 64, 300}});
911 EXPECT_EQ(Expected
, A
);
913 // 2. Shift left by a non multiple of the word size. This should invoke both
914 // a memmove and per-word bit operations.
917 EXPECT_EQ(createBitVector
<TypeParam
>(
918 300, {{1 + 93, 30 + 93}, {60 + 93, 95 + 93}, {200 + 93, 300}}),
921 // 1. Shift right by an exact multiple of the word size. This should invoke
922 // only a memmove and no per-word bit operations.
926 createBitVector
<TypeParam
>(300, {{0, 95 - 64}, {200 - 64, 275 - 64}}), A
);
928 // 2. Shift left by a non multiple of the word size. This should invoke both
929 // a memmove and per-word bit operations.
933 createBitVector
<TypeParam
>(300, {{0, 95 - 93}, {200 - 93, 275 - 93}}), A
);
936 TYPED_TEST(BitVectorTest
, RangeOps
) {
942 EXPECT_FALSE(A
.test(0));
943 EXPECT_TRUE( A
.test(1));
944 EXPECT_TRUE( A
.test(23));
945 EXPECT_TRUE( A
.test(254));
946 EXPECT_FALSE(A
.test(255));
953 EXPECT_TRUE( B
.test(0));
954 EXPECT_FALSE(B
.test(1));
955 EXPECT_FALSE(B
.test(23));
956 EXPECT_FALSE(B
.test(254));
957 EXPECT_TRUE( B
.test(255));
964 EXPECT_TRUE(C
.test(0));
965 EXPECT_FALSE( C
.test(1));
966 EXPECT_FALSE( C
.test(2));
973 EXPECT_FALSE(D
.test(0));
974 EXPECT_TRUE( D
.test(1));
975 EXPECT_TRUE( D
.test(2));
982 EXPECT_FALSE(E
.test(0));
983 EXPECT_TRUE( E
.test(1));
984 EXPECT_TRUE( E
.test(32));
985 EXPECT_FALSE(E
.test(33));
987 TypeParam BufferOverrun
;
988 unsigned size
= sizeof(unsigned long) * 8;
989 BufferOverrun
.resize(size
);
990 BufferOverrun
.reset(0, size
);
991 BufferOverrun
.set(0, size
);
994 TYPED_TEST(BitVectorTest
, CompoundTestReset
) {
995 TypeParam
A(50, true);
996 TypeParam
B(50, false);
998 TypeParam
C(100, true);
999 TypeParam
D(100, false);
1001 EXPECT_FALSE(A
.test(A
));
1002 EXPECT_TRUE(A
.test(B
));
1003 EXPECT_FALSE(A
.test(C
));
1004 EXPECT_TRUE(A
.test(D
));
1005 EXPECT_FALSE(B
.test(A
));
1006 EXPECT_FALSE(B
.test(B
));
1007 EXPECT_FALSE(B
.test(C
));
1008 EXPECT_FALSE(B
.test(D
));
1009 EXPECT_TRUE(C
.test(A
));
1010 EXPECT_TRUE(C
.test(B
));
1011 EXPECT_FALSE(C
.test(C
));
1012 EXPECT_TRUE(C
.test(D
));
1016 EXPECT_TRUE(A
.all());
1018 EXPECT_TRUE(A
.none());
1021 EXPECT_TRUE(A
.none());
1025 EXPECT_EQ(50, C
.find_first());
1027 EXPECT_TRUE(C
.none());
1030 TYPED_TEST(BitVectorTest
, MoveConstructor
) {
1031 TypeParam
A(10, true);
1032 TypeParam
B(std::move(A
));
1033 // Check that the move ctor leaves the moved-from object in a valid state.
1034 // The following line used to crash.
1037 TypeParam
C(10, true);
1042 TYPED_TEST(BitVectorTest
, MoveAssignment
) {
1043 TypeParam
A(10, true);
1046 // Check that move assignment leaves the moved-from object in a valid state.
1047 // The following line used to crash.
1050 TypeParam
C(10, true);
1055 template<class TypeParam
>
1056 static void testEmpty(const TypeParam
&A
) {
1057 EXPECT_TRUE(A
.empty());
1058 EXPECT_EQ((size_t)0, A
.size());
1059 EXPECT_EQ((size_t)0, A
.count());
1060 EXPECT_FALSE(A
.any());
1061 EXPECT_TRUE(A
.all());
1062 EXPECT_TRUE(A
.none());
1063 EXPECT_EQ(-1, A
.find_first());
1064 EXPECT_EQ(A
, TypeParam());
1067 /// Tests whether BitVector behaves well with Bits==nullptr, Capacity==0
1068 TYPED_TEST(BitVectorTest
, EmptyVector
) {
1092 TYPED_TEST(BitVectorTest
, Iterators
) {
1093 TypeParam
Filled(10, true);
1094 EXPECT_NE(Filled
.set_bits_begin(), Filled
.set_bits_end());
1095 unsigned Counter
= 0;
1096 for (unsigned Bit
: Filled
.set_bits())
1097 EXPECT_EQ(Bit
, Counter
++);
1100 EXPECT_EQ(Empty
.set_bits_begin(), Empty
.set_bits_end());
1101 for (unsigned Bit
: Empty
.set_bits()) {
1106 TypeParam
ToFill(100, false);
1108 EXPECT_NE(ToFill
.set_bits_begin(), ToFill
.set_bits_end());
1109 EXPECT_EQ(++ToFill
.set_bits_begin(), ToFill
.set_bits_end());
1110 EXPECT_EQ(*ToFill
.set_bits_begin(), 0U);
1112 EXPECT_EQ(ToFill
.set_bits_begin(), ToFill
.set_bits_end());
1114 const unsigned List
[] = {1, 10, 25, 99};
1115 for (unsigned Num
: List
)
1118 for (unsigned Bit
: ToFill
.set_bits())
1119 EXPECT_EQ(List
[i
++], Bit
);
1122 TYPED_TEST(BitVectorTest
, PushBack
) {
1123 TypeParam
Vec(10, false);
1124 EXPECT_EQ(-1, Vec
.find_first());
1125 EXPECT_EQ(10U, Vec
.size());
1126 EXPECT_EQ(0U, Vec
.count());
1128 Vec
.push_back(true);
1129 EXPECT_EQ(10, Vec
.find_first());
1130 EXPECT_EQ(11U, Vec
.size());
1131 EXPECT_EQ(1U, Vec
.count());
1133 Vec
.push_back(false);
1134 EXPECT_EQ(10, Vec
.find_first());
1135 EXPECT_EQ(12U, Vec
.size());
1136 EXPECT_EQ(1U, Vec
.count());
1138 Vec
.push_back(true);
1139 EXPECT_EQ(10, Vec
.find_first());
1140 EXPECT_EQ(13U, Vec
.size());
1141 EXPECT_EQ(2U, Vec
.count());
1143 // Add a lot of values to cause reallocation.
1144 for (int i
= 0; i
!= 100; ++i
) {
1145 Vec
.push_back(true);
1146 Vec
.push_back(false);
1148 EXPECT_EQ(10, Vec
.find_first());
1149 EXPECT_EQ(213U, Vec
.size());
1150 EXPECT_EQ(102U, Vec
.count());