diff --git a/include/etl/private/bitset_new.h b/include/etl/private/bitset_new.h index ef1152cd..ba397209 100644 --- a/include/etl/private/bitset_new.h +++ b/include/etl/private/bitset_new.h @@ -554,17 +554,9 @@ namespace etl //************************************************************************* /// Flip all of the bits. //************************************************************************* - static ETL_CONSTEXPR14 void flip_all(pointer pbuffer, size_t /*number_of_elements*/) ETL_NOEXCEPT + static ETL_CONSTEXPR14 void flip_all(pointer pbuffer, size_t /*number_of_elements*/, element_type top_mask) ETL_NOEXCEPT { - *pbuffer = ~*pbuffer; - } - - //************************************************************************* - /// Flip some of the bits. - //************************************************************************* - static ETL_CONSTEXPR14 void flip_bits(pointer pbuffer, element_type mask = etl::integral_limits::max) ETL_NOEXCEPT - { - *pbuffer ^= mask; + *pbuffer = (~*pbuffer) & top_mask; } //************************************************************************* @@ -673,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) { @@ -694,6 +678,7 @@ namespace etl else { *pbuffer <<= shift; + *pbuffer &= top_mask; } } @@ -724,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; } //************************************************************************* @@ -1253,9 +1239,9 @@ namespace etl //************************************************************************* /// Flip all of the bits. //************************************************************************* - static ETL_CONSTEXPR14 void flip_all(pointer pbuffer, size_t number_of_elements) ETL_NOEXCEPT + static ETL_CONSTEXPR14 void flip_all(pointer pbuffer, size_t number_of_elements, element_type top_mask) ETL_NOEXCEPT { - operator_not(pbuffer, number_of_elements); + operator_not(pbuffer, number_of_elements, top_mask); } //************************************************************************* @@ -1398,19 +1384,23 @@ namespace etl //************************************************************************* /// operator not //************************************************************************* - static ETL_CONSTEXPR14 void operator_not(pointer pbuffer, size_t number_of_elements) ETL_NOEXCEPT + static ETL_CONSTEXPR14 void operator_not(pointer pbuffer, size_t number_of_elements, element_type top_mask) ETL_NOEXCEPT { while (number_of_elements-- != 0) { *pbuffer = ~*pbuffer; ++pbuffer; } + + --pbuffer; // Step back to the last element. + *pbuffer &= top_mask; // Clear unused bits. } //************************************************************************* /// 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) { @@ -1463,6 +1453,8 @@ namespace etl { pbuffer[i] = 0; } + + pbuffer[(number_of_elements - 1U)] &= top_mask; } } @@ -1543,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; @@ -1555,6 +1547,8 @@ namespace etl { pbuffer[i++] = All_Clear_Element; } + + pbuffer[number_of_elements - 1] &= top_mask; } //************************************************************************* @@ -1565,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; @@ -1583,6 +1577,8 @@ namespace etl { pbuffer[i++] = All_Clear_Element; } + + pbuffer[number_of_elements - 1] &= top_mask; } //************************************************************************* @@ -1815,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); } //************************************************************************* @@ -2248,7 +2244,7 @@ namespace etl //************************************************************************* ETL_CONSTEXPR14 bitset& flip() ETL_NOEXCEPT { - implementation::flip_all(buffer, Number_Of_Elements); + implementation::flip_all(buffer, Number_Of_Elements, Top_Mask); return *this; } @@ -2389,7 +2385,7 @@ namespace etl { etl::bitset temp(*this); - implementation::flip_all(temp.buffer, Number_Of_Elements); + implementation::flip_all(temp.buffer, Number_Of_Elements, Top_Mask); return temp; } @@ -2401,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; } @@ -2417,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; @@ -2754,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); } //************************************************************************* @@ -2764,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); } //************************************************************************* @@ -3245,7 +3241,7 @@ namespace etl //************************************************************************* ETL_CONSTEXPR14 bitset_ext& flip() ETL_NOEXCEPT { - implementation::flip_all(pbuffer, Number_Of_Elements); + implementation::flip_all(pbuffer, Number_Of_Elements, Top_Mask); return *this; } @@ -3348,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_legacy.cpp b/test/test_bitset_legacy.cpp index 643dfc27..90056064 100644 --- a/test/test_bitset_legacy.cpp +++ b/test/test_bitset_legacy.cpp @@ -1505,13 +1505,13 @@ namespace { etl::bitset<5U> bits; std::bitset<5U> stdbits; - etl::bitset<5U> negbits = ~bits; - std::bitset<5U> stdnegbits = ~stdbits; + etl::bitset<5U> negated_bits = ~bits; + std::bitset<5U> stdnegbits = ~stdbits; CHECK_EQUAL(stdbits.count(), bits.count()); CHECK_EQUAL(stdbits.all(), bits.all()); - CHECK_EQUAL(stdnegbits.count(), negbits.count()); - CHECK_EQUAL(stdnegbits.all(), negbits.all()); + CHECK_EQUAL(stdnegbits.count(), negated_bits.count()); + CHECK_EQUAL(stdnegbits.all(), negated_bits.all()); } //************************************************************************* diff --git a/test/test_bitset_new_default_element_type.cpp b/test/test_bitset_new_default_element_type.cpp index 9ecd1bf6..5caae790 100644 --- a/test/test_bitset_new_default_element_type.cpp +++ b/test/test_bitset_new_default_element_type.cpp @@ -310,6 +310,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -325,6 +327,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -340,6 +344,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -355,17 +361,59 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* - TEST(test_construct_from_nullptr_char_string) + TEST(test_construct_from_64_bit_char_string_where_the_bitset_size_is_60) { - const char* s = nullptr; + std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001"); + etl::bitset<60> data("110001001000110100010101100111001100010010001101000101011001"); - etl::bitset<60> data(s); + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + + for (size_t i = 0UL; i < data.size(); ++i) + { + CHECK_EQUAL(compare.test(i), data.test(i)); + } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); + } + + //************************************************************************* + TEST(test_construct_from_69_bit_char_string_where_the_bitset_size_is_64) + { + std::bitset<64> compare("110001001000110100010101100111001100010010001101000101011001111100001"); + etl::bitset<64> data("110001001000110100010101100111001100010010001101000101011001111100001"); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + CHECK_EQUAL(compare.none(), data.none()); + CHECK_EQUAL(compare.any(), data.any()); + CHECK_EQUAL(compare.all(), data.all()); + + for (size_t i = 0UL; i < data.size(); ++i) + { + CHECK_EQUAL(compare.test(i), data.test(i)); + } + + 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"); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); CHECK_EQUAL(60, data.size()); CHECK_EQUAL(0, data.count()); + CHECK_EQUAL(0, data.to_ullong()); } //************************************************************************* @@ -377,6 +425,7 @@ namespace CHECK_EQUAL(60, data.size()); CHECK_EQUAL(0, data.count()); + CHECK_EQUAL(0, data.to_ullong()); } //************************************************************************* @@ -388,6 +437,7 @@ namespace CHECK_EQUAL(60, data.size()); CHECK_EQUAL(0, data.count()); + CHECK_EQUAL(0, data.to_ullong()); } //************************************************************************* @@ -399,26 +449,7 @@ namespace CHECK_EQUAL(60, data.size()); CHECK_EQUAL(0, data.count()); - } - - //************************************************************************* - TEST(test_construct_from_excess_string) - { - std::bitset<64> compare("110001001000110100010101100111001100010010001101000101011001111100001"); - etl::bitset<64> data("110001001000110100010101100111001100010010001101000101011001111100001"); - - CHECK_EQUAL(compare.size(), data.size()); - CHECK_EQUAL(compare.count(), data.count()); - CHECK_EQUAL(compare.none(), data.none()); - 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(0, data.to_ullong()); } //************************************************************************* @@ -2710,5 +2741,110 @@ namespace CHECK_EQUAL(32, ETL_OR_STD17::size(b)); } + + //************************************************************************* + TEST(test_issue_497_for_flip) + { + 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. + CHECK_EQUAL(0, bits.count()); + CHECK_FALSE(bits.all()); + + // Copy flipped bits to a new bitset. + Bitset negated_bits = ~bits; + + CHECK_EQUAL(Size, negated_bits.count()); + CHECK_TRUE(negated_bits.all()); + + // Flip the bits in the original bitset. + bits.flip(); + + CHECK_EQUAL(11, bits.count()); + CHECK_TRUE(bits.all()); + } + + //************************************************************************* + 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 3a44690b..abe543d0 100644 --- a/test/test_bitset_new_explicit_single_element_type.cpp +++ b/test/test_bitset_new_explicit_single_element_type.cpp @@ -307,6 +307,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -322,6 +324,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -337,6 +341,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -352,6 +358,47 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + 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, uint64_t> data("110001001000110100010101100111001100010010001101000101011001"); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + + for (size_t i = 0UL; i < data.size(); ++i) + { + CHECK_EQUAL(compare.test(i), data.test(i)); + } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); + } + + //************************************************************************* + TEST(test_construct_from_69_bit_char_string_where_the_bitset_size_is_64) + { + std::bitset<64> compare("110001001000110100010101100111001100010010001101000101011001111100001"); + etl::bitset<64, uint64_t> data("110001001000110100010101100111001100010010001101000101011001111100001"); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + CHECK_EQUAL(compare.none(), data.none()); + 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_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -412,26 +459,11 @@ namespace CHECK_EQUAL(0U, data_wchar.count()); CHECK_EQUAL(0U, data_u16.count()); CHECK_EQUAL(0U, data_u32.count()); - } - //************************************************************************* - TEST(test_construct_from_excess_string) - { - std::bitset<64> compare("110001001000110100010101100111001100010010001101000101011001111100001"); - etl::bitset<64, uint64_t> data("110001001000110100010101100111001100010010001101000101011001111100001"); - - CHECK_EQUAL(compare.size(), data.size()); - CHECK_EQUAL(compare.count(), data.count()); - CHECK_EQUAL(compare.none(), data.none()); - 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(0U, data_char.to_ullong()); + CHECK_EQUAL(0U, data_wchar.to_ullong()); + CHECK_EQUAL(0U, data_u16.to_ullong()); + CHECK_EQUAL(0U, data_u32.to_ullong()); } //************************************************************************* @@ -2840,5 +2872,110 @@ namespace CHECK_EQUAL(32, ETL_OR_STD17::size(b)); } + + //************************************************************************* + TEST(test_issue_497_for_flip) + { + 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. + CHECK_EQUAL(0, bits.count()); + CHECK_FALSE(bits.all()); + + // Copy flipped bits to a new bitset. + Bitset negated_bits = ~bits; + + CHECK_EQUAL(Size, negated_bits.count()); + CHECK_TRUE(negated_bits.all()); + + // Flip the bits in the original bitset. + bits.flip(); + + CHECK_EQUAL(11, bits.count()); + CHECK_TRUE(bits.all()); + } + + //************************************************************************* + 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 d506b8f3..17f61a69 100644 --- a/test/test_bitset_new_ext_default_element_type.cpp +++ b/test/test_bitset_new_ext_default_element_type.cpp @@ -251,6 +251,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -267,6 +269,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -283,6 +287,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -299,6 +305,49 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); + } + + //************************************************************************* + TEST(test_construct_from_64_bit_char_string_where_the_bitset_size_is_60) + { + etl::bitset_ext<64>::buffer_type buffer; + std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001"); + etl::bitset_ext<60> data("110001001000110100010101100111001100010010001101000101011001", buffer); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + + for (size_t i = 0UL; i < data.size(); ++i) + { + CHECK_EQUAL(compare.test(i), data.test(i)); + } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); + } + + //************************************************************************* + TEST(test_construct_from_69_bit_char_string_where_the_bitset_size_is_64) + { + etl::bitset_ext<64>::buffer_type buffer; + std::bitset<64> compare("110001001000110100010101100111001100010010001101000101011001111100001"); + etl::bitset_ext<64> data("110001001000110100010101100111001100010010001101000101011001111100001", buffer); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + CHECK_EQUAL(compare.none(), data.none()); + 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_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -349,27 +398,6 @@ namespace CHECK_EQUAL(0, data.count()); } - //************************************************************************* - TEST(test_construct_from_excess_string) - { - etl::bitset_ext<64>::buffer_type buffer; - std::bitset<64> compare("110001001000110100010101100111001100010010001101000101011001111100001"); - etl::bitset_ext<64> data("110001001000110100010101100111001100010010001101000101011001111100001", buffer); - - CHECK_EQUAL(compare.size(), data.size()); - CHECK_EQUAL(compare.count(), data.count()); - CHECK_EQUAL(compare.none(), data.none()); - 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)); - } - } - //************************************************************************* TEST(test_set) { @@ -2460,5 +2488,85 @@ namespace CHECK_EQUAL(32, ETL_OR_STD17::size(b)); } + + //************************************************************************* + TEST(test_issue_497_for_flip) + { + 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. + CHECK_EQUAL(0, bits.count()); + CHECK_FALSE(bits.all()); + + // Copy flipped bits to a new bitset. + // Copy flipped bits to a new bitset. + Buffer buffer2; + Bitset negated_bits(bits.flip(), buffer2); + + CHECK_EQUAL(Size, negated_bits.count()); + CHECK_TRUE(negated_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_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 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 e51ccb7e..42d4003a 100644 --- a/test/test_bitset_new_ext_explicit_single_element_type.cpp +++ b/test/test_bitset_new_ext_explicit_single_element_type.cpp @@ -279,6 +279,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -295,6 +297,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -311,6 +315,8 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); } //************************************************************************* @@ -327,6 +333,47 @@ namespace { CHECK_EQUAL(compare.test(i), data.test(i)); } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); + } + + //************************************************************************* + TEST(test_construct_from_64_bit_char_string_where_the_bitset_size_is_60) + { + etl::bitset_ext<64, uint64_t>::buffer_type buffer; + std::bitset<60> compare("110001001000110100010101100111001100010010001101000101011001"); + etl::bitset_ext<60, uint64_t> data("110001001000110100010101100111001100010010001101000101011001", buffer); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + + for (size_t i = 0UL; i < data.size(); ++i) + { + CHECK_EQUAL(compare.test(i), data.test(i)); + } + + CHECK_EQUAL_HEX(compare.to_ullong(), data.to_ullong()); + } + + //************************************************************************* + TEST(test_construct_from_69_bit_char_string_where_the_bitset_size_is_64) + { + etl::bitset_ext<64, uint64_t>::buffer_type buffer; + std::bitset<64> compare("110001001000110100010101100111001100010010001101000101011001111100001"); + etl::bitset_ext<64, uint64_t> data("110001001000110100010101100111001100010010001101000101011001111100001", buffer); + + CHECK_EQUAL(compare.size(), data.size()); + CHECK_EQUAL(compare.count(), data.count()); + CHECK_EQUAL(compare.none(), data.none()); + 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)); + } } //************************************************************************* @@ -377,27 +424,6 @@ namespace CHECK_EQUAL(0, data.count()); } - //************************************************************************* - TEST(test_construct_from_excess_string) - { - etl::bitset_ext<64, uint64_t>::buffer_type buffer; - std::bitset<64> compare("110001001000110100010101100111001100010010001101000101011001111100001"); - etl::bitset_ext<64, uint64_t> data("110001001000110100010101100111001100010010001101000101011001111100001", buffer); - - CHECK_EQUAL(compare.size(), data.size()); - CHECK_EQUAL(compare.count(), data.count()); - CHECK_EQUAL(compare.none(), data.none()); - 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)); - } - } - //************************************************************************* TEST(test_set) { @@ -2628,5 +2654,85 @@ namespace CHECK_EQUAL(32, ETL_OR_STD17::size(b)); } + + //************************************************************************* + TEST(test_issue_497_for_flip) + { + 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. + CHECK_EQUAL(0, bits.count()); + CHECK_FALSE(bits.all()); + + // Copy flipped bits to a new bitset. + // Copy flipped bits to a new bitset. + Buffer buffer2; + Bitset negated_bits(bits.flip(), buffer2); + + CHECK_EQUAL(Size, negated_bits.count()); + CHECK_TRUE(negated_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_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