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 // Some of these tests fail on PowerPC for unknown reasons.
12 #include "llvm/ADT/BitVector.h"
13 #include "llvm/ADT/SmallBitVector.h"
14 #include "gtest/gtest.h"
22 class BitVectorTest
: public ::testing::Test
{ };
24 // Test both BitVector and SmallBitVector with the same suite of tests.
25 typedef ::testing::Types
<BitVector
, SmallBitVector
> BitVectorTestTypes
;
26 TYPED_TEST_CASE(BitVectorTest
, BitVectorTestTypes
);
28 TYPED_TEST(BitVectorTest
, TrivialOperation
) {
30 EXPECT_EQ(0U, Vec
.count());
31 EXPECT_EQ(0U, Vec
.size());
32 EXPECT_FALSE(Vec
.any());
33 EXPECT_TRUE(Vec
.all());
34 EXPECT_TRUE(Vec
.none());
35 EXPECT_TRUE(Vec
.empty());
38 EXPECT_EQ(5U, Vec
.count());
39 EXPECT_EQ(5U, Vec
.size());
40 EXPECT_TRUE(Vec
.any());
41 EXPECT_TRUE(Vec
.all());
42 EXPECT_FALSE(Vec
.none());
43 EXPECT_FALSE(Vec
.empty());
46 EXPECT_EQ(5U, Vec
.count());
47 EXPECT_EQ(11U, Vec
.size());
48 EXPECT_TRUE(Vec
.any());
49 EXPECT_FALSE(Vec
.all());
50 EXPECT_FALSE(Vec
.none());
51 EXPECT_FALSE(Vec
.empty());
55 EXPECT_EQ(6U, Inv
.count());
56 EXPECT_EQ(11U, Inv
.size());
57 EXPECT_TRUE(Inv
.any());
58 EXPECT_FALSE(Inv
.all());
59 EXPECT_FALSE(Inv
.none());
60 EXPECT_FALSE(Inv
.empty());
62 EXPECT_FALSE(Inv
== Vec
);
63 EXPECT_TRUE(Inv
!= Vec
);
65 EXPECT_TRUE(Inv
== Vec
);
66 EXPECT_FALSE(Inv
!= Vec
);
68 // Add some "interesting" data to Vec.
70 Vec
.resize(25, false);
72 Vec
.resize(29, false);
74 Vec
.resize(57, false);
76 for (unsigned i
= Vec
.find_first(); i
!= -1u; i
= Vec
.find_next(i
)) {
79 EXPECT_TRUE(Vec
.test(i
));
81 EXPECT_EQ(Count
, Vec
.count());
82 EXPECT_EQ(Count
, 23u);
85 EXPECT_FALSE(Vec
[56]);
86 Vec
.resize(61, false);
89 TypeParam
Alt(3, false);
92 EXPECT_TRUE(Copy
== Alt
);
93 EXPECT_TRUE(Vec
.size() == 6);
94 EXPECT_TRUE(Vec
.count() == 3);
95 EXPECT_TRUE(Vec
.find_first() == 3);
98 // Add some more "interesting" data.
100 Vec
.resize(78, false);
101 Vec
.resize(89, true);
102 Vec
.resize(90, false);
103 Vec
.resize(91, true);
104 Vec
.resize(130, false);
106 for (unsigned i
= Vec
.find_first(); i
!= -1u; i
= Vec
.find_next(i
)) {
109 EXPECT_TRUE(Vec
.test(i
));
111 EXPECT_EQ(Count
, Vec
.count());
112 EXPECT_EQ(Count
, 42u);
113 EXPECT_FALSE(Vec
[0]);
114 EXPECT_TRUE(Vec
[32]);
115 EXPECT_FALSE(Vec
[60]);
116 EXPECT_FALSE(Vec
[129]);
119 EXPECT_TRUE(Vec
[60]);
120 EXPECT_EQ(Count
+ 1, Vec
.count());
122 EXPECT_FALSE(Vec
[60]);
123 EXPECT_EQ(Count
, Vec
.count());
126 EXPECT_FALSE(Vec
[32]);
127 EXPECT_EQ(Count
- 1, Vec
.count());
129 EXPECT_TRUE(Vec
[32]);
130 EXPECT_EQ(Count
, Vec
.count());
133 EXPECT_EQ(Vec
.size() - Count
, Vec
.count());
136 EXPECT_EQ(0U, Vec
.count());
137 EXPECT_EQ(130U, Vec
.size());
138 EXPECT_FALSE(Vec
.any());
139 EXPECT_FALSE(Vec
.all());
140 EXPECT_TRUE(Vec
.none());
141 EXPECT_FALSE(Vec
.empty());
144 EXPECT_EQ(130U, Vec
.count());
145 EXPECT_EQ(130U, Vec
.size());
146 EXPECT_TRUE(Vec
.any());
147 EXPECT_TRUE(Vec
.all());
148 EXPECT_FALSE(Vec
.none());
149 EXPECT_FALSE(Vec
.empty());
152 EXPECT_EQ(64U, Vec
.count());
153 EXPECT_EQ(64U, Vec
.size());
154 EXPECT_TRUE(Vec
.any());
155 EXPECT_TRUE(Vec
.all());
156 EXPECT_FALSE(Vec
.none());
157 EXPECT_FALSE(Vec
.empty());
160 EXPECT_EQ(0U, Vec
.count());
161 EXPECT_EQ(64U, Vec
.size());
162 EXPECT_FALSE(Vec
.any());
163 EXPECT_FALSE(Vec
.all());
164 EXPECT_TRUE(Vec
.none());
165 EXPECT_FALSE(Vec
.empty());
167 Inv
= TypeParam().flip();
168 EXPECT_EQ(0U, Inv
.count());
169 EXPECT_EQ(0U, Inv
.size());
170 EXPECT_FALSE(Inv
.any());
171 EXPECT_TRUE(Inv
.all());
172 EXPECT_TRUE(Inv
.none());
173 EXPECT_TRUE(Inv
.empty());
176 EXPECT_EQ(0U, Vec
.count());
177 EXPECT_EQ(0U, Vec
.size());
178 EXPECT_FALSE(Vec
.any());
179 EXPECT_TRUE(Vec
.all());
180 EXPECT_TRUE(Vec
.none());
181 EXPECT_TRUE(Vec
.empty());
184 TYPED_TEST(BitVectorTest
, SimpleFindOpsMultiWord
) {
187 // Test finding next set and unset bits in a BitVector with multiple words
193 EXPECT_EQ(75, A
.find_last());
194 EXPECT_EQ(12, A
.find_first());
195 EXPECT_EQ(13, A
.find_next(12));
196 EXPECT_EQ(75, A
.find_next(13));
197 EXPECT_EQ(-1, A
.find_next(75));
199 EXPECT_EQ(-1, A
.find_prev(12));
200 EXPECT_EQ(12, A
.find_prev(13));
201 EXPECT_EQ(13, A
.find_prev(75));
202 EXPECT_EQ(75, A
.find_prev(90));
204 EXPECT_EQ(0, A
.find_first_unset());
205 EXPECT_EQ(99, A
.find_last_unset());
206 EXPECT_EQ(14, A
.find_next_unset(11));
207 EXPECT_EQ(14, A
.find_next_unset(12));
208 EXPECT_EQ(14, A
.find_next_unset(13));
209 EXPECT_EQ(16, A
.find_next_unset(15));
210 EXPECT_EQ(76, A
.find_next_unset(74));
211 EXPECT_EQ(76, A
.find_next_unset(75));
212 EXPECT_EQ(-1, A
.find_next_unset(99));
215 EXPECT_EQ(100U, A
.count());
216 EXPECT_EQ(0, A
.find_first());
217 EXPECT_EQ(-1, A
.find_first_unset());
218 EXPECT_EQ(-1, A
.find_last_unset());
219 EXPECT_EQ(99, A
.find_last());
220 EXPECT_EQ(99, A
.find_next(98));
223 EXPECT_EQ(0U, A
.count());
224 EXPECT_EQ(-1, A
.find_first());
225 EXPECT_EQ(-1, A
.find_last());
226 EXPECT_EQ(0, A
.find_first_unset());
227 EXPECT_EQ(99, A
.find_last_unset());
228 EXPECT_EQ(99, A
.find_next_unset(98));
231 // Check if a SmallBitVector is in small mode. This check is used in tests
232 // that run for both SmallBitVector and BitVector. This check doesn't apply
233 // to BitVector so we provide an overload that returns true to get the tests
235 static bool SmallBitVectorIsSmallMode(const SmallBitVector
&bv
) {
238 static bool SmallBitVectorIsSmallMode(const BitVector
&) { return true; }
240 // These tests are intended to exercise the single-word case of BitVector
241 // and the small-mode case of SmallBitVector.
242 TYPED_TEST(BitVectorTest
, SimpleFindOpsSingleWord
) {
243 // Test finding in an empty BitVector.
245 ASSERT_TRUE(SmallBitVectorIsSmallMode(A
));
246 EXPECT_EQ(-1, A
.find_first());
247 EXPECT_EQ(-1, A
.find_last());
248 EXPECT_EQ(-1, A
.find_first_unset());
249 EXPECT_EQ(-1, A
.find_last_unset());
255 ASSERT_TRUE(SmallBitVectorIsSmallMode(A
));
256 EXPECT_EQ(16, A
.find_last());
257 EXPECT_EQ(3, A
.find_first());
258 EXPECT_EQ(3, A
.find_next(1));
259 EXPECT_EQ(4, A
.find_next(3));
260 EXPECT_EQ(16, A
.find_next(4));
261 EXPECT_EQ(-1, A
.find_next(16));
263 EXPECT_EQ(-1, A
.find_prev(3));
264 EXPECT_EQ(3, A
.find_prev(4));
265 EXPECT_EQ(4, A
.find_prev(16));
266 EXPECT_EQ(16, A
.find_prev(18));
268 EXPECT_EQ(0, A
.find_first_unset());
269 EXPECT_EQ(19, A
.find_last_unset());
270 EXPECT_EQ(5, A
.find_next_unset(3));
271 EXPECT_EQ(5, A
.find_next_unset(4));
272 EXPECT_EQ(13, A
.find_next_unset(12));
273 EXPECT_EQ(17, A
.find_next_unset(15));
276 TEST(BitVectorTest
, FindInRangeMultiWord
) {
288 EXPECT_EQ(-1, Vec
.find_first_in(0, 0));
289 EXPECT_EQ(-1, Vec
.find_first_in(24, 24));
290 EXPECT_EQ(-1, Vec
.find_first_in(7, 24));
292 EXPECT_EQ(3, Vec
.find_first_in(0, 10));
293 EXPECT_EQ(4, Vec
.find_first_in(4, 10));
294 EXPECT_EQ(150, Vec
.find_first_in(100, 200));
295 EXPECT_EQ(152, Vec
.find_first_in(151, 200));
296 EXPECT_EQ(154, Vec
.find_first_in(153, 200));
298 EXPECT_EQ(-1, Vec
.find_first_in(155, 200));
300 EXPECT_EQ(199, Vec
.find_first_in(199, 200));
304 EXPECT_EQ(-1, Vec
.find_last_in(0, 0));
305 EXPECT_EQ(-1, Vec
.find_last_in(24, 24));
306 EXPECT_EQ(-1, Vec
.find_last_in(7, 24));
308 EXPECT_EQ(6, Vec
.find_last_in(0, 10));
309 EXPECT_EQ(5, Vec
.find_last_in(0, 6));
310 EXPECT_EQ(154, Vec
.find_last_in(100, 155));
311 EXPECT_EQ(152, Vec
.find_last_in(100, 154));
312 EXPECT_EQ(150, Vec
.find_last_in(100, 152));
313 EXPECT_EQ(-1, Vec
.find_last_in(100, 150));
315 EXPECT_EQ(199, Vec
.find_last_in(199, 200));
319 EXPECT_EQ(-1, Vec
.find_first_unset_in(0, 0));
320 EXPECT_EQ(-1, Vec
.find_first_unset_in(23, 23));
321 EXPECT_EQ(-1, Vec
.find_first_unset_in(24, 35));
323 EXPECT_EQ(0, Vec
.find_first_unset_in(0, 10));
324 EXPECT_EQ(1, Vec
.find_first_unset_in(1, 10));
325 EXPECT_EQ(7, Vec
.find_first_unset_in(5, 25));
326 EXPECT_EQ(151, Vec
.find_first_unset_in(150, 200));
327 EXPECT_EQ(151, Vec
.find_first_unset_in(151, 200));
328 EXPECT_EQ(153, Vec
.find_first_unset_in(152, 200));
329 EXPECT_EQ(153, Vec
.find_first_unset_in(153, 200));
330 EXPECT_EQ(155, Vec
.find_first_unset_in(154, 200));
331 EXPECT_EQ(155, Vec
.find_first_unset_in(155, 200));
332 EXPECT_EQ(199, Vec
.find_first_unset_in(199, 200));
335 EXPECT_EQ(-1, Vec
.find_last_unset_in(0, 0));
336 EXPECT_EQ(-1, Vec
.find_last_unset_in(23, 23));
337 EXPECT_EQ(-1, Vec
.find_last_unset_in(24, 35));
339 EXPECT_EQ(9, Vec
.find_last_unset_in(0, 10));
340 EXPECT_EQ(8, Vec
.find_last_unset_in(0, 9));
341 EXPECT_EQ(2, Vec
.find_last_unset_in(0, 7));
342 EXPECT_EQ(149, Vec
.find_last_unset_in(100, 151));
343 EXPECT_EQ(151, Vec
.find_last_unset_in(100, 152));
344 EXPECT_EQ(151, Vec
.find_last_unset_in(100, 153));
345 EXPECT_EQ(153, Vec
.find_last_unset_in(100, 154));
346 EXPECT_EQ(153, Vec
.find_last_unset_in(100, 155));
347 EXPECT_EQ(155, Vec
.find_last_unset_in(100, 156));
348 EXPECT_EQ(199, Vec
.find_last_unset_in(199, 200));
351 TEST(BitVectorTest
, FindInRangeSingleWord
) {
352 // When the bit vector contains only a single word, this is slightly different
353 // than when the bit vector contains multiple words, because masks are applied
354 // to the front and back of the same word. So make sure this works.
366 EXPECT_EQ(-1, Vec
.find_first_in(0, 0));
367 EXPECT_EQ(-1, Vec
.find_first_in(24, 24));
368 EXPECT_EQ(-1, Vec
.find_first_in(9, 12));
370 EXPECT_EQ(2, Vec
.find_first_in(0, 10));
371 EXPECT_EQ(6, Vec
.find_first_in(4, 10));
372 EXPECT_EQ(19, Vec
.find_first_in(18, 25));
373 EXPECT_EQ(21, Vec
.find_first_in(20, 25));
374 EXPECT_EQ(23, Vec
.find_first_in(22, 25));
375 EXPECT_EQ(-1, Vec
.find_first_in(24, 25));
378 EXPECT_EQ(-1, Vec
.find_last_in(0, 0));
379 EXPECT_EQ(-1, Vec
.find_last_in(24, 24));
380 EXPECT_EQ(-1, Vec
.find_last_in(9, 12));
382 EXPECT_EQ(8, Vec
.find_last_in(0, 10));
383 EXPECT_EQ(3, Vec
.find_last_in(0, 6));
384 EXPECT_EQ(23, Vec
.find_last_in(18, 25));
385 EXPECT_EQ(21, Vec
.find_last_in(18, 23));
386 EXPECT_EQ(19, Vec
.find_last_in(18, 21));
387 EXPECT_EQ(-1, Vec
.find_last_in(18, 19));
390 EXPECT_EQ(-1, Vec
.find_first_unset_in(0, 0));
391 EXPECT_EQ(-1, Vec
.find_first_unset_in(23, 23));
392 EXPECT_EQ(-1, Vec
.find_first_unset_in(6, 9));
394 EXPECT_EQ(0, Vec
.find_first_unset_in(0, 6));
395 EXPECT_EQ(1, Vec
.find_first_unset_in(1, 6));
396 EXPECT_EQ(9, Vec
.find_first_unset_in(7, 13));
397 EXPECT_EQ(18, Vec
.find_first_unset_in(18, 25));
398 EXPECT_EQ(20, Vec
.find_first_unset_in(19, 25));
399 EXPECT_EQ(20, Vec
.find_first_unset_in(20, 25));
400 EXPECT_EQ(22, Vec
.find_first_unset_in(21, 25));
401 EXPECT_EQ(22, Vec
.find_first_unset_in(22, 25));
402 EXPECT_EQ(24, Vec
.find_first_unset_in(23, 25));
403 EXPECT_EQ(24, Vec
.find_first_unset_in(24, 25));
406 EXPECT_EQ(-1, Vec
.find_last_unset_in(0, 0));
407 EXPECT_EQ(-1, Vec
.find_last_unset_in(23, 23));
408 EXPECT_EQ(-1, Vec
.find_last_unset_in(6, 9));
410 EXPECT_EQ(5, Vec
.find_last_unset_in(0, 6));
411 EXPECT_EQ(4, Vec
.find_last_unset_in(0, 5));
412 EXPECT_EQ(1, Vec
.find_last_unset_in(0, 4));
413 EXPECT_EQ(11, Vec
.find_last_unset_in(7, 13));
414 EXPECT_EQ(24, Vec
.find_last_unset_in(18, 25));
415 EXPECT_EQ(22, Vec
.find_last_unset_in(18, 24));
416 EXPECT_EQ(22, Vec
.find_last_unset_in(18, 23));
417 EXPECT_EQ(20, Vec
.find_last_unset_in(18, 22));
418 EXPECT_EQ(20, Vec
.find_last_unset_in(18, 21));
419 EXPECT_EQ(18, Vec
.find_last_unset_in(18, 20));
420 EXPECT_EQ(18, Vec
.find_last_unset_in(18, 19));
423 TYPED_TEST(BitVectorTest
, CompoundAssignment
) {
435 EXPECT_TRUE(A
.test(4));
436 EXPECT_TRUE(A
.test(5));
437 EXPECT_TRUE(A
.test(7));
438 EXPECT_TRUE(A
.test(18));
439 EXPECT_EQ(4U, A
.count());
440 EXPECT_EQ(50U, A
.size());
447 EXPECT_FALSE(A
.test(2));
448 EXPECT_FALSE(A
.test(7));
449 EXPECT_TRUE(A
.test(4));
450 EXPECT_TRUE(A
.test(5));
451 EXPECT_EQ(2U, A
.count());
452 EXPECT_EQ(50U, A
.size());
458 EXPECT_TRUE(A
.test(2));
459 EXPECT_TRUE(A
.test(7));
460 EXPECT_EQ(98U, A
.count());
461 EXPECT_EQ(100U, A
.size());
464 // Test SmallBitVector operations with mixed big/small representations
465 TYPED_TEST(BitVectorTest
, MixedBigSmall
) {
481 EXPECT_TRUE(Small
.test(0));
482 EXPECT_EQ(1u, Small
.count());
483 // FIXME BitVector and SmallBitVector behave differently here.
484 // SmallBitVector resizes the LHS to max(LHS.size(), RHS.size())
485 // but BitVector does not.
486 // EXPECT_EQ(20u, Small.size());
504 EXPECT_TRUE(Big
.test(0));
505 EXPECT_EQ(1u, Big
.count());
506 // FIXME BitVector and SmallBitVector behave differently here.
507 // SmallBitVector resizes the LHS to max(LHS.size(), RHS.size())
508 // but BitVector does not.
509 // EXPECT_EQ(20u, Big.size());
527 EXPECT_TRUE(Small
.test(0));
528 EXPECT_TRUE(Small
.test(1));
529 EXPECT_TRUE(Small
.test(2));
530 EXPECT_TRUE(Small
.test(16));
531 EXPECT_EQ(4u, Small
.count());
532 EXPECT_EQ(20u, Small
.size());
550 EXPECT_TRUE(Big
.test(0));
551 EXPECT_TRUE(Big
.test(1));
552 EXPECT_TRUE(Big
.test(2));
553 EXPECT_TRUE(Big
.test(16));
554 EXPECT_EQ(4u, Big
.count());
555 EXPECT_EQ(20u, Big
.size());
573 EXPECT_TRUE(Small
.test(1));
574 EXPECT_TRUE(Small
.test(2));
575 EXPECT_TRUE(Small
.test(16));
576 EXPECT_EQ(3u, Small
.count());
577 EXPECT_EQ(20u, Small
.size());
595 EXPECT_TRUE(Big
.test(1));
596 EXPECT_TRUE(Big
.test(2));
597 EXPECT_TRUE(Big
.test(16));
598 EXPECT_EQ(3u, Big
.count());
599 EXPECT_EQ(20u, Big
.size());
617 EXPECT_TRUE(Small
.test(1));
618 EXPECT_EQ(1u, Small
.count());
619 EXPECT_EQ(10u, Small
.size());
637 EXPECT_TRUE(Big
.test(2));
638 EXPECT_TRUE(Big
.test(16));
639 EXPECT_EQ(2u, Big
.count());
640 EXPECT_EQ(20u, Big
.size());
655 EXPECT_FALSE(Big
== Small
);
656 EXPECT_FALSE(Small
== Big
);
658 EXPECT_TRUE(Big
== Small
);
659 EXPECT_TRUE(Small
== Big
);
673 EXPECT_FALSE(Small
.anyCommon(Big
));
674 EXPECT_FALSE(Big
.anyCommon(Small
));
676 EXPECT_TRUE(Small
.anyCommon(Big
));
677 EXPECT_TRUE(Big
.anyCommon(Small
));
692 EXPECT_TRUE(Small
.test(Big
));
693 EXPECT_FALSE(Big
.test(Small
));
695 EXPECT_FALSE(Small
.test(Big
));
696 EXPECT_FALSE(Big
.test(Small
));
700 TYPED_TEST(BitVectorTest
, ProxyIndex
) {
702 EXPECT_TRUE(Vec
.none());
703 Vec
[0] = Vec
[1] = Vec
[2] = true;
704 EXPECT_EQ(Vec
.size(), Vec
.count());
705 Vec
[2] = Vec
[1] = Vec
[0] = false;
706 EXPECT_TRUE(Vec
.none());
709 TYPED_TEST(BitVectorTest
, PortableBitMask
) {
711 const uint32_t Mask1
[] = { 0x80000000, 6, 5 };
714 A
.setBitsInMask(Mask1
, 1);
715 EXPECT_EQ(10u, A
.size());
716 EXPECT_FALSE(A
.test(0));
719 A
.setBitsInMask(Mask1
, 1);
720 EXPECT_FALSE(A
.test(0));
721 EXPECT_TRUE(A
.test(31));
722 EXPECT_EQ(1u, A
.count());
725 A
.setBitsInMask(Mask1
, 1);
726 EXPECT_EQ(1u, A
.count());
727 A
.setBitsInMask(Mask1
, 2);
728 EXPECT_EQ(1u, A
.count());
731 A
.setBitsInMask(Mask1
, 2);
732 EXPECT_EQ(2u, A
.count());
735 A
.setBitsInMask(Mask1
, 3);
736 EXPECT_EQ(4u, A
.count());
738 A
.setBitsNotInMask(Mask1
, 1);
739 EXPECT_EQ(32u+3u, A
.count());
741 A
.setBitsNotInMask(Mask1
, 3);
742 EXPECT_EQ(65u, A
.count());
745 EXPECT_EQ(65u, A
.count());
749 A
.setBitsNotInMask(Mask1
, 3);
750 EXPECT_EQ(96u-5u, A
.count());
752 A
.clearBitsNotInMask(Mask1
, 1);
753 EXPECT_EQ(64-4u, A
.count());
756 TYPED_TEST(BitVectorTest
, BinOps
) {
761 EXPECT_FALSE(A
.anyCommon(B
));
762 EXPECT_FALSE(B
.anyCommon(B
));
766 EXPECT_FALSE(A
.anyCommon(B
));
767 EXPECT_FALSE(B
.anyCommon(A
));
770 EXPECT_FALSE(A
.anyCommon(B
));
771 EXPECT_FALSE(B
.anyCommon(A
));
774 EXPECT_TRUE(A
.anyCommon(B
));
775 EXPECT_TRUE(B
.anyCommon(A
));
781 EXPECT_FALSE(A
.anyCommon(B
));
782 EXPECT_FALSE(B
.anyCommon(A
));
785 typedef std::vector
<std::pair
<int, int>> RangeList
;
787 template <typename VecType
>
788 static inline VecType
createBitVector(uint32_t Size
,
789 const RangeList
&setRanges
) {
792 for (auto &R
: setRanges
)
793 V
.set(R
.first
, R
.second
);
797 TYPED_TEST(BitVectorTest
, ShiftOpsSingleWord
) {
798 // Test that shift ops work when the desired shift amount is less
801 // 1. Case where the number of bits in the BitVector also fit into a single
803 TypeParam A
= createBitVector
<TypeParam
>(12, {{2, 4}, {8, 10}});
806 EXPECT_EQ(4U, A
.count());
807 EXPECT_TRUE(A
.test(2));
808 EXPECT_TRUE(A
.test(3));
809 EXPECT_TRUE(A
.test(8));
810 EXPECT_TRUE(A
.test(9));
813 EXPECT_EQ(createBitVector
<TypeParam
>(12, {{1, 3}, {7, 9}}), A
);
819 EXPECT_EQ(createBitVector
<TypeParam
>(12, {}), A
);
823 EXPECT_EQ(createBitVector
<TypeParam
>(12, {}), A
);
825 // 2. Case where the number of bits in the BitVector do not fit into a single
828 // 31----------------------------------------------------------------------0
829 // XXXXXXXX XXXXXXXX XXXXXXXX 00000111 | 11111110 00000000 00001111 11111111
830 A
= createBitVector
<TypeParam
>(40, {{0, 12}, {25, 35}});
831 EXPECT_EQ(40U, A
.size());
832 EXPECT_EQ(22U, A
.count());
834 // 2a. Make sure that left shifting some 1 bits out of the vector works.
835 // 31----------------------------------------------------------------------0
837 // XXXXXXXX XXXXXXXX XXXXXXXX 00000111 | 11111110 00000000 00001111 11111111
839 // XXXXXXXX XXXXXXXX XXXXXXXX 11111100 | 00000000 00011111 11111110 00000000
841 EXPECT_EQ(createBitVector
<TypeParam
>(40, {{9, 21}, {34, 40}}), A
);
843 // 2b. Make sure that keeping the number of one bits unchanged works.
844 // 31----------------------------------------------------------------------0
846 // XXXXXXXX XXXXXXXX XXXXXXXX 11111100 | 00000000 00011111 11111110 00000000
848 // XXXXXXXX XXXXXXXX XXXXXXXX 00000011 | 11110000 00000000 01111111 11111000
850 EXPECT_EQ(createBitVector
<TypeParam
>(40, {{3, 15}, {28, 34}}), A
);
852 // 2c. Make sure that right shifting some 1 bits out of the vector works.
853 // 31----------------------------------------------------------------------0
855 // XXXXXXXX XXXXXXXX XXXXXXXX 00000011 | 11110000 00000000 01111111 11111000
857 // XXXXXXXX XXXXXXXX XXXXXXXX 00000000 | 00000000 11111100 00000000 00011111
859 EXPECT_EQ(createBitVector
<TypeParam
>(40, {{0, 5}, {18, 24}}), A
);
862 A
= createBitVector
<TypeParam
>(300, {{1, 30}, {60, 95}, {200, 275}});
864 EXPECT_EQ(createBitVector
<TypeParam
>(
865 300, {{1 + 29, 30 + 29}, {60 + 29, 95 + 29}, {200 + 29, 300}}),
869 TYPED_TEST(BitVectorTest
, ShiftOpsMultiWord
) {
870 // Test that shift ops work when the desired shift amount is greater than or
871 // equal to the size of a single word.
872 auto A
= createBitVector
<TypeParam
>(300, {{1, 30}, {60, 95}, {200, 275}});
874 // Make a copy so we can re-use it later.
877 // 1. Shift left by an exact multiple of the word size. This should invoke
878 // only a memmove and no per-word bit operations.
880 auto Expected
= createBitVector
<TypeParam
>(
881 300, {{1 + 64, 30 + 64}, {60 + 64, 95 + 64}, {200 + 64, 300}});
882 EXPECT_EQ(Expected
, A
);
884 // 2. Shift left by a non multiple of the word size. This should invoke both
885 // a memmove and per-word bit operations.
888 EXPECT_EQ(createBitVector
<TypeParam
>(
889 300, {{1 + 93, 30 + 93}, {60 + 93, 95 + 93}, {200 + 93, 300}}),
892 // 1. Shift right by an exact multiple of the word size. This should invoke
893 // only a memmove and no per-word bit operations.
897 createBitVector
<TypeParam
>(300, {{0, 95 - 64}, {200 - 64, 275 - 64}}), A
);
899 // 2. Shift left by a non multiple of the word size. This should invoke both
900 // a memmove and per-word bit operations.
904 createBitVector
<TypeParam
>(300, {{0, 95 - 93}, {200 - 93, 275 - 93}}), A
);
907 TYPED_TEST(BitVectorTest
, RangeOps
) {
913 EXPECT_FALSE(A
.test(0));
914 EXPECT_TRUE( A
.test(1));
915 EXPECT_TRUE( A
.test(23));
916 EXPECT_TRUE( A
.test(254));
917 EXPECT_FALSE(A
.test(255));
924 EXPECT_TRUE( B
.test(0));
925 EXPECT_FALSE(B
.test(1));
926 EXPECT_FALSE(B
.test(23));
927 EXPECT_FALSE(B
.test(254));
928 EXPECT_TRUE( B
.test(255));
935 EXPECT_TRUE(C
.test(0));
936 EXPECT_FALSE( C
.test(1));
937 EXPECT_FALSE( C
.test(2));
944 EXPECT_FALSE(D
.test(0));
945 EXPECT_TRUE( D
.test(1));
946 EXPECT_TRUE( D
.test(2));
953 EXPECT_FALSE(E
.test(0));
954 EXPECT_TRUE( E
.test(1));
955 EXPECT_TRUE( E
.test(32));
956 EXPECT_FALSE(E
.test(33));
958 TypeParam BufferOverrun
;
959 unsigned size
= sizeof(unsigned long) * 8;
960 BufferOverrun
.resize(size
);
961 BufferOverrun
.reset(0, size
);
962 BufferOverrun
.set(0, size
);
965 TYPED_TEST(BitVectorTest
, CompoundTestReset
) {
966 TypeParam
A(50, true);
967 TypeParam
B(50, false);
969 TypeParam
C(100, true);
970 TypeParam
D(100, false);
972 EXPECT_FALSE(A
.test(A
));
973 EXPECT_TRUE(A
.test(B
));
974 EXPECT_FALSE(A
.test(C
));
975 EXPECT_TRUE(A
.test(D
));
976 EXPECT_FALSE(B
.test(A
));
977 EXPECT_FALSE(B
.test(B
));
978 EXPECT_FALSE(B
.test(C
));
979 EXPECT_FALSE(B
.test(D
));
980 EXPECT_TRUE(C
.test(A
));
981 EXPECT_TRUE(C
.test(B
));
982 EXPECT_FALSE(C
.test(C
));
983 EXPECT_TRUE(C
.test(D
));
987 EXPECT_TRUE(A
.all());
989 EXPECT_TRUE(A
.none());
992 EXPECT_TRUE(A
.none());
996 EXPECT_EQ(50, C
.find_first());
998 EXPECT_TRUE(C
.none());
1001 TYPED_TEST(BitVectorTest
, MoveConstructor
) {
1002 TypeParam
A(10, true);
1003 TypeParam
B(std::move(A
));
1004 // Check that the move ctor leaves the moved-from object in a valid state.
1005 // The following line used to crash.
1008 TypeParam
C(10, true);
1013 TYPED_TEST(BitVectorTest
, MoveAssignment
) {
1014 TypeParam
A(10, true);
1017 // Check that move assignment leaves the moved-from object in a valid state.
1018 // The following line used to crash.
1021 TypeParam
C(10, true);
1026 template<class TypeParam
>
1027 static void testEmpty(const TypeParam
&A
) {
1028 EXPECT_TRUE(A
.empty());
1029 EXPECT_EQ((size_t)0, A
.size());
1030 EXPECT_EQ((size_t)0, A
.count());
1031 EXPECT_FALSE(A
.any());
1032 EXPECT_TRUE(A
.all());
1033 EXPECT_TRUE(A
.none());
1034 EXPECT_EQ(-1, A
.find_first());
1035 EXPECT_EQ(A
, TypeParam());
1038 /// Tests whether BitVector behaves well with Bits==nullptr, Capacity==0
1039 TYPED_TEST(BitVectorTest
, EmptyVector
) {
1063 TYPED_TEST(BitVectorTest
, Iterators
) {
1064 TypeParam
Filled(10, true);
1065 EXPECT_NE(Filled
.set_bits_begin(), Filled
.set_bits_end());
1066 unsigned Counter
= 0;
1067 for (unsigned Bit
: Filled
.set_bits())
1068 EXPECT_EQ(Bit
, Counter
++);
1071 EXPECT_EQ(Empty
.set_bits_begin(), Empty
.set_bits_end());
1072 for (unsigned Bit
: Empty
.set_bits()) {
1077 TypeParam
ToFill(100, false);
1079 EXPECT_NE(ToFill
.set_bits_begin(), ToFill
.set_bits_end());
1080 EXPECT_EQ(++ToFill
.set_bits_begin(), ToFill
.set_bits_end());
1081 EXPECT_EQ(*ToFill
.set_bits_begin(), 0U);
1083 EXPECT_EQ(ToFill
.set_bits_begin(), ToFill
.set_bits_end());
1085 const unsigned List
[] = {1, 10, 25, 99};
1086 for (unsigned Num
: List
)
1089 for (unsigned Bit
: ToFill
.set_bits())
1090 EXPECT_EQ(List
[i
++], Bit
);
1093 TYPED_TEST(BitVectorTest
, PushBack
) {
1094 TypeParam
Vec(10, false);
1095 EXPECT_EQ(-1, Vec
.find_first());
1096 EXPECT_EQ(10U, Vec
.size());
1097 EXPECT_EQ(0U, Vec
.count());
1099 Vec
.push_back(true);
1100 EXPECT_EQ(10, Vec
.find_first());
1101 EXPECT_EQ(11U, Vec
.size());
1102 EXPECT_EQ(1U, Vec
.count());
1104 Vec
.push_back(false);
1105 EXPECT_EQ(10, Vec
.find_first());
1106 EXPECT_EQ(12U, Vec
.size());
1107 EXPECT_EQ(1U, Vec
.count());
1109 Vec
.push_back(true);
1110 EXPECT_EQ(10, Vec
.find_first());
1111 EXPECT_EQ(13U, Vec
.size());
1112 EXPECT_EQ(2U, Vec
.count());
1114 // Add a lot of values to cause reallocation.
1115 for (int i
= 0; i
!= 100; ++i
) {
1116 Vec
.push_back(true);
1117 Vec
.push_back(false);
1119 EXPECT_EQ(10, Vec
.find_first());
1120 EXPECT_EQ(213U, Vec
.size());
1121 EXPECT_EQ(102U, Vec
.count());