1#include "storm-config.h"
8TEST(BitVectorTest, InitToZero) {
11 for (uint64_t i = 0; i < 32; ++i) {
12 ASSERT_FALSE(vector.
get(i));
15 ASSERT_TRUE(vector.
empty());
16 ASSERT_FALSE(vector.
full());
19TEST(BitVectorTest, InitToOne) {
22 for (uint64_t i = 0; i < 32; ++i) {
23 ASSERT_TRUE(vector.
get(i));
25 ASSERT_FALSE(vector.
empty());
26 ASSERT_TRUE(vector.
full());
29TEST(BitVectorTest, InitFromIterator) {
30 std::vector<uint64_t> valueVector = {0, 4, 10};
33 ASSERT_EQ(32ul, vector.
size());
35 for (uint64_t i = 0; i < 32; ++i) {
36 if (i == 0 || i == 4 || i == 10) {
37 ASSERT_TRUE(vector.
get(i));
39 ASSERT_FALSE(vector.
get(i));
44TEST(BitVectorTest, InitFromIntVector) {
45 std::vector<uint64_t> valueVector = {0, 4, 10};
48 ASSERT_EQ(32ul, vector.
size());
50 for (uint64_t i = 0; i < 32; ++i) {
51 if (i == 0 || i == 4 || i == 10) {
52 ASSERT_TRUE(vector.
get(i));
54 ASSERT_FALSE(vector.
get(i));
59TEST(BitVectorTest, GetSet) {
62 for (uint64_t i = 0; i < 32; ++i) {
63 vector.
set(i, i % 2 == 0);
66 for (uint64_t i = 0; i < 32; ++i) {
67 ASSERT_EQ(i % 2 == 0, vector.
get(i));
71TEST(BitVectorTest, GetAsInt) {
79 EXPECT_EQ(3ul, vector.
getAsInt(0, 64));
80 EXPECT_EQ(1ul, vector.
getAsInt(62, 1));
81 EXPECT_EQ(3ul, vector.
getAsInt(62, 2));
82 EXPECT_EQ(7ul, vector.
getAsInt(62, 3));
83 EXPECT_EQ(15ul, vector.
getAsInt(62, 4));
85 vector.
set(64,
false);
87 EXPECT_EQ(1ul, vector.
getAsInt(62, 1));
88 EXPECT_EQ(3ul, vector.
getAsInt(62, 2));
89 EXPECT_EQ(6ul, vector.
getAsInt(62, 3));
90 EXPECT_EQ(13ul, vector.
getAsInt(62, 4));
93 vector.
set(62,
false);
94 EXPECT_EQ(2ul, vector.
getAsInt(61, 2));
97TEST(BitVectorTest, SetFromInt) {
102 EXPECT_TRUE(vector.
get(62));
103 EXPECT_FALSE(vector.
get(63));
104 EXPECT_FALSE(vector.
get(64));
105 EXPECT_FALSE(vector.
get(65));
109 EXPECT_TRUE(vector.
get(61));
110 EXPECT_FALSE(vector.
get(62));
111 EXPECT_FALSE(vector.
get(63));
115 EXPECT_TRUE(vector.
get(61));
116 EXPECT_FALSE(vector.
get(62));
117 EXPECT_TRUE(vector.
get(63));
122 EXPECT_TRUE(vector.
get(62));
123 EXPECT_TRUE(vector.
get(63));
124 EXPECT_TRUE(vector.
get(64));
125 EXPECT_TRUE(vector.
get(65));
130TEST(BitVectorTest, GetSetInt) {
134 EXPECT_EQ(2ul, vector.
getAsInt(63, 3));
137TEST(BitVectorDeathTest, GetSetAssertion) {
141 EXPECT_DEATH_IF_SUPPORTED(vector.
get(32),
"");
142 EXPECT_DEATH_IF_SUPPORTED(vector.
set(32),
"");
144 std::cerr <<
"WARNING: Not testing GetSetAssertions, as they are disabled in release mode.\n";
152 for (uint64_t i = 0; i < 32; ++i) {
158 ASSERT_EQ(70ul, vector.
size());
161 for (uint64_t i = 0; i < 32; ++i) {
162 ASSERT_TRUE(vector.
get(i));
165 for (uint64_t i = 32; i < 70; ++i) {
167 ASSERT_NO_THROW(result = vector.
get(i));
168 ASSERT_FALSE(result);
173 ASSERT_EQ(72ul, vector.
size());
176 for (uint64_t i = 0; i < 32; ++i) {
177 ASSERT_TRUE(vector.
get(i));
179 for (uint64_t i = 32; i < 70; ++i) {
181 ASSERT_NO_THROW(result = vector.
get(i));
182 ASSERT_FALSE(result);
184 for (uint64_t i = 70; i < 72; ++i) {
185 ASSERT_TRUE(vector.
get(i));
189 ASSERT_EQ(16ul, vector.
size());
192 for (uint64_t i = 0; i < 16; ++i) {
193 ASSERT_TRUE(vector.
get(i));
197 ASSERT_EQ(65ul, vector.
size());
198 ASSERT_TRUE(vector.
full());
201TEST(BitVectorTest, OperatorAnd) {
205 for (
int i = 0; i < 32; ++i) {
206 vector1.
set(i, i % 2 == 0);
207 vector2.
set(i, i % 2 == 1);
213 for (uint64_t i = 0; i < 31; ++i) {
214 ASSERT_FALSE(andResult.
get(i));
216 ASSERT_TRUE(andResult.
get(31));
219TEST(BitVectorTest, OperatorAndEqual) {
223 for (
int i = 0; i < 32; ++i) {
224 vector1.
set(i, i % 2 == 0);
225 vector2.
set(i, i % 2 == 1);
232 for (uint64_t i = 0; i < 31; ++i) {
233 ASSERT_FALSE(vector1.
get(i));
235 ASSERT_TRUE(vector1.
get(31));
238TEST(BitVectorTest, OperatorOr) {
242 for (uint64_t i = 0; i < 32; ++i) {
243 vector1.
set(i, i % 2 == 0);
244 vector2.
set(i, i % 2 == 1);
246 vector1.
set(31,
false);
247 vector2.
set(31,
false);
251 for (uint64_t i = 0; i < 31; ++i) {
252 ASSERT_TRUE(orResult.
get(i));
254 ASSERT_FALSE(orResult.
get(31));
257TEST(BitVectorTest, OperatorOrEqual) {
261 for (uint64_t i = 0; i < 32; ++i) {
262 vector1.
set(i, i % 2 == 0);
263 vector2.
set(i, i % 2 == 1);
265 vector1.
set(31,
false);
266 vector2.
set(31,
false);
270 for (uint64_t i = 0; i < 31; ++i) {
271 ASSERT_TRUE(vector1.
get(i));
273 ASSERT_FALSE(vector1.
get(31));
276TEST(BitVectorTest, OperatorXor) {
280 for (uint64_t i = 0; i < 32; ++i) {
282 vector2.
set(i, i % 2 == 1);
289#pragma clang diagnostic push
290#pragma clang diagnostic ignored "-Wself-assign-overloaded"
292#pragma clang diagnostic pop
294 for (uint64_t i = 0; i < 32; ++i) {
295 ASSERT_EQ(vector3.
get(i), vector4.
get(i));
296 ASSERT_EQ(vector1.
get(i), vector3.
get(i));
297 ASSERT_FALSE(vector5.
get(i));
298 ASSERT_FALSE(vector2.
get(i));
302TEST(BitVectorTest, OperatorModulo) {
306 for (uint64_t i = 0; i < 15; ++i) {
307 vector2.
set(i, i % 2 == 0);
316 ASSERT_EQ(8ul, moduloResult.
size());
319 for (uint64_t i = 0; i < 8; ++i) {
320 if (i == 1 || i == 3) {
321 ASSERT_TRUE(moduloResult.
get(i));
323 ASSERT_FALSE(moduloResult.
get(i));
328TEST(BitVectorTest, OperatorNot) {
332 for (uint64_t i = 0; i < 32; ++i) {
333 vector1.
set(i, i % 2 == 0);
334 vector2.
set(i, i % 2 == 1);
339 for (uint64_t i = 0; i < 32; ++i) {
340 ASSERT_EQ(vector1.
get(i), notResult.
get(i));
344TEST(BitVectorTest, Complement) {
348 for (uint64_t i = 0; i < 32; ++i) {
349 vector1.
set(i, i % 2 == 0);
350 vector2.
set(i, i % 2 == 1);
355 for (uint64_t i = 0; i < 32; ++i) {
356 ASSERT_EQ(vector1.
get(i), vector2.
get(i));
360TEST(BitVectorTest, Increment) {
366 vector2.
set(0,
true);
367 EXPECT_EQ(vector1, vector2);
372 vector2.
set(1,
true);
373 EXPECT_EQ(vector1, vector2);
377 vector2.
set(0,
true);
378 EXPECT_EQ(vector1, vector2);
381 for (uint64_t i = 0; i < 66; ++i) {
382 vector1.
set(i,
true);
387 vector2.
set(66,
true);
388 EXPECT_EQ(vector1, vector2);
392 vector2.
set(0,
true);
393 EXPECT_EQ(vector1, vector2);
397 EXPECT_TRUE(vector1.
full());
399 EXPECT_TRUE(vector1.
empty());
404 std::vector<uint64_t> inversePermutation = {0, 1, 3, 2, 4, 6, 5, 8, 7};
407 EXPECT_TRUE(vector2.
get(2));
408 EXPECT_TRUE(vector2.
get(6));
411TEST(BitVectorTest, permuteGrouped) {
413 std::vector<uint64_t> inversePermutation = {1, 0, 2};
414 std::vector<uint64_t> groupIndices = {0, 3, 5, 6};
417 EXPECT_EQ(expected, permuted);
424 for (uint64_t i = 0; i < 32; ++i) {
425 vector1.
set(i, i % 2 == 0);
427 vector2.
set(31,
false);
428 vector2.
set(30,
false);
432 for (uint64_t i = 0; i < 30; ++i) {
433 ASSERT_TRUE(impliesResult.
get(i));
435 ASSERT_FALSE(impliesResult.
get(30));
436 ASSERT_TRUE(impliesResult.
get(31));
443 for (uint64_t i = 0; i < 32; ++i) {
444 vector1.
set(i, i % 2 == 0);
449 vector2.
set(16,
false);
458 for (uint64_t i = 0; i < 32; ++i) {
459 vector1.
set(i, i % 2 == 0);
460 vector2.
set(i, i % 2 == 1);
465 vector2.
set(16,
true);
473 ASSERT_TRUE(vector.
empty());
475 vector.
set(17,
true);
477 ASSERT_FALSE(vector.
empty());
479 vector.
set(17,
false);
480 vector.
set(18,
false);
482 ASSERT_TRUE(vector.
empty());
488 ASSERT_TRUE(vector.
full());
490 vector.
set(17,
false);
492 ASSERT_FALSE(vector.
full());
494 vector.
set(17,
true);
495 vector.
set(18,
true);
497 ASSERT_TRUE(vector.
full());
500TEST(BitVectorTest, NumberOfSetBits) {
503 for (uint64_t i = 0; i < 32; ++i) {
504 vector.
set(i, i % 2 == 0);
510TEST(BitVectorTest, NumberOfSetBitsBeforeIndex) {
513 for (uint64_t i = 0; i < 32; ++i) {
514 vector.
set(i, i % 2 == 0);
523 ASSERT_TRUE(vector.
begin() == vector.
end());
527 ASSERT_FALSE(vector.
begin() == vector.
end());
529 vector.
set(17,
false);
531 ASSERT_TRUE(vector.
begin() == vector.
end());
534TEST(BitVectorTest, NextSetIndex) {
547TEST(BitVectorTest, NextUnsetIndex) {
562TEST(BitVectorTest, SequenceBefore) {
569 auto vector_compl = ~vector;
571 for (uint64_t i = 0; i <= 65; ++i) {
575 }
else if (i <= 17) {
577 }
else if (i <= 64) {
583 ASSERT_EQ(expected, vector_compl.getStartOfOneSequenceBefore(i)) <<
" input index is i=" << i;
590 for (uint64_t i = 0; i < 32; ++i) {
591 vector.
set(i, i % 2 == 0);
595 for (uint64_t bit : vector) {
602TEST(BitVectorTest, ReverseIterator) {
606 for (; i < vector.
size(); i += 3) {
610 for (
auto bitIt = vector.
rbegin(); bitIt != vector.
rend(); ++bitIt) {
612 ASSERT_EQ(i, *bitIt);
617TEST(BitVectorTest, CompareAndSwap) {
619 vector.
setFromInt(0, 64, 2377830234574424100);
620 vector.
setFromInt(64, 64, 1152921504607379586);
624 ASSERT_FALSE(result);
635 ASSERT_EQ(129ul, vector1.size());
636 ASSERT_TRUE(vector1.get(3));
637 ASSERT_TRUE(vector1.get(5));
638 ASSERT_TRUE(vector1.get(10 + 64));
639 ASSERT_TRUE(vector1.get(12 + 64));
640 ASSERT_EQ(4ul, vector1.getNumberOfSetBits());
646 ASSERT_EQ(64ul, vector1.size());
647 ASSERT_EQ(2ul, vector1.getNumberOfSetBits());
650 ASSERT_EQ(128ul, vector2.size());
651 ASSERT_EQ(2ul, vector2.getNumberOfSetBits());
654TEST(BitVectorTest, Assignment) {
658 ASSERT_TRUE(v1.get(9999));
661TEST(BitVectorTest, ZeroSized) {
663 EXPECT_EQ(0ul, v.
size());
664 EXPECT_TRUE(v.
empty());
665 EXPECT_TRUE(v.
full());
671 for (uint64_t entry : v) {
672 FAIL() <<
"Should not iterate over an empty bit vector.";
TEST(BitVectorTest, InitToZero)
PositionIteratorType Iterator
A bit vector that is internally represented as a vector of 64-bit values.
void complement()
Negates all bits in the bit vector.
const_reverse_iterator rbegin() const
Returns a reverse iterator to the indices of the set bits in the bit vector.
uint64_t getNextSetIndex(uint64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to true in the bit vector.
bool isDisjointFrom(BitVector const &other) const
Checks whether none of the bits that are set in the current bit vector are also set in the given bit ...
bool full() const
Retrieves whether all bits are set in this bit vector.
const_reverse_iterator rend() const
Returns a reverse iterator pointing at the element past the front of the bit vector.
const_iterator end() const
Returns an iterator pointing at the element past the back of the bit vector.
bool empty() const
Retrieves whether no bits are set to true in this bit vector.
void clear()
Removes all set bits from the bit vector.
bool isSubsetOf(BitVector const &other) const
Checks whether all bits that are set in the current bit vector are also set in the given bit vector.
BitVector implies(BitVector const &other) const
Performs a logical "implies" with the given bit vector.
uint64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
uint64_t getNextUnsetIndex(uint64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to false in the bit vector.
bool compareAndSwap(uint64_t start1, uint64_t start2, uint64_t length)
Compare two intervals [start1, start1+length] and [start2, start2+length] and swap them if the second...
void setFromInt(uint64_t bitIndex, uint64_t numberOfBits, uint64_t value)
Sets the selected number of lowermost bits of the provided value at the given bit index.
BitVector permute(std::vector< uint64_t > const &inversePermutation) const
Apply a permutation of entries.
void set(uint64_t index, bool value=true)
Sets the given truth value at the given index.
void increment()
Increments the (unsigned) number represented by this BitVector by one.
const_iterator begin() const
Returns an iterator to the indices of the set bits in the bit vector.
uint64_t getStartOfZeroSequenceBefore(uint64_t endIndex) const
Retrieves the smallest index i such that all bits in the range [i,endIndex) are 0.
uint64_t getAsInt(uint64_t bitIndex, uint64_t numberOfBits) const
Retrieves the content of the current bit vector at the given index for the given number of bits as an...
size_t size() const
Retrieves the number of bits this bit vector can store.
void resize(uint64_t newLength, bool init=false)
Resizes the bit vector to hold the given new number of bits.
void expandSize(bool init=false)
bool get(uint64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
uint64_t getNumberOfSetBitsBeforeIndex(uint64_t index) const
Retrieves the number of bits set in this bit vector with an index strictly smaller than the given one...
BitVector permuteGroupedVector(std::vector< uint64_t > const &inversePermutation, std::vector< uint64_t > const &rowGroupIndices) const
Apply a permutation of entries assuming a grouped vector.
void concat(BitVector const &extension)
Concatenate this bitvector with another bitvector.