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 std::cout << vector << std::endl;
174 ASSERT_EQ(72ul, vector.
size());
177 for (uint64_t i = 0; i < 32; ++i) {
178 ASSERT_TRUE(vector.
get(i));
180 for (uint64_t i = 32; i < 70; ++i) {
182 ASSERT_NO_THROW(result = vector.
get(i));
183 ASSERT_FALSE(result);
185 for (uint64_t i = 70; i < 72; ++i) {
186 ASSERT_TRUE(vector.
get(i));
190 ASSERT_EQ(16ul, vector.
size());
193 for (uint64_t i = 0; i < 16; ++i) {
194 ASSERT_TRUE(vector.
get(i));
198 ASSERT_EQ(65ul, vector.
size());
199 ASSERT_TRUE(vector.
full());
202TEST(BitVectorTest, OperatorAnd) {
206 for (
int i = 0; i < 32; ++i) {
207 vector1.
set(i, i % 2 == 0);
208 vector2.
set(i, i % 2 == 1);
214 for (uint64_t i = 0; i < 31; ++i) {
215 ASSERT_FALSE(andResult.
get(i));
217 ASSERT_TRUE(andResult.
get(31));
220TEST(BitVectorTest, OperatorAndEqual) {
224 for (
int i = 0; i < 32; ++i) {
225 vector1.
set(i, i % 2 == 0);
226 vector2.
set(i, i % 2 == 1);
233 for (uint64_t i = 0; i < 31; ++i) {
234 ASSERT_FALSE(vector1.
get(i));
236 ASSERT_TRUE(vector1.
get(31));
239TEST(BitVectorTest, OperatorOr) {
243 for (uint64_t i = 0; i < 32; ++i) {
244 vector1.
set(i, i % 2 == 0);
245 vector2.
set(i, i % 2 == 1);
247 vector1.
set(31,
false);
248 vector2.
set(31,
false);
252 for (uint64_t i = 0; i < 31; ++i) {
253 ASSERT_TRUE(orResult.
get(i));
255 ASSERT_FALSE(orResult.
get(31));
258TEST(BitVectorTest, OperatorOrEqual) {
262 for (uint64_t i = 0; i < 32; ++i) {
263 vector1.
set(i, i % 2 == 0);
264 vector2.
set(i, i % 2 == 1);
266 vector1.
set(31,
false);
267 vector2.
set(31,
false);
271 for (uint64_t i = 0; i < 31; ++i) {
272 ASSERT_TRUE(vector1.
get(i));
274 ASSERT_FALSE(vector1.
get(31));
277TEST(BitVectorTest, OperatorXor) {
281 for (uint64_t i = 0; i < 32; ++i) {
283 vector2.
set(i, i % 2 == 1);
290 for (uint64_t i = 0; i < 32; ++i) {
291 ASSERT_EQ(vector3.
get(i), vector4.
get(i));
292 ASSERT_FALSE(vector5.
get(i));
296TEST(BitVectorTest, OperatorModulo) {
300 for (uint64_t i = 0; i < 15; ++i) {
301 vector2.
set(i, i % 2 == 0);
310 ASSERT_EQ(8ul, moduloResult.
size());
313 for (uint64_t i = 0; i < 8; ++i) {
314 if (i == 1 || i == 3) {
315 ASSERT_TRUE(moduloResult.
get(i));
317 ASSERT_FALSE(moduloResult.
get(i));
322TEST(BitVectorTest, OperatorNot) {
326 for (uint64_t i = 0; i < 32; ++i) {
327 vector1.
set(i, i % 2 == 0);
328 vector2.
set(i, i % 2 == 1);
333 for (uint64_t i = 0; i < 32; ++i) {
334 ASSERT_EQ(vector1.
get(i), notResult.
get(i));
338TEST(BitVectorTest, Complement) {
342 for (uint64_t i = 0; i < 32; ++i) {
343 vector1.
set(i, i % 2 == 0);
344 vector2.
set(i, i % 2 == 1);
349 for (uint64_t i = 0; i < 32; ++i) {
350 ASSERT_EQ(vector1.
get(i), vector2.
get(i));
354TEST(BitVectorTest, Increment) {
360 vector2.
set(0,
true);
361 EXPECT_EQ(vector1, vector2);
366 vector2.
set(1,
true);
367 EXPECT_EQ(vector1, vector2);
371 vector2.
set(0,
true);
372 EXPECT_EQ(vector1, vector2);
375 for (uint64_t i = 0; i < 66; ++i) {
376 vector1.
set(i,
true);
381 vector2.
set(66,
true);
382 EXPECT_EQ(vector1, vector2);
386 vector2.
set(0,
true);
387 EXPECT_EQ(vector1, vector2);
391 EXPECT_TRUE(vector1.
full());
393 EXPECT_TRUE(vector1.
empty());
398 std::vector<uint64_t> inversePermutation = {0, 1, 3, 2, 4, 6, 5, 8, 7};
401 EXPECT_TRUE(vector2.
get(2));
402 EXPECT_TRUE(vector2.
get(6));
405TEST(BitVectorTest, permuteGrouped) {
407 std::vector<uint64_t> inversePermutation = {1, 0, 2};
408 std::vector<uint64_t> groupIndices = {0, 3, 5, 6};
411 EXPECT_EQ(expected, permuted);
418 for (uint64_t i = 0; i < 32; ++i) {
419 vector1.
set(i, i % 2 == 0);
421 vector2.
set(31,
false);
422 vector2.
set(30,
false);
426 for (uint64_t i = 0; i < 30; ++i) {
427 ASSERT_TRUE(impliesResult.
get(i));
429 ASSERT_FALSE(impliesResult.
get(30));
430 ASSERT_TRUE(impliesResult.
get(31));
437 for (uint64_t i = 0; i < 32; ++i) {
438 vector1.
set(i, i % 2 == 0);
443 vector2.
set(16,
false);
452 for (uint64_t i = 0; i < 32; ++i) {
453 vector1.
set(i, i % 2 == 0);
454 vector2.
set(i, i % 2 == 1);
459 vector2.
set(16,
true);
467 ASSERT_TRUE(vector.
empty());
469 vector.
set(17,
true);
471 ASSERT_FALSE(vector.
empty());
473 vector.
set(17,
false);
474 vector.
set(18,
false);
476 ASSERT_TRUE(vector.
empty());
482 ASSERT_TRUE(vector.
full());
484 vector.
set(17,
false);
486 ASSERT_FALSE(vector.
full());
488 vector.
set(17,
true);
489 vector.
set(18,
true);
491 ASSERT_TRUE(vector.
full());
494TEST(BitVectorTest, NumberOfSetBits) {
497 for (uint64_t i = 0; i < 32; ++i) {
498 vector.
set(i, i % 2 == 0);
504TEST(BitVectorTest, NumberOfSetBitsBeforeIndex) {
507 for (uint64_t i = 0; i < 32; ++i) {
508 vector.
set(i, i % 2 == 0);
517 ASSERT_TRUE(vector.
begin() == vector.
end());
521 ASSERT_FALSE(vector.
begin() == vector.
end());
523 vector.
set(17,
false);
525 ASSERT_TRUE(vector.
begin() == vector.
end());
528TEST(BitVectorTest, NextSetIndex) {
541TEST(BitVectorTest, NextUnsetIndex) {
556TEST(BitVectorTest, SequenceBefore) {
563 auto vector_compl = ~vector;
565 for (uint64_t i = 0; i <= 65; ++i) {
569 }
else if (i <= 17) {
571 }
else if (i <= 64) {
577 ASSERT_EQ(expected, vector_compl.getStartOfOneSequenceBefore(i)) <<
" input index is i=" << i;
584 for (uint64_t i = 0; i < 32; ++i) {
585 vector.
set(i, i % 2 == 0);
589 for (
auto bit : vector) {
596TEST(BitVectorTest, ReverseIterator) {
600 for (; i < vector.
size(); i += 3) {
604 for (
auto bitIt = vector.
rbegin(); bitIt != vector.
rend(); ++bitIt) {
606 ASSERT_EQ(i, *bitIt);
611TEST(BitVectorTest, CompareAndSwap) {
613 vector.
setFromInt(0, 64, 2377830234574424100);
614 vector.
setFromInt(64, 64, 1152921504607379586);
618 ASSERT_FALSE(result);
629 ASSERT_EQ(129ul, vector1.size());
630 ASSERT_TRUE(vector1.get(3));
631 ASSERT_TRUE(vector1.get(5));
632 ASSERT_TRUE(vector1.get(10 + 64));
633 ASSERT_TRUE(vector1.get(12 + 64));
634 ASSERT_EQ(4ul, vector1.getNumberOfSetBits());
640 ASSERT_EQ(64ul, vector1.size());
641 ASSERT_EQ(2ul, vector1.getNumberOfSetBits());
644 ASSERT_EQ(128ul, vector2.size());
645 ASSERT_EQ(2ul, vector2.getNumberOfSetBits());
648TEST(BitVectorTest, Assignment) {
652 ASSERT_TRUE(v1.get(9999));
655TEST(BitVectorTest, ZeroSized) {
657 EXPECT_EQ(0ul, v.
size());
658 EXPECT_TRUE(v.
empty());
659 EXPECT_TRUE(v.
full());
665 for (
auto const& entry : v) {
666 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.