llvm.org GIT mirror llvm / 904cf82
Merge the SmallBitVector and BitVector unit tests with gtest's typed test magic and bring SmallBitVector up to date. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@158600 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 8 years ago
3 changed file(s) with 91 addition(s) and 212 deletion(s). Raw diff Collapse all Expand all
353353 return (*this)[Idx];
354354 }
355355
356 /// Test if any common bits are set.
357 bool anyCommon(const SmallBitVector &RHS) const {
358 if (isSmall() && RHS.isSmall())
359 return (getSmallBits() & RHS.getSmallBits()) != 0;
360 if (!isSmall() && !RHS.isSmall())
361 return getPointer()->anyCommon(*RHS.getPointer());
362
363 for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i)
364 if (test(i) && RHS.test(i))
365 return true;
366 return false;
367 }
368
356369 // Comparison operators.
357370 bool operator==(const SmallBitVector &RHS) const {
358371 if (size() != RHS.size())
441454 void swap(SmallBitVector &RHS) {
442455 std::swap(X, RHS.X);
443456 }
457
458 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
459 /// This computes "*this |= Mask".
460 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
461 if (isSmall())
462 applyMask(Mask, MaskWords);
463 else
464 getPointer()->setBitsInMask(Mask, MaskWords);
465 }
466
467 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
468 /// Don't resize. This computes "*this &= ~Mask".
469 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
470 if (isSmall())
471 applyMask(Mask, MaskWords);
472 else
473 getPointer()->clearBitsInMask(Mask, MaskWords);
474 }
475
476 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
477 /// Don't resize. This computes "*this |= ~Mask".
478 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
479 if (isSmall())
480 applyMask(Mask, MaskWords);
481 else
482 getPointer()->setBitsNotInMask(Mask, MaskWords);
483 }
484
485 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
486 /// Don't resize. This computes "*this &= Mask".
487 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
488 if (isSmall())
489 applyMask(Mask, MaskWords);
490 else
491 getPointer()->clearBitsNotInMask(Mask, MaskWords);
492 }
493
494 private:
495 template
496 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
497 assert((NumBaseBits == 64 || NumBaseBits == 32) && "Unsupported word size");
498 if (NumBaseBits == 64 && MaskWords >= 2) {
499 uint64_t M = Mask[0] | (uint64_t(Mask[1]) << 32);
500 if (InvertMask) M = ~M;
501 if (AddBits) setSmallBits(getSmallBits() | M);
502 else setSmallBits(getSmallBits() & ~M);
503 } else {
504 uint32_t M = Mask[0];
505 if (InvertMask) M = ~M;
506 if (AddBits) setSmallBits(getSmallBits() | M);
507 else setSmallBits(getSmallBits() & ~M);
508 }
509 }
444510 };
445511
446512 inline SmallBitVector
1010 #ifndef __ppc__
1111
1212 #include "llvm/ADT/BitVector.h"
13 #include "llvm/ADT/SmallBitVector.h"
1314 #include "gtest/gtest.h"
1415
1516 using namespace llvm;
1617
1718 namespace {
1819
19 TEST(BitVectorTest, TrivialOperation) {
20 BitVector Vec;
20 // Test fixture
21 template
22 class BitVectorTest : public ::testing::Test { };
23
24 // Test both BitVector and SmallBitVector with the same suite of tests.
25 typedef ::testing::Types BitVectorTestTypes;
26 TYPED_TEST_CASE(BitVectorTest, BitVectorTestTypes);
27
28 TYPED_TEST(BitVectorTest, TrivialOperation) {
29 TypeParam Vec;
2130 EXPECT_EQ(0U, Vec.count());
2231 EXPECT_EQ(0U, Vec.size());
2332 EXPECT_FALSE(Vec.any());
4150 EXPECT_FALSE(Vec.none());
4251 EXPECT_FALSE(Vec.empty());
4352
44 BitVector Inv = Vec;
53 TypeParam Inv = Vec;
4554 Inv.flip();
4655 EXPECT_EQ(6U, Inv.count());
4756 EXPECT_EQ(11U, Inv.size());
7685 EXPECT_FALSE(Vec[56]);
7786 Vec.resize(61, false);
7887
79 BitVector Copy = Vec;
80 BitVector Alt(3, false);
88 TypeParam Copy = Vec;
89 TypeParam Alt(3, false);
8190 Alt.resize(6, true);
8291 std::swap(Alt, Vec);
8392 EXPECT_TRUE(Copy == Alt);
131140 EXPECT_TRUE(Vec.none());
132141 EXPECT_FALSE(Vec.empty());
133142
134 Inv = BitVector().flip();
143 Inv = TypeParam().flip();
135144 EXPECT_EQ(0U, Inv.count());
136145 EXPECT_EQ(0U, Inv.size());
137146 EXPECT_FALSE(Inv.any());
148157 EXPECT_TRUE(Vec.empty());
149158 }
150159
151 TEST(BitVectorTest, CompoundAssignment) {
152 BitVector A;
160 TYPED_TEST(BitVectorTest, CompoundAssignment) {
161 TypeParam A;
153162 A.resize(10);
154163 A.set(4);
155164 A.set(7);
156165
157 BitVector B;
166 TypeParam B;
158167 B.resize(50);
159168 B.set(5);
160169 B.set(18);
187196 EXPECT_EQ(100U, A.size());
188197 }
189198
190 TEST(BitVectorTest, ProxyIndex) {
191 BitVector Vec(3);
199 TYPED_TEST(BitVectorTest, ProxyIndex) {
200 TypeParam Vec(3);
192201 EXPECT_TRUE(Vec.none());
193202 Vec[0] = Vec[1] = Vec[2] = true;
194203 EXPECT_EQ(Vec.size(), Vec.count());
196205 EXPECT_TRUE(Vec.none());
197206 }
198207
199 TEST(BitVectorTest, PortableBitMask) {
200 BitVector A;
208 TYPED_TEST(BitVectorTest, PortableBitMask) {
209 TypeParam A;
201210 const uint32_t Mask1[] = { 0x80000000, 6, 5 };
202211
203212 A.resize(10);
243252 EXPECT_EQ(64-4u, A.count());
244253 }
245254
246 TEST(BitVectorTest, BinOps) {
247 BitVector A;
248 BitVector B;
255 TYPED_TEST(BitVectorTest, BinOps) {
256 TypeParam A;
257 TypeParam B;
249258
250259 A.resize(65);
251260 EXPECT_FALSE(A.anyCommon(B));
+0
-196
unittests/ADT/SmallBitVectorTest.cpp less more
None //===- llvm/unittest/ADT/SmallBitVectorTest.cpp - SmallBitVector tests ----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/SmallBitVector.h"
10 #include "gtest/gtest.h"
11
12 using namespace llvm;
13
14 namespace {
15
16 TEST(SmallBitVectorTest, TrivialOperation) {
17 SmallBitVector Vec;
18 EXPECT_EQ(0U, Vec.count());
19 EXPECT_EQ(0U, Vec.size());
20 EXPECT_FALSE(Vec.any());
21 EXPECT_TRUE(Vec.all());
22 EXPECT_TRUE(Vec.none());
23 EXPECT_TRUE(Vec.empty());
24
25 Vec.resize(5, true);
26 EXPECT_EQ(5U, Vec.count());
27 EXPECT_EQ(5U, Vec.size());
28 EXPECT_TRUE(Vec.any());
29 EXPECT_TRUE(Vec.all());
30 EXPECT_FALSE(Vec.none());
31 EXPECT_FALSE(Vec.empty());
32
33 Vec.resize(11);
34 EXPECT_EQ(5U, Vec.count());
35 EXPECT_EQ(11U, Vec.size());
36 EXPECT_TRUE(Vec.any());
37 EXPECT_FALSE(Vec.all());
38 EXPECT_FALSE(Vec.none());
39 EXPECT_FALSE(Vec.empty());
40
41 SmallBitVector Inv = ~Vec;
42 EXPECT_EQ(6U, Inv.count());
43 EXPECT_EQ(11U, Inv.size());
44 EXPECT_TRUE(Inv.any());
45 EXPECT_FALSE(Inv.all());
46 EXPECT_FALSE(Inv.none());
47 EXPECT_FALSE(Inv.empty());
48
49 EXPECT_FALSE(Inv == Vec);
50 EXPECT_TRUE(Inv != Vec);
51 Vec = ~Vec;
52 EXPECT_TRUE(Inv == Vec);
53 EXPECT_FALSE(Inv != Vec);
54
55 // Add some "interesting" data to Vec.
56 Vec.resize(23, true);
57 Vec.resize(25, false);
58 Vec.resize(26, true);
59 Vec.resize(29, false);
60 Vec.resize(33, true);
61 Vec.resize(57, false);
62 unsigned Count = 0;
63 for (unsigned i = Vec.find_first(); i != -1u; i = Vec.find_next(i)) {
64 ++Count;
65 EXPECT_TRUE(Vec[i]);
66 EXPECT_TRUE(Vec.test(i));
67 }
68 EXPECT_EQ(Count, Vec.count());
69 EXPECT_EQ(Count, 23u);
70 EXPECT_FALSE(Vec[0]);
71 EXPECT_TRUE(Vec[32]);
72 EXPECT_FALSE(Vec[56]);
73 Vec.resize(61, false);
74
75 SmallBitVector Copy = Vec;
76 SmallBitVector Alt(3, false);
77 Alt.resize(6, true);
78 std::swap(Alt, Vec);
79 EXPECT_TRUE(Copy == Alt);
80 EXPECT_TRUE(Vec.size() == 6);
81 EXPECT_TRUE(Vec.count() == 3);
82 EXPECT_TRUE(Vec.find_first() == 3);
83 std::swap(Copy, Vec);
84
85 // Add some more "interesting" data.
86 Vec.resize(68, true);
87 Vec.resize(78, false);
88 Vec.resize(89, true);
89 Vec.resize(90, false);
90 Vec.resize(91, true);
91 Vec.resize(130, false);
92 Count = 0;
93 for (unsigned i = Vec.find_first(); i != -1u; i = Vec.find_next(i)) {
94 ++Count;
95 EXPECT_TRUE(Vec[i]);
96 EXPECT_TRUE(Vec.test(i));
97 }
98 EXPECT_EQ(Count, Vec.count());
99 EXPECT_EQ(Count, 42u);
100 EXPECT_FALSE(Vec[0]);
101 EXPECT_TRUE(Vec[32]);
102 EXPECT_FALSE(Vec[60]);
103 EXPECT_FALSE(Vec[129]);
104
105 Vec.flip(60);
106 EXPECT_TRUE(Vec[60]);
107 EXPECT_EQ(Count + 1, Vec.count());
108 Vec.flip(60);
109 EXPECT_FALSE(Vec[60]);
110 EXPECT_EQ(Count, Vec.count());
111
112 Vec.reset(32);
113 EXPECT_FALSE(Vec[32]);
114 EXPECT_EQ(Count - 1, Vec.count());
115 Vec.set(32);
116 EXPECT_TRUE(Vec[32]);
117 EXPECT_EQ(Count, Vec.count());
118
119 Vec.flip();
120 EXPECT_EQ(Vec.size() - Count, Vec.count());
121
122 Vec.reset();
123 EXPECT_EQ(0U, Vec.count());
124 EXPECT_EQ(130U, Vec.size());
125 EXPECT_FALSE(Vec.any());
126 EXPECT_FALSE(Vec.all());
127 EXPECT_TRUE(Vec.none());
128 EXPECT_FALSE(Vec.empty());
129
130 Inv = ~SmallBitVector();
131 EXPECT_EQ(0U, Inv.count());
132 EXPECT_EQ(0U, Inv.size());
133 EXPECT_FALSE(Inv.any());
134 EXPECT_TRUE(Inv.all());
135 EXPECT_TRUE(Inv.none());
136 EXPECT_TRUE(Inv.empty());
137
138 Vec.clear();
139 EXPECT_EQ(0U, Vec.count());
140 EXPECT_EQ(0U, Vec.size());
141 EXPECT_FALSE(Vec.any());
142 EXPECT_TRUE(Vec.all());
143 EXPECT_TRUE(Vec.none());
144 EXPECT_TRUE(Vec.empty());
145 }
146
147 TEST(SmallBitVectorTest, CompoundAssignment) {
148 SmallBitVector A;
149 A.resize(10);
150 A.set(4);
151 A.set(7);
152
153 SmallBitVector B;
154 B.resize(50);
155 B.set(5);
156 B.set(18);
157
158 A |= B;
159 EXPECT_TRUE(A.test(4));
160 EXPECT_TRUE(A.test(5));
161 EXPECT_TRUE(A.test(7));
162 EXPECT_TRUE(A.test(18));
163 EXPECT_EQ(4U, A.count());
164 EXPECT_EQ(50U, A.size());
165
166 B.resize(10);
167 B.set();
168 B.reset(2);
169 B.reset(7);
170 A &= B;
171 EXPECT_FALSE(A.test(2));
172 EXPECT_FALSE(A.test(7));
173 EXPECT_EQ(2U, A.count());
174 EXPECT_EQ(50U, A.size());
175
176 B.resize(100);
177 B.set();
178
179 A ^= B;
180 EXPECT_TRUE(A.test(2));
181 EXPECT_TRUE(A.test(7));
182 EXPECT_EQ(98U, A.count());
183 EXPECT_EQ(100U, A.size());
184 }
185
186 TEST(SmallBitVectorTest, ProxyIndex) {
187 SmallBitVector Vec(3);
188 EXPECT_TRUE(Vec.none());
189 Vec[0] = Vec[1] = Vec[2] = true;
190 EXPECT_EQ(Vec.size(), Vec.count());
191 Vec[2] = Vec[1] = Vec[0] = false;
192 EXPECT_TRUE(Vec.none());
193 }
194
195 }