Added more masking fixes + tests

This commit is contained in:
John Wellbelove 2026-10-15 17:24:46 +01:00
parent 7e111e81d6
commit 82a616fb6a
5 changed files with 369 additions and 150 deletions

View File

@ -665,19 +665,11 @@ namespace etl
*lhs_pbuffer ^= *rhs_pbuffer; *lhs_pbuffer ^= *rhs_pbuffer;
} }
//*************************************************************************
/// operator ~
/// NOT the value in the buffer
//*************************************************************************
static ETL_CONSTEXPR14 void operator_not(pointer pbuffer, size_t /*number_of_elements*/) ETL_NOEXCEPT
{
*pbuffer = ~*pbuffer;
}
//************************************************************************* //*************************************************************************
/// operator_shift_left /// operator_shift_left
//************************************************************************* //*************************************************************************
static ETL_CONSTEXPR14 void operator_shift_left(pointer pbuffer, size_t /*number_of_elements*/, size_t active_bits, size_t shift) ETL_NOEXCEPT static ETL_CONSTEXPR14 void operator_shift_left(pointer pbuffer, size_t /*number_of_elements*/, size_t active_bits, size_t shift,
element_type top_mask) ETL_NOEXCEPT
{ {
if (shift >= active_bits) if (shift >= active_bits)
{ {
@ -686,6 +678,7 @@ namespace etl
else else
{ {
*pbuffer <<= shift; *pbuffer <<= shift;
*pbuffer &= top_mask;
} }
} }
@ -716,9 +709,10 @@ namespace etl
/// Initialise from an unsigned long long. /// Initialise from an unsigned long long.
//************************************************************************* //*************************************************************************
template <typename TElementType> template <typename TElementType>
static ETL_CONSTEXPR14 void initialise(pointer pbuffer, size_t /*number_of_elements*/, unsigned long long value) ETL_NOEXCEPT static ETL_CONSTEXPR14 void initialise(pointer pbuffer, size_t /*number_of_elements*/, unsigned long long value, TElementType top_mask)
ETL_NOEXCEPT
{ {
*pbuffer = static_cast<TElementType>(value); *pbuffer = static_cast<TElementType>(value) & top_mask;
} }
//************************************************************************* //*************************************************************************
@ -1405,7 +1399,8 @@ namespace etl
//************************************************************************* //*************************************************************************
/// operator_shift_left /// operator_shift_left
//************************************************************************* //*************************************************************************
static ETL_CONSTEXPR14 void operator_shift_left(pointer pbuffer, size_t number_of_elements, size_t active_bits, size_t shift) ETL_NOEXCEPT static ETL_CONSTEXPR14 void operator_shift_left(pointer pbuffer, size_t number_of_elements, size_t active_bits, size_t shift,
element_type top_mask) ETL_NOEXCEPT
{ {
if (shift >= active_bits) if (shift >= active_bits)
{ {
@ -1458,6 +1453,8 @@ namespace etl
{ {
pbuffer[i] = 0; pbuffer[i] = 0;
} }
pbuffer[(number_of_elements - 1U)] &= top_mask;
} }
} }
@ -1538,7 +1535,7 @@ namespace etl
template <typename TElementType> template <typename TElementType>
static static
ETL_CONSTEXPR14 typename etl::enable_if< etl::integral_limits<TElementType>::bits == etl::integral_limits<unsigned long long>::bits, void>::type ETL_CONSTEXPR14 typename etl::enable_if< etl::integral_limits<TElementType>::bits == etl::integral_limits<unsigned long long>::bits, void>::type
initialise(pointer pbuffer, size_t number_of_elements, unsigned long long value) ETL_NOEXCEPT initialise(pointer pbuffer, size_t number_of_elements, unsigned long long value, TElementType top_mask) ETL_NOEXCEPT
{ {
size_t i = 0UL; size_t i = 0UL;
@ -1550,6 +1547,8 @@ namespace etl
{ {
pbuffer[i++] = All_Clear_Element; pbuffer[i++] = All_Clear_Element;
} }
pbuffer[number_of_elements - 1] &= top_mask;
} }
//************************************************************************* //*************************************************************************
@ -1560,7 +1559,7 @@ namespace etl
template <typename TElementType> template <typename TElementType>
static static
ETL_CONSTEXPR14 typename etl::enable_if< etl::integral_limits<TElementType>::bits != etl::integral_limits<unsigned long long>::bits, void>::type ETL_CONSTEXPR14 typename etl::enable_if< etl::integral_limits<TElementType>::bits != etl::integral_limits<unsigned long long>::bits, void>::type
initialise(pointer pbuffer, size_t number_of_elements, unsigned long long value) ETL_NOEXCEPT initialise(pointer pbuffer, size_t number_of_elements, unsigned long long value, element_type top_mask) ETL_NOEXCEPT
{ {
size_t i = 0UL; size_t i = 0UL;
@ -1578,6 +1577,8 @@ namespace etl
{ {
pbuffer[i++] = All_Clear_Element; pbuffer[i++] = All_Clear_Element;
} }
pbuffer[number_of_elements - 1] &= top_mask;
} }
//************************************************************************* //*************************************************************************
@ -1810,7 +1811,7 @@ namespace etl
ETL_CONSTEXPR14 bitset(TValue value, typename etl::enable_if<is_integral<TValue>::value>::type* = 0) ETL_NOEXCEPT ETL_CONSTEXPR14 bitset(TValue value, typename etl::enable_if<is_integral<TValue>::value>::type* = 0) ETL_NOEXCEPT
: buffer() : buffer()
{ {
implementation::template initialise<element_type>(buffer, Number_Of_Elements, static_cast<unsigned long long>(value)); implementation::template initialise<element_type>(buffer, Number_Of_Elements, static_cast<unsigned long long>(value), Top_Mask);
} }
//************************************************************************* //*************************************************************************
@ -2396,7 +2397,7 @@ namespace etl
{ {
etl::bitset<Active_Bits, TElement> temp(*this); etl::bitset<Active_Bits, TElement> temp(*this);
implementation::operator_shift_left(temp.buffer, Number_Of_Elements, Active_Bits, shift); implementation::operator_shift_left(temp.buffer, Number_Of_Elements, Active_Bits, shift, Top_Mask);
return temp; return temp;
} }
@ -2412,7 +2413,7 @@ namespace etl
} }
else else
{ {
implementation::operator_shift_left(buffer, Number_Of_Elements, Active_Bits, shift); implementation::operator_shift_left(buffer, Number_Of_Elements, Active_Bits, shift, Top_Mask);
} }
return *this; return *this;
@ -2749,7 +2750,7 @@ namespace etl
: pbuffer(pbuffer_) : pbuffer(pbuffer_)
{ {
ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer)); ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer));
implementation::template initialise<element_type>(pbuffer, Number_Of_Elements, value); implementation::template initialise<element_type>(pbuffer, Number_Of_Elements, value, Top_Mask);
} }
//************************************************************************* //*************************************************************************
@ -2759,7 +2760,7 @@ namespace etl
ETL_CONSTEXPR14 bitset_ext(TValue value, buffer_type& buffer, typename etl::enable_if<is_integral<TValue>::value>::type* = 0) ETL_NOEXCEPT ETL_CONSTEXPR14 bitset_ext(TValue value, buffer_type& buffer, typename etl::enable_if<is_integral<TValue>::value>::type* = 0) ETL_NOEXCEPT
: pbuffer(buffer.data()) : pbuffer(buffer.data())
{ {
implementation::template initialise<element_type>(pbuffer, Number_Of_Elements, static_cast<unsigned long long>(value)); implementation::template initialise<element_type>(pbuffer, Number_Of_Elements, static_cast<unsigned long long>(value), Top_Mask);
} }
//************************************************************************* //*************************************************************************
@ -3343,7 +3344,7 @@ namespace etl
//************************************************************************* //*************************************************************************
ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement>& operator<<=(size_t shift) ETL_NOEXCEPT ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement>& operator<<=(size_t shift) ETL_NOEXCEPT
{ {
implementation::operator_shift_left(pbuffer, Number_Of_Elements, Active_Bits, shift); implementation::operator_shift_left(pbuffer, Number_Of_Elements, Active_Bits, shift, Top_Mask);
return *this; return *this;
} }

View File

@ -311,7 +311,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -328,7 +328,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -345,7 +345,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -362,7 +362,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -379,7 +379,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -394,14 +394,12 @@ namespace
CHECK_EQUAL(compare.any(), data.any()); CHECK_EQUAL(compare.any(), data.any());
CHECK_EQUAL(compare.all(), data.all()); CHECK_EQUAL(compare.all(), data.all());
CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
for (size_t i = 0UL; i < data.size(); ++i) for (size_t i = 0UL; i < data.size(); ++i)
{ {
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -2743,40 +2741,108 @@ namespace
} }
//************************************************************************* //*************************************************************************
TEST(test_issue_497) TEST(test_issue_497_for_flip)
{ {
etl::bitset<11, uint8_t> bits; const int Size = 11;
using Bitset = etl::bitset<Size, uint8_t>;
Bitset bits;
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(Bitset::Number_Of_Elements > 1);
// Check the initial state of the bitset. // Check the initial state of the bitset.
size_t count_set = bits.count(); CHECK_EQUAL(0, bits.count());
size_t count_size = bits.size(); CHECK_FALSE(bits.all());
bool all_set = bits.all();
CHECK_EQUAL(0, count_set);
CHECK_EQUAL(11, count_size);
CHECK_EQUAL(false, all_set);
// Copy flipped bits to a new bitset. // Copy flipped bits to a new bitset.
etl::bitset<11, uint8_t> negated_bits = ~bits; Bitset negated_bits = ~bits;
count_set = negated_bits.count(); CHECK_EQUAL(Size, negated_bits.count());
count_size = negated_bits.size(); CHECK_TRUE(negated_bits.all());
all_set = negated_bits.all();
CHECK_EQUAL(11, count_set);
CHECK_EQUAL(11, count_size);
CHECK_EQUAL(true, all_set);
// Flip the bits in the original bitset. // Flip the bits in the original bitset.
bits.flip(); bits.flip();
count_set = bits.count(); CHECK_EQUAL(11, bits.count());
count_size = bits.size(); CHECK_TRUE(bits.all());
all_set = bits.all(); }
CHECK_EQUAL(11, count_set); //*************************************************************************
CHECK_EQUAL(11, count_size); TEST(test_issue_497_for_left_shift)
CHECK_EQUAL(true, all_set); {
const int Size = 11;
const unsigned long long Initial = (1ULL << Size) - 1;
using Bitset = etl::bitset<Size, uint8_t>;
// Create a bitset with all bits set to 1.
Bitset bits(Initial);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(Bitset::Number_Of_Elements > 1);
// Check the initial state of the bitset.
CHECK_EQUAL(Size, bits.count());
CHECK_TRUE(bits.all());
// The amount to shift so that the value is moved to the msb of the storage.
size_t shift_amount = Bitset::Allocated_Bits - Size;
bits = bits << shift_amount;
CHECK_EQUAL(Size - shift_amount, bits.count());
CHECK_FALSE(bits.all());
}
//*************************************************************************
TEST(test_issue_497_for_left_shift_equals)
{
const int Size = 11;
const unsigned long long Initial = (1ULL << Size) - 1;
using Bitset = etl::bitset<Size, uint8_t>;
// Create a bitset with all bits set to 1.
Bitset bits(Initial);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(bits.Number_Of_Elements > 1);
// Check the initial state of the bitset.
CHECK_EQUAL(Size, bits.count());
CHECK_TRUE(bits.all());
// The amount to shift so that the value is moved to the msb of the storage.
size_t shift_amount = bits.Allocated_Bits - Size;
bits <<= shift_amount;
CHECK_EQUAL(Size - shift_amount, bits.count());
CHECK_FALSE(bits.all());
}
//*************************************************************************
TEST(test_issue_497_for_initial_value)
{
const int Size = 11;
using Bitset = etl::bitset<Size, uint8_t>;
const unsigned long long Initial = std::numeric_limits<uint16_t>::max();
const unsigned long long Expected = ((1ULL << Size) - 1);
// Create a bitset from a value larger than the bitset size.
Bitset bits(Initial);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(Bitset::Number_Of_Elements > 1);
// Check the initial state of the bitset.
CHECK_EQUAL(Expected, bits.to_ullong());
CHECK_EQUAL(Size, bits.count());
CHECK_TRUE(bits.all());
} }
} }
} // namespace } // namespace

View File

@ -308,7 +308,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -325,7 +325,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -342,7 +342,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -359,14 +359,14 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
TEST(test_construct_from_64_bit_char_string_where_the_bitset_size_is_60) TEST(test_construct_from_64_bit_char_string_where_the_bitset_size_is_60)
{ {
std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001"); std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001");
etl::bitset<60> data("110001001000110100010101100111001100010010001101000101011001"); etl::bitset<60, uint64_t> data("110001001000110100010101100111001100010010001101000101011001");
CHECK_EQUAL(compare.size(), data.size()); CHECK_EQUAL(compare.size(), data.size());
CHECK_EQUAL(compare.count(), data.count()); CHECK_EQUAL(compare.count(), data.count());
@ -376,7 +376,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -398,7 +398,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -2874,40 +2874,108 @@ namespace
} }
//************************************************************************* //*************************************************************************
TEST(test_issue_497) TEST(test_issue_497_for_flip)
{ {
etl::bitset<5, uint8_t> bits; const int Size = 11;
using Bitset = etl::bitset<Size, uint16_t>;
Bitset bits;
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(Bitset::Number_Of_Elements == 1);
// Check the initial state of the bitset. // Check the initial state of the bitset.
size_t count_set = bits.count(); CHECK_EQUAL(0, bits.count());
size_t count_size = bits.size(); CHECK_FALSE(bits.all());
bool all_set = bits.all();
CHECK_EQUAL(0, count_set);
CHECK_EQUAL(5, count_size);
CHECK_EQUAL(false, all_set);
// Copy flipped bits to a new bitset. // Copy flipped bits to a new bitset.
etl::bitset<5, uint8_t> negated_bits = ~bits; Bitset negated_bits = ~bits;
count_set = negated_bits.count(); CHECK_EQUAL(Size, negated_bits.count());
count_size = negated_bits.size(); CHECK_TRUE(negated_bits.all());
all_set = negated_bits.all();
CHECK_EQUAL(5, count_set);
CHECK_EQUAL(5, count_size);
CHECK_EQUAL(true, all_set);
// Flip the bits in the original bitset. // Flip the bits in the original bitset.
bits.flip(); bits.flip();
count_set = bits.count(); CHECK_EQUAL(11, bits.count());
count_size = bits.size(); CHECK_TRUE(bits.all());
all_set = bits.all(); }
CHECK_EQUAL(5, count_set); //*************************************************************************
CHECK_EQUAL(5, count_size); TEST(test_issue_497_for_left_shift)
CHECK_EQUAL(true, all_set); {
const int Size = 11;
const unsigned long long Initial = (1ULL << Size) - 1;
using Bitset = etl::bitset<Size, uint16_t>;
// Create a bitset with all bits set to 1.
Bitset bits(Initial);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(Bitset::Number_Of_Elements == 1);
// Check the initial state of the bitset.
CHECK_EQUAL(Size, bits.count());
CHECK_TRUE(bits.all());
// The amount to shift so that the value is moved to the msb of the storage.
size_t shift_amount = Bitset::Allocated_Bits - Size;
bits = bits << shift_amount;
CHECK_EQUAL(Size - shift_amount, bits.count());
CHECK_FALSE(bits.all());
}
//*************************************************************************
TEST(test_issue_497_for_left_shift_equals)
{
const int Size = 11;
const unsigned long long Initial = (1ULL << Size) - 1;
using Bitset = etl::bitset<Size, uint16_t>;
// Create a bitset with all bits set to 1.
Bitset bits(Initial);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(bits.Number_Of_Elements == 1);
// Check the initial state of the bitset.
CHECK_EQUAL(Size, bits.count());
CHECK_TRUE(bits.all());
// The amount to shift so that the value is moved to the msb of the storage.
size_t shift_amount = bits.Allocated_Bits - Size;
bits <<= shift_amount;
CHECK_EQUAL(Size - shift_amount, bits.count());
CHECK_FALSE(bits.all());
}
//*************************************************************************
TEST(test_issue_497_for_initial_value)
{
const int Size = 11;
using Bitset = etl::bitset<Size, uint16_t>;
const unsigned long long Initial = std::numeric_limits<uint16_t>::max();
const unsigned long long Expected = ((1ULL << Size) - 1);
// Create a bitset from a value larger than the bitset size.
Bitset bits(Initial);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(Bitset::Number_Of_Elements == 1);
// Check the initial state of the bitset.
CHECK_EQUAL(Expected, bits.to_ullong());
CHECK_EQUAL(Size, bits.count());
CHECK_TRUE(bits.all());
} }
} }
} // namespace } // namespace

View File

@ -252,7 +252,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -270,7 +270,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -288,7 +288,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -306,7 +306,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -324,7 +324,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -347,7 +347,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -2490,40 +2490,83 @@ namespace
} }
//************************************************************************* //*************************************************************************
TEST(test_issue_497) TEST(test_issue_497_for_flip)
{ {
etl::bitset_ext<11>::buffer_type buffer1; const int Size = 11;
etl::bitset_ext<11> bits(buffer1);
using Bitset = etl::bitset_ext<Size, uint8_t>;
using Buffer = Bitset::buffer_type;
Buffer buffer1;
Bitset bits(buffer1);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(Bitset::Number_Of_Elements > 1);
// Check the initial state of the bitset. // Check the initial state of the bitset.
size_t count_set = bits.count(); CHECK_EQUAL(0, bits.count());
size_t count_size = bits.size(); CHECK_FALSE(bits.all());
bool all_set = bits.all();
CHECK_EQUAL(0, count_set);
CHECK_EQUAL(11, count_size);
CHECK_EQUAL(false, all_set);
// Copy flipped bits to a new bitset. // Copy flipped bits to a new bitset.
etl::bitset_ext<11>::buffer_type buffer2; // Copy flipped bits to a new bitset.
etl::bitset_ext<11> negated_bits(bits.flip(), buffer2); Buffer buffer2;
Bitset negated_bits(bits.flip(), buffer2);
count_set = negated_bits.count(); CHECK_EQUAL(Size, negated_bits.count());
count_size = negated_bits.size(); CHECK_TRUE(negated_bits.all());
all_set = negated_bits.all(); }
CHECK_EQUAL(11, count_set); //*************************************************************************
CHECK_EQUAL(11, count_size); TEST(test_issue_497_for_left_shift_equals)
CHECK_EQUAL(true, all_set); {
const int Size = 11;
const unsigned long long Initial = (1ULL << Size) - 1;
// Test the flipped the bits in the original bitset. using Bitset = etl::bitset_ext<Size, uint8_t>;
count_set = bits.count(); using Buffer = Bitset::buffer_type;
count_size = bits.size();
all_set = bits.all();
CHECK_EQUAL(11, count_set); // Create a bitset with all bits set to 1.
CHECK_EQUAL(11, count_size); Buffer buffer1;
CHECK_EQUAL(true, all_set); Bitset bits(Initial, buffer1);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(bits.Number_Of_Elements > 1);
// Check the initial state of the bitset.
CHECK_EQUAL(Size, bits.count());
CHECK_TRUE(bits.all());
// The amount to shift so that the value is moved to the msb of the storage.
size_t shift_amount = bits.Allocated_Bits - Size;
bits <<= shift_amount;
CHECK_EQUAL(Size - shift_amount, bits.count());
CHECK_FALSE(bits.all());
}
//*************************************************************************
TEST(test_issue_497_for_initial_value)
{
const int Size = 11;
using Bitset = etl::bitset_ext<Size, uint8_t>;
using Buffer = Bitset::buffer_type;
const unsigned long long Initial = std::numeric_limits<uint16_t>::max();
const unsigned long long Expected = ((1ULL << Size) - 1);
// Create a bitset from a value larger than the bitset size.
Buffer buffer1;
Bitset bits(Initial, buffer1);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(Bitset::Number_Of_Elements > 1);
// Check the initial state of the bitset.
CHECK_EQUAL(Expected, bits.to_ullong());
CHECK_EQUAL(Size, bits.count());
CHECK_TRUE(bits.all());
} }
} }
} // namespace } // namespace

View File

@ -280,7 +280,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -298,7 +298,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -316,7 +316,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -334,7 +334,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -352,7 +352,7 @@ namespace
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong()); CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -374,8 +374,6 @@ namespace
{ {
CHECK_EQUAL(compare.test(i), data.test(i)); CHECK_EQUAL(compare.test(i), data.test(i));
} }
CHECK_EQUAL(compare.to_ullong(), data.to_ullong());
} }
//************************************************************************* //*************************************************************************
@ -2656,42 +2654,85 @@ namespace
CHECK_EQUAL(32, ETL_OR_STD17::size(b)); CHECK_EQUAL(32, ETL_OR_STD17::size(b));
} }
}
//************************************************************************* //*************************************************************************
TEST(test_issue_497) TEST(test_issue_497_for_flip)
{ {
etl::bitset_ext<11, uint8_t>::buffer_type buffer1; const int Size = 11;
etl::bitset_ext<11, uint8_t> bits(buffer1);
// Check the initial state of the bitset. using Bitset = etl::bitset_ext<Size, uint16_t>;
size_t count_set = bits.count(); using Buffer = Bitset::buffer_type;
size_t count_size = bits.size();
bool all_set = bits.all();
CHECK_EQUAL(0, count_set); Buffer buffer1;
CHECK_EQUAL(11, count_size); Bitset bits(buffer1);
CHECK_EQUAL(false, all_set);
// Copy flipped bits to a new bitset. // Ensure that we are using multiple elements for the bitset.
etl::bitset_ext<11, uint8_t>::buffer_type buffer2; CHECK_TRUE(Bitset::Number_Of_Elements == 1);
etl::bitset_ext<11, uint8_t> negated_bits(bits.flip(), buffer2);
count_set = negated_bits.count(); // Check the initial state of the bitset.
count_size = negated_bits.size(); CHECK_EQUAL(0, bits.count());
all_set = negated_bits.all(); CHECK_FALSE(bits.all());
CHECK_EQUAL(11, count_set); // Copy flipped bits to a new bitset.
CHECK_EQUAL(11, count_size); // Copy flipped bits to a new bitset.
CHECK_EQUAL(true, all_set); Buffer buffer2;
Bitset negated_bits(bits.flip(), buffer2);
// Test the flipped the bits in the original bitset. CHECK_EQUAL(Size, negated_bits.count());
count_set = bits.count(); CHECK_TRUE(negated_bits.all());
count_size = bits.size(); }
all_set = bits.all();
CHECK_EQUAL(11, count_set); //*************************************************************************
CHECK_EQUAL(11, count_size); TEST(test_issue_497_for_left_shift_equals)
CHECK_EQUAL(true, all_set); {
const int Size = 11;
const unsigned long long Initial = (1ULL << Size) - 1;
using Bitset = etl::bitset_ext<Size, uint16_t>;
using Buffer = Bitset::buffer_type;
// Create a bitset with all bits set to 1.
Buffer buffer1;
Bitset bits(Initial, buffer1);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(bits.Number_Of_Elements == 1);
// Check the initial state of the bitset.
CHECK_EQUAL(Size, bits.count());
CHECK_TRUE(bits.all());
// The amount to shift so that the value is moved to the msb of the storage.
size_t shift_amount = bits.Allocated_Bits - Size;
bits <<= shift_amount;
CHECK_EQUAL(Size - shift_amount, bits.count());
CHECK_FALSE(bits.all());
}
//*************************************************************************
TEST(test_issue_497_for_initial_value)
{
const int Size = 11;
using Bitset = etl::bitset_ext<Size, uint16_t>;
using Buffer = Bitset::buffer_type;
const unsigned long long Initial = std::numeric_limits<uint16_t>::max();
const unsigned long long Expected = ((1ULL << Size) - 1);
// Create a bitset from a value larger than the bitset size.
Buffer buffer1;
Bitset bits(Initial, buffer1);
// Ensure that we are using multiple elements for the bitset.
CHECK_TRUE(Bitset::Number_Of_Elements == 1);
// Check the initial state of the bitset.
CHECK_EQUAL(Expected, bits.to_ullong());
CHECK_EQUAL(Size, bits.count());
CHECK_TRUE(bits.all());
}
} }
} // namespace } // namespace