From 4cf19a2b0a43cf8618fe531440edb7cc2dfdffea Mon Sep 17 00:00:00 2001 From: Gennaro Prota Date: Tue, 16 Sep 2025 11:31:25 +0200 Subject: [PATCH] Fix append< BlockInputIterator >() for input iterators We were using `AllocatorOrContainer` as if it were still guaranteed to be an allocator type. Unfortunately, the append() tests didn't exercise input iterators, so this went unnoticed. I've now added tests. --- .../dynamic_bitset/impl/dynamic_bitset.ipp | 5 +++-- test/bitset_test.hpp | 17 +++++++++++++++++ test/dyn_bitset_unit_tests1.cpp | 13 +++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/include/boost/dynamic_bitset/impl/dynamic_bitset.ipp b/include/boost/dynamic_bitset/impl/dynamic_bitset.ipp index 4d00b08..3c1e568 100644 --- a/include/boost/dynamic_bitset/impl/dynamic_bitset.ipp +++ b/include/boost/dynamic_bitset/impl/dynamic_bitset.ipp @@ -2145,8 +2145,9 @@ template< typename BlockInputIterator > void dynamic_bitset< Block, AllocatorOrContainer >::m_append( BlockInputIterator first, BlockInputIterator last, std::input_iterator_tag ) { - std::vector< Block, AllocatorOrContainer > v( first, last ); - m_append( v.begin(), v.end(), std::random_access_iterator_tag() ); + for ( ; first != last; ++first ) { + append( *first ); + } } template< typename Block, typename AllocatorOrContainer > diff --git a/test/bitset_test.hpp b/test/bitset_test.hpp index 3e4f83a..d28b725 100644 --- a/test/bitset_test.hpp +++ b/test/bitset_test.hpp @@ -22,7 +22,10 @@ #include "boost/limits.hpp" #include #include // is sometimes macro-guarded :-( +#include +#include #include +#include #include #if ! defined( BOOST_NO_STD_LOCALE ) @@ -528,6 +531,20 @@ struct bitset_test BOOST_TEST( b == c ); } + static void + append_block_range_input_iter( const Bitset & lhs ) + { + if ( ! std::is_same< Block, unsigned char >::value ) { + Bitset b( lhs ), c( lhs ); + std::istringstream ss( "1 2 3" ); + b.append( std::istream_iterator< Block >( ss ), std::istream_iterator< Block >() ); + c.append( 1 ); + c.append( 2 ); + c.append( 3 ); + BOOST_TEST( b == c ); + } + } + // operator[] and reference members // PRE: b[i] == bit_vec[i] static void diff --git a/test/dyn_bitset_unit_tests1.cpp b/test/dyn_bitset_unit_tests1.cpp index 80b0252..b9d0acd 100644 --- a/test/dyn_bitset_unit_tests1.cpp +++ b/test/dyn_bitset_unit_tests1.cpp @@ -543,6 +543,19 @@ run_test_cases() blocks[ 2 ] = all_1s; Tests::append_block_range( a, blocks ); } + // Test with input iterators + { + bitset_type b; + Tests::append_block_range_input_iter( b ); + } + { + bitset_type b( "0" ); + Tests::append_block_range_input_iter( b ); + } + { + bitset_type b( long_string.c_str() ); + Tests::append_block_range_input_iter( b ); + } //===================================================================== // Test bracket operator {