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 for (uint64_t i = 0; i < 32; ++i) {
290 ASSERT_EQ(vector3.
get(i), vector4.
get(i));
291 ASSERT_FALSE(vector5.
get(i));
295TEST(BitVectorTest, OperatorModulo) {
299 for (uint64_t i = 0; i < 15; ++i) {
300 vector2.
set(i, i % 2 == 0);
309 ASSERT_EQ(8ul, moduloResult.
size());
312 for (uint64_t i = 0; i < 8; ++i) {
313 if (i == 1 || i == 3) {
314 ASSERT_TRUE(moduloResult.
get(i));
316 ASSERT_FALSE(moduloResult.
get(i));
321TEST(BitVectorTest, OperatorNot) {
325 for (uint64_t i = 0; i < 32; ++i) {
326 vector1.
set(i, i % 2 == 0);
327 vector2.
set(i, i % 2 == 1);
332 for (uint64_t i = 0; i < 32; ++i) {
333 ASSERT_EQ(vector1.
get(i), notResult.
get(i));
337TEST(BitVectorTest, Complement) {
341 for (uint64_t i = 0; i < 32; ++i) {
342 vector1.
set(i, i % 2 == 0);
343 vector2.
set(i, i % 2 == 1);
348 for (uint64_t i = 0; i < 32; ++i) {
349 ASSERT_EQ(vector1.
get(i), vector2.
get(i));
353TEST(BitVectorTest, Increment) {
359 vector2.
set(0,
true);
360 EXPECT_EQ(vector1, vector2);
365 vector2.
set(1,
true);
366 EXPECT_EQ(vector1, vector2);
370 vector2.
set(0,
true);
371 EXPECT_EQ(vector1, vector2);
374 for (uint64_t i = 0; i < 66; ++i) {
375 vector1.
set(i,
true);
380 vector2.
set(66,
true);
381 EXPECT_EQ(vector1, vector2);
385 vector2.
set(0,
true);
386 EXPECT_EQ(vector1, vector2);
390 EXPECT_TRUE(vector1.
full());
392 EXPECT_TRUE(vector1.
empty());
397 std::vector<uint64_t> inversePermutation = {0, 1, 3, 2, 4, 6, 5, 8, 7};
400 EXPECT_TRUE(vector2.
get(2));
401 EXPECT_TRUE(vector2.
get(6));
404TEST(BitVectorTest, permuteGrouped) {
406 std::vector<uint64_t> inversePermutation = {1, 0, 2};
407 std::vector<uint64_t> groupIndices = {0, 3, 5, 6};
410 EXPECT_EQ(expected, permuted);
417 for (uint64_t i = 0; i < 32; ++i) {
418 vector1.
set(i, i % 2 == 0);
420 vector2.
set(31,
false);
421 vector2.
set(30,
false);
425 for (uint64_t i = 0; i < 30; ++i) {
426 ASSERT_TRUE(impliesResult.
get(i));
428 ASSERT_FALSE(impliesResult.
get(30));
429 ASSERT_TRUE(impliesResult.
get(31));
436 for (uint64_t i = 0; i < 32; ++i) {
437 vector1.
set(i, i % 2 == 0);
442 vector2.
set(16,
false);
451 for (uint64_t i = 0; i < 32; ++i) {
452 vector1.
set(i, i % 2 == 0);
453 vector2.
set(i, i % 2 == 1);
458 vector2.
set(16,
true);
466 ASSERT_TRUE(vector.
empty());
468 vector.
set(17,
true);
470 ASSERT_FALSE(vector.
empty());
472 vector.
set(17,
false);
473 vector.
set(18,
false);
475 ASSERT_TRUE(vector.
empty());
481 ASSERT_TRUE(vector.
full());
483 vector.
set(17,
false);
485 ASSERT_FALSE(vector.
full());
487 vector.
set(17,
true);
488 vector.
set(18,
true);
490 ASSERT_TRUE(vector.
full());
493TEST(BitVectorTest, NumberOfSetBits) {
496 for (uint64_t i = 0; i < 32; ++i) {
497 vector.
set(i, i % 2 == 0);
503TEST(BitVectorTest, NumberOfSetBitsBeforeIndex) {
506 for (uint64_t i = 0; i < 32; ++i) {
507 vector.
set(i, i % 2 == 0);
516 ASSERT_TRUE(vector.
begin() == vector.
end());
520 ASSERT_FALSE(vector.
begin() == vector.
end());
522 vector.
set(17,
false);
524 ASSERT_TRUE(vector.
begin() == vector.
end());
527TEST(BitVectorTest, NextSetIndex) {
540TEST(BitVectorTest, NextUnsetIndex) {
555TEST(BitVectorTest, SequenceBefore) {
562 auto vector_compl = ~vector;
564 for (uint64_t i = 0; i <= 65; ++i) {
568 }
else if (i <= 17) {
570 }
else if (i <= 64) {
576 ASSERT_EQ(expected, vector_compl.getStartOfOneSequenceBefore(i)) <<
" input index is i=" << i;
583 for (uint64_t i = 0; i < 32; ++i) {
584 vector.
set(i, i % 2 == 0);
588 for (uint64_t bit : vector) {
595TEST(BitVectorTest, ReverseIterator) {
599 for (; i < vector.
size(); i += 3) {
603 for (
auto bitIt = vector.
rbegin(); bitIt != vector.
rend(); ++bitIt) {
605 ASSERT_EQ(i, *bitIt);
610TEST(BitVectorTest, CompareAndSwap) {
612 vector.
setFromInt(0, 64, 2377830234574424100);
613 vector.
setFromInt(64, 64, 1152921504607379586);
617 ASSERT_FALSE(result);
628 ASSERT_EQ(129ul, vector1.size());
629 ASSERT_TRUE(vector1.get(3));
630 ASSERT_TRUE(vector1.get(5));
631 ASSERT_TRUE(vector1.get(10 + 64));
632 ASSERT_TRUE(vector1.get(12 + 64));
633 ASSERT_EQ(4ul, vector1.getNumberOfSetBits());
639 ASSERT_EQ(64ul, vector1.size());
640 ASSERT_EQ(2ul, vector1.getNumberOfSetBits());
643 ASSERT_EQ(128ul, vector2.size());
644 ASSERT_EQ(2ul, vector2.getNumberOfSetBits());
647TEST(BitVectorTest, Assignment) {
651 ASSERT_TRUE(v1.get(9999));
654TEST(BitVectorTest, ZeroSized) {
656 EXPECT_EQ(0ul, v.
size());
657 EXPECT_TRUE(v.
empty());
658 EXPECT_TRUE(v.
full());
664 for (uint64_t entry : v) {
665 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.