diff --git a/include/etl/private/bitset_new.h b/include/etl/private/bitset_new.h index 7d2b2679..ba397209 100644 --- a/include/etl/private/bitset_new.h +++ b/include/etl/private/bitset_new.h @@ -665,19 +665,11 @@ namespace etl *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 //************************************************************************* - 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) { @@ -686,6 +678,7 @@ namespace etl else { *pbuffer <<= shift; + *pbuffer &= top_mask; } } @@ -716,9 +709,10 @@ namespace etl /// Initialise from an unsigned long long. //************************************************************************* template - 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(value); + *pbuffer = static_cast(value) & top_mask; } //************************************************************************* @@ -1405,7 +1399,8 @@ namespace etl //************************************************************************* /// 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) { @@ -1458,6 +1453,8 @@ namespace etl { pbuffer[i] = 0; } + + pbuffer[(number_of_elements - 1U)] &= top_mask; } } @@ -1538,7 +1535,7 @@ namespace etl template static ETL_CONSTEXPR14 typename etl::enable_if< etl::integral_limits::bits == etl::integral_limits::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; @@ -1550,6 +1547,8 @@ namespace etl { pbuffer[i++] = All_Clear_Element; } + + pbuffer[number_of_elements - 1] &= top_mask; } //************************************************************************* @@ -1560,7 +1559,7 @@ namespace etl template static ETL_CONSTEXPR14 typename etl::enable_if< etl::integral_limits::bits != etl::integral_limits::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; @@ -1578,6 +1577,8 @@ namespace etl { 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::value>::type* = 0) ETL_NOEXCEPT : buffer() { - implementation::template initialise(buffer, Number_Of_Elements, static_cast(value)); + implementation::template initialise(buffer, Number_Of_Elements, static_cast(value), Top_Mask); } //************************************************************************* @@ -2396,7 +2397,7 @@ namespace etl { etl::bitset 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; } @@ -2412,7 +2413,7 @@ namespace etl } 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; @@ -2749,7 +2750,7 @@ namespace etl : pbuffer(pbuffer_) { ETL_ASSERT(pbuffer != ETL_NULLPTR, ETL_ERROR(etl::bitset_invalid_buffer)); - implementation::template initialise(pbuffer, Number_Of_Elements, value); + implementation::template initialise(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::value>::type* = 0) ETL_NOEXCEPT : pbuffer(buffer.data()) { - implementation::template initialise(pbuffer, Number_Of_Elements, static_cast(value)); + implementation::template initialise(pbuffer, Number_Of_Elements, static_cast(value), Top_Mask); } //************************************************************************* @@ -3343,7 +3344,7 @@ namespace etl //************************************************************************* ETL_CONSTEXPR14 bitset_ext& 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; } diff --git a/test/test_bitset_new_default_element_type.cpp b/test/test_bitset_new_default_element_type.cpp index 0321519b..a1d12e77 100644 --- a/test/test_bitset_new_default_element_type.cpp +++ b/test/test_bitset_new_default_element_type.cpp @@ -311,7 +311,7 @@ namespace 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.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.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.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.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.all(), data.all()); - CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); - for (size_t i = 0UL; i < data.size(); ++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; + + 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. - size_t count_set = bits.count(); - size_t count_size = bits.size(); - bool all_set = bits.all(); - - CHECK_EQUAL(0, count_set); - CHECK_EQUAL(11, count_size); - CHECK_EQUAL(false, all_set); + CHECK_EQUAL(0, bits.count()); + CHECK_FALSE(bits.all()); // Copy flipped bits to a new bitset. - etl::bitset<11, uint8_t> negated_bits = ~bits; + Bitset negated_bits = ~bits; - count_set = negated_bits.count(); - count_size = negated_bits.size(); - all_set = negated_bits.all(); - - CHECK_EQUAL(11, count_set); - CHECK_EQUAL(11, count_size); - CHECK_EQUAL(true, all_set); + CHECK_EQUAL(Size, negated_bits.count()); + CHECK_TRUE(negated_bits.all()); // Flip the bits in the original bitset. bits.flip(); - count_set = bits.count(); - count_size = bits.size(); - all_set = bits.all(); + CHECK_EQUAL(11, bits.count()); + CHECK_TRUE(bits.all()); + } - CHECK_EQUAL(11, count_set); - CHECK_EQUAL(11, count_size); - CHECK_EQUAL(true, all_set); + //************************************************************************* + TEST(test_issue_497_for_left_shift) + { + const int Size = 11; + const unsigned long long Initial = (1ULL << Size) - 1; + + using Bitset = etl::bitset; + + // 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; + + // 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; + + const unsigned long long Initial = std::numeric_limits::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 diff --git a/test/test_bitset_new_explicit_single_element_type.cpp b/test/test_bitset_new_explicit_single_element_type.cpp index 3d32174d..abe543d0 100644 --- a/test/test_bitset_new_explicit_single_element_type.cpp +++ b/test/test_bitset_new_explicit_single_element_type.cpp @@ -308,7 +308,7 @@ namespace 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.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.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.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) { - std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001"); - etl::bitset<60> data("110001001000110100010101100111001100010010001101000101011001"); + std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001"); + etl::bitset<60, uint64_t> data("110001001000110100010101100111001100010010001101000101011001"); CHECK_EQUAL(compare.size(), data.size()); CHECK_EQUAL(compare.count(), data.count()); @@ -376,7 +376,7 @@ namespace 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.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; + + 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. - size_t count_set = bits.count(); - size_t count_size = bits.size(); - bool all_set = bits.all(); - - CHECK_EQUAL(0, count_set); - CHECK_EQUAL(5, count_size); - CHECK_EQUAL(false, all_set); + CHECK_EQUAL(0, bits.count()); + CHECK_FALSE(bits.all()); // Copy flipped bits to a new bitset. - etl::bitset<5, uint8_t> negated_bits = ~bits; + Bitset negated_bits = ~bits; - count_set = negated_bits.count(); - count_size = negated_bits.size(); - all_set = negated_bits.all(); - - CHECK_EQUAL(5, count_set); - CHECK_EQUAL(5, count_size); - CHECK_EQUAL(true, all_set); + CHECK_EQUAL(Size, negated_bits.count()); + CHECK_TRUE(negated_bits.all()); // Flip the bits in the original bitset. bits.flip(); - count_set = bits.count(); - count_size = bits.size(); - all_set = bits.all(); + CHECK_EQUAL(11, bits.count()); + CHECK_TRUE(bits.all()); + } - CHECK_EQUAL(5, count_set); - CHECK_EQUAL(5, count_size); - CHECK_EQUAL(true, all_set); + //************************************************************************* + TEST(test_issue_497_for_left_shift) + { + const int Size = 11; + const unsigned long long Initial = (1ULL << Size) - 1; + + using Bitset = etl::bitset; + + // 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; + + // 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; + + const unsigned long long Initial = std::numeric_limits::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 diff --git a/test/test_bitset_new_ext_default_element_type.cpp b/test/test_bitset_new_ext_default_element_type.cpp index dfa5c5aa..17f61a69 100644 --- a/test/test_bitset_new_ext_default_element_type.cpp +++ b/test/test_bitset_new_ext_default_element_type.cpp @@ -252,7 +252,7 @@ namespace 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.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.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.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.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.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; - etl::bitset_ext<11> bits(buffer1); + const int Size = 11; + + using Bitset = etl::bitset_ext; + 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. - size_t count_set = bits.count(); - size_t count_size = bits.size(); - bool all_set = bits.all(); - - CHECK_EQUAL(0, count_set); - CHECK_EQUAL(11, count_size); - CHECK_EQUAL(false, all_set); + CHECK_EQUAL(0, bits.count()); + CHECK_FALSE(bits.all()); // Copy flipped bits to a new bitset. - etl::bitset_ext<11>::buffer_type buffer2; - etl::bitset_ext<11> negated_bits(bits.flip(), buffer2); + // Copy flipped bits to a new bitset. + Buffer buffer2; + Bitset negated_bits(bits.flip(), buffer2); - count_set = negated_bits.count(); - count_size = negated_bits.size(); - all_set = negated_bits.all(); + CHECK_EQUAL(Size, negated_bits.count()); + CHECK_TRUE(negated_bits.all()); + } - CHECK_EQUAL(11, count_set); - CHECK_EQUAL(11, count_size); - CHECK_EQUAL(true, all_set); + //************************************************************************* + TEST(test_issue_497_for_left_shift_equals) + { + const int Size = 11; + const unsigned long long Initial = (1ULL << Size) - 1; - // Test the flipped the bits in the original bitset. - count_set = bits.count(); - count_size = bits.size(); - all_set = bits.all(); + using Bitset = etl::bitset_ext; + using Buffer = Bitset::buffer_type; - CHECK_EQUAL(11, count_set); - CHECK_EQUAL(11, count_size); - CHECK_EQUAL(true, all_set); + // 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; + using Buffer = Bitset::buffer_type; + + const unsigned long long Initial = std::numeric_limits::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 diff --git a/test/test_bitset_new_ext_explicit_single_element_type.cpp b/test/test_bitset_new_ext_explicit_single_element_type.cpp index 889ff78c..42d4003a 100644 --- a/test/test_bitset_new_ext_explicit_single_element_type.cpp +++ b/test/test_bitset_new_ext_explicit_single_element_type.cpp @@ -280,7 +280,7 @@ namespace 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.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.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.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.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.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -2656,42 +2654,85 @@ namespace CHECK_EQUAL(32, ETL_OR_STD17::size(b)); } - } - //************************************************************************* - TEST(test_issue_497) - { - etl::bitset_ext<11, uint8_t>::buffer_type buffer1; - etl::bitset_ext<11, uint8_t> bits(buffer1); + //************************************************************************* + TEST(test_issue_497_for_flip) + { + const int Size = 11; - // Check the initial state of the bitset. - size_t count_set = bits.count(); - size_t count_size = bits.size(); - bool all_set = bits.all(); + using Bitset = etl::bitset_ext; + using Buffer = Bitset::buffer_type; - CHECK_EQUAL(0, count_set); - CHECK_EQUAL(11, count_size); - CHECK_EQUAL(false, all_set); + Buffer buffer1; + Bitset bits(buffer1); - // Copy flipped bits to a new bitset. - etl::bitset_ext<11, uint8_t>::buffer_type buffer2; - etl::bitset_ext<11, uint8_t> negated_bits(bits.flip(), buffer2); + // Ensure that we are using multiple elements for the bitset. + CHECK_TRUE(Bitset::Number_Of_Elements == 1); - count_set = negated_bits.count(); - count_size = negated_bits.size(); - all_set = negated_bits.all(); + // Check the initial state of the bitset. + CHECK_EQUAL(0, bits.count()); + CHECK_FALSE(bits.all()); - CHECK_EQUAL(11, count_set); - CHECK_EQUAL(11, count_size); - CHECK_EQUAL(true, all_set); + // Copy flipped bits to a new bitset. + // Copy flipped bits to a new bitset. + Buffer buffer2; + Bitset negated_bits(bits.flip(), buffer2); - // Test the flipped the bits in the original bitset. - count_set = bits.count(); - count_size = bits.size(); - all_set = bits.all(); + CHECK_EQUAL(Size, negated_bits.count()); + CHECK_TRUE(negated_bits.all()); + } - CHECK_EQUAL(11, count_set); - CHECK_EQUAL(11, count_size); - CHECK_EQUAL(true, all_set); + //************************************************************************* + TEST(test_issue_497_for_left_shift_equals) + { + const int Size = 11; + const unsigned long long Initial = (1ULL << Size) - 1; + + using Bitset = etl::bitset_ext; + 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; + using Buffer = Bitset::buffer_type; + + const unsigned long long Initial = std::numeric_limits::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