From 12acc401238ffe0cfc829b9df920a0b455228ece Mon Sep 17 00:00:00 2001 From: Gennaro Prota Date: Tue, 24 Jun 2025 15:46:38 +0200 Subject: [PATCH] Reformat all the C++ code (with ClangFormat) --- example/example1.cpp | 17 +- example/example2.cpp | 21 +- example/example3.cpp | 28 +- include/boost/dynamic_bitset/config.hpp | 37 +- .../dynamic_bitset/detail/dynamic_bitset.hpp | 181 +- .../dynamic_bitset/detail/lowest_bit.hpp | 8 +- .../boost/dynamic_bitset/dynamic_bitset.hpp | 459 ++- .../dynamic_bitset/impl/dynamic_bitset.tpp | 1747 +++++------ .../boost/dynamic_bitset/serialization.hpp | 39 +- include/boost/dynamic_bitset_fwd.hpp | 3 +- test/bitset_test.hpp | 2779 +++++++++-------- test/dyn_bitset_unit_tests1.cpp | 963 +++--- test/dyn_bitset_unit_tests2.cpp | 685 ++-- test/dyn_bitset_unit_tests3.cpp | 1553 +++++---- test/dyn_bitset_unit_tests4.cpp | 462 ++- test/dyn_bitset_unit_tests5.cpp | 124 +- test/test_ambiguous_set.cpp | 21 +- test/test_boost_hash.cpp | 33 +- test/test_lowest_bit.cpp | 11 +- test/test_std_hash.cpp | 24 +- 20 files changed, 4568 insertions(+), 4627 deletions(-) diff --git a/example/example1.cpp b/example/example1.cpp index 2ad0b01..bb0e542 100644 --- a/example/example1.cpp +++ b/example/example1.cpp @@ -3,8 +3,6 @@ // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) - - // An example of setting and reading some bits. Note that operator[] // goes from the least-significant bit at 0 to the most significant // bit at size()-1. The operator<< for dynamic_bitset prints the @@ -20,14 +18,15 @@ #include "boost/dynamic_bitset.hpp" #include -int main() +int +main() { - boost::dynamic_bitset<> x(5); // all 0's by default - x[0] = 1; - x[1] = 1; - x[4] = 1; - for (boost::dynamic_bitset<>::size_type i = 0; i < x.size(); ++i) - std::cout << x[i]; + boost::dynamic_bitset<> x( 5 ); // all 0's by default + x[ 0 ] = 1; + x[ 1 ] = 1; + x[ 4 ] = 1; + for ( boost::dynamic_bitset<>::size_type i = 0; i < x.size(); ++i ) + std::cout << x[ i ]; std::cout << "\n"; std::cout << x << "\n"; diff --git a/example/example2.cpp b/example/example2.cpp index 8d9facc..bc7faae 100644 --- a/example/example2.cpp +++ b/example/example2.cpp @@ -13,19 +13,20 @@ #include "boost/dynamic_bitset.hpp" #include -int main() +int +main() { - const boost::dynamic_bitset<> b0(2, 0ul); - std::cout << "bits(0) = " << b0 << std::endl; + const boost::dynamic_bitset<> b0( 2, 0ul ); + std::cout << "bits(0) = " << b0 << std::endl; - const boost::dynamic_bitset<> b1(2, 1ul); - std::cout << "bits(1) = " << b1 << std::endl; + const boost::dynamic_bitset<> b1( 2, 1ul ); + std::cout << "bits(1) = " << b1 << std::endl; - const boost::dynamic_bitset<> b2(2, 2ul); - std::cout << "bits(2) = " << b2 << std::endl; + const boost::dynamic_bitset<> b2( 2, 2ul ); + std::cout << "bits(2) = " << b2 << std::endl; - const boost::dynamic_bitset<> b3(2, 3ul); - std::cout << "bits(3) = " << b3 << std::endl; + const boost::dynamic_bitset<> b3( 2, 3ul ); + std::cout << "bits(3) = " << b3 << std::endl; - return 0; + return 0; } diff --git a/example/example3.cpp b/example/example3.cpp index 6b8b725..c2f40da 100644 --- a/example/example3.cpp +++ b/example/example3.cpp @@ -19,38 +19,36 @@ // Shifted left by 1: 001000100 // Shifted right by 1: 010010001 - - #include "boost/dynamic_bitset.hpp" - #include #include -int main() +int +main() { - boost::dynamic_bitset<> mask(12, 2730ul); + boost::dynamic_bitset<> mask( 12, 2730ul ); std::cout << "mask = " << mask << std::endl; boost::dynamic_bitset<> x; std::cout << "x.size() = " << x.size() << std::endl; std::cout << "Enter a bitset in binary: x = " << std::flush; - if (std::cin >> x) { + if ( std::cin >> x ) { const std::size_t sz = x.size(); std::cout << std::endl; std::cout << "Input number: " << x << std::endl; std::cout << "x.size() is now: " << sz << std::endl; - bool fits_in_ulong = true; - unsigned long ul = 0; + bool fits_in_ulong = true; + unsigned long ul = 0; try { ul = x.to_ulong(); - } catch(std::overflow_error &) { + } catch ( std::overflow_error & ) { fits_in_ulong = false; } std::cout << "As unsigned long: "; - if(fits_in_ulong) { + if ( fits_in_ulong ) { std::cout << ul; } else { std::cout << "(overflow exception)"; @@ -58,14 +56,14 @@ int main() std::cout << std::endl; - mask.resize(sz); + mask.resize( sz ); std::cout << "Mask (possibly resized): " << mask << std::endl; - std::cout << "And with mask: " << (x & mask) << std::endl; - std::cout << "Or with mask: " << (x | mask) << std::endl; - std::cout << "Shifted left by 1: " << (x << 1) << std::endl; - std::cout << "Shifted right by 1: " << (x >> 1) << std::endl; + std::cout << "And with mask: " << ( x & mask ) << std::endl; + std::cout << "Or with mask: " << ( x | mask ) << std::endl; + std::cout << "Shifted left by 1: " << ( x << 1 ) << std::endl; + std::cout << "Shifted right by 1: " << ( x >> 1 ) << std::endl; } return 0; } diff --git a/include/boost/dynamic_bitset/config.hpp b/include/boost/dynamic_bitset/config.hpp index 029236a..b68f457 100644 --- a/include/boost/dynamic_bitset/config.hpp +++ b/include/boost/dynamic_bitset/config.hpp @@ -16,15 +16,22 @@ // no-op function to workaround gcc bug c++/8419 // -namespace boost { namespace detail { - template T make_non_const(T t) { return t; } -}} +namespace boost { +namespace detail { +template< typename T > +T +make_non_const( T t ) +{ + return t; +} +} +} -#if defined(__GNUC__) -# define BOOST_DYNAMIC_BITSET_WRAP_CONSTANT(expr) \ - (boost::detail::make_non_const(expr)) +#if defined( __GNUC__ ) +# define BOOST_DYNAMIC_BITSET_WRAP_CONSTANT( expr ) \ + ( boost::detail::make_non_const( expr ) ) #else -# define BOOST_DYNAMIC_BITSET_WRAP_CONSTANT(expr) (expr) +# define BOOST_DYNAMIC_BITSET_WRAP_CONSTANT( expr ) ( expr ) #endif // A couple of macros to cope with libraries without locale @@ -34,18 +41,18 @@ namespace boost { namespace detail { // the first macro is a no-op and the second one just expands // to its parameter c. // -#if defined (BOOST_USE_FACET) +#if defined( BOOST_USE_FACET ) -#define BOOST_DYNAMIC_BITSET_CTYPE_FACET(ch, name, loc) \ - const std::ctype & name = \ - BOOST_USE_FACET(std::ctype, loc) /**/ +# define BOOST_DYNAMIC_BITSET_CTYPE_FACET( ch, name, loc ) \ + const std::ctype< ch > & name = \ + BOOST_USE_FACET( std::ctype< ch >, loc ) /**/ -#define BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, c) \ - (fac.widen(c)) +# define BOOST_DYNAMIC_BITSET_WIDEN_CHAR( fac, c ) \ + ( fac.widen( c ) ) #else -#define BOOST_DYNAMIC_BITSET_CTYPE_FACET(ch, name, loc) /**/ -#define BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, c) c +# define BOOST_DYNAMIC_BITSET_CTYPE_FACET( ch, name, loc ) /**/ +# define BOOST_DYNAMIC_BITSET_WIDEN_CHAR( fac, c ) c #endif diff --git a/include/boost/dynamic_bitset/detail/dynamic_bitset.hpp b/include/boost/dynamic_bitset/detail/dynamic_bitset.hpp index 6abf1d0..2f30a32 100644 --- a/include/boost/dynamic_bitset/detail/dynamic_bitset.hpp +++ b/include/boost/dynamic_bitset/detail/dynamic_bitset.hpp @@ -23,111 +23,130 @@ namespace boost { - namespace detail { - namespace dynamic_bitset_impl { +namespace detail { +namespace dynamic_bitset_impl { - template - struct max_limit { - BOOST_STATIC_CONSTEXPR T value = static_cast(-1); - }; +template< class T > +struct max_limit +{ + BOOST_STATIC_CONSTEXPR T value = static_cast< T >( -1 ); +}; - template - BOOST_CONSTEXPR_OR_CONST T max_limit::value; +template< class T > +BOOST_CONSTEXPR_OR_CONST T max_limit< T >::value; - template - struct shifter +template< typename T, int amount, int width /* = default */ > +struct shifter +{ + static void + left_shift( T & v ) { - static void left_shift(T & v) { - amount >= width ? (v = 0) - : (v >>= BOOST_DYNAMIC_BITSET_WRAP_CONSTANT(amount)); - } - }; - - template - struct value_to_type - { - value_to_type() {} - }; - // Some library implementations simply return a dummy - // value such as - // - // size_type(-1) / sizeof(T) - // - // from vector<>::max_size. This tries to get more - // meaningful info. - // - template - inline typename T::size_type vector_max_size_workaround(const T & v) - BOOST_NOEXCEPT - { - typedef typename T::allocator_type allocator_type; - - const allocator_type& alloc = v.get_allocator(); - - typename boost::allocator_size_type::type alloc_max = - boost::allocator_max_size(alloc); - - const typename T::size_type container_max = v.max_size(); - - return alloc_max < container_max ? alloc_max : container_max; + amount >= width ? ( v = 0 ) + : ( v >>= BOOST_DYNAMIC_BITSET_WRAP_CONSTANT( amount ) ); } +}; - // for static_asserts - template - struct allowed_block_type { - enum { value = T(-1) > 0 }; // ensure T has no sign +template< bool value > +struct value_to_type +{ + value_to_type() + { + } +}; +// Some library implementations simply return a dummy +// value such as +// +// size_type(-1) / sizeof(T) +// +// from vector<>::max_size. This tries to get more +// meaningful info. +// +template< typename T > +inline typename T::size_type +vector_max_size_workaround( const T & v ) + BOOST_NOEXCEPT +{ + typedef typename T::allocator_type allocator_type; + + const allocator_type & alloc = v.get_allocator(); + + typename boost::allocator_size_type< allocator_type >::type alloc_max = + boost::allocator_max_size( alloc ); + + const typename T::size_type container_max = v.max_size(); + return alloc_max < container_max ? alloc_max : container_max; +} + +// for static_asserts +template< typename T > +struct allowed_block_type +{ + enum + { + value = T( -1 ) > 0 // ensure T has no sign }; +}; - template <> - struct allowed_block_type { - enum { value = false }; +template<> +struct allowed_block_type< bool > +{ + enum + { + value = false }; +}; - - template - struct is_numeric { - enum { value = false }; +template< typename T > +struct is_numeric +{ + enum + { + value = false }; +}; -# define BOOST_dynamic_bitset_is_numeric(x) \ - template<> \ - struct is_numeric< x > { \ - enum { value = true }; \ - } /**/ +#define BOOST_dynamic_bitset_is_numeric( x ) \ + template<> \ + struct is_numeric< x > \ + { \ + enum \ + { \ + value = true \ + }; \ + } /**/ - BOOST_dynamic_bitset_is_numeric(bool); - BOOST_dynamic_bitset_is_numeric(char); +BOOST_dynamic_bitset_is_numeric( bool ); +BOOST_dynamic_bitset_is_numeric( char ); -#if !defined(BOOST_NO_INTRINSIC_WCHAR_T) - BOOST_dynamic_bitset_is_numeric(wchar_t); +#if ! defined( BOOST_NO_INTRINSIC_WCHAR_T ) +BOOST_dynamic_bitset_is_numeric( wchar_t ); #endif - BOOST_dynamic_bitset_is_numeric(signed char); - BOOST_dynamic_bitset_is_numeric(short int); - BOOST_dynamic_bitset_is_numeric(int); - BOOST_dynamic_bitset_is_numeric(long int); +BOOST_dynamic_bitset_is_numeric( signed char ); +BOOST_dynamic_bitset_is_numeric( short int ); +BOOST_dynamic_bitset_is_numeric( int ); +BOOST_dynamic_bitset_is_numeric( long int ); - BOOST_dynamic_bitset_is_numeric(unsigned char); - BOOST_dynamic_bitset_is_numeric(unsigned short); - BOOST_dynamic_bitset_is_numeric(unsigned int); - BOOST_dynamic_bitset_is_numeric(unsigned long); +BOOST_dynamic_bitset_is_numeric( unsigned char ); +BOOST_dynamic_bitset_is_numeric( unsigned short ); +BOOST_dynamic_bitset_is_numeric( unsigned int ); +BOOST_dynamic_bitset_is_numeric( unsigned long ); -#if defined(BOOST_HAS_LONG_LONG) - BOOST_dynamic_bitset_is_numeric(::boost::long_long_type); - BOOST_dynamic_bitset_is_numeric(::boost::ulong_long_type); +#if defined( BOOST_HAS_LONG_LONG ) +BOOST_dynamic_bitset_is_numeric( ::boost::long_long_type ); +BOOST_dynamic_bitset_is_numeric( ::boost::ulong_long_type ); #endif - // intentionally omitted - //BOOST_dynamic_bitset_is_numeric(float); - //BOOST_dynamic_bitset_is_numeric(double); - //BOOST_dynamic_bitset_is_numeric(long double); +// intentionally omitted +// BOOST_dynamic_bitset_is_numeric(float); +// BOOST_dynamic_bitset_is_numeric(double); +// BOOST_dynamic_bitset_is_numeric(long double); #undef BOOST_dynamic_bitset_is_numeric - } // dynamic_bitset_impl - } // namespace detail +} // dynamic_bitset_impl +} // namespace detail } // namespace boost #endif // include guard - diff --git a/include/boost/dynamic_bitset/detail/lowest_bit.hpp b/include/boost/dynamic_bitset/detail/lowest_bit.hpp index 4ab3d85..79f7225 100644 --- a/include/boost/dynamic_bitset/detail/lowest_bit.hpp +++ b/include/boost/dynamic_bitset/detail/lowest_bit.hpp @@ -20,16 +20,16 @@ namespace boost { namespace detail { -template +template< typename T > int -lowest_bit(T x) +lowest_bit( T x ) { - BOOST_ASSERT(x >= 1); + BOOST_ASSERT( x >= 1 ); // Clear all the bits that are set except the rightmost one, // then calculate the logarithm to base 2. // - return boost::integer_log2(x - (x & (x - 1))); + return boost::integer_log2< T >( x - ( x & ( x - 1 ) ) ); } } diff --git a/include/boost/dynamic_bitset/dynamic_bitset.hpp b/include/boost/dynamic_bitset/dynamic_bitset.hpp index 17abf49..eb19e91 100644 --- a/include/boost/dynamic_bitset/dynamic_bitset.hpp +++ b/include/boost/dynamic_bitset/dynamic_bitset.hpp @@ -31,7 +31,6 @@ #include "boost/move/move.hpp" #include "boost/static_assert.hpp" #include "boost/throw_exception.hpp" - #include #include #include @@ -42,64 +41,61 @@ #include #ifndef BOOST_NO_STD_LOCALE -# include +# include #endif namespace boost { -template +template< typename Block, typename Allocator > class dynamic_bitset { - BOOST_STATIC_ASSERT((bool)detail::dynamic_bitset_impl::allowed_block_type::value); - typedef std::vector buffer_type; + BOOST_STATIC_ASSERT( (bool)detail::dynamic_bitset_impl::allowed_block_type< Block >::value ); + typedef std::vector< Block, Allocator > buffer_type; public: - typedef Block block_type; - typedef Allocator allocator_type; - typedef std::size_t size_type; + typedef Block block_type; + typedef Allocator allocator_type; + typedef std::size_t size_type; typedef typename buffer_type::size_type block_width_type; - BOOST_STATIC_CONSTANT(block_width_type, bits_per_block = (std::numeric_limits::digits)); - BOOST_STATIC_CONSTANT(size_type, npos = static_cast(-1)); - + BOOST_STATIC_CONSTANT( block_width_type, bits_per_block = ( std::numeric_limits< Block >::digits ) ); + BOOST_STATIC_CONSTANT( size_type, npos = static_cast< size_type >( -1 ) ); // A proxy class to simulate lvalues of bit type. // class reference { - friend class dynamic_bitset; - + friend class dynamic_bitset< Block, Allocator >; // the one and only non-copy ctor - reference(block_type & b, block_width_type pos); + reference( block_type & b, block_width_type pos ); void operator&(); // left undefined public: - // copy constructor: compiler generated operator bool() const; - bool operator~() const; + bool operator~() const; - reference& flip(); + reference & flip(); - reference& operator=(bool x); - reference& operator=(const reference& rhs); + reference & operator=( bool x ); + reference & operator=( const reference & rhs ); - reference& operator|=(bool x); - reference& operator&=(bool x); - reference& operator^=(bool x); - reference& operator-=(bool x); + reference & operator|=( bool x ); + reference & operator&=( bool x ); + reference & operator^=( bool x ); + reference & operator-=( bool x ); - private: - block_type & m_block; + private: + block_type & m_block; const block_type m_mask; - void do_set(); - void do_reset(); - void do_flip(); - void do_assign(bool x); + void do_set(); + void do_reset(); + void do_flip(); + void do_assign( bool x ); }; typedef bool const_reference; @@ -107,13 +103,9 @@ public: // constructors, etc. dynamic_bitset(); - explicit - dynamic_bitset(const Allocator& alloc); - - explicit - dynamic_bitset(size_type num_bits, unsigned long value = 0, - const Allocator& alloc = Allocator()); + explicit dynamic_bitset( const Allocator & alloc ); + explicit dynamic_bitset( size_type num_bits, unsigned long value = 0, const Allocator & alloc = Allocator() ); // WARNING: you should avoid using this constructor. // @@ -126,322 +118,281 @@ public: // NOTE 2: // split into two constructors because of bugs in MSVC 6.0sp5 with STLport - template - dynamic_bitset(const std::basic_string& s, - typename std::basic_string::size_type pos, - typename std::basic_string::size_type n, - size_type num_bits = npos, - const Allocator& alloc = Allocator()); + template< typename CharT, typename Traits, typename Alloc > + dynamic_bitset( const std::basic_string< CharT, Traits, Alloc > & s, typename std::basic_string< CharT, Traits, Alloc >::size_type pos, typename std::basic_string< CharT, Traits, Alloc >::size_type n, size_type num_bits = npos, const Allocator & alloc = Allocator() ); - template - explicit - dynamic_bitset(const std::basic_string& s, - typename std::basic_string::size_type pos = 0); + template< typename CharT, typename Traits, typename Alloc > + explicit dynamic_bitset( const std::basic_string< CharT, Traits, Alloc > & s, typename std::basic_string< CharT, Traits, Alloc >::size_type pos = 0 ); // The first bit in *first is the least significant bit, and the // last bit in the block just before *last is the most significant bit. - template - dynamic_bitset(BlockInputIterator first, BlockInputIterator last, - const Allocator& alloc = Allocator()); + template< typename BlockInputIterator > + dynamic_bitset( BlockInputIterator first, BlockInputIterator last, const Allocator & alloc = Allocator() ); // copy constructor - dynamic_bitset(const dynamic_bitset& b); + dynamic_bitset( const dynamic_bitset & b ); ~dynamic_bitset(); - void swap(dynamic_bitset& b) BOOST_NOEXCEPT; - dynamic_bitset& operator=(const dynamic_bitset& b); + void swap( dynamic_bitset & b ) BOOST_NOEXCEPT; + dynamic_bitset & operator=( const dynamic_bitset & b ); #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - dynamic_bitset(dynamic_bitset&& src); - dynamic_bitset& operator=(dynamic_bitset&& src); + dynamic_bitset( dynamic_bitset && src ); + dynamic_bitset & operator=( dynamic_bitset && src ); #endif // BOOST_NO_CXX11_RVALUE_REFERENCES allocator_type get_allocator() const; // size changing operations - void resize(size_type num_bits, bool value = false); - void clear(); - void push_back(bool bit); - void pop_back(); - void append(Block block); + void resize( size_type num_bits, bool value = false ); + void clear(); + void push_back( bool bit ); + void pop_back(); + void append( Block block ); - template - void append(BlockInputIterator first, BlockInputIterator last); // strong guarantee + template< typename BlockInputIterator > + void append( BlockInputIterator first, BlockInputIterator last ); // strong guarantee // bitset operations - dynamic_bitset& operator&=(const dynamic_bitset& b); - dynamic_bitset& operator|=(const dynamic_bitset& b); - dynamic_bitset& operator^=(const dynamic_bitset& b); - dynamic_bitset& operator-=(const dynamic_bitset& b); - dynamic_bitset& operator<<=(size_type n); - dynamic_bitset& operator>>=(size_type n); - dynamic_bitset operator<<(size_type n) const; - dynamic_bitset operator>>(size_type n) const; + dynamic_bitset & operator&=( const dynamic_bitset & b ); + dynamic_bitset & operator|=( const dynamic_bitset & b ); + dynamic_bitset & operator^=( const dynamic_bitset & b ); + dynamic_bitset & operator-=( const dynamic_bitset & b ); + dynamic_bitset & operator<<=( size_type n ); + dynamic_bitset & operator>>=( size_type n ); + dynamic_bitset operator<<( size_type n ) const; + dynamic_bitset operator>>( size_type n ) const; // basic bit operations - dynamic_bitset& set(size_type n, size_type len, bool val /* = true */); // default would make it ambiguous - dynamic_bitset& set(size_type n, bool val = true); - dynamic_bitset& set(); - dynamic_bitset& reset(size_type n, size_type len); - dynamic_bitset& reset(size_type n); - dynamic_bitset& reset(); - dynamic_bitset& flip(size_type n, size_type len); - dynamic_bitset& flip(size_type n); - dynamic_bitset& flip(); - reference at(size_type n); - bool at(size_type n) const; - bool test(size_type n) const; - bool test_set(size_type n, bool val = true); - bool all() const; - bool any() const; - bool none() const; - dynamic_bitset operator~() const; - size_type count() const BOOST_NOEXCEPT; + dynamic_bitset & set( size_type n, size_type len, bool val /* = true */ ); // default would make it ambiguous + dynamic_bitset & set( size_type n, bool val = true ); + dynamic_bitset & set(); + dynamic_bitset & reset( size_type n, size_type len ); + dynamic_bitset & reset( size_type n ); + dynamic_bitset & reset(); + dynamic_bitset & flip( size_type n, size_type len ); + dynamic_bitset & flip( size_type n ); + dynamic_bitset & flip(); + reference at( size_type n ); + bool at( size_type n ) const; + bool test( size_type n ) const; + bool test_set( size_type n, bool val = true ); + bool all() const; + bool any() const; + bool none() const; + dynamic_bitset operator~() const; + size_type count() const BOOST_NOEXCEPT; // subscript - reference operator[](size_type pos); - bool operator[](size_type pos) const; + reference operator[]( size_type pos ); + bool operator[]( size_type pos ) const; - unsigned long to_ulong() const; + unsigned long to_ulong() const; - size_type size() const BOOST_NOEXCEPT; - size_type num_blocks() const BOOST_NOEXCEPT; - size_type max_size() const BOOST_NOEXCEPT; - bool empty() const BOOST_NOEXCEPT; - size_type capacity() const BOOST_NOEXCEPT; - void reserve(size_type num_bits); - void shrink_to_fit(); + size_type size() const BOOST_NOEXCEPT; + size_type num_blocks() const BOOST_NOEXCEPT; + size_type max_size() const BOOST_NOEXCEPT; + bool empty() const BOOST_NOEXCEPT; + size_type capacity() const BOOST_NOEXCEPT; + void reserve( size_type num_bits ); + void shrink_to_fit(); - bool is_subset_of(const dynamic_bitset& a) const; - bool is_proper_subset_of(const dynamic_bitset& a) const; - bool intersects(const dynamic_bitset & a) const; + bool is_subset_of( const dynamic_bitset & a ) const; + bool is_proper_subset_of( const dynamic_bitset & a ) const; + bool intersects( const dynamic_bitset & a ) const; // lookup - size_type find_first() const; - size_type find_first(size_type pos) const; - size_type find_next(size_type pos) const; + size_type find_first() const; + size_type find_first( size_type pos ) const; + size_type find_next( size_type pos ) const; // lexicographical comparison - template - friend bool operator==(const dynamic_bitset& a, - const dynamic_bitset& b); + template< typename B, typename A > + friend bool operator==( const dynamic_bitset< B, A > & a, const dynamic_bitset< B, A > & b ); - template - friend bool operator<(const dynamic_bitset& a, - const dynamic_bitset& b); + template< typename B, typename A > + friend bool operator<( const dynamic_bitset< B, A > & a, const dynamic_bitset< B, A > & b ); - template - friend void to_block_range(const dynamic_bitset& b, - BlockOutputIterator result); + template< typename B, typename A, typename BlockOutputIterator > + friend void to_block_range( const dynamic_bitset< B, A > & b, BlockOutputIterator result ); - template - friend void from_block_range(BlockIterator first, BlockIterator last, - dynamic_bitset& result); + template< typename BlockIterator, typename B, typename A > + friend void from_block_range( BlockIterator first, BlockIterator last, dynamic_bitset< B, A > & result ); + template< typename CharT, typename Traits, typename B, typename A > + friend std::basic_istream< CharT, Traits > & operator>>( std::basic_istream< CharT, Traits > & is, dynamic_bitset< B, A > & b ); - template - friend std::basic_istream& operator>>(std::basic_istream& is, - dynamic_bitset& b); - - template - friend void to_string_helper(const dynamic_bitset & b, stringT & s, bool dump_all); - - template - friend std::size_t hash_value(const dynamic_bitset& a); + template< typename B, typename A, typename stringT > + friend void to_string_helper( const dynamic_bitset< B, A > & b, stringT & s, bool dump_all ); + template< typename B, typename A > + friend std::size_t hash_value( const dynamic_bitset< B, A > & a ); // forward declaration for optional zero-copy serialization support class serialize_impl; friend class serialize_impl; private: - BOOST_STATIC_CONSTANT(block_width_type, ulong_width = std::numeric_limits::digits); + BOOST_STATIC_CONSTANT( block_width_type, ulong_width = std::numeric_limits< unsigned long >::digits ); - dynamic_bitset& range_operation(size_type pos, size_type len, - Block (*partial_block_operation)(Block, size_type, size_type), - Block (*full_block_operation)(Block)); - void m_zero_unused_bits(); - bool m_check_invariants() const; + dynamic_bitset & range_operation( size_type pos, size_type len, Block ( *partial_block_operation )( Block, size_type, size_type ), Block ( *full_block_operation )( Block ) ); + void m_zero_unused_bits(); + bool m_check_invariants() const; - static bool m_not_empty(Block x); - size_type m_do_find_from(size_type first_block) const; + static bool m_not_empty( Block x ); + size_type m_do_find_from( size_type first_block ) const; - block_width_type count_extra_bits() const BOOST_NOEXCEPT; - static size_type block_index(size_type pos) BOOST_NOEXCEPT; - static block_width_type bit_index(size_type pos) BOOST_NOEXCEPT; - static Block bit_mask(size_type pos) BOOST_NOEXCEPT; - static Block bit_mask(size_type first, size_type last) BOOST_NOEXCEPT; - static Block set_block_bits(Block block, size_type first, - size_type last, bool val) BOOST_NOEXCEPT; + block_width_type count_extra_bits() const BOOST_NOEXCEPT; + static size_type block_index( size_type pos ) BOOST_NOEXCEPT; + static block_width_type bit_index( size_type pos ) BOOST_NOEXCEPT; + static Block bit_mask( size_type pos ) BOOST_NOEXCEPT; + static Block bit_mask( size_type first, size_type last ) BOOST_NOEXCEPT; + static Block set_block_bits( Block block, size_type first, size_type last, bool val ) BOOST_NOEXCEPT; // Functions for operations on ranges - inline static Block set_block_partial(Block block, size_type first, - size_type last) BOOST_NOEXCEPT; - inline static Block set_block_full(Block) BOOST_NOEXCEPT; - inline static Block reset_block_partial(Block block, size_type first, - size_type last) BOOST_NOEXCEPT; - inline static Block reset_block_full(Block) BOOST_NOEXCEPT; - inline static Block flip_block_partial(Block block, size_type first, - size_type last) BOOST_NOEXCEPT; - inline static Block flip_block_full(Block block) BOOST_NOEXCEPT; + inline static Block set_block_partial( Block block, size_type first, size_type last ) BOOST_NOEXCEPT; + inline static Block set_block_full( Block ) BOOST_NOEXCEPT; + inline static Block reset_block_partial( Block block, size_type first, size_type last ) BOOST_NOEXCEPT; + inline static Block reset_block_full( Block ) BOOST_NOEXCEPT; + inline static Block flip_block_partial( Block block, size_type first, size_type last ) BOOST_NOEXCEPT; + inline static Block flip_block_full( Block block ) BOOST_NOEXCEPT; - template - void dispatch_init(T num_bits, unsigned long value, - detail::dynamic_bitset_impl::value_to_type); + template< typename T > + void dispatch_init( T num_bits, unsigned long value, detail::dynamic_bitset_impl::value_to_type< true > ); - template - void dispatch_init(T first, T last, - detail::dynamic_bitset_impl::value_to_type); + template< typename T > + void dispatch_init( T first, T last, detail::dynamic_bitset_impl::value_to_type< false > ); - template - void init_from_block_range(BlockIter first, BlockIter last); + template< typename BlockIter > + void init_from_block_range( BlockIter first, BlockIter last ); - template - void init_from_string(const std::basic_string& s, - typename std::basic_string::size_type pos, - typename std::basic_string::size_type n, - size_type num_bits); + template< typename CharT, typename Traits, typename Alloc > + void init_from_string( const std::basic_string< CharT, Traits, Alloc > & s, typename std::basic_string< CharT, Traits, Alloc >::size_type pos, typename std::basic_string< CharT, Traits, Alloc >::size_type n, size_type num_bits ); - void init_from_unsigned_long(size_type num_bits, - unsigned long value/*, - const Allocator& alloc*/); + void init_from_unsigned_long( size_type num_bits, unsigned long value /*, + const Allocator& alloc*/ + ); - template - void m_append(BlockInputIterator first, BlockInputIterator last, std::input_iterator_tag); + template< typename BlockInputIterator > + void m_append( BlockInputIterator first, BlockInputIterator last, std::input_iterator_tag ); - template - void m_append(BlockInputIterator first, BlockInputIterator last, std::forward_iterator_tag); + template< typename BlockInputIterator > + void m_append( BlockInputIterator first, BlockInputIterator last, std::forward_iterator_tag ); - bool m_unchecked_test(size_type pos) const; - static size_type calc_num_blocks(size_type num_bits); + bool m_unchecked_test( size_type pos ) const; + static size_type calc_num_blocks( size_type num_bits ); - Block& m_highest_block(); - const Block& m_highest_block() const; - - buffer_type m_bits; - size_type m_num_bits; + Block & m_highest_block(); + const Block & m_highest_block() const; + buffer_type m_bits; + size_type m_num_bits; class bit_appender; friend class bit_appender; - class bit_appender { - // helper for stream >> - // Supplies to the lack of an efficient append at the less - // significant end: bits are actually appended "at left" but - // rearranged in the destructor. From the perspective of - // client code everything works *as if* dynamic_bitset<> had - // an append_at_right() function (eventually throwing the same - // exceptions as push_back) except that the function is in fact - // called bit_appender::do_append(). - // - dynamic_bitset & bs; - size_type n; - Block mask; - Block * current; + class bit_appender + { + // helper for stream >> + // Supplies to the lack of an efficient append at the less + // significant end: bits are actually appended "at left" but + // rearranged in the destructor. From the perspective of + // client code everything works *as if* dynamic_bitset<> had + // an append_at_right() function (eventually throwing the same + // exceptions as push_back) except that the function is in fact + // called bit_appender::do_append(). + // + dynamic_bitset & bs; + size_type n; + Block mask; + Block * current; - // not implemented - bit_appender(const bit_appender &); - bit_appender & operator=(const bit_appender &); + // not implemented + bit_appender( const bit_appender & ); + bit_appender & operator=( const bit_appender & ); public: - bit_appender(dynamic_bitset & r); + bit_appender( dynamic_bitset & r ); ~bit_appender(); - inline void do_append(bool value); - size_type get_count() const; + inline void do_append( bool value ); + size_type get_count() const; }; }; -#if !defined BOOST_NO_INCLASS_MEMBER_INITIALIZATION +#if ! defined BOOST_NO_INCLASS_MEMBER_INITIALIZATION -template -const typename dynamic_bitset::block_width_type -dynamic_bitset::bits_per_block; +template< typename Block, typename Allocator > +const typename dynamic_bitset< Block, Allocator >::block_width_type + dynamic_bitset< Block, Allocator >::bits_per_block; -template -const typename dynamic_bitset::size_type -dynamic_bitset::npos; +template< typename Block, typename Allocator > +const typename dynamic_bitset< Block, Allocator >::size_type + dynamic_bitset< Block, Allocator >::npos; -template -const typename dynamic_bitset::block_width_type -dynamic_bitset::ulong_width; +template< typename Block, typename Allocator > +const typename dynamic_bitset< Block, Allocator >::block_width_type + dynamic_bitset< Block, Allocator >::ulong_width; #endif // Global Functions: // comparison -template -bool operator!=(const dynamic_bitset& a, - const dynamic_bitset& b); +template< typename Block, typename Allocator > +bool operator!=( const dynamic_bitset< Block, Allocator > & a, const dynamic_bitset< Block, Allocator > & b ); -template -bool operator<=(const dynamic_bitset& a, - const dynamic_bitset& b); +template< typename Block, typename Allocator > +bool operator<=( const dynamic_bitset< Block, Allocator > & a, const dynamic_bitset< Block, Allocator > & b ); -template -bool operator>(const dynamic_bitset& a, - const dynamic_bitset& b); +template< typename Block, typename Allocator > +bool operator>( const dynamic_bitset< Block, Allocator > & a, const dynamic_bitset< Block, Allocator > & b ); -template -bool operator>=(const dynamic_bitset& a, - const dynamic_bitset& b); +template< typename Block, typename Allocator > +bool operator>=( const dynamic_bitset< Block, Allocator > & a, const dynamic_bitset< Block, Allocator > & b ); // stream operators -template -std::basic_ostream& -operator<<(std::basic_ostream& os, - const dynamic_bitset& b); +template< typename CharT, typename Traits, typename Block, typename Allocator > +std::basic_ostream< CharT, Traits > & +operator<<( std::basic_ostream< CharT, Traits > & os, const dynamic_bitset< Block, Allocator > & b ); -template -std::basic_istream& -operator>>(std::basic_istream& is, - dynamic_bitset& b); +template< typename CharT, typename Traits, typename Block, typename Allocator > +std::basic_istream< CharT, Traits > & +operator>>( std::basic_istream< CharT, Traits > & is, dynamic_bitset< Block, Allocator > & b ); // bitset operations -template -dynamic_bitset -operator&(const dynamic_bitset& b1, - const dynamic_bitset& b2); +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +operator&( const dynamic_bitset< Block, Allocator > & b1, const dynamic_bitset< Block, Allocator > & b2 ); -template -dynamic_bitset -operator|(const dynamic_bitset& b1, - const dynamic_bitset& b2); +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +operator|( const dynamic_bitset< Block, Allocator > & b1, const dynamic_bitset< Block, Allocator > & b2 ); -template -dynamic_bitset -operator^(const dynamic_bitset& b1, - const dynamic_bitset& b2); +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +operator^( const dynamic_bitset< Block, Allocator > & b1, const dynamic_bitset< Block, Allocator > & b2 ); -template -dynamic_bitset -operator-(const dynamic_bitset& b1, - const dynamic_bitset& b2); +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +operator-( const dynamic_bitset< Block, Allocator > & b1, const dynamic_bitset< Block, Allocator > & b2 ); // namespace scope swap -template -void swap(dynamic_bitset& b1, - dynamic_bitset& b2) BOOST_NOEXCEPT; +template< typename Block, typename Allocator > +void swap( dynamic_bitset< Block, Allocator > & b1, dynamic_bitset< Block, Allocator > & b2 ) BOOST_NOEXCEPT; - -template +template< typename Block, typename Allocator, typename stringT > void -to_string(const dynamic_bitset& b, stringT & s); +to_string( const dynamic_bitset< Block, Allocator > & b, stringT & s ); -template +template< typename Block, typename Allocator, typename BlockOutputIterator > void -to_block_range(const dynamic_bitset& b, - BlockOutputIterator result); +to_block_range( const dynamic_bitset< Block, Allocator > & b, BlockOutputIterator result ); - -template +template< typename BlockIterator, typename B, typename A > inline void -from_block_range(BlockIterator first, BlockIterator last, - dynamic_bitset& result); +from_block_range( BlockIterator first, BlockIterator last, dynamic_bitset< B, A > & result ); } #include "impl/dynamic_bitset.tpp" #endif // include guard - diff --git a/include/boost/dynamic_bitset/impl/dynamic_bitset.tpp b/include/boost/dynamic_bitset/impl/dynamic_bitset.tpp index e54bebb..389dfb1 100644 --- a/include/boost/dynamic_bitset/impl/dynamic_bitset.tpp +++ b/include/boost/dynamic_bitset/impl/dynamic_bitset.tpp @@ -18,260 +18,258 @@ namespace boost { -template -dynamic_bitset::reference::reference(block_type & b, block_width_type pos) - : m_block(b), - m_mask( (BOOST_ASSERT(pos < bits_per_block), block_type(1) << pos )) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator >::reference::reference( block_type & b, block_width_type pos ) + : m_block( b ), m_mask( ( BOOST_ASSERT( pos < bits_per_block ), block_type( 1 ) << pos ) ) { } -template -dynamic_bitset::reference::operator bool() const +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator >::reference:: +operator bool() const { - return (m_block & m_mask) != 0; + return ( m_block & m_mask ) != 0; } -template +template< typename Block, typename Allocator > bool -dynamic_bitset::reference::operator~() const +dynamic_bitset< Block, Allocator >::reference::operator~() const { - return (m_block & m_mask) == 0; + return ( m_block & m_mask ) == 0; } -template -typename dynamic_bitset::reference& -dynamic_bitset::reference::flip() +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::reference & +dynamic_bitset< Block, Allocator >::reference::flip() { do_flip(); return *this; } -template -typename dynamic_bitset::reference& -dynamic_bitset::reference::operator=(bool x) +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::reference & +dynamic_bitset< Block, Allocator >::reference::operator=( bool x ) { - do_assign(x); + do_assign( x ); return *this; } -template -typename dynamic_bitset::reference& -dynamic_bitset::reference::operator=(const reference& rhs) +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::reference & +dynamic_bitset< Block, Allocator >::reference::operator=( const reference & rhs ) { - do_assign(rhs); + do_assign( rhs ); return *this; } -template -typename dynamic_bitset::reference& -dynamic_bitset::reference::operator|=(bool x) +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::reference & +dynamic_bitset< Block, Allocator >::reference::operator|=( bool x ) { - if (x) { + if ( x ) { do_set(); } return *this; } - -template -typename dynamic_bitset::reference& -dynamic_bitset::reference::operator&=(bool x) +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::reference & +dynamic_bitset< Block, Allocator >::reference::operator&=( bool x ) { - if (!x) { + if ( ! x ) { do_reset(); } return *this; } -template -typename dynamic_bitset::reference& -dynamic_bitset::reference::operator^=(bool x) +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::reference & +dynamic_bitset< Block, Allocator >::reference::operator^=( bool x ) { - if (x) { + if ( x ) { do_flip(); } return *this; } -template -typename dynamic_bitset::reference& -dynamic_bitset::reference::operator-=(bool x) +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::reference & +dynamic_bitset< Block, Allocator >::reference::operator-=( bool x ) { - if (x) { + if ( x ) { do_reset(); } return *this; } -template +template< typename Block, typename Allocator > void -dynamic_bitset::reference::do_set() +dynamic_bitset< Block, Allocator >::reference::do_set() { m_block |= m_mask; } -template +template< typename Block, typename Allocator > void -dynamic_bitset::reference::do_reset() +dynamic_bitset< Block, Allocator >::reference::do_reset() { m_block &= ~m_mask; } -template +template< typename Block, typename Allocator > void -dynamic_bitset::reference::do_flip() +dynamic_bitset< Block, Allocator >::reference::do_flip() { m_block ^= m_mask; } -template +template< typename Block, typename Allocator > void -dynamic_bitset::reference::do_assign(bool x) +dynamic_bitset< Block, Allocator >::reference::do_assign( bool x ) { - if (x) { + if ( x ) { do_set(); } else { do_reset(); } } -template +template< typename BlockIterator, typename B, typename A > inline void -from_block_range(BlockIterator first, BlockIterator last, - dynamic_bitset& result) +from_block_range( BlockIterator first, BlockIterator last, dynamic_bitset< B, A > & result ) { // PRE: distance(first, last) <= numblocks() - std::copy (first, last, result.m_bits.begin()); + std::copy( first, last, result.m_bits.begin() ); } - -template -dynamic_bitset::dynamic_bitset() - : m_num_bits(0) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator >::dynamic_bitset() + : m_num_bits( 0 ) { } -template -dynamic_bitset::dynamic_bitset(const Allocator& alloc) - : m_bits(alloc), m_num_bits(0) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator >::dynamic_bitset( const Allocator & alloc ) + : m_bits( alloc ), m_num_bits( 0 ) { } -template -dynamic_bitset:: -dynamic_bitset(size_type num_bits, unsigned long value, const Allocator& alloc) - : m_bits(alloc), - m_num_bits(0) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator >:: + dynamic_bitset( size_type num_bits, unsigned long value, const Allocator & alloc ) + : m_bits( alloc ), m_num_bits( 0 ) { - init_from_unsigned_long(num_bits, value); + init_from_unsigned_long( num_bits, value ); } -template -template -dynamic_bitset::dynamic_bitset( - const std::basic_string& s, - typename std::basic_string::size_type pos, - typename std::basic_string::size_type n, - size_type num_bits, - const Allocator& alloc) +template< typename Block, typename Allocator > +template< typename CharT, typename Traits, typename Alloc > +dynamic_bitset< Block, Allocator >::dynamic_bitset( + const std::basic_string< CharT, Traits, Alloc > & s, + typename std::basic_string< CharT, Traits, Alloc >::size_type pos, + typename std::basic_string< CharT, Traits, Alloc >::size_type n, + size_type num_bits, + const Allocator & alloc ) -:m_bits(alloc), - m_num_bits(0) + : m_bits( alloc ), m_num_bits( 0 ) { - init_from_string(s, pos, n, num_bits); + init_from_string( s, pos, n, num_bits ); } -template -template -dynamic_bitset::dynamic_bitset( - const std::basic_string& s, - typename std::basic_string::size_type pos) - : m_bits(Allocator()), - m_num_bits(0) +template< typename Block, typename Allocator > +template< typename CharT, typename Traits, typename Alloc > +dynamic_bitset< Block, Allocator >::dynamic_bitset( + const std::basic_string< CharT, Traits, Alloc > & s, + typename std::basic_string< CharT, Traits, Alloc >::size_type pos ) + : m_bits( Allocator() ), m_num_bits( 0 ) { - init_from_string(s, pos, (std::basic_string::npos), npos); + init_from_string( s, pos, ( std::basic_string< CharT, Traits, Alloc >::npos ), npos ); } -template -template -dynamic_bitset::dynamic_bitset( +template< typename Block, typename Allocator > +template< typename BlockInputIterator > +dynamic_bitset< Block, Allocator >::dynamic_bitset( BlockInputIterator first, BlockInputIterator last, - const Allocator& alloc) - : m_bits(alloc), - m_num_bits(0) + const Allocator & alloc ) + : m_bits( alloc ), m_num_bits( 0 ) { - using boost::detail::dynamic_bitset_impl::value_to_type; using boost::detail::dynamic_bitset_impl::is_numeric; + using boost::detail::dynamic_bitset_impl::value_to_type; const value_to_type< - is_numeric::value> selector; + is_numeric< BlockInputIterator >::value > + selector; - dispatch_init(first, last, selector); + dispatch_init( first, last, selector ); } - // copy constructor -template -inline dynamic_bitset:: dynamic_bitset(const dynamic_bitset& b) - : m_bits(b.m_bits), m_num_bits(b.m_num_bits) +template< typename Block, typename Allocator > +inline dynamic_bitset< Block, Allocator >::dynamic_bitset( const dynamic_bitset & b ) + : m_bits( b.m_bits ), m_num_bits( b.m_num_bits ) { } -template -inline dynamic_bitset:: ~dynamic_bitset() +template< typename Block, typename Allocator > +inline dynamic_bitset< Block, Allocator >::~dynamic_bitset() { - BOOST_ASSERT(m_check_invariants()); + BOOST_ASSERT( m_check_invariants() ); } -template -inline void dynamic_bitset:: -swap(dynamic_bitset& b) BOOST_NOEXCEPT +template< typename Block, typename Allocator > +inline void +dynamic_bitset< Block, Allocator >:: + swap( dynamic_bitset< Block, Allocator > & b ) BOOST_NOEXCEPT { - std::swap(m_bits, b.m_bits); - std::swap(m_num_bits, b.m_num_bits); + std::swap( m_bits, b.m_bits ); + std::swap( m_num_bits, b.m_num_bits ); } -template -dynamic_bitset& dynamic_bitset:: -operator=(const dynamic_bitset& b) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >:: +operator=( const dynamic_bitset< Block, Allocator > & b ) { - m_bits = b.m_bits; + m_bits = b.m_bits; m_num_bits = b.m_num_bits; return *this; } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES -template -inline dynamic_bitset:: -dynamic_bitset(dynamic_bitset&& b) - : m_bits(boost::move(b.m_bits)), m_num_bits(boost::move(b.m_num_bits)) +template< typename Block, typename Allocator > +inline dynamic_bitset< Block, Allocator >:: + dynamic_bitset( dynamic_bitset< Block, Allocator > && b ) + : m_bits( boost::move( b.m_bits ) ), m_num_bits( boost::move( b.m_num_bits ) ) { // Required so that BOOST_ASSERT(m_check_invariants()); works. - BOOST_ASSERT((b.m_bits = buffer_type(get_allocator())).empty()); + BOOST_ASSERT( ( b.m_bits = buffer_type( get_allocator() ) ).empty() ); b.m_num_bits = 0; } -template -inline dynamic_bitset& dynamic_bitset:: -operator=(dynamic_bitset&& b) +template< typename Block, typename Allocator > +inline dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >:: +operator=( dynamic_bitset< Block, Allocator > && b ) { - if (&b == this) { return *this; } + if ( &b == this ) { + return *this; + } - m_bits = boost::move(b.m_bits); - m_num_bits = boost::move(b.m_num_bits); + m_bits = boost::move( b.m_bits ); + m_num_bits = boost::move( b.m_num_bits ); // Required so that BOOST_ASSERT(m_check_invariants()); works. - BOOST_ASSERT((b.m_bits = buffer_type(get_allocator())).empty()); + BOOST_ASSERT( ( b.m_bits = buffer_type( get_allocator() ) ).empty() ); b.m_num_bits = 0; return *this; } #endif // BOOST_NO_CXX11_RVALUE_REFERENCES -template -inline typename dynamic_bitset::allocator_type -dynamic_bitset::get_allocator() const +template< typename Block, typename Allocator > +inline typename dynamic_bitset< Block, Allocator >::allocator_type +dynamic_bitset< Block, Allocator >::get_allocator() const { return m_bits.get_allocator(); } @@ -279,21 +277,20 @@ dynamic_bitset::get_allocator() const //----------------------------------------------------------------------------- // size changing operations -template -void dynamic_bitset:: -resize(size_type num_bits, bool value) // strong guarantee +template< typename Block, typename Allocator > +void +dynamic_bitset< Block, Allocator >:: + resize( size_type num_bits, bool value ) // strong guarantee { + const size_type old_num_blocks = num_blocks(); + const size_type required_blocks = calc_num_blocks( num_bits ); - const size_type old_num_blocks = num_blocks(); - const size_type required_blocks = calc_num_blocks(num_bits); + const block_type v = value ? detail::dynamic_bitset_impl::max_limit< Block >::value : Block( 0 ); - const block_type v = value? detail::dynamic_bitset_impl::max_limit::value : Block(0); - - if (required_blocks != old_num_blocks) { - m_bits.resize(required_blocks, v); // s.g. (copy) + if ( required_blocks != old_num_blocks ) { + m_bits.resize( required_blocks, v ); // s.g. (copy) } - // At this point: // // - if the buffer was shrunk, we have nothing more to do, @@ -304,128 +301,127 @@ resize(size_type num_bits, bool value) // strong guarantee // any, that were 'unused bits' before enlarging: if value == true, // they must be set. - if (value && (num_bits > m_num_bits)) { - + if ( value && ( num_bits > m_num_bits ) ) { const block_width_type extra_bits = count_extra_bits(); - if (extra_bits) { - BOOST_ASSERT(old_num_blocks >= 1 && old_num_blocks <= m_bits.size()); + if ( extra_bits ) { + BOOST_ASSERT( old_num_blocks >= 1 && old_num_blocks <= m_bits.size() ); // Set them. - m_bits[old_num_blocks - 1] |= (v << extra_bits); + m_bits[ old_num_blocks - 1 ] |= ( v << extra_bits ); } - } m_num_bits = num_bits; m_zero_unused_bits(); } -template -void dynamic_bitset:: -clear() // no throw +template< typename Block, typename Allocator > +void +dynamic_bitset< Block, Allocator >:: + clear() // no throw { m_bits.clear(); m_num_bits = 0; } -template -void dynamic_bitset:: -push_back(bool bit) +template< typename Block, typename Allocator > +void +dynamic_bitset< Block, Allocator >:: + push_back( bool bit ) { const size_type sz = size(); - resize(sz + 1, bit); + resize( sz + 1, bit ); } -template -void dynamic_bitset:: -pop_back() +template< typename Block, typename Allocator > +void +dynamic_bitset< Block, Allocator >:: + pop_back() { - BOOST_ASSERT( !empty() ); - const size_type old_num_blocks = num_blocks(); - const size_type required_blocks = calc_num_blocks(m_num_bits - 1); + BOOST_ASSERT( ! empty() ); + const size_type old_num_blocks = num_blocks(); + const size_type required_blocks = calc_num_blocks( m_num_bits - 1 ); - if (required_blocks != old_num_blocks) { - m_bits.pop_back(); + if ( required_blocks != old_num_blocks ) { + m_bits.pop_back(); } --m_num_bits; m_zero_unused_bits(); } - -template -void dynamic_bitset:: -append(Block value) // strong guarantee +template< typename Block, typename Allocator > +void +dynamic_bitset< Block, Allocator >:: + append( Block value ) // strong guarantee { const block_width_type r = count_extra_bits(); - if (r == 0) { + if ( r == 0 ) { // the buffer is empty, or all blocks are filled - m_bits.push_back(value); - } - else { - m_bits.push_back(value >> (bits_per_block - r)); - m_bits[m_bits.size() - 2] |= (value << r); // m_bits.size() >= 2 + m_bits.push_back( value ); + } else { + m_bits.push_back( value >> ( bits_per_block - r ) ); + m_bits[ m_bits.size() - 2 ] |= ( value << r ); // m_bits.size() >= 2 } m_num_bits += bits_per_block; - BOOST_ASSERT(m_check_invariants()); - + BOOST_ASSERT( m_check_invariants() ); } -template -template +template< typename Block, typename Allocator > +template< typename BlockInputIterator > void -dynamic_bitset::append(BlockInputIterator first, BlockInputIterator last) // strong guarantee +dynamic_bitset< Block, Allocator >::append( BlockInputIterator first, BlockInputIterator last ) // strong guarantee { - if (first != last) { - typename std::iterator_traits::iterator_category cat; - m_append(first, last, cat); + if ( first != last ) { + typename std::iterator_traits< BlockInputIterator >::iterator_category cat; + m_append( first, last, cat ); } } //----------------------------------------------------------------------------- // bitset operations -template -dynamic_bitset& -dynamic_bitset::operator&=(const dynamic_bitset& rhs) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::operator&=( const dynamic_bitset & rhs ) { - BOOST_ASSERT(size() == rhs.size()); - for (size_type i = 0; i < num_blocks(); ++i) - m_bits[i] &= rhs.m_bits[i]; + BOOST_ASSERT( size() == rhs.size() ); + for ( size_type i = 0; i < num_blocks(); ++i ) + m_bits[ i ] &= rhs.m_bits[ i ]; return *this; } -template -dynamic_bitset& -dynamic_bitset::operator|=(const dynamic_bitset& rhs) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::operator|=( const dynamic_bitset & rhs ) { - BOOST_ASSERT(size() == rhs.size()); - for (size_type i = 0; i < num_blocks(); ++i) - m_bits[i] |= rhs.m_bits[i]; - //m_zero_unused_bits(); + BOOST_ASSERT( size() == rhs.size() ); + for ( size_type i = 0; i < num_blocks(); ++i ) + m_bits[ i ] |= rhs.m_bits[ i ]; + // m_zero_unused_bits(); return *this; } -template -dynamic_bitset& -dynamic_bitset::operator^=(const dynamic_bitset& rhs) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::operator^=( const dynamic_bitset & rhs ) { - BOOST_ASSERT(size() == rhs.size()); - for (size_type i = 0; i < this->num_blocks(); ++i) - m_bits[i] ^= rhs.m_bits[i]; - //m_zero_unused_bits(); + BOOST_ASSERT( size() == rhs.size() ); + for ( size_type i = 0; i < this->num_blocks(); ++i ) + m_bits[ i ] ^= rhs.m_bits[ i ]; + // m_zero_unused_bits(); return *this; } -template -dynamic_bitset& -dynamic_bitset::operator-=(const dynamic_bitset& rhs) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::operator-=( const dynamic_bitset & rhs ) { - BOOST_ASSERT(size() == rhs.size()); - for (size_type i = 0; i < num_blocks(); ++i) - m_bits[i] &= ~rhs.m_bits[i]; - //m_zero_unused_bits(); + BOOST_ASSERT( size() == rhs.size() ); + for ( size_type i = 0; i < num_blocks(); ++i ) + m_bits[ i ] &= ~rhs.m_bits[ i ]; + // m_zero_unused_bits(); return *this; } @@ -434,43 +430,39 @@ dynamic_bitset::operator-=(const dynamic_bitset& rhs) // behavior when the left hand operand of >> isn't promoted to a // wider type (because rs would be too large). // -template -dynamic_bitset& -dynamic_bitset::operator<<=(size_type n) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::operator<<=( size_type n ) { - if (n >= m_num_bits) + if ( n >= m_num_bits ) return reset(); - //else - if (n > 0) { - - const size_type last = num_blocks() - 1; // num_blocks() is >= 1 - const size_type div = n / bits_per_block; // div is <= last - const block_width_type r = bit_index(n); - block_type * const b = &m_bits[0]; - - if (r != 0) { + // else + if ( n > 0 ) { + const size_type last = num_blocks() - 1; // num_blocks() is >= 1 + const size_type div = n / bits_per_block; // div is <= last + const block_width_type r = bit_index( n ); + block_type * const b = &m_bits[ 0 ]; + if ( r != 0 ) { const block_width_type rs = bits_per_block - r; - for (size_type i = last-div; i>0; --i) { - b[i+div] = (b[i] << r) | (b[i-1] >> rs); + for ( size_type i = last - div; i > 0; --i ) { + b[ i + div ] = ( b[ i ] << r ) | ( b[ i - 1 ] >> rs ); } - b[div] = b[0] << r; + b[ div ] = b[ 0 ] << r; - } - else { - for (size_type i = last-div; i>0; --i) { - b[i+div] = b[i]; + } else { + for ( size_type i = last - div; i > 0; --i ) { + b[ i + div ] = b[ i ]; } - b[div] = b[0]; + b[ div ] = b[ 0 ]; } // zero out div blocks at the less significant end - std::fill_n(m_bits.begin(), div, static_cast(0)); + std::fill_n( m_bits.begin(), div, static_cast< block_type >( 0 ) ); // zero out any 1 bit that flowed into the unused part m_zero_unused_bits(); // thanks to Lester Gong - } return *this; @@ -480,247 +472,250 @@ dynamic_bitset::operator<<=(size_type n) // NOTE: // See the comments to operator <<=. // -template -dynamic_bitset & dynamic_bitset::operator>>=(size_type n) { - if (n >= m_num_bits) { +template< typename B, typename A > +dynamic_bitset< B, A > & +dynamic_bitset< B, A >::operator>>=( size_type n ) +{ + if ( n >= m_num_bits ) { return reset(); } - //else - if (n>0) { - - const size_type last = num_blocks() - 1; // num_blocks() is >= 1 - const size_type div = n / bits_per_block; // div is <= last - const block_width_type r = bit_index(n); - block_type * const b = &m_bits[0]; - - if (r != 0) { + // else + if ( n > 0 ) { + const size_type last = num_blocks() - 1; // num_blocks() is >= 1 + const size_type div = n / bits_per_block; // div is <= last + const block_width_type r = bit_index( n ); + block_type * const b = &m_bits[ 0 ]; + if ( r != 0 ) { const block_width_type ls = bits_per_block - r; - for (size_type i = div; i < last; ++i) { - b[i-div] = (b[i] >> r) | (b[i+1] << ls); + for ( size_type i = div; i < last; ++i ) { + b[ i - div ] = ( b[ i ] >> r ) | ( b[ i + 1 ] << ls ); } // r bits go to zero - b[last-div] = b[last] >> r; + b[ last - div ] = b[ last ] >> r; } else { - for (size_type i = div; i <= last; ++i) { - b[i-div] = b[i]; + for ( size_type i = div; i <= last; ++i ) { + b[ i - div ] = b[ i ]; } // note the '<=': the last iteration 'absorbs' // b[last-div] = b[last] >> 0; } // div blocks are zero filled at the most significant end - std::fill_n(m_bits.begin() + (num_blocks()-div), div, static_cast(0)); + std::fill_n( m_bits.begin() + ( num_blocks() - div ), div, static_cast< block_type >( 0 ) ); } return *this; } - -template -dynamic_bitset -dynamic_bitset::operator<<(size_type n) const +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +dynamic_bitset< Block, Allocator >::operator<<( size_type n ) const { - dynamic_bitset r(*this); + dynamic_bitset r( *this ); return r <<= n; } -template -dynamic_bitset -dynamic_bitset::operator>>(size_type n) const +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +dynamic_bitset< Block, Allocator >::operator>>( size_type n ) const { - dynamic_bitset r(*this); + dynamic_bitset r( *this ); return r >>= n; } - //----------------------------------------------------------------------------- // basic bit operations -template -dynamic_bitset& -dynamic_bitset::set(size_type pos, - size_type len, bool val) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::set( size_type pos, size_type len, bool val ) { - if (val) - return range_operation(pos, len, set_block_partial, set_block_full); + if ( val ) + return range_operation( pos, len, set_block_partial, set_block_full ); else - return range_operation(pos, len, reset_block_partial, reset_block_full); + return range_operation( pos, len, reset_block_partial, reset_block_full ); } -template -dynamic_bitset& -dynamic_bitset::set(size_type pos, bool val) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::set( size_type pos, bool val ) { - BOOST_ASSERT(pos < m_num_bits); + BOOST_ASSERT( pos < m_num_bits ); - if (val) - m_bits[block_index(pos)] |= bit_mask(pos); + if ( val ) + m_bits[ block_index( pos ) ] |= bit_mask( pos ); else - reset(pos); + reset( pos ); return *this; } -template -dynamic_bitset& -dynamic_bitset::set() +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::set() { - std::fill(m_bits.begin(), m_bits.end(), detail::dynamic_bitset_impl::max_limit::value); - m_zero_unused_bits(); - return *this; -} - -template -inline dynamic_bitset& -dynamic_bitset::reset(size_type pos, size_type len) -{ - return range_operation(pos, len, reset_block_partial, reset_block_full); -} - -template -dynamic_bitset& -dynamic_bitset::reset(size_type pos) -{ - BOOST_ASSERT(pos < m_num_bits); - m_bits[block_index(pos)] &= ~bit_mask(pos); - return *this; -} - -template -dynamic_bitset& -dynamic_bitset::reset() -{ - std::fill(m_bits.begin(), m_bits.end(), Block(0)); - return *this; -} - -template -dynamic_bitset& -dynamic_bitset::flip(size_type pos, size_type len) -{ - return range_operation(pos, len, flip_block_partial, flip_block_full); -} - -template -dynamic_bitset& -dynamic_bitset::flip(size_type pos) -{ - BOOST_ASSERT(pos < m_num_bits); - m_bits[block_index(pos)] ^= bit_mask(pos); - return *this; -} - -template -dynamic_bitset& -dynamic_bitset::flip() -{ - for (size_type i = 0; i < num_blocks(); ++i) - m_bits[i] = ~m_bits[i]; + std::fill( m_bits.begin(), m_bits.end(), detail::dynamic_bitset_impl::max_limit< Block >::value ); m_zero_unused_bits(); return *this; } -template -typename dynamic_bitset::reference -dynamic_bitset::at(size_type pos) +template< typename Block, typename Allocator > +inline dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::reset( size_type pos, size_type len ) { - if (pos >= m_num_bits) - BOOST_THROW_EXCEPTION(std::out_of_range("boost::dynamic_bitset::at out_of_range")); - - return (*this)[pos]; + return range_operation( pos, len, reset_block_partial, reset_block_full ); } -template -bool dynamic_bitset::at(size_type pos) const +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::reset( size_type pos ) { - if (pos >= m_num_bits) - BOOST_THROW_EXCEPTION(std::out_of_range("boost::dynamic_bitset::at out_of_range")); - - return (*this)[pos]; + BOOST_ASSERT( pos < m_num_bits ); + m_bits[ block_index( pos ) ] &= ~bit_mask( pos ); + return *this; } -template -bool dynamic_bitset::test(size_type pos) const +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::reset() { - BOOST_ASSERT(pos < m_num_bits); - return m_unchecked_test(pos); + std::fill( m_bits.begin(), m_bits.end(), Block( 0 ) ); + return *this; } -template -bool dynamic_bitset::test_set(size_type pos, bool val) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::flip( size_type pos, size_type len ) { - const bool b = test(pos); - if (b != val) { - set(pos, val); + return range_operation( pos, len, flip_block_partial, flip_block_full ); +} + +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::flip( size_type pos ) +{ + BOOST_ASSERT( pos < m_num_bits ); + m_bits[ block_index( pos ) ] ^= bit_mask( pos ); + return *this; +} + +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::flip() +{ + for ( size_type i = 0; i < num_blocks(); ++i ) + m_bits[ i ] = ~m_bits[ i ]; + m_zero_unused_bits(); + return *this; +} + +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::reference +dynamic_bitset< Block, Allocator >::at( size_type pos ) +{ + if ( pos >= m_num_bits ) + BOOST_THROW_EXCEPTION( std::out_of_range( "boost::dynamic_bitset::at out_of_range" ) ); + + return ( *this )[ pos ]; +} + +template< typename Block, typename Allocator > +bool +dynamic_bitset< Block, Allocator >::at( size_type pos ) const +{ + if ( pos >= m_num_bits ) + BOOST_THROW_EXCEPTION( std::out_of_range( "boost::dynamic_bitset::at out_of_range" ) ); + + return ( *this )[ pos ]; +} + +template< typename Block, typename Allocator > +bool +dynamic_bitset< Block, Allocator >::test( size_type pos ) const +{ + BOOST_ASSERT( pos < m_num_bits ); + return m_unchecked_test( pos ); +} + +template< typename Block, typename Allocator > +bool +dynamic_bitset< Block, Allocator >::test_set( size_type pos, bool val ) +{ + const bool b = test( pos ); + if ( b != val ) { + set( pos, val ); } return b; } -template -bool dynamic_bitset::all() const +template< typename Block, typename Allocator > +bool +dynamic_bitset< Block, Allocator >::all() const { - if (empty()) { + if ( empty() ) { return true; } const block_width_type extra_bits = count_extra_bits(); - const block_type all_ones = detail::dynamic_bitset_impl::max_limit::value; + const block_type all_ones = detail::dynamic_bitset_impl::max_limit< Block >::value; - if (extra_bits == 0) { - for (size_type i = 0, e = num_blocks(); i < e; ++i) { - if (m_bits[i] != all_ones) { + if ( extra_bits == 0 ) { + for ( size_type i = 0, e = num_blocks(); i < e; ++i ) { + if ( m_bits[ i ] != all_ones ) { return false; } } } else { - for (size_type i = 0, e = num_blocks() - 1; i < e; ++i) { - if (m_bits[i] != all_ones) { + for ( size_type i = 0, e = num_blocks() - 1; i < e; ++i ) { + if ( m_bits[ i ] != all_ones ) { return false; } } - const block_type mask = (block_type(1) << extra_bits) - 1; - if (m_highest_block() != mask) { + const block_type mask = ( block_type( 1 ) << extra_bits ) - 1; + if ( m_highest_block() != mask ) { return false; } } return true; } -template -bool dynamic_bitset::any() const +template< typename Block, typename Allocator > +bool +dynamic_bitset< Block, Allocator >::any() const { - for (size_type i = 0; i < num_blocks(); ++i) - if (m_bits[i]) + for ( size_type i = 0; i < num_blocks(); ++i ) + if ( m_bits[ i ] ) return true; return false; } -template -inline bool dynamic_bitset::none() const +template< typename Block, typename Allocator > +inline bool +dynamic_bitset< Block, Allocator >::none() const { - return !any(); + return ! any(); } -template -dynamic_bitset -dynamic_bitset::operator~() const +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +dynamic_bitset< Block, Allocator >::operator~() const { - dynamic_bitset b(*this); + dynamic_bitset b( *this ); b.flip(); return b; } -template -typename dynamic_bitset::size_type -dynamic_bitset::count() const BOOST_NOEXCEPT +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::count() const BOOST_NOEXCEPT { size_type result = 0; - for (block_type block : m_bits) { - result += core::popcount(block); + for ( block_type block : m_bits ) { + result += core::popcount( block ); } return result; } @@ -728,104 +723,102 @@ dynamic_bitset::count() const BOOST_NOEXCEPT //----------------------------------------------------------------------------- // subscript -template -typename dynamic_bitset::reference -dynamic_bitset::operator[](size_type pos) +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::reference +dynamic_bitset< Block, Allocator >::operator[]( size_type pos ) { - return reference(m_bits[block_index(pos)], bit_index(pos)); + return reference( m_bits[ block_index( pos ) ], bit_index( pos ) ); } -template +template< typename Block, typename Allocator > bool -dynamic_bitset::operator[](size_type pos) const +dynamic_bitset< Block, Allocator >::operator[]( size_type pos ) const { - return test(pos); + return test( pos ); } //----------------------------------------------------------------------------- // conversions -template -unsigned long dynamic_bitset:: -to_ulong() const +template< typename Block, typename Allocator > +unsigned long +dynamic_bitset< Block, Allocator >:: + to_ulong() const { + if ( m_num_bits == 0 ) + return 0; // convention - if (m_num_bits == 0) - return 0; // convention + // Check for overflows. This may be a performance burden on very large + // bitsets but is required by the specification, sorry. + if ( find_first( ulong_width ) != npos ) + BOOST_THROW_EXCEPTION( std::overflow_error( "boost::dynamic_bitset::to_ulong overflow" ) ); - // Check for overflows. This may be a performance burden on very large - // bitsets but is required by the specification, sorry. - if (find_first(ulong_width) != npos) - BOOST_THROW_EXCEPTION(std::overflow_error("boost::dynamic_bitset::to_ulong overflow")); + // Ok, from now on we can be sure there's no "on" bit beyond the + // "allowed" positions. + typedef unsigned long result_type; + const size_type maximum_size = + (std::min)( m_num_bits, static_cast< size_type >( ulong_width ) ); - // Ok, from now on we can be sure there's no "on" bit beyond the - // "allowed" positions. - typedef unsigned long result_type; + const size_type last_block = block_index( maximum_size - 1 ); - const size_type maximum_size = - (std::min)(m_num_bits, static_cast(ulong_width)); + BOOST_ASSERT( ( last_block * bits_per_block ) < static_cast< size_type >( ulong_width ) ); - const size_type last_block = block_index( maximum_size - 1 ); + result_type result = 0; + for ( size_type i = 0; i <= last_block; ++i ) { + const size_type offset = i * bits_per_block; + result |= ( static_cast< result_type >( m_bits[ i ] ) << offset ); + } - BOOST_ASSERT((last_block * bits_per_block) < static_cast(ulong_width)); - - result_type result = 0; - for (size_type i = 0; i <= last_block; ++i) { - const size_type offset = i * bits_per_block; - result |= (static_cast(m_bits[i]) << offset); - } - - return result; + return result; } // A comment similar to the one about the constructor from basic_string // can be done here. Thanks to James Kanze for making me (Gennaro) // realize this important separation of concerns issue, as well as many // things about i18n. -template +template< typename Block, typename Allocator, typename stringT > inline void -to_string(const dynamic_bitset& b, stringT& s) +to_string( const dynamic_bitset< Block, Allocator > & b, stringT & s ) { - to_string_helper(b, s, false); + to_string_helper( b, s, false ); } // Differently from to_string this function dumps out every bit of the // internal representation (may be useful for debugging purposes) -template +template< typename B, typename A, typename stringT > inline void -dump_to_string(const dynamic_bitset& b, stringT& s) +dump_to_string( const dynamic_bitset< B, A > & b, stringT & s ) { - to_string_helper(b, s, true /* =dump_all*/); + to_string_helper( b, s, true /* =dump_all*/ ); } -template +template< typename Block, typename Allocator, typename BlockOutputIterator > inline void -to_block_range(const dynamic_bitset& b, - BlockOutputIterator result) +to_block_range( const dynamic_bitset< Block, Allocator > & b, BlockOutputIterator result ) { // Note how this copies *all* bits, including the unused ones in the // last block (which are zero). - std::copy(b.m_bits.begin(), b.m_bits.end(), result); + std::copy( b.m_bits.begin(), b.m_bits.end(), result ); } -template -inline typename dynamic_bitset::size_type -dynamic_bitset::size() const BOOST_NOEXCEPT +template< typename Block, typename Allocator > +inline typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::size() const BOOST_NOEXCEPT { return m_num_bits; } -template -inline typename dynamic_bitset::size_type -dynamic_bitset::num_blocks() const BOOST_NOEXCEPT +template< typename Block, typename Allocator > +inline typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::num_blocks() const BOOST_NOEXCEPT { return m_bits.size(); } -template -inline typename dynamic_bitset::size_type -dynamic_bitset::max_size() const BOOST_NOEXCEPT +template< typename Block, typename Allocator > +inline typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::max_size() const BOOST_NOEXCEPT { // The semantics of vector<>::max_size() aren't very clear (see lib // issue 197) and many library implementations simply return dummy @@ -836,78 +829,83 @@ dynamic_bitset::max_size() const BOOST_NOEXCEPT // allocator. const size_type m = detail::dynamic_bitset_impl:: - vector_max_size_workaround(m_bits); + vector_max_size_workaround( m_bits ); - return m <= (size_type(-1)/bits_per_block) ? - m * bits_per_block : - size_type(-1); + return m <= ( size_type( -1 ) / bits_per_block ) ? m * bits_per_block : size_type( -1 ); } -template -inline bool dynamic_bitset::empty() const BOOST_NOEXCEPT +template< typename Block, typename Allocator > +inline bool +dynamic_bitset< Block, Allocator >::empty() const BOOST_NOEXCEPT { - return size() == 0; + return size() == 0; } -template -inline typename dynamic_bitset::size_type -dynamic_bitset::capacity() const BOOST_NOEXCEPT +template< typename Block, typename Allocator > +inline typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::capacity() const BOOST_NOEXCEPT { return m_bits.capacity() * bits_per_block; } -template -inline void dynamic_bitset::reserve(size_type num_bits) +template< typename Block, typename Allocator > +inline void +dynamic_bitset< Block, Allocator >::reserve( size_type num_bits ) { - m_bits.reserve(calc_num_blocks(num_bits)); + m_bits.reserve( calc_num_blocks( num_bits ) ); } -template -void dynamic_bitset::shrink_to_fit() +template< typename Block, typename Allocator > +void +dynamic_bitset< Block, Allocator >::shrink_to_fit() { - if (m_bits.size() < m_bits.capacity()) { - buffer_type(m_bits).swap(m_bits); + if ( m_bits.size() < m_bits.capacity() ) { + buffer_type( m_bits ).swap( m_bits ); } } -template -bool dynamic_bitset:: -is_subset_of(const dynamic_bitset& a) const +template< typename Block, typename Allocator > +bool +dynamic_bitset< Block, Allocator >:: + is_subset_of( const dynamic_bitset< Block, Allocator > & a ) const { - BOOST_ASSERT(size() == a.size()); - for (size_type i = 0; i < num_blocks(); ++i) - if (m_bits[i] & ~a.m_bits[i]) + BOOST_ASSERT( size() == a.size() ); + for ( size_type i = 0; i < num_blocks(); ++i ) + if ( m_bits[ i ] & ~a.m_bits[ i ] ) return false; return true; } -template -bool dynamic_bitset:: -is_proper_subset_of(const dynamic_bitset& a) const +template< typename Block, typename Allocator > +bool +dynamic_bitset< Block, Allocator >:: + is_proper_subset_of( const dynamic_bitset< Block, Allocator > & a ) const { - BOOST_ASSERT(size() == a.size()); + BOOST_ASSERT( size() == a.size() ); bool proper = false; - for (size_type i = 0; i < num_blocks(); ++i) { - const Block & bt = m_bits[i]; - const Block & ba = a.m_bits[i]; + for ( size_type i = 0; i < num_blocks(); ++i ) { + const Block & bt = m_bits[ i ]; + const Block & ba = a.m_bits[ i ]; - if (bt & ~ba) + if ( bt & ~ba ) return false; // not a subset at all - if (ba & ~bt) + if ( ba & ~bt ) proper = true; } return proper; } -template -bool dynamic_bitset::intersects(const dynamic_bitset & b) const +template< typename Block, typename Allocator > +bool +dynamic_bitset< Block, Allocator >::intersects( const dynamic_bitset & b ) const { size_type common_blocks = num_blocks() < b.num_blocks() - ? num_blocks() : b.num_blocks(); + ? num_blocks() + : b.num_blocks(); - for(size_type i = 0; i < common_blocks; ++i) { - if(m_bits[i] & b.m_bits[i]) + for ( size_type i = 0; i < common_blocks; ++i ) { + if ( m_bits[ i ] & b.m_bits[ i ] ) return true; } return false; @@ -918,474 +916,458 @@ bool dynamic_bitset::intersects(const dynamic_bitset & b) cons // Look for the first bit "on", starting from the block with index // first_block. -template -typename dynamic_bitset::size_type -dynamic_bitset::m_do_find_from(size_type first_block) const +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::m_do_find_from( size_type first_block ) const { + size_type i = std::distance( m_bits.begin(), std::find_if( m_bits.begin() + first_block, m_bits.end(), m_not_empty ) ); - size_type i = std::distance(m_bits.begin(), - std::find_if(m_bits.begin() + first_block, m_bits.end(), m_not_empty) ); - - if (i >= num_blocks()) + if ( i >= num_blocks() ) return npos; // not found - return i * bits_per_block + static_cast(detail::lowest_bit(m_bits[i])); + return i * bits_per_block + static_cast< size_type >( detail::lowest_bit( m_bits[ i ] ) ); } - -template -typename dynamic_bitset::size_type -dynamic_bitset::find_first() const +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::find_first() const { - return m_do_find_from(0); + return m_do_find_from( 0 ); } -template -typename dynamic_bitset::size_type -dynamic_bitset::find_first(size_type pos) const +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::find_first( size_type pos ) const { const size_type sz = size(); - if (pos >= sz) return npos; + if ( pos >= sz ) + return npos; - const size_type blk = block_index(pos); - const block_width_type ind = bit_index(pos); + const size_type blk = block_index( pos ); + const block_width_type ind = bit_index( pos ); // shift bits upto one immediately after current - const Block fore = m_bits[blk] >> ind; + const Block fore = m_bits[ blk ] >> ind; - return fore? - pos + static_cast(detail::lowest_bit(fore)) - : - m_do_find_from(blk + 1); + return fore ? pos + static_cast< size_type >( detail::lowest_bit( fore ) ) + : m_do_find_from( blk + 1 ); } - -template -typename dynamic_bitset::size_type -dynamic_bitset::find_next(size_type pos) const +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::find_next( size_type pos ) const { - if (pos == npos) return npos; - return find_first(pos + 1); + if ( pos == npos ) + return npos; + return find_first( pos + 1 ); } //----------------------------------------------------------------------------- // comparison -template -bool operator==(const dynamic_bitset& a, - const dynamic_bitset& b) +template< typename Block, typename Allocator > +bool +operator==( const dynamic_bitset< Block, Allocator > & a, const dynamic_bitset< Block, Allocator > & b ) { - return (a.m_num_bits == b.m_num_bits) - && (a.m_bits == b.m_bits); + return ( a.m_num_bits == b.m_num_bits ) + && ( a.m_bits == b.m_bits ); } -template -inline bool operator!=(const dynamic_bitset& a, - const dynamic_bitset& b) +template< typename Block, typename Allocator > +inline bool +operator!=( const dynamic_bitset< Block, Allocator > & a, const dynamic_bitset< Block, Allocator > & b ) { - return !(a == b); + return ! ( a == b ); } -template -bool operator<(const dynamic_bitset& a, - const dynamic_bitset& b) +template< typename Block, typename Allocator > +bool +operator<( const dynamic_bitset< Block, Allocator > & a, const dynamic_bitset< Block, Allocator > & b ) { - typedef BOOST_DEDUCED_TYPENAME dynamic_bitset::size_type size_type; + typedef BOOST_DEDUCED_TYPENAME dynamic_bitset< Block, Allocator >::size_type size_type; - size_type asize(a.size()); - size_type bsize(b.size()); + size_type asize( a.size() ); + size_type bsize( b.size() ); - if (!bsize) - { + if ( ! bsize ) { return false; - } - else if (!asize) - { + } else if ( ! asize ) { return true; - } - else if (asize == bsize) - { - for (size_type ii = a.num_blocks(); ii > 0; --ii) - { - size_type i = ii-1; - if (a.m_bits[i] < b.m_bits[i]) + } else if ( asize == bsize ) { + for ( size_type ii = a.num_blocks(); ii > 0; --ii ) { + size_type i = ii - 1; + if ( a.m_bits[ i ] < b.m_bits[ i ] ) return true; - else if (a.m_bits[i] > b.m_bits[i]) + else if ( a.m_bits[ i ] > b.m_bits[ i ] ) return false; - } + } return false; - } - else - { - size_type leqsize(std::min BOOST_PREVENT_MACRO_SUBSTITUTION(asize,bsize)); + } else { + size_type leqsize( std::min BOOST_PREVENT_MACRO_SUBSTITUTION( asize, bsize ) ); - for (size_type ii = 0; ii < leqsize; ++ii,--asize,--bsize) - { - size_type i = asize-1; - size_type j = bsize-1; - if (a[i] < b[j]) + for ( size_type ii = 0; ii < leqsize; ++ii, --asize, --bsize ) { + size_type i = asize - 1; + size_type j = bsize - 1; + if ( a[ i ] < b[ j ] ) return true; - else if (a[i] > b[j]) + else if ( a[ i ] > b[ j ] ) return false; - } - return (a.size() < b.size()); } + return ( a.size() < b.size() ); + } } -template -inline bool operator<=(const dynamic_bitset& a, - const dynamic_bitset& b) +template< typename Block, typename Allocator > +inline bool +operator<=( const dynamic_bitset< Block, Allocator > & a, const dynamic_bitset< Block, Allocator > & b ) { - return !(a > b); + return ! ( a > b ); } -template -inline bool operator>(const dynamic_bitset& a, - const dynamic_bitset& b) +template< typename Block, typename Allocator > +inline bool +operator>( const dynamic_bitset< Block, Allocator > & a, const dynamic_bitset< Block, Allocator > & b ) { return b < a; } -template -inline bool operator>=(const dynamic_bitset& a, - const dynamic_bitset& b) +template< typename Block, typename Allocator > +inline bool +operator>=( const dynamic_bitset< Block, Allocator > & a, const dynamic_bitset< Block, Allocator > & b ) { - return !(a < b); + return ! ( a < b ); } -template -void to_string_helper(const dynamic_bitset & b, stringT & s, - bool dump_all) +template< typename B, typename A, typename stringT > +void +to_string_helper( const dynamic_bitset< B, A > & b, stringT & s, bool dump_all ) { typedef typename stringT::traits_type Tr; typedef typename stringT::value_type Ch; - BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, std::locale()); - const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0'); - const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1'); + BOOST_DYNAMIC_BITSET_CTYPE_FACET( Ch, fac, std::locale() ); + const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR( fac, '0' ); + const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR( fac, '1' ); // Note that this function may access (when // dump_all == true) bits beyond position size() - 1 - typedef typename dynamic_bitset::size_type size_type; + typedef typename dynamic_bitset< B, A >::size_type size_type; - const size_type len = dump_all? - dynamic_bitset::bits_per_block * b.num_blocks(): - b.size(); - s.assign (len, zero); - - for (size_type i = 0; i < len; ++i) { - if (b.m_unchecked_test(i)) - Tr::assign(s[len - 1 - i], one); + const size_type len = dump_all ? dynamic_bitset< B, A >::bits_per_block * b.num_blocks() : b.size(); + s.assign( len, zero ); + for ( size_type i = 0; i < len; ++i ) { + if ( b.m_unchecked_test( i ) ) + Tr::assign( s[ len - 1 - i ], one ); } } //----------------------------------------------------------------------------- // hash operations -template -inline std::size_t hash_value(const dynamic_bitset& a) +template< typename Block, typename Allocator > +inline std::size_t +hash_value( const dynamic_bitset< Block, Allocator > & a ) { - std::size_t res = hash_value(a.m_num_bits); - boost::hash_combine(res, a.m_bits); + std::size_t res = hash_value( a.m_num_bits ); + boost::hash_combine( res, a.m_bits ); return res; } //----------------------------------------------------------------------------- // stream operations -template -std::basic_ostream& -operator<<(std::basic_ostream& os, - const dynamic_bitset& b) +template< typename Ch, typename Tr, typename Block, typename Alloc > +std::basic_ostream< Ch, Tr > & +operator<<( std::basic_ostream< Ch, Tr > & os, const dynamic_bitset< Block, Alloc > & b ) { - using namespace std; - const ios_base::iostate ok = ios_base::goodbit; - ios_base::iostate err = ok; + const ios_base::iostate ok = ios_base::goodbit; + ios_base::iostate err = ok; - typename basic_ostream::sentry cerberos(os); - if (cerberos) { + typename basic_ostream< Ch, Tr >::sentry cerberos( os ); + if ( cerberos ) { + BOOST_DYNAMIC_BITSET_CTYPE_FACET( Ch, fac, os.getloc() ); + const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR( fac, '0' ); + const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR( fac, '1' ); - BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, os.getloc()); - const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0'); - const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1'); + BOOST_TRY + { + typedef typename dynamic_bitset< Block, Alloc >::size_type bitset_size_type; + typedef basic_streambuf< Ch, Tr > buffer_type; - BOOST_TRY { - - typedef typename dynamic_bitset::size_type bitset_size_type; - typedef basic_streambuf buffer_type; - - buffer_type * buf = os.rdbuf(); + buffer_type * buf = os.rdbuf(); // careful: os.width() is signed (and can be < 0) - const bitset_size_type width = (os.width() <= 0) ? 0 : static_cast(os.width()); - streamsize npad = (width <= b.size()) ? 0 : width - b.size(); + const bitset_size_type width = ( os.width() <= 0 ) ? 0 : static_cast< bitset_size_type >( os.width() ); + streamsize npad = ( width <= b.size() ) ? 0 : width - b.size(); - const Ch fill_char = os.fill(); - const ios_base::fmtflags adjustfield = os.flags() & ios_base::adjustfield; + const Ch fill_char = os.fill(); + const ios_base::fmtflags adjustfield = os.flags() & ios_base::adjustfield; // if needed fill at left; pad is decreased along the way - if (adjustfield != ios_base::left) { - for (; 0 < npad; --npad) - if (Tr::eq_int_type(Tr::eof(), buf->sputc(fill_char))) { - err |= ios_base::failbit; - break; + if ( adjustfield != ios_base::left ) { + for ( ; 0 < npad; --npad ) + if ( Tr::eq_int_type( Tr::eof(), buf->sputc( fill_char ) ) ) { + err |= ios_base::failbit; + break; } } - if (err == ok) { + if ( err == ok ) { // output the bitset - for (bitset_size_type i = b.size(); 0 < i; --i) { + for ( bitset_size_type i = b.size(); 0 < i; --i ) { typename buffer_type::int_type - ret = buf->sputc(b.test(i-1)? one : zero); - if (Tr::eq_int_type(Tr::eof(), ret)) { + ret = buf->sputc( b.test( i - 1 ) ? one : zero ); + if ( Tr::eq_int_type( Tr::eof(), ret ) ) { err |= ios_base::failbit; break; } } } - if (err == ok) { + if ( err == ok ) { // if needed fill at right - for (; 0 < npad; --npad) { - if (Tr::eq_int_type(Tr::eof(), buf->sputc(fill_char))) { + for ( ; 0 < npad; --npad ) { + if ( Tr::eq_int_type( Tr::eof(), buf->sputc( fill_char ) ) ) { err |= ios_base::failbit; break; } } } - - os.width(0); - - } BOOST_CATCH (...) { // see std 27.6.1.1/4 + os.width( 0 ); + } + BOOST_CATCH( ... ) + { // see std 27.6.1.1/4 bool rethrow = false; - BOOST_TRY { os.setstate(ios_base::failbit); } BOOST_CATCH (...) { rethrow = true; } BOOST_CATCH_END + BOOST_TRY + { + os.setstate( ios_base::failbit ); + } + BOOST_CATCH( ... ) + { + rethrow = true; + } + BOOST_CATCH_END - if (rethrow) + if ( rethrow ) BOOST_RETHROW; } BOOST_CATCH_END } - if(err != ok) - os.setstate(err); // may throw exception + if ( err != ok ) + os.setstate( err ); // may throw exception return os; - } -template -std::basic_istream& -operator>>(std::basic_istream& is, dynamic_bitset& b) +template< typename Ch, typename Tr, typename Block, typename Alloc > +std::basic_istream< Ch, Tr > & +operator>>( std::basic_istream< Ch, Tr > & is, dynamic_bitset< Block, Alloc > & b ) { - using namespace std; - typedef dynamic_bitset bitset_type; - typedef typename bitset_type::size_type size_type; + typedef dynamic_bitset< Block, Alloc > bitset_type; + typedef typename bitset_type::size_type size_type; - const streamsize w = is.width(); - const size_type limit = 0 < w && static_cast(w) < b.max_size()? - static_cast(w) : b.max_size(); - - ios_base::iostate err = ios_base::goodbit; - typename basic_istream::sentry cerberos(is); // skips whitespaces - if(cerberos) { + const streamsize w = is.width(); + const size_type limit = 0 < w && static_cast< size_type >( w ) < b.max_size() ? static_cast< size_type >( w ) : b.max_size(); + ios_base::iostate err = ios_base::goodbit; + typename basic_istream< Ch, Tr >::sentry cerberos( is ); // skips whitespaces + if ( cerberos ) { // in accordance with prop. resol. of lib DR 303 [last checked 4 Feb 2004] - BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, is.getloc()); - const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0'); - const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1'); + BOOST_DYNAMIC_BITSET_CTYPE_FACET( Ch, fac, is.getloc() ); + const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR( fac, '0' ); + const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR( fac, '1' ); b.clear(); - BOOST_TRY { - typename bitset_type::bit_appender appender(b); - basic_streambuf * buf = is.rdbuf(); - typename Tr::int_type c = buf->sgetc(); - for( ; appender.get_count() < limit; c = buf->snextc() ) { - - if (Tr::eq_int_type(Tr::eof(), c)) { + BOOST_TRY + { + typename bitset_type::bit_appender appender( b ); + basic_streambuf< Ch, Tr > * buf = is.rdbuf(); + typename Tr::int_type c = buf->sgetc(); + for ( ; appender.get_count() < limit; c = buf->snextc() ) { + if ( Tr::eq_int_type( Tr::eof(), c ) ) { err |= ios_base::eofbit; break; - } - else { - const Ch to_c = Tr::to_char_type(c); - const bool is_one = Tr::eq(to_c, one); + } else { + const Ch to_c = Tr::to_char_type( c ); + const bool is_one = Tr::eq( to_c, one ); - if (!is_one && !Tr::eq(to_c, zero)) + if ( ! is_one && ! Tr::eq( to_c, zero ) ) break; // non digit character - appender.do_append(is_one); - + appender.do_append( is_one ); } } // for } - BOOST_CATCH (...) { + BOOST_CATCH( ... ) + { // catches from stream buf, or from vector: // // bits_stored bits have been extracted and stored, and // either no further character is extractable or we can't // append to the underlying vector (out of memory) - bool rethrow = false; // see std 27.6.1.1/4 - BOOST_TRY { is.setstate(ios_base::badbit); } - BOOST_CATCH(...) { rethrow = true; } + bool rethrow = false; // see std 27.6.1.1/4 + BOOST_TRY + { + is.setstate( ios_base::badbit ); + } + BOOST_CATCH( ... ) + { + rethrow = true; + } BOOST_CATCH_END - if (rethrow) + if ( rethrow ) BOOST_RETHROW; - } BOOST_CATCH_END } - is.width(0); - if (b.size() == 0 /*|| !cerberos*/) + is.width( 0 ); + if ( b.size() == 0 /*|| !cerberos*/ ) err |= ios_base::failbit; - if (err != ios_base::goodbit) - is.setstate (err); // may throw + if ( err != ios_base::goodbit ) + is.setstate( err ); // may throw return is; - } //----------------------------------------------------------------------------- // bitset operations -template -dynamic_bitset -operator&(const dynamic_bitset& x, - const dynamic_bitset& y) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +operator&( const dynamic_bitset< Block, Allocator > & x, const dynamic_bitset< Block, Allocator > & y ) { - dynamic_bitset b(x); + dynamic_bitset< Block, Allocator > b( x ); return b &= y; } -template -dynamic_bitset -operator|(const dynamic_bitset& x, - const dynamic_bitset& y) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +operator|( const dynamic_bitset< Block, Allocator > & x, const dynamic_bitset< Block, Allocator > & y ) { - dynamic_bitset b(x); + dynamic_bitset< Block, Allocator > b( x ); return b |= y; } -template -dynamic_bitset -operator^(const dynamic_bitset& x, - const dynamic_bitset& y) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +operator^( const dynamic_bitset< Block, Allocator > & x, const dynamic_bitset< Block, Allocator > & y ) { - dynamic_bitset b(x); + dynamic_bitset< Block, Allocator > b( x ); return b ^= y; } -template -dynamic_bitset -operator-(const dynamic_bitset& x, - const dynamic_bitset& y) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > +operator-( const dynamic_bitset< Block, Allocator > & x, const dynamic_bitset< Block, Allocator > & y ) { - dynamic_bitset b(x); + dynamic_bitset< Block, Allocator > b( x ); return b -= y; } //----------------------------------------------------------------------------- // namespace scope swap -template +template< typename Block, typename Allocator > inline void -swap(dynamic_bitset& left, - dynamic_bitset& right) BOOST_NOEXCEPT +swap( dynamic_bitset< Block, Allocator > & left, dynamic_bitset< Block, Allocator > & right ) BOOST_NOEXCEPT { - left.swap(right); + left.swap( right ); } -template -bool dynamic_bitset::m_unchecked_test(size_type pos) const +template< typename Block, typename Allocator > +bool +dynamic_bitset< Block, Allocator >::m_unchecked_test( size_type pos ) const { - return (m_bits[block_index(pos)] & bit_mask(pos)) != 0; + return ( m_bits[ block_index( pos ) ] & bit_mask( pos ) ) != 0; } -template -inline typename dynamic_bitset::size_type -dynamic_bitset::calc_num_blocks(size_type num_bits) +template< typename Block, typename Allocator > +inline typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::calc_num_blocks( size_type num_bits ) { return num_bits / bits_per_block - + static_cast( num_bits % bits_per_block != 0 ); + + static_cast< size_type >( num_bits % bits_per_block != 0 ); } // gives a reference to the highest block // -template -inline Block& dynamic_bitset::m_highest_block() +template< typename Block, typename Allocator > +inline Block & +dynamic_bitset< Block, Allocator >::m_highest_block() { - return const_cast - (static_cast(this)->m_highest_block()); + return const_cast< Block & >( static_cast< const dynamic_bitset * >( this )->m_highest_block() ); } // gives a const-reference to the highest block // -template -inline const Block& dynamic_bitset::m_highest_block() const +template< typename Block, typename Allocator > +inline const Block & +dynamic_bitset< Block, Allocator >::m_highest_block() const { - BOOST_ASSERT(size() > 0 && num_blocks() > 0); + BOOST_ASSERT( size() > 0 && num_blocks() > 0 ); return m_bits.back(); } -template -dynamic_bitset& dynamic_bitset::range_operation( - size_type pos, size_type len, - Block (*partial_block_operation)(Block, size_type, size_type), - Block (*full_block_operation)(Block)) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator > & +dynamic_bitset< Block, Allocator >::range_operation( + size_type pos, size_type len, Block ( *partial_block_operation )( Block, size_type, size_type ), Block ( *full_block_operation )( Block ) ) { - BOOST_ASSERT(pos + len <= m_num_bits); + BOOST_ASSERT( pos + len <= m_num_bits ); // Do nothing in case of zero length - if (!len) + if ( ! len ) return *this; // Use an additional asserts in order to detect size_type overflow // For example: pos = 10, len = size_type_limit - 2, pos + len = 7 // In case of overflow, 'pos + len' is always smaller than 'len' - BOOST_ASSERT(pos + len >= len); + BOOST_ASSERT( pos + len >= len ); // Start and end blocks of the [pos; pos + len - 1] sequence - const size_type first_block = block_index(pos); - const size_type last_block = block_index(pos + len - 1); + const size_type first_block = block_index( pos ); + const size_type last_block = block_index( pos + len - 1 ); - const size_type first_bit_index = bit_index(pos); - const size_type last_bit_index = bit_index(pos + len - 1); + const size_type first_bit_index = bit_index( pos ); + const size_type last_bit_index = bit_index( pos + len - 1 ); - if (first_block == last_block) { + if ( first_block == last_block ) { // Filling only a sub-block of a block - m_bits[first_block] = partial_block_operation(m_bits[first_block], - first_bit_index, last_bit_index); + m_bits[ first_block ] = partial_block_operation( m_bits[ first_block ], first_bit_index, last_bit_index ); } else { // Check if the corner blocks won't be fully filled with 'val' - const size_type first_block_shift = bit_index(pos) ? 1 : 0; - const size_type last_block_shift = (bit_index(pos + len - 1) - == bits_per_block - 1) ? 0 : 1; + const size_type first_block_shift = bit_index( pos ) ? 1 : 0; + const size_type last_block_shift = ( bit_index( pos + len - 1 ) + == bits_per_block - 1 ) + ? 0 + : 1; // Blocks that will be filled with ~0 or 0 at once - const size_type first_full_block = first_block + first_block_shift; - const size_type last_full_block = last_block - last_block_shift; + const size_type first_full_block = first_block + first_block_shift; + const size_type last_full_block = last_block - last_block_shift; - for (size_type i = first_full_block; i <= last_full_block; ++i) { - m_bits[i] = full_block_operation(m_bits[i]); + for ( size_type i = first_full_block; i <= last_full_block; ++i ) { + m_bits[ i ] = full_block_operation( m_bits[ i ] ); } // Fill the first block from the 'first' bit index to the end - if (first_block_shift) { - m_bits[first_block] = partial_block_operation(m_bits[first_block], - first_bit_index, bits_per_block - 1); + if ( first_block_shift ) { + m_bits[ first_block ] = partial_block_operation( m_bits[ first_block ], first_bit_index, bits_per_block - 1 ); } // Fill the last block from the start to the 'last' bit index - if (last_block_shift) { - m_bits[last_block] = partial_block_operation(m_bits[last_block], - 0, last_bit_index); + if ( last_block_shift ) { + m_bits[ last_block ] = partial_block_operation( m_bits[ last_block ], 0, last_bit_index ); } } @@ -1395,331 +1377,320 @@ dynamic_bitset& dynamic_bitset::range_operat // If size() is not a multiple of bits_per_block then not all the bits // in the last block are used. This function resets the unused bits // (convenient for the implementation of many member functions). -template -inline void dynamic_bitset::m_zero_unused_bits() +template< typename Block, typename Allocator > +inline void +dynamic_bitset< Block, Allocator >::m_zero_unused_bits() { - BOOST_ASSERT (num_blocks() == calc_num_blocks(m_num_bits)); + BOOST_ASSERT( num_blocks() == calc_num_blocks( m_num_bits ) ); // if != 0, this is the number of bits used in the last block. const block_width_type extra_bits = count_extra_bits(); - if (extra_bits != 0) - m_highest_block() &= (Block(1) << extra_bits) - 1; + if ( extra_bits != 0 ) + m_highest_block() &= ( Block( 1 ) << extra_bits ) - 1; } // check class invariants -template -bool dynamic_bitset::m_check_invariants() const +template< typename Block, typename Allocator > +bool +dynamic_bitset< Block, Allocator >::m_check_invariants() const { const block_width_type extra_bits = count_extra_bits(); - if (extra_bits > 0) { - const block_type mask = detail::dynamic_bitset_impl::max_limit::value << extra_bits; - if ((m_highest_block() & mask) != 0) + if ( extra_bits > 0 ) { + const block_type mask = detail::dynamic_bitset_impl::max_limit< Block >::value << extra_bits; + if ( ( m_highest_block() & mask ) != 0 ) return false; } - if (m_bits.size() > m_bits.capacity() || num_blocks() != calc_num_blocks(size())) + if ( m_bits.size() > m_bits.capacity() || num_blocks() != calc_num_blocks( size() ) ) return false; return true; - } -template +template< typename Block, typename Allocator > bool -dynamic_bitset::m_not_empty(Block x) +dynamic_bitset< Block, Allocator >::m_not_empty( Block x ) { - return x != Block(0); + return x != Block( 0 ); } - -template -typename dynamic_bitset::block_width_type -dynamic_bitset::count_extra_bits() const BOOST_NOEXCEPT +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::block_width_type +dynamic_bitset< Block, Allocator >::count_extra_bits() const BOOST_NOEXCEPT { - return bit_index(size()); + return bit_index( size() ); } -template -typename dynamic_bitset::size_type -dynamic_bitset::block_index(size_type pos) BOOST_NOEXCEPT +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::block_index( size_type pos ) BOOST_NOEXCEPT { return pos / bits_per_block; } -template -typename dynamic_bitset::block_width_type -dynamic_bitset::bit_index(size_type pos) BOOST_NOEXCEPT +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::block_width_type +dynamic_bitset< Block, Allocator >::bit_index( size_type pos ) BOOST_NOEXCEPT { - return static_cast(pos % bits_per_block); + return static_cast< block_width_type >( pos % bits_per_block ); } -template +template< typename Block, typename Allocator > Block -dynamic_bitset::bit_mask(size_type pos) BOOST_NOEXCEPT +dynamic_bitset< Block, Allocator >::bit_mask( size_type pos ) BOOST_NOEXCEPT { - return Block(1) << bit_index(pos); + return Block( 1 ) << bit_index( pos ); } -template +template< typename Block, typename Allocator > Block -dynamic_bitset::bit_mask(size_type first, size_type last) BOOST_NOEXCEPT +dynamic_bitset< Block, Allocator >::bit_mask( size_type first, size_type last ) BOOST_NOEXCEPT { - Block res = (last == bits_per_block - 1) - ? detail::dynamic_bitset_impl::max_limit::value - : ((Block(1) << (last + 1)) - 1); - res ^= (Block(1) << first) - 1; + Block res = ( last == bits_per_block - 1 ) + ? detail::dynamic_bitset_impl::max_limit< Block >::value + : ( ( Block( 1 ) << ( last + 1 ) ) - 1 ); + res ^= ( Block( 1 ) << first ) - 1; return res; } -template +template< typename Block, typename Allocator > Block -dynamic_bitset::set_block_bits( - Block block, +dynamic_bitset< Block, Allocator >::set_block_bits( + Block block, size_type first, size_type last, - bool val) BOOST_NOEXCEPT + bool val ) BOOST_NOEXCEPT { - if (val) - return block | bit_mask(first, last); + if ( val ) + return block | bit_mask( first, last ); else - return block & static_cast(~bit_mask(first, last)); + return block & static_cast< Block >( ~bit_mask( first, last ) ); } - // Functions for operations on ranges -template +// Functions for operations on ranges +template< typename Block, typename Allocator > Block -dynamic_bitset::set_block_partial( - Block block, - size_type first, - size_type last) BOOST_NOEXCEPT +dynamic_bitset< Block, Allocator >::set_block_partial( + Block block, + size_type first, + size_type last ) BOOST_NOEXCEPT { - return set_block_bits(block, first, last, true); + return set_block_bits( block, first, last, true ); } -template +template< typename Block, typename Allocator > Block -dynamic_bitset::set_block_full(Block) BOOST_NOEXCEPT +dynamic_bitset< Block, Allocator >::set_block_full( Block ) BOOST_NOEXCEPT { - return detail::dynamic_bitset_impl::max_limit::value; + return detail::dynamic_bitset_impl::max_limit< Block >::value; } - -template +template< typename Block, typename Allocator > Block -dynamic_bitset::reset_block_partial( - Block block, - size_type first, - size_type last) BOOST_NOEXCEPT +dynamic_bitset< Block, Allocator >::reset_block_partial( + Block block, + size_type first, + size_type last ) BOOST_NOEXCEPT { - return set_block_bits(block, first, last, false); + return set_block_bits( block, first, last, false ); } -template +template< typename Block, typename Allocator > Block -dynamic_bitset::reset_block_full(Block) BOOST_NOEXCEPT +dynamic_bitset< Block, Allocator >::reset_block_full( Block ) BOOST_NOEXCEPT { return 0; } - -template +template< typename Block, typename Allocator > Block -dynamic_bitset::flip_block_partial( - Block block, +dynamic_bitset< Block, Allocator >::flip_block_partial( + Block block, size_type first, - size_type last) BOOST_NOEXCEPT + size_type last ) BOOST_NOEXCEPT { - return block ^ bit_mask(first, last); + return block ^ bit_mask( first, last ); } -template +template< typename Block, typename Allocator > Block -dynamic_bitset::flip_block_full(Block block) BOOST_NOEXCEPT +dynamic_bitset< Block, Allocator >::flip_block_full( Block block ) BOOST_NOEXCEPT { return ~block; } - -template -template +template< typename Block, typename Allocator > +template< typename T > void -dynamic_bitset::dispatch_init( - T num_bits, +dynamic_bitset< Block, Allocator >::dispatch_init( + T num_bits, unsigned long value, - detail::dynamic_bitset_impl::value_to_type) + detail::dynamic_bitset_impl::value_to_type< true > ) { - init_from_unsigned_long(static_cast(num_bits), value); + init_from_unsigned_long( static_cast< size_type >( num_bits ), value ); } - -template -template +template< typename Block, typename Allocator > +template< typename T > void -dynamic_bitset::dispatch_init( +dynamic_bitset< Block, Allocator >::dispatch_init( T first, T last, - detail::dynamic_bitset_impl::value_to_type) + detail::dynamic_bitset_impl::value_to_type< false > ) { - init_from_block_range(first, last); + init_from_block_range( first, last ); } -template -template +template< typename Block, typename Allocator > +template< typename BlockIter > void -dynamic_bitset::init_from_block_range(BlockIter first, BlockIter last) +dynamic_bitset< Block, Allocator >::init_from_block_range( BlockIter first, BlockIter last ) { - BOOST_ASSERT(m_bits.size() == 0); - m_bits.insert(m_bits.end(), first, last); + BOOST_ASSERT( m_bits.size() == 0 ); + m_bits.insert( m_bits.end(), first, last ); m_num_bits = m_bits.size() * bits_per_block; } -template< typename Block, typename Allocator> -template +template< typename Block, typename Allocator > +template< typename CharT, typename Traits, typename Alloc > void -dynamic_bitset::init_from_string( - const std::basic_string& s, - typename std::basic_string::size_type pos, - typename std::basic_string::size_type n, - size_type num_bits) +dynamic_bitset< Block, Allocator >::init_from_string( + const std::basic_string< CharT, Traits, Alloc > & s, + typename std::basic_string< CharT, Traits, Alloc >::size_type pos, + typename std::basic_string< CharT, Traits, Alloc >::size_type n, + size_type num_bits ) { - BOOST_ASSERT(pos <= s.size()); + BOOST_ASSERT( pos <= s.size() ); - typedef typename std::basic_string StrT; - typedef typename StrT::traits_type Tr; + typedef typename std::basic_string< CharT, Traits, Alloc > StrT; + typedef typename StrT::traits_type Tr; - const typename StrT::size_type rlen = (std::min)(n, s.size() - pos); - const size_type sz = ( num_bits != npos? num_bits : rlen); - m_bits.resize(calc_num_blocks(sz)); + const typename StrT::size_type rlen = (std::min)( n, s.size() - pos ); + const size_type sz = ( num_bits != npos ? num_bits : rlen ); + m_bits.resize( calc_num_blocks( sz ) ); m_num_bits = sz; + BOOST_DYNAMIC_BITSET_CTYPE_FACET( CharT, fac, std::locale() ); + const CharT one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR( fac, '1' ); - BOOST_DYNAMIC_BITSET_CTYPE_FACET(CharT, fac, std::locale()); - const CharT one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1'); + const size_type m = num_bits < rlen ? num_bits : rlen; + typename StrT::size_type i = 0; + for ( ; i < m; ++i ) { + const CharT c = s[ ( pos + m - 1 ) - i ]; - const size_type m = num_bits < rlen ? num_bits : rlen; - typename StrT::size_type i = 0; - for( ; i < m; ++i) { + BOOST_ASSERT( Tr::eq( c, one ) || Tr::eq( c, BOOST_DYNAMIC_BITSET_WIDEN_CHAR( fac, '0' ) ) ); - const CharT c = s[(pos + m - 1) - i]; - - BOOST_ASSERT( Tr::eq(c, one) - || Tr::eq(c, BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0')) ); - - if (Tr::eq(c, one)) - set(i); + if ( Tr::eq( c, one ) ) + set( i ); } } -template +template< typename Block, typename Allocator > void -dynamic_bitset::init_from_unsigned_long( - size_type num_bits, - unsigned long value/*, - const Allocator& alloc*/) +dynamic_bitset< Block, Allocator >::init_from_unsigned_long( + size_type num_bits, + unsigned long value /*, + const Allocator& alloc*/ +) { + BOOST_ASSERT( m_bits.size() == 0 ); - BOOST_ASSERT(m_bits.size() == 0); - - m_bits.resize(calc_num_blocks(num_bits)); + m_bits.resize( calc_num_blocks( num_bits ) ); m_num_bits = num_bits; - typedef unsigned long num_type; - typedef boost::detail::dynamic_bitset_impl - ::shifter shifter; + typedef unsigned long num_type; + typedef boost::detail::dynamic_bitset_impl ::shifter< num_type, bits_per_block, ulong_width > shifter; - //if (num_bits == 0) - // return; + // if (num_bits == 0) + // return; // zero out all bits at pos >= num_bits, if any; // note that: num_bits == 0 implies value == 0 - if (num_bits < static_cast(ulong_width)) { - const num_type mask = (num_type(1) << num_bits) - 1; + if ( num_bits < static_cast< size_type >( ulong_width ) ) { + const num_type mask = ( num_type( 1 ) << num_bits ) - 1; value &= mask; } typename buffer_type::iterator it = m_bits.begin(); - for( ; value; shifter::left_shift(value), ++it) { - *it = static_cast(value); + for ( ; value; shifter::left_shift( value ), ++it ) { + *it = static_cast< block_type >( value ); } - } -template -template +template< typename Block, typename Allocator > +template< typename BlockInputIterator > void -dynamic_bitset::m_append(BlockInputIterator first, BlockInputIterator last, std::input_iterator_tag) +dynamic_bitset< Block, Allocator >::m_append( BlockInputIterator first, BlockInputIterator last, std::input_iterator_tag ) { - std::vector v(first, last); - m_append(v.begin(), v.end(), std::random_access_iterator_tag()); + std::vector< Block, Allocator > v( first, last ); + m_append( v.begin(), v.end(), std::random_access_iterator_tag() ); } -template -template +template< typename Block, typename Allocator > +template< typename BlockInputIterator > void -dynamic_bitset::m_append(BlockInputIterator first, BlockInputIterator last, std::forward_iterator_tag) +dynamic_bitset< Block, Allocator >::m_append( BlockInputIterator first, BlockInputIterator last, std::forward_iterator_tag ) { - BOOST_ASSERT(first != last); + BOOST_ASSERT( first != last ); block_width_type r = count_extra_bits(); - std::size_t d = std::distance(first, last); - m_bits.reserve(num_blocks() + d); - if (r == 0) { - for( ; first != last; ++first) - m_bits.push_back(*first); // could use vector<>::insert() - } - else { - m_highest_block() |= (*first << r); + std::size_t d = std::distance( first, last ); + m_bits.reserve( num_blocks() + d ); + if ( r == 0 ) { + for ( ; first != last; ++first ) + m_bits.push_back( *first ); // could use vector<>::insert() + } else { + m_highest_block() |= ( *first << r ); do { - Block b = *first >> (bits_per_block - r); + Block b = *first >> ( bits_per_block - r ); ++first; - m_bits.push_back(b | (first==last? 0 : *first << r)); - } while (first != last); + m_bits.push_back( b | ( first == last ? 0 : *first << r ) ); + } while ( first != last ); } m_num_bits += bits_per_block * d; } // bit appender -template -dynamic_bitset::bit_appender::bit_appender(dynamic_bitset & r) - : bs(r), n(0), mask(0), current(0) +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator >::bit_appender::bit_appender( dynamic_bitset & r ) + : bs( r ), n( 0 ), mask( 0 ), current( 0 ) { } -template -dynamic_bitset::bit_appender::~bit_appender() +template< typename Block, typename Allocator > +dynamic_bitset< Block, Allocator >::bit_appender::~bit_appender() { // Reverse the order of the blocks, shift if needed, and then // resize. // - std::reverse(bs.m_bits.begin(), bs.m_bits.end()); - const block_width_type offs = bit_index(n); - if (offs) - bs >>= (bits_per_block - offs); - bs.resize(n); // doesn't enlarge, so can't throw - BOOST_ASSERT(bs.m_check_invariants()); + std::reverse( bs.m_bits.begin(), bs.m_bits.end() ); + const block_width_type offs = bit_index( n ); + if ( offs ) + bs >>= ( bits_per_block - offs ); + bs.resize( n ); // doesn't enlarge, so can't throw + BOOST_ASSERT( bs.m_check_invariants() ); } -template +template< typename Block, typename Allocator > void -dynamic_bitset< Block, Allocator>::bit_appender::do_append(bool value) +dynamic_bitset< Block, Allocator >::bit_appender::do_append( bool value ) { - - if (mask == 0) { - bs.append(Block(0)); + if ( mask == 0 ) { + bs.append( Block( 0 ) ); current = &bs.m_highest_block(); - mask = Block(1) << (bits_per_block - 1); + mask = Block( 1 ) << ( bits_per_block - 1 ); } - if (value) { + if ( value ) { *current |= mask; } mask /= 2; ++n; } -template -typename dynamic_bitset::size_type -dynamic_bitset::bit_appender::get_count() const +template< typename Block, typename Allocator > +typename dynamic_bitset< Block, Allocator >::size_type +dynamic_bitset< Block, Allocator >::bit_appender::get_count() const { return n; } @@ -1727,19 +1698,19 @@ dynamic_bitset::bit_appender::get_count() const } // namespace boost // std::hash support -#if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) && !defined(BOOST_DYNAMIC_BITSET_NO_STD_HASH) -#include -namespace std +#if ! defined( BOOST_NO_CXX11_HDR_FUNCTIONAL ) && ! defined( BOOST_DYNAMIC_BITSET_NO_STD_HASH ) +# include +namespace std { +template< typename Block, typename Allocator > +struct hash< boost::dynamic_bitset< Block, Allocator > > { -template -struct hash< boost::dynamic_bitset > -{ - typedef boost::dynamic_bitset argument_type; - typedef std::size_t result_type; - result_type operator()(const argument_type& a) const BOOST_NOEXCEPT + typedef boost::dynamic_bitset< Block, Allocator > argument_type; + typedef std::size_t result_type; + result_type + operator()( const argument_type & a ) const BOOST_NOEXCEPT { - boost::hash hasher; - return hasher(a); + boost::hash< argument_type > hasher; + return hasher( a ); } }; } diff --git a/include/boost/dynamic_bitset/serialization.hpp b/include/boost/dynamic_bitset/serialization.hpp index 4a39e06..0dd9ec6 100644 --- a/include/boost/dynamic_bitset/serialization.hpp +++ b/include/boost/dynamic_bitset/serialization.hpp @@ -16,31 +16,34 @@ namespace boost { - // implementation for optional zero-copy serialization support - template - class dynamic_bitset::serialize_impl - { - public: - template - static void serialize(Ar& ar, dynamic_bitset& bs, unsigned) { - ar & boost::make_nvp("m_num_bits", bs.m_num_bits) - & boost::make_nvp("m_bits", bs.m_bits); - } - }; +// implementation for optional zero-copy serialization support +template< typename Block, typename Allocator > +class dynamic_bitset< Block, Allocator >::serialize_impl +{ +public: + template< typename Ar > + static void + serialize( Ar & ar, dynamic_bitset< Block, Allocator > & bs, unsigned ) + { + ar & boost::make_nvp( "m_num_bits", bs.m_num_bits ) + & boost::make_nvp( "m_bits", bs.m_bits ); + } +}; } // ADL hook to Boost Serialization library namespace boost { - namespace serialization { +namespace serialization { - template - void serialize(Ar& ar, dynamic_bitset& bs, unsigned version) { - dynamic_bitset::serialize_impl::serialize(ar, bs, version); - } +template< typename Ar, typename Block, typename Allocator > +void +serialize( Ar & ar, dynamic_bitset< Block, Allocator > & bs, unsigned version ) +{ + dynamic_bitset< Block, Allocator >::serialize_impl::serialize( ar, bs, version ); +} - } // namespace serialization +} // namespace serialization } // namespace boost #endif // include guard - diff --git a/include/boost/dynamic_bitset_fwd.hpp b/include/boost/dynamic_bitset_fwd.hpp index 7bb6e89..d04b847 100644 --- a/include/boost/dynamic_bitset_fwd.hpp +++ b/include/boost/dynamic_bitset_fwd.hpp @@ -16,8 +16,7 @@ namespace boost { -template > +template< typename Block = unsigned long, typename Allocator = std::allocator< Block > > class dynamic_bitset; } diff --git a/test/bitset_test.hpp b/test/bitset_test.hpp index 613c0c6..037d5f8 100644 --- a/test/bitset_test.hpp +++ b/test/bitset_test.hpp @@ -19,1494 +19,1505 @@ #include "boost/dynamic_bitset/dynamic_bitset.hpp" #include "boost/filesystem.hpp" #include "boost/limits.hpp" - #include -#include // is sometimes macro-guarded :-( +#include // is sometimes macro-guarded :-( #include #include -#if !defined (BOOST_NO_STD_LOCALE) -# include +#if ! defined( BOOST_NO_STD_LOCALE ) +# include #endif -template -inline bool nth_bit(Block num, std::size_t n) +template< typename Block > +inline bool +nth_bit( Block num, std::size_t n ) { #ifndef NDEBUG - int block_width = std::numeric_limits::digits; - assert(n < (std::size_t) block_width); + int block_width = std::numeric_limits< Block >::digits; + assert( n < (std::size_t)block_width ); #endif - return (num >> n) & 1; + return ( num >> n ) & 1; } // A long, 'irregular', string useful for various tests -std::string get_long_string() +std::string +get_long_string() { - const char * const p = - // 6 5 4 3 2 1 - // 3210987654321098765432109876543210987654321098765432109876543210 - "1110011100011110000011110000011111110000000000000110101110000000" - "1010101000011100011101010111110000101011111000001111100011100011" - "0000000110000001000000111100000111100010101111111000000011100011" - "1111111111111111111111111111111111111111111111111111111111111100" - "1000001100000001111111111111110000000011111000111100001010100000" - "101000111100011010101110011011000000010"; + const char * const p = + // 6 5 4 3 2 1 + // 3210987654321098765432109876543210987654321098765432109876543210 + "1110011100011110000011110000011111110000000000000110101110000000" + "1010101000011100011101010111110000101011111000001111100011100011" + "0000000110000001000000111100000111100010101111111000000011100011" + "1111111111111111111111111111111111111111111111111111111111111100" + "1000001100000001111111111111110000000011111000111100001010100000" + "101000111100011010101110011011000000010"; - return std::string(p); + return std::string( p ); } class scoped_temp_file { public: - scoped_temp_file() - : m_path(boost::filesystem::unique_path()) - { - } + scoped_temp_file() + : m_path( boost::filesystem::unique_path() ) + { + } - ~scoped_temp_file() - { - boost::filesystem::remove(m_path); - } + ~scoped_temp_file() + { + boost::filesystem::remove( m_path ); + } - const boost::filesystem::path& path() const - { - return m_path; - } + const boost::filesystem::path & + path() const + { + return m_path; + } private: - boost::filesystem::path m_path; + boost::filesystem::path m_path; }; #if defined BOOST_NO_STD_LOCALE -template -bool is_one_or_zero(const Stream & /*s*/, char c) +template< typename Stream > +bool +is_one_or_zero( const Stream & /*s*/, char c ) { - return c == '1' || c == '0'; + return c == '1' || c == '0'; } -template -bool is_white_space(const Stream & /*s*/, char c) +template< typename Stream > +bool +is_white_space( const Stream & /*s*/, char c ) { - return std::isspace(c); + return std::isspace( c ); } #else -template -bool is_one_or_zero(const Stream& s, Ch c) +template< typename Stream, typename Ch > +bool +is_one_or_zero( const Stream & s, Ch c ) { - typedef typename Stream::traits_type Tr; - const Ch zero = s.widen('0'); - const Ch one = s.widen('1'); + typedef typename Stream::traits_type Tr; + const Ch zero = s.widen( '0' ); + const Ch one = s.widen( '1' ); - return Tr::eq(c, one) || Tr::eq(c, zero); + return Tr::eq( c, one ) || Tr::eq( c, zero ); } -template -bool is_white_space(const Stream & s, Ch c) +template< typename Stream, typename Ch > +bool +is_white_space( const Stream & s, Ch c ) { - return std::isspace(c, s.getloc()); + return std::isspace( c, s.getloc() ); } #endif - -template -bool has_flags(const Stream& s, std::ios::iostate flags) +template< typename Stream > +bool +has_flags( const Stream & s, std::ios::iostate flags ) { - return (s.rdstate() & flags) != std::ios::goodbit; + return ( s.rdstate() & flags ) != std::ios::goodbit; } - // constructors // default (can't do this generically) -template -struct bitset_test { +template< typename Bitset > +struct bitset_test +{ + typedef typename Bitset::block_type Block; + BOOST_STATIC_CONSTANT( int, bits_per_block = Bitset::bits_per_block ); - typedef typename Bitset::block_type Block; - BOOST_STATIC_CONSTANT(int, bits_per_block = Bitset::bits_per_block); - - // from unsigned long - // - // Note: this is templatized so that we check that the do-the-right-thing - // constructor dispatch is working correctly. - // - template - static void from_unsigned_long(NumBits num_bits, Value num) - { - // An object of size sz = num_bits is constructed: - // - the first m bit positions are initialized to the corresponding - // bit values in num (m being the smaller of sz and ulong_width) + // from unsigned long // - // - any remaining bit positions are initialized to zero + // Note: this is templatized so that we check that the do-the-right-thing + // constructor dispatch is working correctly. // + template< typename NumBits, typename Value > + static void + from_unsigned_long( NumBits num_bits, Value num ) + { + // An object of size sz = num_bits is constructed: + // - the first m bit positions are initialized to the corresponding + // bit values in num (m being the smaller of sz and ulong_width) + // + // - any remaining bit positions are initialized to zero + // - Bitset b(static_cast(num_bits), static_cast(num)); + Bitset b( static_cast< typename Bitset::size_type >( num_bits ), static_cast< unsigned long >( num ) ); - // OK, we can now cast to size_type - typedef typename Bitset::size_type size_type; - const size_type sz = static_cast(num_bits); + // OK, we can now cast to size_type + typedef typename Bitset::size_type size_type; + const size_type sz = static_cast< size_type >( num_bits ); - BOOST_TEST(b.size() == sz); + BOOST_TEST( b.size() == sz ); - const std::size_t ulong_width = std::numeric_limits::digits; - size_type m = sz; - if (ulong_width < sz) - m = ulong_width; + const std::size_t ulong_width = std::numeric_limits< unsigned long >::digits; + size_type m = sz; + if ( ulong_width < sz ) + m = ulong_width; - size_type i = 0; - for ( ; i < m; ++i) - BOOST_TEST(b.test(i) == nth_bit(static_cast(num), i)); - for ( ; i < sz; ++i) - BOOST_TEST(b.test(i) == 0); - } - - // from string - // - // Note: The corresponding function in dynamic_bitset (constructor - // from a string) has several default arguments. Actually we don't - // test the correct working of those defaults here (except for the - // default of num_bits). I'm not sure what to do in this regard. - // - // Note2: the default argument expression for num_bits doesn't use - // static_cast, to avoid a gcc 2.95.3 'sorry, not implemented' - // - template - static void from_string(const std::basic_string& str, - std::size_t pos, - std::size_t max_char, - std::size_t num_bits = (std::size_t)(-1)) - { - - std::size_t rlen = (std::min)(max_char, str.size() - pos); - - // The resulting size N of the bitset is num_bits, if - // that is different from the default arg, rlen otherwise. - // Put M = the smaller of N and rlen, then character - // position pos + M - 1 corresponds to bit position zero. - // Subsequent decreasing character positions correspond to - // increasing bit positions. - - const bool size_upon_string = num_bits == (std::size_t)(-1); - Bitset b = size_upon_string ? - Bitset(str, pos, max_char) - : Bitset(str, pos, max_char, num_bits); - - const std::size_t actual_size = size_upon_string? rlen : num_bits; - BOOST_TEST(b.size() == actual_size); - std::size_t m = (std::min)(num_bits, rlen); - std::size_t j; - for (j = 0; j < m; ++j) - BOOST_TEST(b[j] == (str[pos + m - 1 - j] == '1')); - // If M < N, remaining bit positions are zero - for (; j < actual_size; ++j) - BOOST_TEST(b[j] == 0); - - - } - - static void to_block_range(const Bitset & b /*, BlockOutputIterator result*/) - { - typedef typename Bitset::size_type size_type; - - Block sentinel = 0xF0; - int s = 8; // number of sentinels (must be *even*) - int offset = s/2; - std::vector v(b.num_blocks() + s, sentinel); - - boost::to_block_range(b, v.begin() + offset); - - assert(v.size() >= (size_type)s && (s >= 2) && (s % 2 == 0)); - // check sentinels at both ends - for(int i = 0; i < s/2; ++i) { - BOOST_TEST(v[i] == sentinel); - BOOST_TEST(v[v.size()-1-i] == sentinel); + size_type i = 0; + for ( ; i < m; ++i ) + BOOST_TEST( b.test( i ) == nth_bit( static_cast< unsigned long >( num ), i ) ); + for ( ; i < sz; ++i ) + BOOST_TEST( b.test( i ) == 0 ); } - typename std::vector::const_iterator p = v.begin() + offset; - for(size_type n = 0; n < b.num_blocks(); ++n, ++p) { - typename Bitset::block_width_type i = 0; - for(; i < bits_per_block; ++i) { - size_type bit = n * bits_per_block + i; - BOOST_TEST(nth_bit(*p, i) == (bit < b.size()? b[bit] : 0)); - } + // from string + // + // Note: The corresponding function in dynamic_bitset (constructor + // from a string) has several default arguments. Actually we don't + // test the correct working of those defaults here (except for the + // default of num_bits). I'm not sure what to do in this regard. + // + // Note2: the default argument expression for num_bits doesn't use + // static_cast, to avoid a gcc 2.95.3 'sorry, not implemented' + // + template< typename Ch, typename Tr, typename Al > + static void + from_string( const std::basic_string< Ch, Tr, Al > & str, std::size_t pos, std::size_t max_char, std::size_t num_bits = (std::size_t)( -1 ) ) + { + std::size_t rlen = (std::min)( max_char, str.size() - pos ); + + // The resulting size N of the bitset is num_bits, if + // that is different from the default arg, rlen otherwise. + // Put M = the smaller of N and rlen, then character + // position pos + M - 1 corresponds to bit position zero. + // Subsequent decreasing character positions correspond to + // increasing bit positions. + + const bool size_upon_string = num_bits == (std::size_t)( -1 ); + Bitset b = size_upon_string ? Bitset( str, pos, max_char ) + : Bitset( str, pos, max_char, num_bits ); + + const std::size_t actual_size = size_upon_string ? rlen : num_bits; + BOOST_TEST( b.size() == actual_size ); + std::size_t m = (std::min)( num_bits, rlen ); + std::size_t j; + for ( j = 0; j < m; ++j ) + BOOST_TEST( b[ j ] == ( str[ pos + m - 1 - j ] == '1' ) ); + // If M < N, remaining bit positions are zero + for ( ; j < actual_size; ++j ) + BOOST_TEST( b[ j ] == 0 ); } - } - // TODO from_block_range (below) should be splitted + static void + to_block_range( const Bitset & b /*, BlockOutputIterator result*/ ) + { + typedef typename Bitset::size_type size_type; - // PRE: std::equal(first1, last1, first2) == true - static void from_block_range(const std::vector& blocks) - { - { // test constructor from block range - Bitset bset(blocks.begin(), blocks.end()); - std::size_t n = blocks.size(); - for (std::size_t b = 0; b < n; ++b) { - typename Bitset::block_width_type i = 0; - for (; i < bits_per_block; ++i) { - std::size_t bit = b * bits_per_block + i; - BOOST_TEST(bset[bit] == nth_bit(blocks[b], i)); + Block sentinel = 0xF0; + int s = 8; // number of sentinels (must be *even*) + int offset = s / 2; + std::vector< Block > v( b.num_blocks() + s, sentinel ); + + boost::to_block_range( b, v.begin() + offset ); + + assert( v.size() >= (size_type)s && ( s >= 2 ) && ( s % 2 == 0 ) ); + // check sentinels at both ends + for ( int i = 0; i < s / 2; ++i ) { + BOOST_TEST( v[ i ] == sentinel ); + BOOST_TEST( v[ v.size() - 1 - i ] == sentinel ); } - } - BOOST_TEST(bset.size() == n * bits_per_block); - } - { // test boost::from_block_range - const typename Bitset::size_type n = blocks.size(); - Bitset bset(n * bits_per_block); - boost::from_block_range(blocks.begin(), blocks.end(), bset); - for (std::size_t b = 0; b < n; ++b) { - typename Bitset::block_width_type i = 0; - for (; i < bits_per_block; ++i) { - std::size_t bit = b * bits_per_block + i; - BOOST_TEST(bset[bit] == nth_bit(blocks[b], i)); + + typename std::vector< Block >::const_iterator p = v.begin() + offset; + for ( size_type n = 0; n < b.num_blocks(); ++n, ++p ) { + typename Bitset::block_width_type i = 0; + for ( ; i < bits_per_block; ++i ) { + size_type bit = n * bits_per_block + i; + BOOST_TEST( nth_bit( *p, i ) == ( bit < b.size() ? b[ bit ] : 0 ) ); + } } - } - BOOST_TEST(n <= bset.num_blocks()); } - } - // copy constructor (absent from std::bitset) - static void copy_constructor(const Bitset& b) - { - Bitset copy(b); - BOOST_TEST(b == copy); + // TODO from_block_range (below) should be splitted - // Changes to the copy do not affect the original - if (b.size() > 0) { - std::size_t pos = copy.size() / 2; - copy.flip(pos); - BOOST_TEST(copy[pos] != b[pos]); + // PRE: std::equal(first1, last1, first2) == true + static void + from_block_range( const std::vector< Block > & blocks ) + { + { // test constructor from block range + Bitset bset( blocks.begin(), blocks.end() ); + std::size_t n = blocks.size(); + for ( std::size_t b = 0; b < n; ++b ) { + typename Bitset::block_width_type i = 0; + for ( ; i < bits_per_block; ++i ) { + std::size_t bit = b * bits_per_block + i; + BOOST_TEST( bset[ bit ] == nth_bit( blocks[ b ], i ) ); + } + } + BOOST_TEST( bset.size() == n * bits_per_block ); + } + { // test boost::from_block_range + const typename Bitset::size_type n = blocks.size(); + Bitset bset( n * bits_per_block ); + boost::from_block_range( blocks.begin(), blocks.end(), bset ); + for ( std::size_t b = 0; b < n; ++b ) { + typename Bitset::block_width_type i = 0; + for ( ; i < bits_per_block; ++i ) { + std::size_t bit = b * bits_per_block + i; + BOOST_TEST( bset[ bit ] == nth_bit( blocks[ b ], i ) ); + } + } + BOOST_TEST( n <= bset.num_blocks() ); + } } - } - // copy assignment operator (absent from std::bitset) - static void copy_assignment_operator(const Bitset& lhs, const Bitset& rhs) - { - Bitset b(lhs); - b = rhs; - b = b; // self assignment check - BOOST_TEST(b == rhs); + // copy constructor (absent from std::bitset) + static void + copy_constructor( const Bitset & b ) + { + Bitset copy( b ); + BOOST_TEST( b == copy ); - // Changes to the copy do not affect the original - if (b.size() > 0) { - std::size_t pos = b.size() / 2; - b.flip(pos); - BOOST_TEST(b[pos] != rhs[pos]); + // Changes to the copy do not affect the original + if ( b.size() > 0 ) { + std::size_t pos = copy.size() / 2; + copy.flip( pos ); + BOOST_TEST( copy[ pos ] != b[ pos ] ); + } } - } - static void max_size(const Bitset& b) - { - BOOST_TEST(b.max_size() > 0); - } + // copy assignment operator (absent from std::bitset) + static void + copy_assignment_operator( const Bitset & lhs, const Bitset & rhs ) + { + Bitset b( lhs ); + b = rhs; + b = b; // self assignment check + BOOST_TEST( b == rhs ); + + // Changes to the copy do not affect the original + if ( b.size() > 0 ) { + std::size_t pos = b.size() / 2; + b.flip( pos ); + BOOST_TEST( b[ pos ] != rhs[ pos ] ); + } + } + + static void + max_size( const Bitset & b ) + { + BOOST_TEST( b.max_size() > 0 ); + } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - // move constructor (absent from std::bitset) - static void move_constructor(const Bitset& b) - { - Bitset copy(boost::move(b)); - BOOST_TEST(b == copy); - } + // move constructor (absent from std::bitset) + static void + move_constructor( const Bitset & b ) + { + Bitset copy( boost::move( b ) ); + BOOST_TEST( b == copy ); + } - // move assignment operator (absent from std::bitset) - static void move_assignment_operator(const Bitset& lhs, const Bitset& rhs) - { - Bitset b(lhs); - Bitset c(rhs); - b = boost::move(c); - b = boost::move(b); // self assignment check - BOOST_TEST(b == rhs); - } + // move assignment operator (absent from std::bitset) + static void + move_assignment_operator( const Bitset & lhs, const Bitset & rhs ) + { + Bitset b( lhs ); + Bitset c( rhs ); + b = boost::move( c ); + b = boost::move( b ); // self assignment check + BOOST_TEST( b == rhs ); + } #endif // BOOST_NO_CXX11_RVALUE_REFERENCES - static void swap(const Bitset& lhs, const Bitset& rhs) - { - // bitsets must be swapped - Bitset copy1(lhs); - Bitset copy2(rhs); - copy1.swap(copy2); - - BOOST_TEST(copy1 == rhs); - BOOST_TEST(copy2 == lhs); - - // references must be stable under a swap - for(typename Bitset::size_type i = 0; i < lhs.size(); ++i) { - Bitset b1(lhs); - Bitset b2(rhs); - typename Bitset::reference ref = b1[i]; - bool x = ref; - if (i < b2.size()) - b2[i] = !x; // make sure b2[i] is different - b1.swap(b2); - BOOST_TEST(b2[i] == x); // now it must be equal.. - b2.flip(i); - BOOST_TEST(ref == b2[i]); // .. and ref must be into b2 - BOOST_TEST(ref == !x); - } - - } - - static void resize(const Bitset& lhs) - { - Bitset b(lhs); - - // Test no change in size - b.resize(lhs.size()); - BOOST_TEST(b == lhs); - - // Test increase in size - b.resize(lhs.size() * 2, true); - - std::size_t i; - for (i = 0; i < lhs.size(); ++i) - BOOST_TEST(b[i] == lhs[i]); - for (; i < b.size(); ++i) - BOOST_TEST(b[i] == true); - - // Test decrease in size - b.resize(lhs.size()); - for (i = 0; i < lhs.size(); ++i) - BOOST_TEST(b[i] == lhs[i]); - } - - static void clear(const Bitset& lhs) - { - Bitset b(lhs); - b.clear(); - BOOST_TEST(b.size() == 0); - } - - static void pop_back(const Bitset& lhs) - { - Bitset b(lhs); - b.pop_back(); - BOOST_TEST(b.size() == lhs.size() - 1); - for (std::size_t i = 0; i < b.size(); ++i) - BOOST_TEST(b[i] == lhs[i]); - - b.pop_back(); - BOOST_TEST(b.size() == lhs.size() - 2); - for (std::size_t j = 0; j < b.size(); ++j) - BOOST_TEST(b[j] == lhs[j]); - } - - static void append_bit(const Bitset& lhs) - { - Bitset b(lhs); - b.push_back(true); - BOOST_TEST(b.size() == lhs.size() + 1); - BOOST_TEST(b[b.size() - 1] == true); - for (std::size_t i = 0; i < lhs.size(); ++i) - BOOST_TEST(b[i] == lhs[i]); - - b.push_back(false); - BOOST_TEST(b.size() == lhs.size() + 2); - BOOST_TEST(b[b.size() - 1] == false); - BOOST_TEST(b[b.size() - 2] == true); - for (std::size_t j = 0; j < lhs.size(); ++j) - BOOST_TEST(b[j] == lhs[j]); - } - - static void append_block(const Bitset& lhs) - { - Bitset b(lhs); - Block value(128); - b.append(value); - BOOST_TEST(b.size() == lhs.size() + bits_per_block); - for (typename Bitset::block_width_type i = 0; i < bits_per_block; ++i) - BOOST_TEST(b[lhs.size() + i] == bool((value >> i) & 1)); - } - - static void append_block_range(const Bitset& lhs, const std::vector& blocks) - { - Bitset b(lhs), c(lhs); - b.append(blocks.begin(), blocks.end()); - for (typename std::vector::const_iterator i = blocks.begin(); - i != blocks.end(); ++i) - c.append(*i); - BOOST_TEST(b == c); - } - - // operator[] and reference members - // PRE: b[i] == bit_vec[i] - static void operator_bracket(const Bitset& lhs, const std::vector& bit_vec) - { - Bitset b(lhs); - std::size_t i, j, k; - - // x = b[i] - // x = ~b[i] - for (i = 0; i < b.size(); ++i) { - bool x = b[i]; - BOOST_TEST(x == bit_vec[i]); - x = ~b[i]; - BOOST_TEST(x == !bit_vec[i]); - } - Bitset prev(b); - - // b[i] = x - for (j = 0; j < b.size(); ++j) { - bool x = !prev[j]; - b[j] = x; - for (k = 0; k < b.size(); ++k) - if (j == k) - BOOST_TEST(b[k] == x); - else - BOOST_TEST(b[k] == prev[k]); - b[j] = prev[j]; - } - b.flip(); - - // b[i] = b[j] - for (i = 0; i < b.size(); ++i) { - b[i] = prev[i]; - for (j = 0; j < b.size(); ++j) { - if (i == j) - BOOST_TEST(b[j] == prev[j]); - else - BOOST_TEST(b[j] == !prev[j]); - } - b[i] = !prev[i]; - } - - // b[i].flip() - for (i = 0; i < b.size(); ++i) { - b[i].flip(); - for (j = 0; j < b.size(); ++j) { - if (i == j) - BOOST_TEST(b[j] == prev[j]); - else - BOOST_TEST(b[j] == !prev[j]); - } - b[i].flip(); - } - } - - //=========================================================================== - // bitwise operators - - // bitwise and assignment - - // PRE: b.size() == rhs.size() - static void and_assignment(const Bitset& b, const Bitset& rhs) - { - Bitset lhs(b); - Bitset prev(lhs); - lhs &= rhs; - // Clears each bit in lhs for which the corresponding bit in rhs is - // clear, and leaves all other bits unchanged. - for (std::size_t I = 0; I < lhs.size(); ++I) - if (rhs[I] == 0) - BOOST_TEST(lhs[I] == 0); - else - BOOST_TEST(lhs[I] == prev[I]); - } - - // PRE: b.size() == rhs.size() - static void or_assignment(const Bitset& b, const Bitset& rhs) - { - Bitset lhs(b); - Bitset prev(lhs); - lhs |= rhs; - // Sets each bit in lhs for which the corresponding bit in rhs is set, and - // leaves all other bits unchanged. - for (std::size_t I = 0; I < lhs.size(); ++I) - if (rhs[I] == 1) - BOOST_TEST(lhs[I] == 1); - else - BOOST_TEST(lhs[I] == prev[I]); - } - - // PRE: b.size() == rhs.size() - static void xor_assignment(const Bitset& b, const Bitset& rhs) - { - Bitset lhs(b); - Bitset prev(lhs); - lhs ^= rhs; - // Flips each bit in lhs for which the corresponding bit in rhs is set, - // and leaves all other bits unchanged. - for (std::size_t I = 0; I < lhs.size(); ++I) - if (rhs[I] == 1) - BOOST_TEST(lhs[I] == !prev[I]); - else - BOOST_TEST(lhs[I] == prev[I]); - } - - // PRE: b.size() == rhs.size() - static void sub_assignment(const Bitset& b, const Bitset& rhs) - { - Bitset lhs(b); - Bitset prev(lhs); - lhs -= rhs; - // Resets each bit in lhs for which the corresponding bit in rhs is set, - // and leaves all other bits unchanged. - for (std::size_t I = 0; I < lhs.size(); ++I) - if (rhs[I] == 1) - BOOST_TEST(lhs[I] == 0); - else - BOOST_TEST(lhs[I] == prev[I]); - } - - static void shift_left_assignment(const Bitset& b, std::size_t pos) - { - Bitset lhs(b); - Bitset prev(lhs); - lhs <<= pos; - // Replaces each bit at position I in lhs with the following value: - // - If I < pos, the new value is zero - // - If I >= pos, the new value is the previous value of the bit at - // position I - pos - for (std::size_t I = 0; I < lhs.size(); ++I) - if (I < pos) - BOOST_TEST(lhs[I] == 0); - else - BOOST_TEST(lhs[I] == prev[I - pos]); - } - - static void shift_right_assignment(const Bitset& b, std::size_t pos) - { - Bitset lhs(b); - Bitset prev(lhs); - lhs >>= pos; - // Replaces each bit at position I in lhs with the following value: - // - If pos >= N - I, the new value is zero - // - If pos < N - I, the new value is the previous value of the bit at - // position I + pos - std::size_t N = lhs.size(); - for (std::size_t I = 0; I < N; ++I) - if (pos >= N - I) - BOOST_TEST(lhs[I] == 0); - else - BOOST_TEST(lhs[I] == prev[I + pos]); - } - - - static void set_all(const Bitset& b) - { - Bitset lhs(b); - lhs.set(); - for (std::size_t I = 0; I < lhs.size(); ++I) - BOOST_TEST(lhs[I] == 1); - } - - static void set_one(const Bitset& b, std::size_t pos, bool value) - { - Bitset lhs(b); - std::size_t N = lhs.size(); - if (pos < N) { - Bitset prev(lhs); - // Stores a new value in the bit at position pos in lhs. - lhs.set(pos, value); - BOOST_TEST(lhs[pos] == value); - - // All other values of lhs remain unchanged - for (std::size_t I = 0; I < N; ++I) - if (I != pos) - BOOST_TEST(lhs[I] == prev[I]); - } else { - // Not in range, doesn't satisfy precondition. - } - } - - static void set_segment(const Bitset& b, std::size_t pos, - std::size_t len, bool value) - { - Bitset lhs(b); - std::size_t N = lhs.size(); - Bitset prev(lhs); - lhs.set(pos, len, value); - for (std::size_t I = 0; I < N; ++I) + static void + swap( const Bitset & lhs, const Bitset & rhs ) { - if (I < pos || I >= pos + len) - BOOST_TEST(lhs[I] == prev[I]); - else - BOOST_TEST(lhs[I] == value); + // bitsets must be swapped + Bitset copy1( lhs ); + Bitset copy2( rhs ); + copy1.swap( copy2 ); + + BOOST_TEST( copy1 == rhs ); + BOOST_TEST( copy2 == lhs ); + + // references must be stable under a swap + for ( typename Bitset::size_type i = 0; i < lhs.size(); ++i ) { + Bitset b1( lhs ); + Bitset b2( rhs ); + typename Bitset::reference ref = b1[ i ]; + bool x = ref; + if ( i < b2.size() ) + b2[ i ] = ! x; // make sure b2[i] is different + b1.swap( b2 ); + BOOST_TEST( b2[ i ] == x ); // now it must be equal.. + b2.flip( i ); + BOOST_TEST( ref == b2[ i ] ); // .. and ref must be into b2 + BOOST_TEST( ref == ! x ); + } } - } - static void reset_all(const Bitset& b) - { - Bitset lhs(b); - // Resets all bits in lhs - lhs.reset(); - for (std::size_t I = 0; I < lhs.size(); ++I) - BOOST_TEST(lhs[I] == 0); - } - - static void reset_one(const Bitset& b, std::size_t pos) - { - Bitset lhs(b); - std::size_t N = lhs.size(); - if (pos < N) { - Bitset prev(lhs); - lhs.reset(pos); - // Resets the bit at position pos in lhs - BOOST_TEST(lhs[pos] == 0); - - // All other values of lhs remain unchanged - for (std::size_t I = 0; I < N; ++I) - if (I != pos) - BOOST_TEST(lhs[I] == prev[I]); - } else { - // Not in range, doesn't satisfy precondition. - } - } - - static void reset_segment(const Bitset& b, std::size_t pos, - std::size_t len) - { - Bitset lhs(b); - std::size_t N = lhs.size(); - Bitset prev(lhs); - lhs.reset(pos, len); - for (std::size_t I = 0; I < N; ++I) + static void + resize( const Bitset & lhs ) { - if (I < pos || I >= pos + len) - BOOST_TEST(lhs[I] == prev[I]); - else - BOOST_TEST(!lhs[I]); + Bitset b( lhs ); + + // Test no change in size + b.resize( lhs.size() ); + BOOST_TEST( b == lhs ); + + // Test increase in size + b.resize( lhs.size() * 2, true ); + + std::size_t i; + for ( i = 0; i < lhs.size(); ++i ) + BOOST_TEST( b[ i ] == lhs[ i ] ); + for ( ; i < b.size(); ++i ) + BOOST_TEST( b[ i ] == true ); + + // Test decrease in size + b.resize( lhs.size() ); + for ( i = 0; i < lhs.size(); ++i ) + BOOST_TEST( b[ i ] == lhs[ i ] ); } - } - static void operator_flip(const Bitset& b) - { - Bitset lhs(b); - Bitset x(lhs); - BOOST_TEST(~lhs == x.flip()); - } - - static void flip_all(const Bitset& b) - { - Bitset lhs(b); - std::size_t N = lhs.size(); - Bitset prev(lhs); - lhs.flip(); - // Toggles all the bits in lhs - for (std::size_t I = 0; I < N; ++I) - BOOST_TEST(lhs[I] == !prev[I]); - } - - static void flip_one(const Bitset& b, std::size_t pos) - { - Bitset lhs(b); - std::size_t N = lhs.size(); - if (pos < N) { - Bitset prev(lhs); - lhs.flip(pos); - // Toggles the bit at position pos in lhs - BOOST_TEST(lhs[pos] == !prev[pos]); - - // All other values of lhs remain unchanged - for (std::size_t I = 0; I < N; ++I) - if (I != pos) - BOOST_TEST(lhs[I] == prev[I]); - } else { - // Not in range, doesn't satisfy precondition. - } - } - - static void flip_segment(const Bitset& b, std::size_t pos, - std::size_t len) - { - Bitset lhs(b); - std::size_t N = lhs.size(); - Bitset prev(lhs); - lhs.flip(pos, len); - for (std::size_t I = 0; I < N; ++I) + static void + clear( const Bitset & lhs ) { - if (I < pos || I >= pos + len) - BOOST_TEST(lhs[I] == prev[I]); - else - BOOST_TEST(lhs[I] != prev[I]); + Bitset b( lhs ); + b.clear(); + BOOST_TEST( b.size() == 0 ); } - } - // empty - static void empty(const Bitset& b) - { - BOOST_TEST(b.empty() == (b.size() == 0)); - } + static void + pop_back( const Bitset & lhs ) + { + Bitset b( lhs ); + b.pop_back(); + BOOST_TEST( b.size() == lhs.size() - 1 ); + for ( std::size_t i = 0; i < b.size(); ++i ) + BOOST_TEST( b[ i ] == lhs[ i ] ); - // to_ulong() - static void to_ulong(const Bitset& lhs) - { - typedef unsigned long result_type; - std::size_t n = std::numeric_limits::digits; - std::size_t sz = lhs.size(); - - bool will_overflow = false; - for (std::size_t i = n; i < sz; ++i) { - if (lhs.test(i) != 0) { - will_overflow = true; - break; - } + b.pop_back(); + BOOST_TEST( b.size() == lhs.size() - 2 ); + for ( std::size_t j = 0; j < b.size(); ++j ) + BOOST_TEST( b[ j ] == lhs[ j ] ); } - if (will_overflow) { - try { - (void)lhs.to_ulong(); - BOOST_TEST(false); // It should have thrown an exception - } catch (std::overflow_error & ex) { - // Good! - BOOST_TEST(!!ex.what()); - } catch (...) { - BOOST_TEST(false); // threw the wrong exception - } - } else { - result_type num = lhs.to_ulong(); - // Be sure the number is right - if (sz == 0) - BOOST_TEST(num == 0); - else { - for (std::size_t i = 0; i < sz; ++i) - BOOST_TEST(lhs[i] == (i < n ? nth_bit(num, i) : 0)); - } + + static void + append_bit( const Bitset & lhs ) + { + Bitset b( lhs ); + b.push_back( true ); + BOOST_TEST( b.size() == lhs.size() + 1 ); + BOOST_TEST( b[ b.size() - 1 ] == true ); + for ( std::size_t i = 0; i < lhs.size(); ++i ) + BOOST_TEST( b[ i ] == lhs[ i ] ); + + b.push_back( false ); + BOOST_TEST( b.size() == lhs.size() + 2 ); + BOOST_TEST( b[ b.size() - 1 ] == false ); + BOOST_TEST( b[ b.size() - 2 ] == true ); + for ( std::size_t j = 0; j < lhs.size(); ++j ) + BOOST_TEST( b[ j ] == lhs[ j ] ); } - } - // to_string() - static void to_string(const Bitset& b) - { - std::string str; - boost::to_string(b, str); - BOOST_TEST(str.size() == b.size()); - for (std::size_t i = 0; i < b.size(); ++i) - BOOST_TEST(str[b.size() - 1 - i] ==(b.test(i)? '1':'0')); - } - - static void count(const Bitset& b) - { - std::size_t c = b.count(); - std::size_t actual = 0; - for (std::size_t i = 0; i < b.size(); ++i) - if (b[i]) - ++actual; - BOOST_TEST(c == actual); - } - - static void size(const Bitset& b) - { - BOOST_TEST(Bitset(b).set().count() == b.size()); - } - - static void capacity_test_one(const Bitset& lhs) - { - //empty bitset - Bitset b(lhs); - BOOST_TEST(b.capacity() == 0); - } - - static void capacity_test_two(const Bitset& lhs) - { - //bitset constructed with size "100" - Bitset b(lhs); - BOOST_TEST(b.capacity() >= 100); - b.resize(200); - BOOST_TEST(b.capacity() >= 200); - } - - static void reserve_test_one(const Bitset& lhs) - { - //empty bitset - Bitset b(lhs); - b.reserve(16); - BOOST_TEST(b.capacity() >= 16); - } - - static void reserve_test_two(const Bitset& lhs) - { - //bitset constructed with size "100" - Bitset b(lhs); - BOOST_TEST(b.capacity() >= 100); - b.reserve(60); - BOOST_TEST(b.size() == 100); - BOOST_TEST(b.capacity() >= 100); - b.reserve(160); - BOOST_TEST(b.size() == 100); - BOOST_TEST(b.capacity() >= 160); - } - - static void shrink_to_fit_test_one(const Bitset& lhs) - { - //empty bitset - Bitset b(lhs); - b.shrink_to_fit(); - BOOST_TEST(b.size() == 0); - BOOST_TEST(b.capacity() == 0); - } - - static void shrink_to_fit_test_two(const Bitset& lhs) - { - //bitset constructed with size "100" - Bitset b(lhs); - b.shrink_to_fit(); - BOOST_TEST(b.capacity() >= 100); - BOOST_TEST(b.size() == 100); - b.reserve(200); - BOOST_TEST(b.capacity() >= 200); - BOOST_TEST(b.size() == 100); - b.shrink_to_fit(); - BOOST_TEST(b.capacity() < 200); - BOOST_TEST(b.size() == 100); - } - - static void all(const Bitset& b) - { - BOOST_TEST(b.all() == (b.count() == b.size())); - bool result = true; - for(std::size_t i = 0; i < b.size(); ++i) - if(!b[i]) { - result = false; - break; - } - BOOST_TEST(b.all() == result); - } - - static void any(const Bitset& b) - { - BOOST_TEST(b.any() == (b.count() != 0)); - bool result = false; - for(std::size_t i = 0; i < b.size(); ++i) - if(b[i]) { - result = true; - break; - } - BOOST_TEST(b.any() == result); - } - - static void none(const Bitset& b) - { - bool result = true; - for(std::size_t i = 0; i < b.size(); ++i) { - if(b[i]) { - result = false; - break; - } + static void + append_block( const Bitset & lhs ) + { + Bitset b( lhs ); + Block value( 128 ); + b.append( value ); + BOOST_TEST( b.size() == lhs.size() + bits_per_block ); + for ( typename Bitset::block_width_type i = 0; i < bits_per_block; ++i ) + BOOST_TEST( b[ lhs.size() + i ] == bool( ( value >> i ) & 1 ) ); } - BOOST_TEST(b.none() == result); - // sanity - BOOST_TEST(b.none() == !b.any()); - BOOST_TEST(b.none() == (b.count() == 0)); - } + static void + append_block_range( const Bitset & lhs, const std::vector< Block > & blocks ) + { + Bitset b( lhs ), c( lhs ); + b.append( blocks.begin(), blocks.end() ); + for ( typename std::vector< Block >::const_iterator i = blocks.begin(); + i != blocks.end(); + ++i ) + c.append( *i ); + BOOST_TEST( b == c ); + } - static void subset(const Bitset& a, const Bitset& b) - { - BOOST_TEST(a.size() == b.size()); // PRE + // operator[] and reference members + // PRE: b[i] == bit_vec[i] + static void + operator_bracket( const Bitset & lhs, const std::vector< bool > & bit_vec ) + { + Bitset b( lhs ); + std::size_t i, j, k; - bool is_subset = true; - if (b.size()) { // could use b.any() but let's be safe - for(std::size_t i = 0; i < a.size(); ++i) { - if(a.test(i) && !b.test(i)) { - is_subset = false; - break; + // x = b[i] + // x = ~b[i] + for ( i = 0; i < b.size(); ++i ) { + bool x = b[ i ]; + BOOST_TEST( x == bit_vec[ i ] ); + x = ~b[ i ]; + BOOST_TEST( x == ! bit_vec[ i ] ); } - } - } - else { - // sanity - BOOST_TEST(a.count() == 0); - BOOST_TEST(a.any() == false); + Bitset prev( b ); - //is_subset = (a.any() == false); - } - - BOOST_TEST(a.is_subset_of(b) == is_subset); - } - - static void proper_subset(const Bitset& a, const Bitset& b) - { - // PRE: a.size() == b.size() - BOOST_TEST(a.size() == b.size()); - - bool is_proper = false; - - if (b.size() != 0) { - - // check it's a subset - subset(a, b); - - // is it proper? - for (std::size_t i = 0; i < a.size(); ++i) { - if (!a.test(i) && b.test(i)) { - is_proper = true; - // sanity - BOOST_TEST(a.count() < b.count()); - BOOST_TEST(b.any()); + // b[i] = x + for ( j = 0; j < b.size(); ++j ) { + bool x = ! prev[ j ]; + b[ j ] = x; + for ( k = 0; k < b.size(); ++k ) + if ( j == k ) + BOOST_TEST( b[ k ] == x ); + else + BOOST_TEST( b[ k ] == prev[ k ] ); + b[ j ] = prev[ j ]; } - } - } - - BOOST_TEST(a.is_proper_subset_of(b) == is_proper); - if (is_proper) - BOOST_TEST(b.is_proper_subset_of(a) != is_proper);// antisymmetry - } - - static void intersects(const Bitset& a, const Bitset& b) - { - bool have_intersection = false; - - typename Bitset::size_type m = a.size() < b.size() ? a.size() : b.size(); - for(typename Bitset::size_type i = 0; i < m && !have_intersection; ++i) - if(a[i] == true && b[i] == true) - have_intersection = true; - - BOOST_TEST(a.intersects(b) == have_intersection); - // also check commutativity - BOOST_TEST(b.intersects(a) == have_intersection); - } - - static void find_first(const Bitset& b, typename Bitset::size_type offset = 0) - { - // find first non-null bit from offset onwards, if any - typename Bitset::size_type i = offset; - while (i < b.size() && b[i] == 0) - ++i; - - if (i >= b.size()) - BOOST_TEST(b.find_first(offset) == Bitset::npos); // not found; - else { - BOOST_TEST(b.find_first(offset) == i); - BOOST_TEST(b.test(i) == true); - } - } - - static void find_pos(const Bitset& b, typename Bitset::size_type pos) - { - find_first(b, pos); - BOOST_TEST(next_bit_on(b, pos) == b.find_next(pos)); - } - - static void operator_equal(const Bitset& a, const Bitset& b) - { - if (a == b) { - for (std::size_t I = 0; I < a.size(); ++I) - BOOST_TEST(a[I] == b[I]); - } else { - if (a.size() == b.size()) { - bool diff = false; - for (std::size_t I = 0; I < a.size(); ++I) - if (a[I] != b[I]) { - diff = true; - break; - } - BOOST_TEST(diff); - } - } - } - - static void operator_not_equal(const Bitset& a, const Bitset& b) - { - if (a != b) { - if (a.size() == b.size()) { - bool diff = false; - for (std::size_t I = 0; I < a.size(); ++I) - if (a[I] != b[I]) { - diff = true; - break; - } - BOOST_TEST(diff); - } - } else { - for (std::size_t I = 0; I < a.size(); ++I) - BOOST_TEST(a[I] == b[I]); - } - } - - static bool less_than(const Bitset& a, const Bitset& b) - { - - typedef BOOST_DEDUCED_TYPENAME Bitset::size_type size_type; - - size_type asize(a.size()); - size_type bsize(b.size()); - - if (!bsize) - { - return false; - } - else if (!asize) - { - return true; - } - else - { - - // Compare from most significant to least. - - size_type leqsize(std::min BOOST_PREVENT_MACRO_SUBSTITUTION(asize,bsize)); - size_type I; - for (I = 0; I < leqsize; ++I,--asize,--bsize) - { - - size_type i = asize-1; - size_type j = bsize-1; - - if (a[i] < b[j]) - return true; - else if (a[i] > b[j]) - return false; - // if (a[i] = b[j]) skip to next - } - return (a.size() < b.size()); - } - } - - static typename Bitset::size_type next_bit_on(const Bitset& b, typename Bitset::size_type prev) - { - // helper function for find_next() - // - - if (b.none() == true || prev == Bitset::npos) - return Bitset::npos; - - ++prev; - - if (prev >= b.size()) - return Bitset::npos; - - typename Bitset::size_type i = prev; - while (i < b.size() && b[i] == 0) - ++i; - - return i==b.size() ? Bitset::npos : i; - - } - - static void operator_less_than(const Bitset& a, const Bitset& b) - { - if (less_than(a, b)) - BOOST_TEST(a < b); - else - BOOST_TEST(!(a < b)); - } - - static void operator_greater_than(const Bitset& a, const Bitset& b) - { - if (less_than(a, b) || a == b) - BOOST_TEST(!(a > b)); - else - BOOST_TEST(a > b); - } - - static void operator_less_than_eq(const Bitset& a, const Bitset& b) - { - if (less_than(a, b) || a == b) - BOOST_TEST(a <= b); - else - BOOST_TEST(!(a <= b)); - } - - static void operator_greater_than_eq(const Bitset& a, const Bitset& b) - { - if (less_than(a, b)) - BOOST_TEST(!(a >= b)); - else - BOOST_TEST(a >= b); - } - - static void test_bit(const Bitset& b, std::size_t pos) - { - Bitset lhs(b); - std::size_t N = lhs.size(); - if (pos < N) { - BOOST_TEST(lhs.test(pos) == lhs[pos]); - } else { - // Not in range, doesn't satisfy precondition. - } - } - - static void test_set_bit(const Bitset& b, std::size_t pos, bool value) - { - Bitset lhs(b); - std::size_t N = lhs.size(); - if (pos < N) { - Bitset prev(lhs); - // Stores a new value in the bit at position pos in lhs. - BOOST_TEST(lhs.test_set(pos, value) == prev[pos]); - BOOST_TEST(lhs[pos] == value); - - // All other values of lhs remain unchanged - for (std::size_t I = 0; I < N; ++I) - if (I != pos) - BOOST_TEST(lhs[I] == prev[I]); - } else { - // Not in range, doesn't satisfy precondition. - } - } - - static void operator_shift_left(const Bitset& lhs, std::size_t pos) - { - Bitset x(lhs); - BOOST_TEST((lhs << pos) == (x <<= pos)); - } - - static void operator_shift_right(const Bitset& lhs, std::size_t pos) - { - Bitset x(lhs); - BOOST_TEST((lhs >> pos) == (x >>= pos)); - } - - static void at(const Bitset& lhs, const std::vector& bit_vec) - { - Bitset b(lhs); - std::size_t i, j, k; - - // x = b.at(i) - // x = ~b.at(i) - for (i = 0; i < b.size(); ++i) - { - bool x = b.at(i); - BOOST_TEST(x == bit_vec.at(i)); - x = ~b.at(i); - BOOST_TEST(x == !bit_vec.at(i)); - } - Bitset prev(b); - - // b.at(i) = x - for (j = 0; j < b.size(); ++j) - { - bool x = !prev.at(j); - b.at(j) = x; - for (k = 0; k < b.size(); ++k) - if (j == k) - BOOST_TEST(b.at(k) == x); - else - BOOST_TEST(b.at(k) == prev.at(k)); - b.at(j) = prev.at(j); - } - b.flip(); - - // b.at(i) = b.at(j) - for (i = 0; i < b.size(); ++i) - { - b.at(i) = prev.at(i); - for (j = 0; j < b.size(); ++j) - { - if (i == j) - BOOST_TEST(b.at(j) == prev.at(j)); - else - BOOST_TEST(b.at(j) == !prev.at(j)); - } - b.at(i) = !prev.at(i); - } - - // b.at(i).flip() - for (i = 0; i < b.size(); ++i) - { - b.at(i).flip(); - for (j = 0; j < b.size(); ++j) - { - if (i == j) - BOOST_TEST(b.at(j) == prev.at(j)); - else - BOOST_TEST(b.at(j) == !prev.at(j)); - } - b.at(i).flip(); - } - - // b.at(b.size()) - bool will_out_of_range = false; - for (i = 0; i <= b.size(); ++i) - { - if (i == b.size()) - { - will_out_of_range = true; - break; - } - b.at(i); - } - if (will_out_of_range) - { - try - { - b.at(b.size()); - BOOST_TEST(false); // It should have thrown an exception - } - catch (const std::out_of_range& ex) - { - // Good! - BOOST_TEST(!!ex.what()); - } - catch (...) - { - BOOST_TEST(false); // threw the wrong exception - } - } - } - - // operator| - static - void operator_or(const Bitset& lhs, const Bitset& rhs) - { - Bitset x(lhs); - BOOST_TEST((lhs | rhs) == (x |= rhs)); - } - - // operator& - static - void operator_and(const Bitset& lhs, const Bitset& rhs) - { - Bitset x(lhs); - BOOST_TEST((lhs & rhs) == (x &= rhs)); - } - - // operator^ - static - void operator_xor(const Bitset& lhs, const Bitset& rhs) - { - Bitset x(lhs); - BOOST_TEST((lhs ^ rhs) == (x ^= rhs)); - } - - // operator- - static - void operator_sub(const Bitset& lhs, const Bitset& rhs) - { - Bitset x(lhs); - BOOST_TEST((lhs - rhs) == (x -= rhs)); - } - -//------------------------------------------------------------------------------ -// I/O TESTS - // The following tests assume the results of extraction (i.e.: contents, - // state and width of is, contents of b) only depend on input (the string - // str). In other words, they don't consider "unexpected" errors such as - // stream corruption or out of memory. The reason is simple: if e.g. the - // stream buffer throws, the stream layer may eat the exception and - // transform it into a badbit. But we can't trust the stream state here, - // because one of the things that we want to test is exactly whether it - // is set correctly. Similarly for insertion. - // - // To provide for these cases would require that the test functions know - // in advance whether the stream buffer and/or allocations will fail, and - // when; that is, we should write both a special allocator and a special - // stream buffer capable of throwing "on demand" and pass them here. - - // Seems overkill for these kinds of unit tests. - //------------------------------------------------------------------------- - - // operator<<( [basic_]ostream, - template - static void stream_inserter(const Bitset & b, - Stream & s, - const char * file_name - ) - { - typedef typename Stream::char_type char_type; - typedef std::basic_string string_type; - typedef std::basic_ifstream corresponding_input_stream_type; - - std::ios::iostate except = s.exceptions(); - - typedef typename Bitset::size_type size_type; - std::streamsize w = s.width(); - char_type fill_char = s.fill(); - std::ios::iostate oldstate = s.rdstate(); - bool stream_was_good = s.good(); - - bool did_throw = false; - try { - s << b; - } - catch (const std::ios_base::failure &) { - BOOST_TEST((except & s.rdstate()) != 0); - did_throw = true; - } catch (...) { - did_throw = true; - } - - BOOST_TEST(did_throw || !stream_was_good || (s.width() == 0)); - - if (!stream_was_good) { - BOOST_TEST(s.good() == false); - - // this should actually be oldstate == s.rdstate() - // but some implementations add badbit in the - // sentry constructor - // - BOOST_TEST((oldstate & s.rdstate()) == oldstate); - BOOST_TEST(s.width() == w); - } - else { - if(!did_throw) - BOOST_TEST(s.width() == 0); - // This test require that os be an output _and_ input stream. - // Of course dynamic_bitset's operator << doesn't require that. - - size_type total_len = w <= 0 || static_cast(w) < b.size()? b.size() : static_cast(w); - const string_type padding (total_len - b.size(), fill_char); - string_type expected; - boost::to_string(b, expected); - if ((s.flags() & std::ios::adjustfield) != std::ios::left) - expected = padding + expected; - else - expected = expected + padding; - - assert(expected.length() == total_len); - - // close, and reopen the file stream to verify contents - s.close(); - corresponding_input_stream_type is(file_name); - string_type contents; - std::getline(is, contents, char_type()); - BOOST_TEST(contents == expected); - } - } - - // operator>>( [basic_]istream - template - static void stream_extractor(Bitset& b, - Stream& is, - const String& str - ) - { - // save necessary info then do extraction - // - const std::streamsize w = is.width(); - Bitset a_copy(b); - bool stream_was_good = is.good(); - - bool did_throw = false; - - const std::ios::iostate except = is.exceptions(); - bool has_stream_exceptions = true; - try { - is >> b; - } - catch(const std::ios::failure &) { - did_throw = true; - } - - // postconditions - BOOST_TEST(except == is.exceptions()); // paranoid - //------------------------------------------------------------------ - - // postconditions - BOOST_TEST(b.size() <= b.max_size()); - if(w > 0) - BOOST_TEST(b.size() <= static_cast(w)); - - // throw if and only if required - if(has_stream_exceptions) { - const bool exceptional_state = has_flags(is, is.exceptions()); - BOOST_TEST(exceptional_state == did_throw); - } - - typedef typename String::size_type size_type; - typedef typename String::value_type Ch; - size_type after_digits = 0; - - if(!stream_was_good) { - BOOST_TEST(has_flags(is, std::ios::failbit)); - BOOST_TEST(b == a_copy); - BOOST_TEST(is.width() == (did_throw ? w : 0)); - } - else { - // stream was good(), parse the string; - // it may contain three parts, all of which are optional - // {spaces} {digits} {non-digits} - // opt opt opt - // - // The values of b.max_size() and is.width() may lead to - // ignore part of the digits, if any. - - size_type pos = 0; - size_type len = str.length(); - // {spaces} - for( ; pos < len && is_white_space(is, str[pos]); ++pos) - {} - size_type after_spaces = pos; - // {digits} or part of them - const typename Bitset::size_type max_digits = - w > 0 && static_cast(w) < b.max_size() - ? static_cast(w) : b.max_size(); - - for( ; pos < len && (pos - after_spaces) < max_digits; ++pos) { - if(!is_one_or_zero(is, str[pos])) - break; - } - after_digits = pos; - size_type num_digits = after_digits - after_spaces; - - // eofbit - if((after_digits == len && max_digits > num_digits )) - BOOST_TEST(has_flags(is, std::ios::eofbit)); - else - BOOST_TEST(!has_flags(is, std::ios::eofbit)); - - // failbit <=> there are no digits, except for the library - // issue explained below. - // - if(num_digits == 0) { - if(after_digits == len && has_stream_exceptions && - (is.exceptions() & std::ios::eofbit) != std::ios::goodbit) { - // This is a special case related to library issue 195: - // reaching eof when skipping whitespaces in the sentry ctor. - // The resolution says the sentry constructor should set *both* - // eofbit and failbit; but many implementations deliberately - // set eofbit only. See for instance: - // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00086.html - // - BOOST_TEST(did_throw); - + b.flip(); + + // b[i] = b[j] + for ( i = 0; i < b.size(); ++i ) { + b[ i ] = prev[ i ]; + for ( j = 0; j < b.size(); ++j ) { + if ( i == j ) + BOOST_TEST( b[ j ] == prev[ j ] ); + else + BOOST_TEST( b[ j ] == ! prev[ j ] ); } - else { - BOOST_TEST(has_flags(is, std::ios::failbit)); + b[ i ] = ! prev[ i ]; } - } - else - BOOST_TEST(!has_flags(is, std::ios::failbit)); - - if(num_digits == 0 && after_digits == len) { - // The VC6 library has a bug/non-conformity in the sentry - // constructor. It uses code like - // // skip whitespaces... - // int_type _C = rdbuf()->sgetc(); - // while (!_Tr::eq_int_type(_Tr::eof(), _C) ... - // - // For an empty file the while statement is never "entered" - // and the stream remains in good() state; thus the sentry - // object gives "true" when converted to bool. This is worse - // than the case above, because not only failbit is not set, - // but no bit is set at all, end we end up clearing the - // bitset though there's nothing in the file to be extracted. - // Note that the dynamic_bitset docs say a sentry object is - // constructed and then converted to bool, thus we rely on - // what the underlying library does. - // -#if !defined(BOOST_DINKUMWARE_STDLIB) || (BOOST_DINKUMWARE_STDLIB >= 306) - BOOST_TEST(b == a_copy); -#else - BOOST_TEST(b.empty() == true); -#endif - } - else { - String sub = str.substr(after_spaces, num_digits); - BOOST_TEST(b == Bitset(sub)); - } - - // check width - BOOST_TEST(is.width() == 0 - || (after_digits == len && num_digits == 0 && did_throw)); + // b[i].flip() + for ( i = 0; i < b.size(); ++i ) { + b[ i ].flip(); + for ( j = 0; j < b.size(); ++j ) { + if ( i == j ) + BOOST_TEST( b[ j ] == prev[ j ] ); + else + BOOST_TEST( b[ j ] == ! prev[ j ] ); + } + b[ i ].flip(); + } } + //=========================================================================== + // bitwise operators - // clear the stream to allow further reading then - // retrieve any remaining chars with a single getline() - is.exceptions(std::ios::goodbit); - is.clear(); - String remainder; - std::getline(is, remainder, Ch()); - if(stream_was_good) - BOOST_TEST(remainder == str.substr(after_digits)); - else - BOOST_TEST(remainder == str); + // bitwise and assignment - } + // PRE: b.size() == rhs.size() + static void + and_assignment( const Bitset & b, const Bitset & rhs ) + { + Bitset lhs( b ); + Bitset prev( lhs ); + lhs &= rhs; + // Clears each bit in lhs for which the corresponding bit in rhs is + // clear, and leaves all other bits unchanged. + for ( std::size_t I = 0; I < lhs.size(); ++I ) + if ( rhs[ I ] == 0 ) + BOOST_TEST( lhs[ I ] == 0 ); + else + BOOST_TEST( lhs[ I ] == prev[ I ] ); + } + // PRE: b.size() == rhs.size() + static void + or_assignment( const Bitset & b, const Bitset & rhs ) + { + Bitset lhs( b ); + Bitset prev( lhs ); + lhs |= rhs; + // Sets each bit in lhs for which the corresponding bit in rhs is set, and + // leaves all other bits unchanged. + for ( std::size_t I = 0; I < lhs.size(); ++I ) + if ( rhs[ I ] == 1 ) + BOOST_TEST( lhs[ I ] == 1 ); + else + BOOST_TEST( lhs[ I ] == prev[ I ] ); + } + // PRE: b.size() == rhs.size() + static void + xor_assignment( const Bitset & b, const Bitset & rhs ) + { + Bitset lhs( b ); + Bitset prev( lhs ); + lhs ^= rhs; + // Flips each bit in lhs for which the corresponding bit in rhs is set, + // and leaves all other bits unchanged. + for ( std::size_t I = 0; I < lhs.size(); ++I ) + if ( rhs[ I ] == 1 ) + BOOST_TEST( lhs[ I ] == ! prev[ I ] ); + else + BOOST_TEST( lhs[ I ] == prev[ I ] ); + } + + // PRE: b.size() == rhs.size() + static void + sub_assignment( const Bitset & b, const Bitset & rhs ) + { + Bitset lhs( b ); + Bitset prev( lhs ); + lhs -= rhs; + // Resets each bit in lhs for which the corresponding bit in rhs is set, + // and leaves all other bits unchanged. + for ( std::size_t I = 0; I < lhs.size(); ++I ) + if ( rhs[ I ] == 1 ) + BOOST_TEST( lhs[ I ] == 0 ); + else + BOOST_TEST( lhs[ I ] == prev[ I ] ); + } + + static void + shift_left_assignment( const Bitset & b, std::size_t pos ) + { + Bitset lhs( b ); + Bitset prev( lhs ); + lhs <<= pos; + // Replaces each bit at position I in lhs with the following value: + // - If I < pos, the new value is zero + // - If I >= pos, the new value is the previous value of the bit at + // position I - pos + for ( std::size_t I = 0; I < lhs.size(); ++I ) + if ( I < pos ) + BOOST_TEST( lhs[ I ] == 0 ); + else + BOOST_TEST( lhs[ I ] == prev[ I - pos ] ); + } + + static void + shift_right_assignment( const Bitset & b, std::size_t pos ) + { + Bitset lhs( b ); + Bitset prev( lhs ); + lhs >>= pos; + // Replaces each bit at position I in lhs with the following value: + // - If pos >= N - I, the new value is zero + // - If pos < N - I, the new value is the previous value of the bit at + // position I + pos + std::size_t N = lhs.size(); + for ( std::size_t I = 0; I < N; ++I ) + if ( pos >= N - I ) + BOOST_TEST( lhs[ I ] == 0 ); + else + BOOST_TEST( lhs[ I ] == prev[ I + pos ] ); + } + + static void + set_all( const Bitset & b ) + { + Bitset lhs( b ); + lhs.set(); + for ( std::size_t I = 0; I < lhs.size(); ++I ) + BOOST_TEST( lhs[ I ] == 1 ); + } + + static void + set_one( const Bitset & b, std::size_t pos, bool value ) + { + Bitset lhs( b ); + std::size_t N = lhs.size(); + if ( pos < N ) { + Bitset prev( lhs ); + // Stores a new value in the bit at position pos in lhs. + lhs.set( pos, value ); + BOOST_TEST( lhs[ pos ] == value ); + + // All other values of lhs remain unchanged + for ( std::size_t I = 0; I < N; ++I ) + if ( I != pos ) + BOOST_TEST( lhs[ I ] == prev[ I ] ); + } else { + // Not in range, doesn't satisfy precondition. + } + } + + static void + set_segment( const Bitset & b, std::size_t pos, std::size_t len, bool value ) + { + Bitset lhs( b ); + std::size_t N = lhs.size(); + Bitset prev( lhs ); + lhs.set( pos, len, value ); + for ( std::size_t I = 0; I < N; ++I ) { + if ( I < pos || I >= pos + len ) + BOOST_TEST( lhs[ I ] == prev[ I ] ); + else + BOOST_TEST( lhs[ I ] == value ); + } + } + + static void + reset_all( const Bitset & b ) + { + Bitset lhs( b ); + // Resets all bits in lhs + lhs.reset(); + for ( std::size_t I = 0; I < lhs.size(); ++I ) + BOOST_TEST( lhs[ I ] == 0 ); + } + + static void + reset_one( const Bitset & b, std::size_t pos ) + { + Bitset lhs( b ); + std::size_t N = lhs.size(); + if ( pos < N ) { + Bitset prev( lhs ); + lhs.reset( pos ); + // Resets the bit at position pos in lhs + BOOST_TEST( lhs[ pos ] == 0 ); + + // All other values of lhs remain unchanged + for ( std::size_t I = 0; I < N; ++I ) + if ( I != pos ) + BOOST_TEST( lhs[ I ] == prev[ I ] ); + } else { + // Not in range, doesn't satisfy precondition. + } + } + + static void + reset_segment( const Bitset & b, std::size_t pos, std::size_t len ) + { + Bitset lhs( b ); + std::size_t N = lhs.size(); + Bitset prev( lhs ); + lhs.reset( pos, len ); + for ( std::size_t I = 0; I < N; ++I ) { + if ( I < pos || I >= pos + len ) + BOOST_TEST( lhs[ I ] == prev[ I ] ); + else + BOOST_TEST( ! lhs[ I ] ); + } + } + + static void + operator_flip( const Bitset & b ) + { + Bitset lhs( b ); + Bitset x( lhs ); + BOOST_TEST( ~lhs == x.flip() ); + } + + static void + flip_all( const Bitset & b ) + { + Bitset lhs( b ); + std::size_t N = lhs.size(); + Bitset prev( lhs ); + lhs.flip(); + // Toggles all the bits in lhs + for ( std::size_t I = 0; I < N; ++I ) + BOOST_TEST( lhs[ I ] == ! prev[ I ] ); + } + + static void + flip_one( const Bitset & b, std::size_t pos ) + { + Bitset lhs( b ); + std::size_t N = lhs.size(); + if ( pos < N ) { + Bitset prev( lhs ); + lhs.flip( pos ); + // Toggles the bit at position pos in lhs + BOOST_TEST( lhs[ pos ] == ! prev[ pos ] ); + + // All other values of lhs remain unchanged + for ( std::size_t I = 0; I < N; ++I ) + if ( I != pos ) + BOOST_TEST( lhs[ I ] == prev[ I ] ); + } else { + // Not in range, doesn't satisfy precondition. + } + } + + static void + flip_segment( const Bitset & b, std::size_t pos, std::size_t len ) + { + Bitset lhs( b ); + std::size_t N = lhs.size(); + Bitset prev( lhs ); + lhs.flip( pos, len ); + for ( std::size_t I = 0; I < N; ++I ) { + if ( I < pos || I >= pos + len ) + BOOST_TEST( lhs[ I ] == prev[ I ] ); + else + BOOST_TEST( lhs[ I ] != prev[ I ] ); + } + } + + // empty + static void + empty( const Bitset & b ) + { + BOOST_TEST( b.empty() == ( b.size() == 0 ) ); + } + + // to_ulong() + static void + to_ulong( const Bitset & lhs ) + { + typedef unsigned long result_type; + std::size_t n = std::numeric_limits< result_type >::digits; + std::size_t sz = lhs.size(); + + bool will_overflow = false; + for ( std::size_t i = n; i < sz; ++i ) { + if ( lhs.test( i ) != 0 ) { + will_overflow = true; + break; + } + } + if ( will_overflow ) { + try { + (void)lhs.to_ulong(); + BOOST_TEST( false ); // It should have thrown an exception + } catch ( std::overflow_error & ex ) { + // Good! + BOOST_TEST( ! ! ex.what() ); + } catch ( ... ) { + BOOST_TEST( false ); // threw the wrong exception + } + } else { + result_type num = lhs.to_ulong(); + // Be sure the number is right + if ( sz == 0 ) + BOOST_TEST( num == 0 ); + else { + for ( std::size_t i = 0; i < sz; ++i ) + BOOST_TEST( lhs[ i ] == ( i < n ? nth_bit( num, i ) : 0 ) ); + } + } + } + + // to_string() + static void + to_string( const Bitset & b ) + { + std::string str; + boost::to_string( b, str ); + BOOST_TEST( str.size() == b.size() ); + for ( std::size_t i = 0; i < b.size(); ++i ) + BOOST_TEST( str[ b.size() - 1 - i ] == ( b.test( i ) ? '1' : '0' ) ); + } + + static void + count( const Bitset & b ) + { + std::size_t c = b.count(); + std::size_t actual = 0; + for ( std::size_t i = 0; i < b.size(); ++i ) + if ( b[ i ] ) + ++actual; + BOOST_TEST( c == actual ); + } + + static void + size( const Bitset & b ) + { + BOOST_TEST( Bitset( b ).set().count() == b.size() ); + } + + static void + capacity_test_one( const Bitset & lhs ) + { + // empty bitset + Bitset b( lhs ); + BOOST_TEST( b.capacity() == 0 ); + } + + static void + capacity_test_two( const Bitset & lhs ) + { + // bitset constructed with size "100" + Bitset b( lhs ); + BOOST_TEST( b.capacity() >= 100 ); + b.resize( 200 ); + BOOST_TEST( b.capacity() >= 200 ); + } + + static void + reserve_test_one( const Bitset & lhs ) + { + // empty bitset + Bitset b( lhs ); + b.reserve( 16 ); + BOOST_TEST( b.capacity() >= 16 ); + } + + static void + reserve_test_two( const Bitset & lhs ) + { + // bitset constructed with size "100" + Bitset b( lhs ); + BOOST_TEST( b.capacity() >= 100 ); + b.reserve( 60 ); + BOOST_TEST( b.size() == 100 ); + BOOST_TEST( b.capacity() >= 100 ); + b.reserve( 160 ); + BOOST_TEST( b.size() == 100 ); + BOOST_TEST( b.capacity() >= 160 ); + } + + static void + shrink_to_fit_test_one( const Bitset & lhs ) + { + // empty bitset + Bitset b( lhs ); + b.shrink_to_fit(); + BOOST_TEST( b.size() == 0 ); + BOOST_TEST( b.capacity() == 0 ); + } + + static void + shrink_to_fit_test_two( const Bitset & lhs ) + { + // bitset constructed with size "100" + Bitset b( lhs ); + b.shrink_to_fit(); + BOOST_TEST( b.capacity() >= 100 ); + BOOST_TEST( b.size() == 100 ); + b.reserve( 200 ); + BOOST_TEST( b.capacity() >= 200 ); + BOOST_TEST( b.size() == 100 ); + b.shrink_to_fit(); + BOOST_TEST( b.capacity() < 200 ); + BOOST_TEST( b.size() == 100 ); + } + + static void + all( const Bitset & b ) + { + BOOST_TEST( b.all() == ( b.count() == b.size() ) ); + bool result = true; + for ( std::size_t i = 0; i < b.size(); ++i ) + if ( ! b[ i ] ) { + result = false; + break; + } + BOOST_TEST( b.all() == result ); + } + + static void + any( const Bitset & b ) + { + BOOST_TEST( b.any() == ( b.count() != 0 ) ); + bool result = false; + for ( std::size_t i = 0; i < b.size(); ++i ) + if ( b[ i ] ) { + result = true; + break; + } + BOOST_TEST( b.any() == result ); + } + + static void + none( const Bitset & b ) + { + bool result = true; + for ( std::size_t i = 0; i < b.size(); ++i ) { + if ( b[ i ] ) { + result = false; + break; + } + } + BOOST_TEST( b.none() == result ); + + // sanity + BOOST_TEST( b.none() == ! b.any() ); + BOOST_TEST( b.none() == ( b.count() == 0 ) ); + } + + static void + subset( const Bitset & a, const Bitset & b ) + { + BOOST_TEST( a.size() == b.size() ); // PRE + + bool is_subset = true; + if ( b.size() ) { // could use b.any() but let's be safe + for ( std::size_t i = 0; i < a.size(); ++i ) { + if ( a.test( i ) && ! b.test( i ) ) { + is_subset = false; + break; + } + } + } else { + // sanity + BOOST_TEST( a.count() == 0 ); + BOOST_TEST( a.any() == false ); + + // is_subset = (a.any() == false); + } + + BOOST_TEST( a.is_subset_of( b ) == is_subset ); + } + + static void + proper_subset( const Bitset & a, const Bitset & b ) + { + // PRE: a.size() == b.size() + BOOST_TEST( a.size() == b.size() ); + + bool is_proper = false; + + if ( b.size() != 0 ) { + // check it's a subset + subset( a, b ); + + // is it proper? + for ( std::size_t i = 0; i < a.size(); ++i ) { + if ( ! a.test( i ) && b.test( i ) ) { + is_proper = true; + // sanity + BOOST_TEST( a.count() < b.count() ); + BOOST_TEST( b.any() ); + } + } + } + + BOOST_TEST( a.is_proper_subset_of( b ) == is_proper ); + if ( is_proper ) + BOOST_TEST( b.is_proper_subset_of( a ) != is_proper ); // antisymmetry + } + + static void + intersects( const Bitset & a, const Bitset & b ) + { + bool have_intersection = false; + + typename Bitset::size_type m = a.size() < b.size() ? a.size() : b.size(); + for ( typename Bitset::size_type i = 0; i < m && ! have_intersection; ++i ) + if ( a[ i ] == true && b[ i ] == true ) + have_intersection = true; + + BOOST_TEST( a.intersects( b ) == have_intersection ); + // also check commutativity + BOOST_TEST( b.intersects( a ) == have_intersection ); + } + + static void + find_first( const Bitset & b, typename Bitset::size_type offset = 0 ) + { + // find first non-null bit from offset onwards, if any + typename Bitset::size_type i = offset; + while ( i < b.size() && b[ i ] == 0 ) + ++i; + + if ( i >= b.size() ) + BOOST_TEST( b.find_first( offset ) == Bitset::npos ); // not found; + else { + BOOST_TEST( b.find_first( offset ) == i ); + BOOST_TEST( b.test( i ) == true ); + } + } + + static void + find_pos( const Bitset & b, typename Bitset::size_type pos ) + { + find_first( b, pos ); + BOOST_TEST( next_bit_on( b, pos ) == b.find_next( pos ) ); + } + + static void + operator_equal( const Bitset & a, const Bitset & b ) + { + if ( a == b ) { + for ( std::size_t I = 0; I < a.size(); ++I ) + BOOST_TEST( a[ I ] == b[ I ] ); + } else { + if ( a.size() == b.size() ) { + bool diff = false; + for ( std::size_t I = 0; I < a.size(); ++I ) + if ( a[ I ] != b[ I ] ) { + diff = true; + break; + } + BOOST_TEST( diff ); + } + } + } + + static void + operator_not_equal( const Bitset & a, const Bitset & b ) + { + if ( a != b ) { + if ( a.size() == b.size() ) { + bool diff = false; + for ( std::size_t I = 0; I < a.size(); ++I ) + if ( a[ I ] != b[ I ] ) { + diff = true; + break; + } + BOOST_TEST( diff ); + } + } else { + for ( std::size_t I = 0; I < a.size(); ++I ) + BOOST_TEST( a[ I ] == b[ I ] ); + } + } + + static bool + less_than( const Bitset & a, const Bitset & b ) + { + typedef BOOST_DEDUCED_TYPENAME Bitset::size_type size_type; + + size_type asize( a.size() ); + size_type bsize( b.size() ); + + if ( ! bsize ) { + return false; + } else if ( ! asize ) { + return true; + } else { + // Compare from most significant to least. + + size_type leqsize( std::min BOOST_PREVENT_MACRO_SUBSTITUTION( asize, bsize ) ); + size_type I; + for ( I = 0; I < leqsize; ++I, --asize, --bsize ) { + size_type i = asize - 1; + size_type j = bsize - 1; + + if ( a[ i ] < b[ j ] ) + return true; + else if ( a[ i ] > b[ j ] ) + return false; + // if (a[i] = b[j]) skip to next + } + return ( a.size() < b.size() ); + } + } + + static typename Bitset::size_type + next_bit_on( const Bitset & b, typename Bitset::size_type prev ) + { + // helper function for find_next() + // + + if ( b.none() == true || prev == Bitset::npos ) + return Bitset::npos; + + ++prev; + + if ( prev >= b.size() ) + return Bitset::npos; + + typename Bitset::size_type i = prev; + while ( i < b.size() && b[ i ] == 0 ) + ++i; + + return i == b.size() ? Bitset::npos : i; + } + + static void + operator_less_than( const Bitset & a, const Bitset & b ) + { + if ( less_than( a, b ) ) + BOOST_TEST( a < b ); + else + BOOST_TEST( ! ( a < b ) ); + } + + static void + operator_greater_than( const Bitset & a, const Bitset & b ) + { + if ( less_than( a, b ) || a == b ) + BOOST_TEST( ! ( a > b ) ); + else + BOOST_TEST( a > b ); + } + + static void + operator_less_than_eq( const Bitset & a, const Bitset & b ) + { + if ( less_than( a, b ) || a == b ) + BOOST_TEST( a <= b ); + else + BOOST_TEST( ! ( a <= b ) ); + } + + static void + operator_greater_than_eq( const Bitset & a, const Bitset & b ) + { + if ( less_than( a, b ) ) + BOOST_TEST( ! ( a >= b ) ); + else + BOOST_TEST( a >= b ); + } + + static void + test_bit( const Bitset & b, std::size_t pos ) + { + Bitset lhs( b ); + std::size_t N = lhs.size(); + if ( pos < N ) { + BOOST_TEST( lhs.test( pos ) == lhs[ pos ] ); + } else { + // Not in range, doesn't satisfy precondition. + } + } + + static void + test_set_bit( const Bitset & b, std::size_t pos, bool value ) + { + Bitset lhs( b ); + std::size_t N = lhs.size(); + if ( pos < N ) { + Bitset prev( lhs ); + // Stores a new value in the bit at position pos in lhs. + BOOST_TEST( lhs.test_set( pos, value ) == prev[ pos ] ); + BOOST_TEST( lhs[ pos ] == value ); + + // All other values of lhs remain unchanged + for ( std::size_t I = 0; I < N; ++I ) + if ( I != pos ) + BOOST_TEST( lhs[ I ] == prev[ I ] ); + } else { + // Not in range, doesn't satisfy precondition. + } + } + + static void + operator_shift_left( const Bitset & lhs, std::size_t pos ) + { + Bitset x( lhs ); + BOOST_TEST( ( lhs << pos ) == ( x <<= pos ) ); + } + + static void + operator_shift_right( const Bitset & lhs, std::size_t pos ) + { + Bitset x( lhs ); + BOOST_TEST( ( lhs >> pos ) == ( x >>= pos ) ); + } + + static void + at( const Bitset & lhs, const std::vector< bool > & bit_vec ) + { + Bitset b( lhs ); + std::size_t i, j, k; + + // x = b.at(i) + // x = ~b.at(i) + for ( i = 0; i < b.size(); ++i ) { + bool x = b.at( i ); + BOOST_TEST( x == bit_vec.at( i ) ); + x = ~b.at( i ); + BOOST_TEST( x == ! bit_vec.at( i ) ); + } + Bitset prev( b ); + + // b.at(i) = x + for ( j = 0; j < b.size(); ++j ) { + bool x = ! prev.at( j ); + b.at( j ) = x; + for ( k = 0; k < b.size(); ++k ) + if ( j == k ) + BOOST_TEST( b.at( k ) == x ); + else + BOOST_TEST( b.at( k ) == prev.at( k ) ); + b.at( j ) = prev.at( j ); + } + b.flip(); + + // b.at(i) = b.at(j) + for ( i = 0; i < b.size(); ++i ) { + b.at( i ) = prev.at( i ); + for ( j = 0; j < b.size(); ++j ) { + if ( i == j ) + BOOST_TEST( b.at( j ) == prev.at( j ) ); + else + BOOST_TEST( b.at( j ) == ! prev.at( j ) ); + } + b.at( i ) = ! prev.at( i ); + } + + // b.at(i).flip() + for ( i = 0; i < b.size(); ++i ) { + b.at( i ).flip(); + for ( j = 0; j < b.size(); ++j ) { + if ( i == j ) + BOOST_TEST( b.at( j ) == prev.at( j ) ); + else + BOOST_TEST( b.at( j ) == ! prev.at( j ) ); + } + b.at( i ).flip(); + } + + // b.at(b.size()) + bool will_out_of_range = false; + for ( i = 0; i <= b.size(); ++i ) { + if ( i == b.size() ) { + will_out_of_range = true; + break; + } + b.at( i ); + } + if ( will_out_of_range ) { + try { + b.at( b.size() ); + BOOST_TEST( false ); // It should have thrown an exception + } catch ( const std::out_of_range & ex ) { + // Good! + BOOST_TEST( ! ! ex.what() ); + } catch ( ... ) { + BOOST_TEST( false ); // threw the wrong exception + } + } + } + + // operator| + static void + operator_or( const Bitset & lhs, const Bitset & rhs ) + { + Bitset x( lhs ); + BOOST_TEST( ( lhs | rhs ) == ( x |= rhs ) ); + } + + // operator& + static void + operator_and( const Bitset & lhs, const Bitset & rhs ) + { + Bitset x( lhs ); + BOOST_TEST( ( lhs & rhs ) == ( x &= rhs ) ); + } + + // operator^ + static void + operator_xor( const Bitset & lhs, const Bitset & rhs ) + { + Bitset x( lhs ); + BOOST_TEST( ( lhs ^ rhs ) == ( x ^= rhs ) ); + } + + // operator- + static void + operator_sub( const Bitset & lhs, const Bitset & rhs ) + { + Bitset x( lhs ); + BOOST_TEST( ( lhs - rhs ) == ( x -= rhs ) ); + } + + //------------------------------------------------------------------------------ + // I/O TESTS + // The following tests assume the results of extraction (i.e.: contents, + // state and width of is, contents of b) only depend on input (the string + // str). In other words, they don't consider "unexpected" errors such as + // stream corruption or out of memory. The reason is simple: if e.g. the + // stream buffer throws, the stream layer may eat the exception and + // transform it into a badbit. But we can't trust the stream state here, + // because one of the things that we want to test is exactly whether it + // is set correctly. Similarly for insertion. + // + // To provide for these cases would require that the test functions know + // in advance whether the stream buffer and/or allocations will fail, and + // when; that is, we should write both a special allocator and a special + // stream buffer capable of throwing "on demand" and pass them here. + + // Seems overkill for these kinds of unit tests. + //------------------------------------------------------------------------- + + // operator<<( [basic_]ostream, + template< typename Stream > + static void + stream_inserter( const Bitset & b, Stream & s, const char * file_name ) + { + typedef typename Stream::char_type char_type; + typedef std::basic_string< char_type > string_type; + typedef std::basic_ifstream< char_type > corresponding_input_stream_type; + + std::ios::iostate except = s.exceptions(); + + typedef typename Bitset::size_type size_type; + std::streamsize w = s.width(); + char_type fill_char = s.fill(); + std::ios::iostate oldstate = s.rdstate(); + bool stream_was_good = s.good(); + + bool did_throw = false; + try { + s << b; + } catch ( const std::ios_base::failure & ) { + BOOST_TEST( ( except & s.rdstate() ) != 0 ); + did_throw = true; + } catch ( ... ) { + did_throw = true; + } + + BOOST_TEST( did_throw || ! stream_was_good || ( s.width() == 0 ) ); + + if ( ! stream_was_good ) { + BOOST_TEST( s.good() == false ); + + // this should actually be oldstate == s.rdstate() + // but some implementations add badbit in the + // sentry constructor + // + BOOST_TEST( ( oldstate & s.rdstate() ) == oldstate ); + BOOST_TEST( s.width() == w ); + } else { + if ( ! did_throw ) + BOOST_TEST( s.width() == 0 ); + // This test require that os be an output _and_ input stream. + // Of course dynamic_bitset's operator << doesn't require that. + + size_type total_len = w <= 0 || static_cast< size_type >( w ) < b.size() ? b.size() : static_cast< size_type >( w ); + const string_type padding( total_len - b.size(), fill_char ); + string_type expected; + boost::to_string( b, expected ); + if ( ( s.flags() & std::ios::adjustfield ) != std::ios::left ) + expected = padding + expected; + else + expected = expected + padding; + + assert( expected.length() == total_len ); + + // close, and reopen the file stream to verify contents + s.close(); + corresponding_input_stream_type is( file_name ); + string_type contents; + std::getline( is, contents, char_type() ); + BOOST_TEST( contents == expected ); + } + } + + // operator>>( [basic_]istream + template< typename Stream, typename String > + static void + stream_extractor( Bitset & b, Stream & is, const String & str ) + { + // save necessary info then do extraction + // + const std::streamsize w = is.width(); + Bitset a_copy( b ); + bool stream_was_good = is.good(); + + bool did_throw = false; + + const std::ios::iostate except = is.exceptions(); + bool has_stream_exceptions = true; + try { + is >> b; + } catch ( const std::ios::failure & ) { + did_throw = true; + } + + // postconditions + BOOST_TEST( except == is.exceptions() ); // paranoid + //------------------------------------------------------------------ + + // postconditions + BOOST_TEST( b.size() <= b.max_size() ); + if ( w > 0 ) + BOOST_TEST( b.size() <= static_cast< typename Bitset::size_type >( w ) ); + + // throw if and only if required + if ( has_stream_exceptions ) { + const bool exceptional_state = has_flags( is, is.exceptions() ); + BOOST_TEST( exceptional_state == did_throw ); + } + + typedef typename String::size_type size_type; + typedef typename String::value_type Ch; + size_type after_digits = 0; + + if ( ! stream_was_good ) { + BOOST_TEST( has_flags( is, std::ios::failbit ) ); + BOOST_TEST( b == a_copy ); + BOOST_TEST( is.width() == ( did_throw ? w : 0 ) ); + } else { + // stream was good(), parse the string; + // it may contain three parts, all of which are optional + // {spaces} {digits} {non-digits} + // opt opt opt + // + // The values of b.max_size() and is.width() may lead to + // ignore part of the digits, if any. + + size_type pos = 0; + size_type len = str.length(); + // {spaces} + for ( ; pos < len && is_white_space( is, str[ pos ] ); ++pos ) { + } + size_type after_spaces = pos; + // {digits} or part of them + const typename Bitset::size_type max_digits = + w > 0 && static_cast< typename Bitset::size_type >( w ) < b.max_size() + ? static_cast< typename Bitset::size_type >( w ) + : b.max_size(); + + for ( ; pos < len && ( pos - after_spaces ) < max_digits; ++pos ) { + if ( ! is_one_or_zero( is, str[ pos ] ) ) + break; + } + after_digits = pos; + size_type num_digits = after_digits - after_spaces; + + // eofbit + if ( ( after_digits == len && max_digits > num_digits ) ) + BOOST_TEST( has_flags( is, std::ios::eofbit ) ); + else + BOOST_TEST( ! has_flags( is, std::ios::eofbit ) ); + + // failbit <=> there are no digits, except for the library + // issue explained below. + // + if ( num_digits == 0 ) { + if ( after_digits == len && has_stream_exceptions && ( is.exceptions() & std::ios::eofbit ) != std::ios::goodbit ) { + // This is a special case related to library issue 195: + // reaching eof when skipping whitespaces in the sentry ctor. + // The resolution says the sentry constructor should set *both* + // eofbit and failbit; but many implementations deliberately + // set eofbit only. See for instance: + // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00086.html + // + BOOST_TEST( did_throw ); + + } else { + BOOST_TEST( has_flags( is, std::ios::failbit ) ); + } + } else + BOOST_TEST( ! has_flags( is, std::ios::failbit ) ); + + if ( num_digits == 0 && after_digits == len ) { + // The VC6 library has a bug/non-conformity in the sentry + // constructor. It uses code like + // // skip whitespaces... + // int_type _C = rdbuf()->sgetc(); + // while (!_Tr::eq_int_type(_Tr::eof(), _C) ... + // + // For an empty file the while statement is never "entered" + // and the stream remains in good() state; thus the sentry + // object gives "true" when converted to bool. This is worse + // than the case above, because not only failbit is not set, + // but no bit is set at all, end we end up clearing the + // bitset though there's nothing in the file to be extracted. + // Note that the dynamic_bitset docs say a sentry object is + // constructed and then converted to bool, thus we rely on + // what the underlying library does. + // +#if ! defined( BOOST_DINKUMWARE_STDLIB ) || ( BOOST_DINKUMWARE_STDLIB >= 306 ) + BOOST_TEST( b == a_copy ); +#else + BOOST_TEST( b.empty() == true ); +#endif + } else { + String sub = str.substr( after_spaces, num_digits ); + BOOST_TEST( b == Bitset( sub ) ); + } + + // check width + BOOST_TEST( is.width() == 0 || ( after_digits == len && num_digits == 0 && did_throw ) ); + } + + // clear the stream to allow further reading then + // retrieve any remaining chars with a single getline() + is.exceptions( std::ios::goodbit ); + is.clear(); + String remainder; + std::getline( is, remainder, Ch() ); + if ( stream_was_good ) + BOOST_TEST( remainder == str.substr( after_digits ) ); + else + BOOST_TEST( remainder == str ); + } }; - - #endif // include guard diff --git a/test/dyn_bitset_unit_tests1.cpp b/test/dyn_bitset_unit_tests1.cpp index ec83cbc..00a12c2 100644 --- a/test/dyn_bitset_unit_tests1.cpp +++ b/test/dyn_bitset_unit_tests1.cpp @@ -19,540 +19,533 @@ #include "boost/dynamic_bitset/dynamic_bitset.hpp" #include "boost/limits.hpp" -#if !defined(BOOST_NO_CXX11_ALLOCATOR) -#include +#if ! defined( BOOST_NO_CXX11_ALLOCATOR ) +# include -template -class minimal_allocator { +template< class T > +class minimal_allocator +{ public: typedef T value_type; - minimal_allocator() {} - - template - minimal_allocator(const minimal_allocator&) {} - - T* allocate(std::size_t n) { - void* p = std::malloc(sizeof(T) * n); - if (!p) { - throw std::bad_alloc(); - } - return static_cast(p); + minimal_allocator() + { } - void deallocate(T* p, std::size_t) { - std::free(p); + template< typename U > + minimal_allocator( const minimal_allocator< U > & ) + { + } + + T * + allocate( std::size_t n ) + { + void * p = std::malloc( sizeof( T ) * n ); + if ( ! p ) { + throw std::bad_alloc(); + } + return static_cast< T * >( p ); + } + + void + deallocate( T * p, std::size_t ) + { + std::free( p ); } }; #endif -#define BOOST_BITSET_TEST_COUNT(x) (sizeof(x)/sizeof(x[0])) - +#define BOOST_BITSET_TEST_COUNT( x ) ( sizeof( x ) / sizeof( x[ 0 ] ) ) // Codewarrior 8.3 for Win fails without this. // Thanks Howard Hinnant ;) -#if defined __MWERKS__ && BOOST_WORKAROUND(__MWERKS__, <= 0x3003) // 8.x -# pragma parse_func_templ off +#if defined __MWERKS__ && BOOST_WORKAROUND( __MWERKS__, <= 0x3003 ) // 8.x +# pragma parse_func_templ off #endif - -template -void run_string_tests(const String& s - BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Tests) - ) +template< typename Tests, typename String > +void +run_string_tests( const String & s BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE( Tests ) ) { + const std::size_t len = s.length(); + const std::size_t step = len / 4 ? len / 4 : 1; - const std::size_t len = s.length(); - const std::size_t step = len/4 ? len/4 : 1; - - // bitset length determined by the string-related arguments - std::size_t i; - for (i = 0; i <= len/2 ; i += step) { - Tests::from_string(s, i, len/2); // len/2 - i bits - Tests::from_string(s, i, len); // len - i bits - Tests::from_string(s, i, 1 + len*2); // len - i bits - } - - // bitset length explicitly specified - for (i = 0; i <= len/2; i += step) { - for (std::size_t sz = 0; sz <= len*4; sz+= step*2) { - Tests::from_string(s, i, len/2, sz); - Tests::from_string(s, i, len, sz); - Tests::from_string(s, i, 1 + len*2, sz); - - } - } + // bitset length determined by the string-related arguments + std::size_t i; + for ( i = 0; i <= len / 2; i += step ) { + Tests::from_string( s, i, len / 2 ); // len/2 - i bits + Tests::from_string( s, i, len ); // len - i bits + Tests::from_string( s, i, 1 + len * 2 ); // len - i bits + } + // bitset length explicitly specified + for ( i = 0; i <= len / 2; i += step ) { + for ( std::size_t sz = 0; sz <= len * 4; sz += step * 2 ) { + Tests::from_string( s, i, len / 2, sz ); + Tests::from_string( s, i, len, sz ); + Tests::from_string( s, i, 1 + len * 2, sz ); + } + } } // tests the do-the-right-thing constructor dispatch -template -void run_numeric_ctor_tests( BOOST_EXPLICIT_TEMPLATE_TYPE(Tests) - BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(T) ) +template< typename Tests, typename T > +void +run_numeric_ctor_tests( BOOST_EXPLICIT_TEMPLATE_TYPE( Tests ) + BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE( T ) ) { - const int bits_per_block = Tests::bits_per_block; - const int width = std::numeric_limits::digits; - const T ma = (std::numeric_limits::max)(); - const T mi = (std::numeric_limits::min)(); + const int width = std::numeric_limits< T >::digits; + const T ma = ( std::numeric_limits< T >::max )(); + const T mi = ( std::numeric_limits< T >::min )(); - int sizes[] = { - 0, 7*width/10, width, 13*width/10, 3*width, - 7*bits_per_block/10, bits_per_block, 13*bits_per_block/10, 3*bits_per_block + int sizes[] = { + 0, 7 * width / 10, width, 13 * width / 10, 3 * width, 7 * bits_per_block / 10, bits_per_block, 13 * bits_per_block / 10, 3 * bits_per_block }; const T numbers[] = { - T(-1), T(-3), T(-8), T(-15), T(mi/2), T(mi), - T(0), T(1), T(3), T(8), T(15), T(ma/2), T(ma) + T( -1 ), T( -3 ), T( -8 ), T( -15 ), T( mi / 2 ), T( mi ), T( 0 ), T( 1 ), T( 3 ), T( 8 ), T( 15 ), T( ma / 2 ), T( ma ) }; - for (std::size_t s = 0; s < BOOST_BITSET_TEST_COUNT(sizes); ++s) { - for (std::size_t n = 0; n < BOOST_BITSET_TEST_COUNT(numbers); ++n ) { + for ( std::size_t s = 0; s < BOOST_BITSET_TEST_COUNT( sizes ); ++s ) { + for ( std::size_t n = 0; n < BOOST_BITSET_TEST_COUNT( numbers ); ++n ) { + // can match ctor from ulong or templated one + Tests::from_unsigned_long( sizes[ s ], numbers[ n ] ); - // can match ctor from ulong or templated one - Tests::from_unsigned_long(sizes[s], numbers[n]); + typedef std::size_t compare_type; + const compare_type sz = sizes[ s ]; + // this condition is to be sure that size is representable in T, so + // that for signed T's we avoid implementation-defined behavior [if ma + // is larger than what std::size_t can hold then this is ok for our + // purposes: our sizes are anyhow < max(size_t)], which in turn could + // make the first argument of from_unsigned_long() a small negative, + // later converted to a very large unsigned. Example: signed 8-bit + // char (CHAR_MAX=127), bits_per_block=64, sz = 192 > 127. + const bool fits = + sz <= static_cast< compare_type >( ma ); - typedef std::size_t compare_type; - const compare_type sz = sizes[s]; - // this condition is to be sure that size is representable in T, so - // that for signed T's we avoid implementation-defined behavior [if ma - // is larger than what std::size_t can hold then this is ok for our - // purposes: our sizes are anyhow < max(size_t)], which in turn could - // make the first argument of from_unsigned_long() a small negative, - // later converted to a very large unsigned. Example: signed 8-bit - // char (CHAR_MAX=127), bits_per_block=64, sz = 192 > 127. - const bool fits = - sz <= static_cast(ma); - - if (fits) { - // can match templated ctor only (so we test dispatching) - Tests::from_unsigned_long(static_cast(sizes[s]), numbers[n]); - } - - } + if ( fits ) { + // can match templated ctor only (so we test dispatching) + Tests::from_unsigned_long( static_cast< T >( sizes[ s ] ), numbers[ n ] ); + } + } } - } - -template -void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) +template< typename Block > +void +run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE( Block ) ) { - typedef boost::dynamic_bitset bitset_type; - typedef bitset_test Tests; - const int bits_per_block = bitset_type::bits_per_block; + typedef boost::dynamic_bitset< Block > bitset_type; + typedef bitset_test< bitset_type > Tests; + const int bits_per_block = bitset_type::bits_per_block; - const std::string long_string = get_long_string(); - const Block all_1s = static_cast(-1); + const std::string long_string = get_long_string(); + const Block all_1s = static_cast< Block >( -1 ); - //===================================================================== - // Test construction from unsigned long - { - // NOTE: - // - // 1. keep this in sync with the numeric types supported - // for constructor dispatch (of course) - // 2. bool is tested separately; ugly and inelegant, but - // we don't have much time to think of a better solution - // which is likely to work on broken compilers - // - const int sizes[] = { - 0, 1, 3, - 7*bits_per_block/10, bits_per_block, 13*bits_per_block/10, 3*bits_per_block - }; + //===================================================================== + // Test construction from unsigned long + { + // NOTE: + // + // 1. keep this in sync with the numeric types supported + // for constructor dispatch (of course) + // 2. bool is tested separately; ugly and inelegant, but + // we don't have much time to think of a better solution + // which is likely to work on broken compilers + // + const int sizes[] = { + 0, 1, 3, 7 * bits_per_block / 10, bits_per_block, 13 * bits_per_block / 10, 3 * bits_per_block + }; - const bool values[] = { false, true }; + const bool values[] = { false, true }; - for (std::size_t s = 0; s < BOOST_BITSET_TEST_COUNT(sizes); ++s) { - for (std::size_t v = 0; v < BOOST_BITSET_TEST_COUNT(values); ++v) { - Tests::from_unsigned_long(sizes[s], values[v]); - Tests::from_unsigned_long(sizes[s] != 0, values[v]); - } + for ( std::size_t s = 0; s < BOOST_BITSET_TEST_COUNT( sizes ); ++s ) { + for ( std::size_t v = 0; v < BOOST_BITSET_TEST_COUNT( values ); ++v ) { + Tests::from_unsigned_long( sizes[ s ], values[ v ] ); + Tests::from_unsigned_long( sizes[ s ] != 0, values[ v ] ); + } + } + + run_numeric_ctor_tests< Tests, char >(); + +#if ! defined( BOOST_NO_INTRINSIC_WCHAR_T ) + run_numeric_ctor_tests< Tests, wchar_t >(); +#endif + + run_numeric_ctor_tests< Tests, signed char >(); + run_numeric_ctor_tests< Tests, short int >(); + run_numeric_ctor_tests< Tests, int >(); + run_numeric_ctor_tests< Tests, long int >(); + + run_numeric_ctor_tests< Tests, unsigned char >(); + run_numeric_ctor_tests< Tests, unsigned short >(); + run_numeric_ctor_tests< Tests, unsigned int >(); + run_numeric_ctor_tests< Tests, unsigned long >(); + +#if defined( BOOST_HAS_LONG_LONG ) + run_numeric_ctor_tests< Tests, ::boost::long_long_type >(); + run_numeric_ctor_tests< Tests, ::boost::ulong_long_type >(); +#endif + } + //===================================================================== + // Test construction from a string + { + run_string_tests< Tests >( std::string( "" ) ); // empty string + run_string_tests< Tests >( std::string( "1" ) ); + + run_string_tests< Tests >( long_string ); + +#if ! defined BOOST_NO_STD_WSTRING + // I need to decide what to do for non "C" locales here. On + // one hand I should have better tests. On the other one + // I don't want tests for dynamic_bitset to cope with locales, + // ctype::widen, etc. (but that's what you deserve when you + // don't separate concerns at the library level) + // + run_string_tests< Tests >( + std::wstring( L"11111000000111111111010101010101010101010111111" ) ); +#endif + + // Note that these are _valid_ arguments + Tests::from_string( std::string( "x11y" ), 1, 2 ); + Tests::from_string( std::string( "x11" ), 1, 10 ); + Tests::from_string( std::string( "x11" ), 1, 10, 10 ); + } + //===================================================================== + // test from_block_range + { + std::vector< Block > blocks; + Tests::from_block_range( blocks ); + } + { + std::vector< Block > blocks( 3 ); + blocks[ 0 ] = static_cast< Block >( 0 ); + blocks[ 1 ] = static_cast< Block >( 1 ); + blocks[ 2 ] = all_1s; + Tests::from_block_range( blocks ); + } + { + const unsigned int n = ( std::numeric_limits< unsigned char >::max )(); + std::vector< Block > blocks( n ); + for ( typename std::vector< Block >::size_type i = 0; i < n; ++i ) + blocks[ i ] = static_cast< Block >( i ); + Tests::from_block_range( blocks ); + } + //===================================================================== + // test to_block_range + { + bitset_type b; + Tests::to_block_range( b ); + } + { + bitset_type b( 1, 1ul ); + Tests::to_block_range( b ); + } + { + bitset_type b( long_string ); + Tests::to_block_range( b ); } - run_numeric_ctor_tests(); - -#if !defined(BOOST_NO_INTRINSIC_WCHAR_T) - run_numeric_ctor_tests(); -#endif - - run_numeric_ctor_tests(); - run_numeric_ctor_tests(); - run_numeric_ctor_tests(); - run_numeric_ctor_tests(); - - run_numeric_ctor_tests(); - run_numeric_ctor_tests(); - run_numeric_ctor_tests(); - run_numeric_ctor_tests(); - -#if defined(BOOST_HAS_LONG_LONG) - run_numeric_ctor_tests(); - run_numeric_ctor_tests(); -#endif - - } - //===================================================================== - // Test construction from a string - { - - run_string_tests(std::string("")); // empty string - run_string_tests(std::string("1")); - - run_string_tests(long_string); - -# if !defined BOOST_NO_STD_WSTRING - // I need to decide what to do for non "C" locales here. On - // one hand I should have better tests. On the other one - // I don't want tests for dynamic_bitset to cope with locales, - // ctype::widen, etc. (but that's what you deserve when you - // don't separate concerns at the library level) - // - run_string_tests( - std::wstring(L"11111000000111111111010101010101010101010111111")); -# endif - - // Note that these are _valid_ arguments - Tests::from_string(std::string("x11y"), 1, 2); - Tests::from_string(std::string("x11"), 1, 10); - Tests::from_string(std::string("x11"), 1, 10, 10); - - } - //===================================================================== - // test from_block_range - { - std::vector blocks; - Tests::from_block_range(blocks); - } - { - std::vector blocks(3); - blocks[0] = static_cast(0); - blocks[1] = static_cast(1); - blocks[2] = all_1s; - Tests::from_block_range(blocks); - } - { - const unsigned int n = (std::numeric_limits::max)(); - std::vector blocks(n); - for (typename std::vector::size_type i = 0; i < n; ++i) - blocks[i] = static_cast(i); - Tests::from_block_range(blocks); - } - //===================================================================== - // test to_block_range - { - bitset_type b; - Tests::to_block_range(b); - } - { - bitset_type b(1, 1ul); - Tests::to_block_range(b); - } - { - bitset_type b(long_string); - Tests::to_block_range(b); - } - - //===================================================================== - // Test copy constructor - { - boost::dynamic_bitset b; - Tests::copy_constructor(b); - } - { - boost::dynamic_bitset b(std::string("0")); - Tests::copy_constructor(b); - } - { - boost::dynamic_bitset b(long_string); - Tests::copy_constructor(b); - } - //===================================================================== - // Test copy assignment operator - { - bitset_type a, b; - Tests::copy_assignment_operator(a, b); - } - { - bitset_type a(std::string("1")), b(std::string("0")); - Tests::copy_assignment_operator(a, b); - } - { - bitset_type a(long_string), b(long_string); - Tests::copy_assignment_operator(a, b); - } - { - bitset_type a; - bitset_type b(long_string); // b greater than a, a empty - Tests::copy_assignment_operator(a, b); - } - { - bitset_type a(std::string("0")); - bitset_type b(long_string); // b greater than a - Tests::copy_assignment_operator(a, b); - } + //===================================================================== + // Test copy constructor + { + boost::dynamic_bitset< Block > b; + Tests::copy_constructor( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::copy_constructor( b ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::copy_constructor( b ); + } + //===================================================================== + // Test copy assignment operator + { + bitset_type a, b; + Tests::copy_assignment_operator( a, b ); + } + { + bitset_type a( std::string( "1" ) ), b( std::string( "0" ) ); + Tests::copy_assignment_operator( a, b ); + } + { + bitset_type a( long_string ), b( long_string ); + Tests::copy_assignment_operator( a, b ); + } + { + bitset_type a; + bitset_type b( long_string ); // b greater than a, a empty + Tests::copy_assignment_operator( a, b ); + } + { + bitset_type a( std::string( "0" ) ); + bitset_type b( long_string ); // b greater than a + Tests::copy_assignment_operator( a, b ); + } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES - //===================================================================== - // Test move constructor - { - boost::dynamic_bitset b; - Tests::move_constructor(b); - } - { - boost::dynamic_bitset b(std::string("0")); - Tests::move_constructor(b); - } - { - boost::dynamic_bitset b(long_string); - Tests::move_constructor(b); - } - //===================================================================== - // Test move assignment operator - { - bitset_type a, b; - Tests::move_assignment_operator(a, b); - } - { - bitset_type a(std::string("1")), b(std::string("0")); - Tests::move_assignment_operator(a, b); - } - { - bitset_type a(long_string), b(long_string); - Tests::move_assignment_operator(a, b); - } - { - bitset_type a; - bitset_type b(long_string); // b greater than a, a empty - Tests::move_assignment_operator(a, b); - } - { - bitset_type a(std::string("0")); - bitset_type b(long_string); // b greater than a - Tests::move_assignment_operator(a, b); - } + //===================================================================== + // Test move constructor + { + boost::dynamic_bitset< Block > b; + Tests::move_constructor( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::move_constructor( b ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::move_constructor( b ); + } + //===================================================================== + // Test move assignment operator + { + bitset_type a, b; + Tests::move_assignment_operator( a, b ); + } + { + bitset_type a( std::string( "1" ) ), b( std::string( "0" ) ); + Tests::move_assignment_operator( a, b ); + } + { + bitset_type a( long_string ), b( long_string ); + Tests::move_assignment_operator( a, b ); + } + { + bitset_type a; + bitset_type b( long_string ); // b greater than a, a empty + Tests::move_assignment_operator( a, b ); + } + { + bitset_type a( std::string( "0" ) ); + bitset_type b( long_string ); // b greater than a + Tests::move_assignment_operator( a, b ); + } #endif // BOOST_NO_CXX11_RVALUE_REFERENCES - //===================================================================== - // Test swap - { - bitset_type a; - bitset_type b(std::string("1")); - Tests::swap(a, b); - Tests::swap(b, a); - Tests::swap(a, a); - } - { - bitset_type a; - bitset_type b(long_string); - Tests::swap(a, b); - Tests::swap(b, a); - } - { - bitset_type a(std::string("0")); - bitset_type b(long_string); - Tests::swap(a, b); - Tests::swap(b, a); - Tests::swap(a, a); - Tests::swap(b, b); - } - //===================================================================== - // Test resize - { - boost::dynamic_bitset a; - Tests::resize(a); - } - { - boost::dynamic_bitset a(std::string("0")); - Tests::resize(a); - } - { - boost::dynamic_bitset a(std::string("1")); - Tests::resize(a); - } - { - boost::dynamic_bitset a(long_string); - Tests::resize(a); - } - //===================================================================== - // Test clear - { - boost::dynamic_bitset a; - Tests::clear(a); - } - { - boost::dynamic_bitset a(long_string); - Tests::clear(a); - } - //===================================================================== - // Test pop back - { - boost::dynamic_bitset a(std::string("01")); - Tests::pop_back(a); - } - { - boost::dynamic_bitset a(std::string("10")); - Tests::pop_back(a); - } - { - const int size_to_fill_all_blocks = 4 * bits_per_block; - boost::dynamic_bitset a(size_to_fill_all_blocks, 255ul); - Tests::pop_back(a); - } - { - boost::dynamic_bitset a(long_string); - Tests::pop_back(a); - } - //===================================================================== - // Test append bit - { - boost::dynamic_bitset a; - Tests::append_bit(a); - } - { - boost::dynamic_bitset a(std::string("0")); - Tests::append_bit(a); - } - { - boost::dynamic_bitset a(std::string("1")); - Tests::append_bit(a); - } - { - const int size_to_fill_all_blocks = 4 * bits_per_block; - boost::dynamic_bitset a(size_to_fill_all_blocks, 255ul); - Tests::append_bit(a); - } - { - boost::dynamic_bitset a(long_string); - Tests::append_bit(a); - } - //===================================================================== - // Test append block - { - boost::dynamic_bitset a; - Tests::append_block(a); - } - { - boost::dynamic_bitset a(std::string("0")); - Tests::append_block(a); - } - { - boost::dynamic_bitset a(std::string("1")); - Tests::append_block(a); - } - { - const int size_to_fill_all_blocks = 4 * bits_per_block; - boost::dynamic_bitset a(size_to_fill_all_blocks, 15ul); - Tests::append_block(a); - } - { - boost::dynamic_bitset a(long_string); - Tests::append_block(a); - } - //===================================================================== - // Test append block range - { - boost::dynamic_bitset a; - std::vector blocks; - Tests::append_block_range(a, blocks); - } - { - boost::dynamic_bitset a(std::string("0")); - std::vector blocks(3); - blocks[0] = static_cast(0); - blocks[1] = static_cast(1); - blocks[2] = all_1s; - Tests::append_block_range(a, blocks); - } - { - boost::dynamic_bitset a(std::string("1")); - const unsigned int n = (std::numeric_limits::max)(); - std::vector blocks(n); - for (typename std::vector::size_type i = 0; i < n; ++i) - blocks[i] = static_cast(i); - Tests::append_block_range(a, blocks); - } - { - boost::dynamic_bitset a; - a.append(Block(1)); - a.append(Block(2)); - Block x[] = {3, 4, 5}; - std::size_t sz = sizeof(x) / sizeof(x[0]); - std::vector blocks(x, x + sz); - Tests::append_block_range(a, blocks); - } - { - boost::dynamic_bitset a(long_string); - std::vector blocks(3); - blocks[0] = static_cast(0); - blocks[1] = static_cast(1); - blocks[2] = all_1s; - Tests::append_block_range(a, blocks); - } - //===================================================================== - // Test bracket operator - { - boost::dynamic_bitset b1; - std::vector bitvec1; - Tests::operator_bracket(b1, bitvec1); - } - { - boost::dynamic_bitset b(std::string("1")); - std::vector bit_vec(1, true); - Tests::operator_bracket(b, bit_vec); - } - { - boost::dynamic_bitset b(long_string); - std::size_t n = long_string.size(); - std::vector bit_vec(n); - for (std::size_t i = 0; i < n; ++i) - bit_vec[i] = long_string[n - 1 - i] == '0' ? 0 : 1; - Tests::operator_bracket(b, bit_vec); - } - //===================================================================== - // Test at - { - boost::dynamic_bitset b1; - std::vector bitvec1; - Tests::at(b1, bitvec1); - } - { - boost::dynamic_bitset b(std::string("1")); - std::vector bit_vec(1, true); - Tests::at(b, bit_vec); - } - { - boost::dynamic_bitset b(long_string); - std::size_t n = long_string.size(); - std::vector bit_vec(n); - for (std::size_t i = 0; i < n; ++i) - bit_vec[i] = long_string[n - 1 - i] == '0' ? 0 : 1; - Tests::at(b, bit_vec); - } -#if !defined(BOOST_NO_CXX11_ALLOCATOR) - { - typedef boost::dynamic_bitset > Bitset; - Bitset b; - bitset_test::max_size(b); - } + //===================================================================== + // Test swap + { + bitset_type a; + bitset_type b( std::string( "1" ) ); + Tests::swap( a, b ); + Tests::swap( b, a ); + Tests::swap( a, a ); + } + { + bitset_type a; + bitset_type b( long_string ); + Tests::swap( a, b ); + Tests::swap( b, a ); + } + { + bitset_type a( std::string( "0" ) ); + bitset_type b( long_string ); + Tests::swap( a, b ); + Tests::swap( b, a ); + Tests::swap( a, a ); + Tests::swap( b, b ); + } + //===================================================================== + // Test resize + { + boost::dynamic_bitset< Block > a; + Tests::resize( a ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ); + Tests::resize( a ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ); + Tests::resize( a ); + } + { + boost::dynamic_bitset< Block > a( long_string ); + Tests::resize( a ); + } + //===================================================================== + // Test clear + { + boost::dynamic_bitset< Block > a; + Tests::clear( a ); + } + { + boost::dynamic_bitset< Block > a( long_string ); + Tests::clear( a ); + } + //===================================================================== + // Test pop back + { + boost::dynamic_bitset< Block > a( std::string( "01" ) ); + Tests::pop_back( a ); + } + { + boost::dynamic_bitset< Block > a( std::string( "10" ) ); + Tests::pop_back( a ); + } + { + const int size_to_fill_all_blocks = 4 * bits_per_block; + boost::dynamic_bitset< Block > a( size_to_fill_all_blocks, 255ul ); + Tests::pop_back( a ); + } + { + boost::dynamic_bitset< Block > a( long_string ); + Tests::pop_back( a ); + } + //===================================================================== + // Test append bit + { + boost::dynamic_bitset< Block > a; + Tests::append_bit( a ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ); + Tests::append_bit( a ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ); + Tests::append_bit( a ); + } + { + const int size_to_fill_all_blocks = 4 * bits_per_block; + boost::dynamic_bitset< Block > a( size_to_fill_all_blocks, 255ul ); + Tests::append_bit( a ); + } + { + boost::dynamic_bitset< Block > a( long_string ); + Tests::append_bit( a ); + } + //===================================================================== + // Test append block + { + boost::dynamic_bitset< Block > a; + Tests::append_block( a ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ); + Tests::append_block( a ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ); + Tests::append_block( a ); + } + { + const int size_to_fill_all_blocks = 4 * bits_per_block; + boost::dynamic_bitset< Block > a( size_to_fill_all_blocks, 15ul ); + Tests::append_block( a ); + } + { + boost::dynamic_bitset< Block > a( long_string ); + Tests::append_block( a ); + } + //===================================================================== + // Test append block range + { + boost::dynamic_bitset< Block > a; + std::vector< Block > blocks; + Tests::append_block_range( a, blocks ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ); + std::vector< Block > blocks( 3 ); + blocks[ 0 ] = static_cast< Block >( 0 ); + blocks[ 1 ] = static_cast< Block >( 1 ); + blocks[ 2 ] = all_1s; + Tests::append_block_range( a, blocks ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ); + const unsigned int n = ( std::numeric_limits< unsigned char >::max )(); + std::vector< Block > blocks( n ); + for ( typename std::vector< Block >::size_type i = 0; i < n; ++i ) + blocks[ i ] = static_cast< Block >( i ); + Tests::append_block_range( a, blocks ); + } + { + boost::dynamic_bitset< Block > a; + a.append( Block( 1 ) ); + a.append( Block( 2 ) ); + Block x[] = { 3, 4, 5 }; + std::size_t sz = sizeof( x ) / sizeof( x[ 0 ] ); + std::vector< Block > blocks( x, x + sz ); + Tests::append_block_range( a, blocks ); + } + { + boost::dynamic_bitset< Block > a( long_string ); + std::vector< Block > blocks( 3 ); + blocks[ 0 ] = static_cast< Block >( 0 ); + blocks[ 1 ] = static_cast< Block >( 1 ); + blocks[ 2 ] = all_1s; + Tests::append_block_range( a, blocks ); + } + //===================================================================== + // Test bracket operator + { + boost::dynamic_bitset< Block > b1; + std::vector< bool > bitvec1; + Tests::operator_bracket( b1, bitvec1 ); + } + { + boost::dynamic_bitset< Block > b( std::string( "1" ) ); + std::vector< bool > bit_vec( 1, true ); + Tests::operator_bracket( b, bit_vec ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + std::size_t n = long_string.size(); + std::vector< bool > bit_vec( n ); + for ( std::size_t i = 0; i < n; ++i ) + bit_vec[ i ] = long_string[ n - 1 - i ] == '0' ? 0 : 1; + Tests::operator_bracket( b, bit_vec ); + } + //===================================================================== + // Test at + { + boost::dynamic_bitset< Block > b1; + std::vector< bool > bitvec1; + Tests::at( b1, bitvec1 ); + } + { + boost::dynamic_bitset< Block > b( std::string( "1" ) ); + std::vector< bool > bit_vec( 1, true ); + Tests::at( b, bit_vec ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + std::size_t n = long_string.size(); + std::vector< bool > bit_vec( n ); + for ( std::size_t i = 0; i < n; ++i ) + bit_vec[ i ] = long_string[ n - 1 - i ] == '0' ? 0 : 1; + Tests::at( b, bit_vec ); + } +#if ! defined( BOOST_NO_CXX11_ALLOCATOR ) + { + typedef boost::dynamic_bitset< Block, minimal_allocator< Block > > Bitset; + Bitset b; + bitset_test< Bitset >::max_size( b ); + } #endif - // Test copy-initialize with default constructor - { - boost::dynamic_bitset b[1] = {}; - (void)b; - } + // Test copy-initialize with default constructor + { + boost::dynamic_bitset< Block > b[ 1 ] = {}; + (void)b; + } } int main() { - run_test_cases(); - run_test_cases(); - run_test_cases(); - run_test_cases(); -# ifdef BOOST_HAS_LONG_LONG - run_test_cases< ::boost::ulong_long_type>(); -# endif + run_test_cases< unsigned char >(); + run_test_cases< unsigned short >(); + run_test_cases< unsigned int >(); + run_test_cases< unsigned long >(); +#ifdef BOOST_HAS_LONG_LONG + run_test_cases< ::boost::ulong_long_type >(); +#endif - return boost::report_errors(); + return boost::report_errors(); } diff --git a/test/dyn_bitset_unit_tests2.cpp b/test/dyn_bitset_unit_tests2.cpp index b48bf92..406554e 100644 --- a/test/dyn_bitset_unit_tests2.cpp +++ b/test/dyn_bitset_unit_tests2.cpp @@ -14,370 +14,365 @@ #include "boost/config.hpp" #include "boost/dynamic_bitset/dynamic_bitset.hpp" - -template -void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) +template< typename Block > +void +run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE( Block ) ) { - typedef boost::dynamic_bitset bitset_type; - typedef bitset_test< bitset_type > Tests; - const int bits_per_block = bitset_type::bits_per_block; + typedef boost::dynamic_bitset< Block > bitset_type; + typedef bitset_test< bitset_type > Tests; + const int bits_per_block = bitset_type::bits_per_block; - std::string long_string = get_long_string(); + std::string long_string = get_long_string(); - //===================================================================== - // Test operator&= - { - boost::dynamic_bitset lhs, rhs; - Tests::and_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(std::string("1")), rhs(std::string("0")); - Tests::and_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 0), rhs(long_string); - Tests::and_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 1), rhs(long_string); - Tests::and_assignment(lhs, rhs); - } - //===================================================================== - // Test operator |= - { - boost::dynamic_bitset lhs, rhs; - Tests::or_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(std::string("1")), rhs(std::string("0")); - Tests::or_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 0), rhs(long_string); - Tests::or_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 1), rhs(long_string); - Tests::or_assignment(lhs, rhs); - } - //===================================================================== - // Test operator^= - { - boost::dynamic_bitset lhs, rhs; - Tests::xor_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(std::string("1")), rhs(std::string("0")); - Tests::xor_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(std::string("0")), rhs(std::string("1")); - Tests::xor_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string), rhs(long_string); - Tests::xor_assignment(lhs, rhs); - } - //===================================================================== - // Test operator-= - { - boost::dynamic_bitset lhs, rhs; - Tests::sub_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(std::string("1")), rhs(std::string("0")); - Tests::sub_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(std::string("0")), rhs(std::string("1")); - Tests::sub_assignment(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string), rhs(long_string); - Tests::sub_assignment(lhs, rhs); - } - //===================================================================== - // Test operator<<= - { // case pos == 0 - std::size_t pos = 0; + //===================================================================== + // Test operator&= { - boost::dynamic_bitset b; - Tests::shift_left_assignment(b, pos); + boost::dynamic_bitset< Block > lhs, rhs; + Tests::and_assignment( lhs, rhs ); } { - boost::dynamic_bitset b(std::string("1010")); - Tests::shift_left_assignment(b, pos); + boost::dynamic_bitset< Block > lhs( std::string( "1" ) ), rhs( std::string( "0" ) ); + Tests::and_assignment( lhs, rhs ); } { - boost::dynamic_bitset b(long_string); - Tests::shift_left_assignment(b, pos); + boost::dynamic_bitset< Block > lhs( long_string.size(), 0 ), rhs( long_string ); + Tests::and_assignment( lhs, rhs ); } - } - { - // test with both multiple and - // non multiple of bits_per_block - const int how_many = 10; - for (int i = 1; i <= how_many; ++i) { - std::size_t multiple = i * bits_per_block; - std::size_t non_multiple = multiple - 1; - boost::dynamic_bitset b(long_string); + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 1 ), rhs( long_string ); + Tests::and_assignment( lhs, rhs ); + } + //===================================================================== + // Test operator |= + { + boost::dynamic_bitset< Block > lhs, rhs; + Tests::or_assignment( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( std::string( "1" ) ), rhs( std::string( "0" ) ); + Tests::or_assignment( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 0 ), rhs( long_string ); + Tests::or_assignment( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 1 ), rhs( long_string ); + Tests::or_assignment( lhs, rhs ); + } + //===================================================================== + // Test operator^= + { + boost::dynamic_bitset< Block > lhs, rhs; + Tests::xor_assignment( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( std::string( "1" ) ), rhs( std::string( "0" ) ); + Tests::xor_assignment( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( std::string( "0" ) ), rhs( std::string( "1" ) ); + Tests::xor_assignment( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string ), rhs( long_string ); + Tests::xor_assignment( lhs, rhs ); + } + //===================================================================== + // Test operator-= + { + boost::dynamic_bitset< Block > lhs, rhs; + Tests::sub_assignment( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( std::string( "1" ) ), rhs( std::string( "0" ) ); + Tests::sub_assignment( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( std::string( "0" ) ), rhs( std::string( "1" ) ); + Tests::sub_assignment( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string ), rhs( long_string ); + Tests::sub_assignment( lhs, rhs ); + } + //===================================================================== + // Test operator<<= + { // case pos == 0 + std::size_t pos = 0; + { + boost::dynamic_bitset< Block > b; + Tests::shift_left_assignment( b, pos ); + } + { + boost::dynamic_bitset< Block > b( std::string( "1010" ) ); + Tests::shift_left_assignment( b, pos ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::shift_left_assignment( b, pos ); + } + } + { + // test with both multiple and + // non multiple of bits_per_block + const int how_many = 10; + for ( int i = 1; i <= how_many; ++i ) { + std::size_t multiple = i * bits_per_block; + std::size_t non_multiple = multiple - 1; + boost::dynamic_bitset< Block > b( long_string ); - Tests::shift_left_assignment(b, multiple); - Tests::shift_left_assignment(b, non_multiple); + Tests::shift_left_assignment( b, multiple ); + Tests::shift_left_assignment( b, non_multiple ); + } } - } - { // case pos == size()/2 - std::size_t pos = long_string.size() / 2; - boost::dynamic_bitset b(long_string); - Tests::shift_left_assignment(b, pos); - } - { // case pos >= n - std::size_t pos = long_string.size(); - boost::dynamic_bitset b(long_string); - Tests::shift_left_assignment(b, pos); - } - //===================================================================== - // Test operator>>= - { // case pos == 0 - std::size_t pos = 0; - { - boost::dynamic_bitset b; - Tests::shift_right_assignment(b, pos); + { // case pos == size()/2 + std::size_t pos = long_string.size() / 2; + boost::dynamic_bitset< Block > b( long_string ); + Tests::shift_left_assignment( b, pos ); + } + { // case pos >= n + std::size_t pos = long_string.size(); + boost::dynamic_bitset< Block > b( long_string ); + Tests::shift_left_assignment( b, pos ); + } + //===================================================================== + // Test operator>>= + { // case pos == 0 + std::size_t pos = 0; + { + boost::dynamic_bitset< Block > b; + Tests::shift_right_assignment( b, pos ); + } + { + boost::dynamic_bitset< Block > b( std::string( "1010" ) ); + Tests::shift_right_assignment( b, pos ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::shift_right_assignment( b, pos ); + } } { - boost::dynamic_bitset b(std::string("1010")); - Tests::shift_right_assignment(b, pos); - } - { - boost::dynamic_bitset b(long_string); - Tests::shift_right_assignment(b, pos); - } - } - { - // test with both multiple and - // non multiple of bits_per_block - const int how_many = 10; - for (int i = 1; i <= how_many; ++i) { - std::size_t multiple = i * bits_per_block; - std::size_t non_multiple = multiple - 1; - boost::dynamic_bitset b(long_string); + // test with both multiple and + // non multiple of bits_per_block + const int how_many = 10; + for ( int i = 1; i <= how_many; ++i ) { + std::size_t multiple = i * bits_per_block; + std::size_t non_multiple = multiple - 1; + boost::dynamic_bitset< Block > b( long_string ); - Tests::shift_right_assignment(b, multiple); - Tests::shift_right_assignment(b, non_multiple); + Tests::shift_right_assignment( b, multiple ); + Tests::shift_right_assignment( b, non_multiple ); + } + } + { // case pos == size()/2 + std::size_t pos = long_string.size() / 2; + boost::dynamic_bitset< Block > b( long_string ); + Tests::shift_right_assignment( b, pos ); + } + { // case pos >= n + std::size_t pos = long_string.size(); + boost::dynamic_bitset< Block > b( long_string ); + Tests::shift_right_assignment( b, pos ); + } + //===================================================================== + // test b.set() + { + boost::dynamic_bitset< Block > b; + Tests::set_all( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::set_all( b ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::set_all( b ); + } + //===================================================================== + // Test b.set(pos) + { // case pos >= b.size() + boost::dynamic_bitset< Block > b; + Tests::set_one( b, 0, true ); + Tests::set_one( b, 0, false ); + } + { // case pos < b.size() + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::set_one( b, 0, true ); + Tests::set_one( b, 0, false ); + } + { // case pos == b.size() / 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::set_one( b, long_string.size() / 2, true ); + Tests::set_one( b, long_string.size() / 2, false ); + } + //===================================================================== + // Test b.set(pos, len) + { // case size is 1 + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::set_segment( b, 0, 1, true ); + Tests::set_segment( b, 0, 1, false ); + } + { // case fill the whole set + boost::dynamic_bitset< Block > b( long_string ); + Tests::set_segment( b, 0, b.size(), true ); + Tests::set_segment( b, 0, b.size(), false ); + } + { // case pos = size / 4, len = size / 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::set_segment( b, b.size() / 4, b.size() / 2, true ); + Tests::set_segment( b, b.size() / 4, b.size() / 2, false ); + } + { // case pos = block_size / 2, len = size - block_size + boost::dynamic_bitset< Block > b( long_string ); + Tests::set_segment( b, boost::dynamic_bitset< Block >::bits_per_block / 2, b.size() - boost::dynamic_bitset< Block >::bits_per_block, true ); + Tests::set_segment( b, boost::dynamic_bitset< Block >::bits_per_block / 2, b.size() - boost::dynamic_bitset< Block >::bits_per_block, false ); + } + { // case pos = 1, len = size - 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::set_segment( b, 1, b.size() - 2, true ); + Tests::set_segment( b, 1, b.size() - 2, false ); + } + { // case pos = 3, len = 7 + boost::dynamic_bitset< Block > b( long_string ); + Tests::set_segment( b, 3, 7, true ); + Tests::set_segment( b, 3, 7, false ); + } + //===================================================================== + // Test b.reset() + { + boost::dynamic_bitset< Block > b; + Tests::reset_all( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::reset_all( b ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::reset_all( b ); + } + //===================================================================== + // Test b.reset(pos) + { // case pos >= b.size() + boost::dynamic_bitset< Block > b; + Tests::reset_one( b, 0 ); + } + { // case pos < b.size() + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::reset_one( b, 0 ); + } + { // case pos == b.size() / 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::reset_one( b, long_string.size() / 2 ); + } + //===================================================================== + // Test b.reset(pos, len) + { // case size is 1 + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::reset_segment( b, 0, 1 ); + } + { // case fill the whole set + boost::dynamic_bitset< Block > b( long_string ); + Tests::reset_segment( b, 0, b.size() ); + } + { // case pos = size / 4, len = size / 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::reset_segment( b, b.size() / 4, b.size() / 2 ); + } + { // case pos = block_size / 2, len = size - block_size + boost::dynamic_bitset< Block > b( long_string ); + Tests::reset_segment( b, boost::dynamic_bitset< Block >::bits_per_block / 2, b.size() - boost::dynamic_bitset< Block >::bits_per_block ); + } + { // case pos = 1, len = size - 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::reset_segment( b, 1, b.size() - 2 ); + } + { // case pos = 3, len = 7 + boost::dynamic_bitset< Block > b( long_string ); + Tests::reset_segment( b, 3, 7 ); + } + //===================================================================== + // Test ~b + { + boost::dynamic_bitset< Block > b; + Tests::operator_flip( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "1" ) ); + Tests::operator_flip( b ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::operator_flip( b ); + } + //===================================================================== + // Test b.flip() + { + boost::dynamic_bitset< Block > b; + Tests::flip_all( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "1" ) ); + Tests::flip_all( b ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::flip_all( b ); + } + //===================================================================== + // Test b.flip(pos) + { // case pos >= b.size() + boost::dynamic_bitset< Block > b; + Tests::flip_one( b, 0 ); + } + { // case pos < b.size() + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::flip_one( b, 0 ); + } + { // case pos == b.size() / 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::flip_one( b, long_string.size() / 2 ); + } + //===================================================================== + // Test b.flip(pos, len) + { // case size is 1 + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::flip_segment( b, 0, 1 ); + } + { // case fill the whole set + boost::dynamic_bitset< Block > b( long_string ); + Tests::flip_segment( b, 0, b.size() ); + } + { // case pos = size / 4, len = size / 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::flip_segment( b, b.size() / 4, b.size() / 2 ); + } + { // case pos = block_size / 2, len = size - block_size + boost::dynamic_bitset< Block > b( long_string ); + Tests::flip_segment( b, boost::dynamic_bitset< Block >::bits_per_block / 2, b.size() - boost::dynamic_bitset< Block >::bits_per_block ); + } + { // case pos = 1, len = size - 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::flip_segment( b, 1, b.size() - 2 ); + } + { // case pos = 3, len = 7 + boost::dynamic_bitset< Block > b( long_string ); + Tests::flip_segment( b, 3, 7 ); } - - } - { // case pos == size()/2 - std::size_t pos = long_string.size() / 2; - boost::dynamic_bitset b(long_string); - Tests::shift_right_assignment(b, pos); - } - { // case pos >= n - std::size_t pos = long_string.size(); - boost::dynamic_bitset b(long_string); - Tests::shift_right_assignment(b, pos); - } - //===================================================================== - // test b.set() - { - boost::dynamic_bitset b; - Tests::set_all(b); - } - { - boost::dynamic_bitset b(std::string("0")); - Tests::set_all(b); - } - { - boost::dynamic_bitset b(long_string); - Tests::set_all(b); - } - //===================================================================== - // Test b.set(pos) - { // case pos >= b.size() - boost::dynamic_bitset b; - Tests::set_one(b, 0, true); - Tests::set_one(b, 0, false); - } - { // case pos < b.size() - boost::dynamic_bitset b(std::string("0")); - Tests::set_one(b, 0, true); - Tests::set_one(b, 0, false); - } - { // case pos == b.size() / 2 - boost::dynamic_bitset b(long_string); - Tests::set_one(b, long_string.size()/2, true); - Tests::set_one(b, long_string.size()/2, false); - } - //===================================================================== - // Test b.set(pos, len) - { // case size is 1 - boost::dynamic_bitset b(std::string("0")); - Tests::set_segment(b, 0, 1, true); - Tests::set_segment(b, 0, 1, false); - } - { // case fill the whole set - boost::dynamic_bitset b(long_string); - Tests::set_segment(b, 0, b.size(), true); - Tests::set_segment(b, 0, b.size(), false); - } - { // case pos = size / 4, len = size / 2 - boost::dynamic_bitset b(long_string); - Tests::set_segment(b, b.size() / 4, b.size() / 2, true); - Tests::set_segment(b, b.size() / 4, b.size() / 2, false); - } - { // case pos = block_size / 2, len = size - block_size - boost::dynamic_bitset b(long_string); - Tests::set_segment(b, boost::dynamic_bitset::bits_per_block / 2, - b.size() - boost::dynamic_bitset::bits_per_block, true); - Tests::set_segment(b, boost::dynamic_bitset::bits_per_block / 2, - b.size() - boost::dynamic_bitset::bits_per_block, false); - } - { // case pos = 1, len = size - 2 - boost::dynamic_bitset b(long_string); - Tests::set_segment(b, 1, b.size() - 2, true); - Tests::set_segment(b, 1, b.size() - 2, false); - } - { // case pos = 3, len = 7 - boost::dynamic_bitset b(long_string); - Tests::set_segment(b, 3, 7, true); - Tests::set_segment(b, 3, 7, false); - } - //===================================================================== - // Test b.reset() - { - boost::dynamic_bitset b; - Tests::reset_all(b); - } - { - boost::dynamic_bitset b(std::string("0")); - Tests::reset_all(b); - } - { - boost::dynamic_bitset b(long_string); - Tests::reset_all(b); - } - //===================================================================== - // Test b.reset(pos) - { // case pos >= b.size() - boost::dynamic_bitset b; - Tests::reset_one(b, 0); - } - { // case pos < b.size() - boost::dynamic_bitset b(std::string("0")); - Tests::reset_one(b, 0); - } - { // case pos == b.size() / 2 - boost::dynamic_bitset b(long_string); - Tests::reset_one(b, long_string.size()/2); - } - //===================================================================== - // Test b.reset(pos, len) - { // case size is 1 - boost::dynamic_bitset b(std::string("0")); - Tests::reset_segment(b, 0, 1); - } - { // case fill the whole set - boost::dynamic_bitset b(long_string); - Tests::reset_segment(b, 0, b.size()); - } - { // case pos = size / 4, len = size / 2 - boost::dynamic_bitset b(long_string); - Tests::reset_segment(b, b.size() / 4, b.size() / 2); - } - { // case pos = block_size / 2, len = size - block_size - boost::dynamic_bitset b(long_string); - Tests::reset_segment(b, boost::dynamic_bitset::bits_per_block / 2, - b.size() - boost::dynamic_bitset::bits_per_block); - } - { // case pos = 1, len = size - 2 - boost::dynamic_bitset b(long_string); - Tests::reset_segment(b, 1, b.size() - 2); - } - { // case pos = 3, len = 7 - boost::dynamic_bitset b(long_string); - Tests::reset_segment(b, 3, 7); - } - //===================================================================== - // Test ~b - { - boost::dynamic_bitset b; - Tests::operator_flip(b); - } - { - boost::dynamic_bitset b(std::string("1")); - Tests::operator_flip(b); - } - { - boost::dynamic_bitset b(long_string); - Tests::operator_flip(b); - } - //===================================================================== - // Test b.flip() - { - boost::dynamic_bitset b; - Tests::flip_all(b); - } - { - boost::dynamic_bitset b(std::string("1")); - Tests::flip_all(b); - } - { - boost::dynamic_bitset b(long_string); - Tests::flip_all(b); - } - //===================================================================== - // Test b.flip(pos) - { // case pos >= b.size() - boost::dynamic_bitset b; - Tests::flip_one(b, 0); - } - { // case pos < b.size() - boost::dynamic_bitset b(std::string("0")); - Tests::flip_one(b, 0); - } - { // case pos == b.size() / 2 - boost::dynamic_bitset b(long_string); - Tests::flip_one(b, long_string.size()/2); - } - //===================================================================== - // Test b.flip(pos, len) - { // case size is 1 - boost::dynamic_bitset b(std::string("0")); - Tests::flip_segment(b, 0, 1); - } - { // case fill the whole set - boost::dynamic_bitset b(long_string); - Tests::flip_segment(b, 0, b.size()); - } - { // case pos = size / 4, len = size / 2 - boost::dynamic_bitset b(long_string); - Tests::flip_segment(b, b.size() / 4, b.size() / 2); - } - { // case pos = block_size / 2, len = size - block_size - boost::dynamic_bitset b(long_string); - Tests::flip_segment(b, boost::dynamic_bitset::bits_per_block / 2, - b.size() - boost::dynamic_bitset::bits_per_block); - } - { // case pos = 1, len = size - 2 - boost::dynamic_bitset b(long_string); - Tests::flip_segment(b, 1, b.size() - 2); - } - { // case pos = 3, len = 7 - boost::dynamic_bitset b(long_string); - Tests::flip_segment(b, 3, 7); - } } int main() { - run_test_cases(); - run_test_cases(); - run_test_cases(); - run_test_cases(); -# ifdef BOOST_HAS_LONG_LONG - run_test_cases< ::boost::ulong_long_type>(); -# endif + run_test_cases< unsigned char >(); + run_test_cases< unsigned short >(); + run_test_cases< unsigned int >(); + run_test_cases< unsigned long >(); +#ifdef BOOST_HAS_LONG_LONG + run_test_cases< ::boost::ulong_long_type >(); +#endif - return boost::report_errors(); + return boost::report_errors(); } diff --git a/test/dyn_bitset_unit_tests3.cpp b/test/dyn_bitset_unit_tests3.cpp index 372a7cc..c5e868f 100644 --- a/test/dyn_bitset_unit_tests3.cpp +++ b/test/dyn_bitset_unit_tests3.cpp @@ -16,799 +16,794 @@ #include "boost/limits.hpp" #include -template -void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) +template< typename Block > +void +run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE( Block ) ) { - // a bunch of typedefs which will be handy later on - typedef boost::dynamic_bitset bitset_type; - typedef bitset_test Tests; - // typedef typename bitset_type::size_type size_type; // unusable with Borland 5.5.1 + // a bunch of typedefs which will be handy later on + typedef boost::dynamic_bitset< Block > bitset_type; + typedef bitset_test< bitset_type > Tests; + // typedef typename bitset_type::size_type size_type; // unusable with Borland 5.5.1 - std::string long_string = get_long_string(); - std::size_t ul_width = std::numeric_limits::digits; + std::string long_string = get_long_string(); + std::size_t ul_width = std::numeric_limits< unsigned long >::digits; - //===================================================================== - // Test b.empty() - { - bitset_type b; - Tests::empty(b); - } - { - bitset_type b(1, 1ul); - Tests::empty(b); - } - { - bitset_type b(bitset_type::bits_per_block - + bitset_type::bits_per_block/2, 15ul); - Tests::empty(b); - } - //===================================================================== - // Test b.to_long() - { - boost::dynamic_bitset b; - Tests::to_ulong(b); - } - { - boost::dynamic_bitset b(std::string("1")); - Tests::to_ulong(b); - } - { - boost::dynamic_bitset b(bitset_type::bits_per_block, - static_cast(-1)); - Tests::to_ulong(b); - } - { - std::string str(ul_width - 1, '1'); - boost::dynamic_bitset b(str); - Tests::to_ulong(b); - } - { - std::string ul_str(ul_width, '1'); - boost::dynamic_bitset b(ul_str); - Tests::to_ulong(b); - } - { // case overflow - boost::dynamic_bitset b(long_string); - Tests::to_ulong(b); - } - //===================================================================== - // Test to_string(b, str) - { - boost::dynamic_bitset b; - Tests::to_string(b); - } - { - boost::dynamic_bitset b(std::string("0")); - Tests::to_string(b); - } - { - boost::dynamic_bitset b(long_string); - Tests::to_string(b); - } - //===================================================================== - // Test b.count() - { - boost::dynamic_bitset b; - Tests::count(b); - } - { - boost::dynamic_bitset b(std::string("0")); - Tests::count(b); - } - { - boost::dynamic_bitset b(std::string("1")); - Tests::count(b); - } - { - boost::dynamic_bitset b(8, 255ul); - Tests::count(b); - } - { - boost::dynamic_bitset b(long_string); - Tests::count(b); - } - //===================================================================== - // Test b.size() - { - boost::dynamic_bitset b; - Tests::size(b); - } - { - boost::dynamic_bitset b(std::string("0")); - Tests::size(b); - } - { - boost::dynamic_bitset b(long_string); - Tests::size(b); - } - //===================================================================== - // Test b.capacity() - { - boost::dynamic_bitset b; - Tests::capacity_test_one(b); - } - { - boost::dynamic_bitset b(100); - Tests::capacity_test_two(b); - } - //===================================================================== - // Test b.reserve() - { - boost::dynamic_bitset b; - Tests::reserve_test_one(b); - } - { - boost::dynamic_bitset b(100); - Tests::reserve_test_two(b); - } - //===================================================================== - // Test b.shrink_to_fit() - { - boost::dynamic_bitset b; - Tests::shrink_to_fit_test_one(b); - } - { - boost::dynamic_bitset b(100); - Tests::shrink_to_fit_test_two(b); - } - //===================================================================== - // Test b.all() - { - boost::dynamic_bitset b; - Tests::all(b); - Tests::all(~b); - Tests::all(b.set()); - Tests::all(b.reset()); - } - { - boost::dynamic_bitset b(std::string("0")); - Tests::all(b); - Tests::all(~b); - Tests::all(b.set()); - Tests::all(b.reset()); - } - { - boost::dynamic_bitset b(long_string); - Tests::all(b); - Tests::all(~b); - Tests::all(b.set()); - Tests::all(b.reset()); - } - //===================================================================== - // Test b.any() - { - boost::dynamic_bitset b; - Tests::any(b); - Tests::any(~b); - Tests::any(b.set()); - Tests::any(b.reset()); - } - { - boost::dynamic_bitset b(std::string("0")); - Tests::any(b); - Tests::any(~b); - Tests::any(b.set()); - Tests::any(b.reset()); - } - { - boost::dynamic_bitset b(long_string); - Tests::any(b); - Tests::any(~b); - Tests::any(b.set()); - Tests::any(b.reset()); - } - //===================================================================== - // Test b.none() - { - boost::dynamic_bitset b; - Tests::none(b); - Tests::none(~b); - Tests::none(b.set()); - Tests::none(b.reset()); - } - { - boost::dynamic_bitset b(std::string("0")); - Tests::none(b); - Tests::none(~b); - Tests::none(b.set()); - Tests::none(b.reset()); - } - { - boost::dynamic_bitset b(long_string); - Tests::none(b); - Tests::none(~b); - Tests::none(b.set()); - Tests::none(b.reset()); - } - //===================================================================== - // Test a.is_subset_of(b) - { - boost::dynamic_bitset a, b; - Tests::subset(a, b); - } - { - boost::dynamic_bitset a(std::string("0")), b(std::string("0")); - Tests::subset(a, b); - } - { - boost::dynamic_bitset a(std::string("1")), b(std::string("1")); - Tests::subset(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - Tests::subset(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - a[long_string.size()/2].flip(); - Tests::subset(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - b[long_string.size()/2].flip(); - Tests::subset(a, b); - } - //===================================================================== - // Test a.is_proper_subset_of(b) - { - boost::dynamic_bitset a, b; - Tests::proper_subset(a, b); - } - { - boost::dynamic_bitset a(std::string("0")), b(std::string("0")); - Tests::proper_subset(a, b); - } - { - boost::dynamic_bitset a(std::string("1")), b(std::string("1")); - Tests::proper_subset(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - Tests::proper_subset(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - a[long_string.size()/2].flip(); - Tests::proper_subset(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - b[long_string.size()/2].flip(); - Tests::proper_subset(a, b); - } - //===================================================================== - // Test intersects - { - bitset_type a; // empty - bitset_type b; - Tests::intersects(a, b); - } - { - bitset_type a; - bitset_type b(5, 8ul); - Tests::intersects(a, b); - } - { - bitset_type a(8, 0ul); - bitset_type b(15, 0ul); - b[9] = 1; - Tests::intersects(a, b); - } - { - bitset_type a(15, 0ul); - bitset_type b(22, 0ul); - a[14] = b[14] = 1; - Tests::intersects(a, b); - } - //===================================================================== - // Test find_first - { - // empty bitset - bitset_type b; - Tests::find_first(b); - } - { - // bitset of size 1 - bitset_type b(1, 1ul); - Tests::find_first(b); - } - { - // all-0s bitset - bitset_type b(4 * bitset_type::bits_per_block, 0ul); - Tests::find_first(b); - } - { - // first bit on - bitset_type b(1, 1ul); - Tests::find_first(b); - } - { - // last bit on - bitset_type b(4 * bitset_type::bits_per_block - 1, 0ul); - b.set(b.size() - 1); - Tests::find_first(b); - } - //===================================================================== - // Test find_next and offset find_first - { - // empty bitset - bitset_type b; + //===================================================================== + // Test b.empty() + { + bitset_type b; + Tests::empty( b ); + } + { + bitset_type b( 1, 1ul ); + Tests::empty( b ); + } + { + bitset_type b( bitset_type::bits_per_block + bitset_type::bits_per_block / 2, 15ul ); + Tests::empty( b ); + } + //===================================================================== + // Test b.to_long() + { + boost::dynamic_bitset< Block > b; + Tests::to_ulong( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "1" ) ); + Tests::to_ulong( b ); + } + { + boost::dynamic_bitset< Block > b( bitset_type::bits_per_block, static_cast< unsigned long >( -1 ) ); + Tests::to_ulong( b ); + } + { + std::string str( ul_width - 1, '1' ); + boost::dynamic_bitset< Block > b( str ); + Tests::to_ulong( b ); + } + { + std::string ul_str( ul_width, '1' ); + boost::dynamic_bitset< Block > b( ul_str ); + Tests::to_ulong( b ); + } + { // case overflow + boost::dynamic_bitset< Block > b( long_string ); + Tests::to_ulong( b ); + } + //===================================================================== + // Test to_string(b, str) + { + boost::dynamic_bitset< Block > b; + Tests::to_string( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::to_string( b ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::to_string( b ); + } + //===================================================================== + // Test b.count() + { + boost::dynamic_bitset< Block > b; + Tests::count( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::count( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "1" ) ); + Tests::count( b ); + } + { + boost::dynamic_bitset< Block > b( 8, 255ul ); + Tests::count( b ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::count( b ); + } + //===================================================================== + // Test b.size() + { + boost::dynamic_bitset< Block > b; + Tests::size( b ); + } + { + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::size( b ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::size( b ); + } + //===================================================================== + // Test b.capacity() + { + boost::dynamic_bitset< Block > b; + Tests::capacity_test_one( b ); + } + { + boost::dynamic_bitset< Block > b( 100 ); + Tests::capacity_test_two( b ); + } + //===================================================================== + // Test b.reserve() + { + boost::dynamic_bitset< Block > b; + Tests::reserve_test_one( b ); + } + { + boost::dynamic_bitset< Block > b( 100 ); + Tests::reserve_test_two( b ); + } + //===================================================================== + // Test b.shrink_to_fit() + { + boost::dynamic_bitset< Block > b; + Tests::shrink_to_fit_test_one( b ); + } + { + boost::dynamic_bitset< Block > b( 100 ); + Tests::shrink_to_fit_test_two( b ); + } + //===================================================================== + // Test b.all() + { + boost::dynamic_bitset< Block > b; + Tests::all( b ); + Tests::all( ~b ); + Tests::all( b.set() ); + Tests::all( b.reset() ); + } + { + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::all( b ); + Tests::all( ~b ); + Tests::all( b.set() ); + Tests::all( b.reset() ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::all( b ); + Tests::all( ~b ); + Tests::all( b.set() ); + Tests::all( b.reset() ); + } + //===================================================================== + // Test b.any() + { + boost::dynamic_bitset< Block > b; + Tests::any( b ); + Tests::any( ~b ); + Tests::any( b.set() ); + Tests::any( b.reset() ); + } + { + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::any( b ); + Tests::any( ~b ); + Tests::any( b.set() ); + Tests::any( b.reset() ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::any( b ); + Tests::any( ~b ); + Tests::any( b.set() ); + Tests::any( b.reset() ); + } + //===================================================================== + // Test b.none() + { + boost::dynamic_bitset< Block > b; + Tests::none( b ); + Tests::none( ~b ); + Tests::none( b.set() ); + Tests::none( b.reset() ); + } + { + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::none( b ); + Tests::none( ~b ); + Tests::none( b.set() ); + Tests::none( b.reset() ); + } + { + boost::dynamic_bitset< Block > b( long_string ); + Tests::none( b ); + Tests::none( ~b ); + Tests::none( b.set() ); + Tests::none( b.reset() ); + } + //===================================================================== + // Test a.is_subset_of(b) + { + boost::dynamic_bitset< Block > a, b; + Tests::subset( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ), b( std::string( "0" ) ); + Tests::subset( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ), b( std::string( "1" ) ); + Tests::subset( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + Tests::subset( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + a[ long_string.size() / 2 ].flip(); + Tests::subset( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + b[ long_string.size() / 2 ].flip(); + Tests::subset( a, b ); + } + //===================================================================== + // Test a.is_proper_subset_of(b) + { + boost::dynamic_bitset< Block > a, b; + Tests::proper_subset( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ), b( std::string( "0" ) ); + Tests::proper_subset( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ), b( std::string( "1" ) ); + Tests::proper_subset( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + Tests::proper_subset( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + a[ long_string.size() / 2 ].flip(); + Tests::proper_subset( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + b[ long_string.size() / 2 ].flip(); + Tests::proper_subset( a, b ); + } + //===================================================================== + // Test intersects + { + bitset_type a; // empty + bitset_type b; + Tests::intersects( a, b ); + } + { + bitset_type a; + bitset_type b( 5, 8ul ); + Tests::intersects( a, b ); + } + { + bitset_type a( 8, 0ul ); + bitset_type b( 15, 0ul ); + b[ 9 ] = 1; + Tests::intersects( a, b ); + } + { + bitset_type a( 15, 0ul ); + bitset_type b( 22, 0ul ); + a[ 14 ] = b[ 14 ] = 1; + Tests::intersects( a, b ); + } + //===================================================================== + // Test find_first + { + // empty bitset + bitset_type b; + Tests::find_first( b ); + } + { + // bitset of size 1 + bitset_type b( 1, 1ul ); + Tests::find_first( b ); + } + { + // all-0s bitset + bitset_type b( 4 * bitset_type::bits_per_block, 0ul ); + Tests::find_first( b ); + } + { + // first bit on + bitset_type b( 1, 1ul ); + Tests::find_first( b ); + } + { + // last bit on + bitset_type b( 4 * bitset_type::bits_per_block - 1, 0ul ); + b.set( b.size() - 1 ); + Tests::find_first( b ); + } + //===================================================================== + // Test find_next and offset find_first + { + // empty bitset + bitset_type b; - // check - Tests::find_pos(b, 0); - Tests::find_pos(b, 1); - Tests::find_pos(b, 200); - Tests::find_pos(b, b.npos); - } - { - // bitset of size 1 (find_next can never find) - bitset_type b(1, 1ul); + // check + Tests::find_pos( b, 0 ); + Tests::find_pos( b, 1 ); + Tests::find_pos( b, 200 ); + Tests::find_pos( b, b.npos ); + } + { + // bitset of size 1 (find_next can never find) + bitset_type b( 1, 1ul ); - // check - Tests::find_pos(b, 0); - Tests::find_pos(b, 1); - Tests::find_pos(b, 200); - Tests::find_pos(b, b.npos); - } - { - // all-1s bitset - bitset_type b(16 * bitset_type::bits_per_block); - b.set(); + // check + Tests::find_pos( b, 0 ); + Tests::find_pos( b, 1 ); + Tests::find_pos( b, 200 ); + Tests::find_pos( b, b.npos ); + } + { + // all-1s bitset + bitset_type b( 16 * bitset_type::bits_per_block ); + b.set(); - // check - const typename bitset_type::size_type larger_than_size = 5 + b.size(); - for(typename bitset_type::size_type i = 0; i <= larger_than_size; ++i) { - Tests::find_pos(b, i); - } - Tests::find_pos(b, b.npos); - } - { - // a bitset with 1s at block boundary only - const int num_blocks = 32; - const int block_width = bitset_type::bits_per_block; + // check + const typename bitset_type::size_type larger_than_size = 5 + b.size(); + for ( typename bitset_type::size_type i = 0; i <= larger_than_size; ++i ) { + Tests::find_pos( b, i ); + } + Tests::find_pos( b, b.npos ); + } + { + // a bitset with 1s at block boundary only + const int num_blocks = 32; + const int block_width = bitset_type::bits_per_block; - bitset_type b(num_blocks * block_width); - typename bitset_type::size_type i = block_width - 1; - for ( ; i < b.size(); i += block_width) { + bitset_type b( num_blocks * block_width ); + typename bitset_type::size_type i = block_width - 1; + for ( ; i < b.size(); i += block_width ) { + b.set( i ); + typename bitset_type::size_type first_in_block = i - ( block_width - 1 ); + b.set( first_in_block ); + } - b.set(i); - typename bitset_type::size_type first_in_block = i - (block_width - 1); - b.set(first_in_block); - } + // check + const typename bitset_type::size_type larger_than_size = 5 + b.size(); + for ( i = 0; i <= larger_than_size; ++i ) { + Tests::find_pos( b, i ); + } + Tests::find_pos( b, b.npos ); + } + { + // bitset with alternate 1s and 0s + const typename bitset_type::size_type sz = 1000; + bitset_type b( sz ); - // check - const typename bitset_type::size_type larger_than_size = 5 + b.size(); - for (i = 0; i <= larger_than_size; ++i) { - Tests::find_pos(b, i); - } - Tests::find_pos(b, b.npos); + typename bitset_type::size_type i = 0; + for ( ; i < sz; ++i ) { + b[ i ] = ( i % 2 == 0 ); + } - } - { - // bitset with alternate 1s and 0s - const typename bitset_type::size_type sz = 1000; - bitset_type b(sz); - - typename bitset_type::size_type i = 0; - for ( ; i < sz; ++i) { - b[i] = (i%2 == 0); - } - - // check - const typename bitset_type::size_type larger_than_size = 5 + b.size(); - for (i = 0; i <= larger_than_size; ++i) { - Tests::find_pos(b, i); - } - Tests::find_pos(b, b.npos); - - } - //===================================================================== - // Test operator== - { - boost::dynamic_bitset a, b; - Tests::operator_equal(a, b); - } - { - boost::dynamic_bitset a(std::string("0")), b(std::string("0")); - Tests::operator_equal(a, b); - } - { - boost::dynamic_bitset a(std::string("1")), b(std::string("1")); - Tests::operator_equal(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - Tests::operator_equal(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - a[long_string.size()/2].flip(); - Tests::operator_equal(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - b[long_string.size()/2].flip(); - Tests::operator_equal(a, b); - } - //===================================================================== - // Test operator!= - { - boost::dynamic_bitset a, b; - Tests::operator_not_equal(a, b); - } - { - boost::dynamic_bitset a(std::string("0")), b(std::string("0")); - Tests::operator_not_equal(a, b); - } - { - boost::dynamic_bitset a(std::string("1")), b(std::string("1")); - Tests::operator_not_equal(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - Tests::operator_not_equal(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - a[long_string.size()/2].flip(); - Tests::operator_not_equal(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - b[long_string.size()/2].flip(); - Tests::operator_not_equal(a, b); - } - //===================================================================== - // Test operator< - { - boost::dynamic_bitset a, b; - Tests::operator_less_than(a, b); - } - { - boost::dynamic_bitset a(std::string("0")), b(std::string("0")); - Tests::operator_less_than(a, b); - } - { - boost::dynamic_bitset a(std::string("1")), b(std::string("1")); - Tests::operator_less_than(a, b); - } - { - boost::dynamic_bitset a(std::string("10")), b(std::string("11")); - Tests::operator_less_than(a, b); - } - { - boost::dynamic_bitset a(std::string("101")), b(std::string("11")); - Tests::operator_less_than(a, b); - } - { - boost::dynamic_bitset a(std::string("10")), b(std::string("111")); - Tests::operator_less_than(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - Tests::operator_less_than(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - a[long_string.size()/2].flip(); - Tests::operator_less_than(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - b[long_string.size()/2].flip(); - Tests::operator_less_than(a, b); - } - // check for consistency with ulong behaviour when the sizes are equal - { - boost::dynamic_bitset a(3, 4ul), b(3, 5ul); - BOOST_TEST(a < b); - } - { - boost::dynamic_bitset a(3, 4ul), b(3, 4ul); - BOOST_TEST(!(a < b)); - } - { - boost::dynamic_bitset a(3, 5ul), b(3, 4ul); - BOOST_TEST(!(a < b)); - } - // when the sizes are not equal lexicographic compare does not necessarily correspond to ulong behavior - { - boost::dynamic_bitset a(4, 4ul), b(3, 5ul); - BOOST_TEST(a < b); - } - { - boost::dynamic_bitset a(3, 4ul), b(4, 5ul); - BOOST_TEST(!(a < b)); - } - { - boost::dynamic_bitset a(4, 4ul), b(3, 4ul); - BOOST_TEST(a < b); - } - { - boost::dynamic_bitset a(3, 4ul), b(4, 4ul); - BOOST_TEST(!(a < b)); - } - { - boost::dynamic_bitset a(4, 5ul), b(3, 4ul); - BOOST_TEST(a < b); - } - { - boost::dynamic_bitset a(3, 5ul), b(4, 4ul); - BOOST_TEST(!(a < b)); - } - //===================================================================== - // Test operator<= - { - boost::dynamic_bitset a, b; - Tests::operator_less_than_eq(a, b); - } - { - boost::dynamic_bitset a(std::string("0")), b(std::string("0")); - Tests::operator_less_than_eq(a, b); - } - { - boost::dynamic_bitset a(std::string("1")), b(std::string("1")); - Tests::operator_less_than_eq(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - Tests::operator_less_than_eq(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - a[long_string.size()/2].flip(); - Tests::operator_less_than_eq(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - b[long_string.size()/2].flip(); - Tests::operator_less_than_eq(a, b); - } - // check for consistency with ulong behaviour - { - boost::dynamic_bitset a(3, 4ul), b(3, 5ul); - BOOST_TEST(a <= b); - } - { - boost::dynamic_bitset a(3, 4ul), b(3, 4ul); - BOOST_TEST(a <= b); - } - { - boost::dynamic_bitset a(3, 5ul), b(3, 4ul); - BOOST_TEST(!(a <= b)); - } - //===================================================================== - // Test operator> - { - boost::dynamic_bitset a, b; - Tests::operator_greater_than(a, b); - } - { - boost::dynamic_bitset a(std::string("0")), b(std::string("0")); - Tests::operator_greater_than(a, b); - } - { - boost::dynamic_bitset a(std::string("1")), b(std::string("1")); - Tests::operator_greater_than(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - Tests::operator_greater_than(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - a[long_string.size()/2].flip(); - Tests::operator_greater_than(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - b[long_string.size()/2].flip(); - Tests::operator_greater_than(a, b); - } - // check for consistency with ulong behaviour - { - boost::dynamic_bitset a(3, 4ul), b(3, 5ul); - BOOST_TEST(!(a > b)); - } - { - boost::dynamic_bitset a(3, 4ul), b(3, 4ul); - BOOST_TEST(!(a > b)); - } - { - boost::dynamic_bitset a(3, 5ul), b(3, 4ul); - BOOST_TEST(a > b); - } - //===================================================================== - // Test operator<= - { - boost::dynamic_bitset a, b; - Tests::operator_greater_than_eq(a, b); - } - { - boost::dynamic_bitset a(std::string("0")), b(std::string("0")); - Tests::operator_greater_than_eq(a, b); - } - { - boost::dynamic_bitset a(std::string("1")), b(std::string("1")); - Tests::operator_greater_than_eq(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - Tests::operator_greater_than_eq(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - a[long_string.size()/2].flip(); - Tests::operator_greater_than_eq(a, b); - } - { - boost::dynamic_bitset a(long_string), b(long_string); - b[long_string.size()/2].flip(); - Tests::operator_greater_than_eq(a, b); - } - // check for consistency with ulong behaviour - { - boost::dynamic_bitset a(3, 4ul), b(3, 5ul); - BOOST_TEST(!(a >= b)); - } - { - boost::dynamic_bitset a(3, 4ul), b(3, 4ul); - BOOST_TEST(a >= b); - } - { - boost::dynamic_bitset a(3, 5ul), b(3, 4ul); - BOOST_TEST(a >= b); - } - //===================================================================== - // Test b.test(pos) - { // case pos >= b.size() - boost::dynamic_bitset b; - Tests::test_bit(b, 0); - } - { // case pos < b.size() - boost::dynamic_bitset b(std::string("0")); - Tests::test_bit(b, 0); - } - { // case pos == b.size() / 2 - boost::dynamic_bitset b(long_string); - Tests::test_bit(b, long_string.size()/2); - } - //===================================================================== - // Test b.test_set(pos) - { // case pos >= b.size() - boost::dynamic_bitset b; - Tests::test_set_bit(b, 0, true); - Tests::test_set_bit(b, 0, false); - } - { // case pos < b.size() - boost::dynamic_bitset b(std::string("0")); - Tests::test_set_bit(b, 0, true); - Tests::test_set_bit(b, 0, false); - } - { // case pos == b.size() / 2 - boost::dynamic_bitset b(long_string); - Tests::test_set_bit(b, long_string.size() / 2, true); - Tests::test_set_bit(b, long_string.size() / 2, false); - } - //===================================================================== - // Test b << pos - { // case pos == 0 - std::size_t pos = 0; - boost::dynamic_bitset b(std::string("1010")); - Tests::operator_shift_left(b, pos); - } - { // case pos == size()/2 - std::size_t pos = long_string.size() / 2; - boost::dynamic_bitset b(long_string); - Tests::operator_shift_left(b, pos); - } - { // case pos >= n - std::size_t pos = long_string.size(); - boost::dynamic_bitset b(long_string); - Tests::operator_shift_left(b, pos); - } - //===================================================================== - // Test b >> pos - { // case pos == 0 - std::size_t pos = 0; - boost::dynamic_bitset b(std::string("1010")); - Tests::operator_shift_right(b, pos); - } - { // case pos == size()/2 - std::size_t pos = long_string.size() / 2; - boost::dynamic_bitset b(long_string); - Tests::operator_shift_right(b, pos); - } - { // case pos >= n - std::size_t pos = long_string.size(); - boost::dynamic_bitset b(long_string); - Tests::operator_shift_right(b, pos); - } - //===================================================================== - // Test a & b - { - boost::dynamic_bitset lhs, rhs; - Tests::operator_and(lhs, rhs); - } - { - boost::dynamic_bitset lhs(std::string("1")), rhs(std::string("0")); - Tests::operator_and(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 0), rhs(long_string); - Tests::operator_and(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 1), rhs(long_string); - Tests::operator_and(lhs, rhs); - } - //===================================================================== - // Test a | b - { - boost::dynamic_bitset lhs, rhs; - Tests::operator_or(lhs, rhs); - } - { - boost::dynamic_bitset lhs(std::string("1")), rhs(std::string("0")); - Tests::operator_or(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 0), rhs(long_string); - Tests::operator_or(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 1), rhs(long_string); - Tests::operator_or(lhs, rhs); - } - //===================================================================== - // Test a^b - { - boost::dynamic_bitset lhs, rhs; - Tests::operator_xor(lhs, rhs); - } - { - boost::dynamic_bitset lhs(std::string("1")), rhs(std::string("0")); - Tests::operator_xor(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 0), rhs(long_string); - Tests::operator_xor(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 1), rhs(long_string); - Tests::operator_xor(lhs, rhs); - } - //===================================================================== - // Test a-b - { - boost::dynamic_bitset lhs, rhs; - Tests::operator_sub(lhs, rhs); - } - { - boost::dynamic_bitset lhs(std::string("1")), rhs(std::string("0")); - Tests::operator_sub(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 0), rhs(long_string); - Tests::operator_sub(lhs, rhs); - } - { - boost::dynamic_bitset lhs(long_string.size(), 1), rhs(long_string); - Tests::operator_sub(lhs, rhs); - } + // check + const typename bitset_type::size_type larger_than_size = 5 + b.size(); + for ( i = 0; i <= larger_than_size; ++i ) { + Tests::find_pos( b, i ); + } + Tests::find_pos( b, b.npos ); + } + //===================================================================== + // Test operator== + { + boost::dynamic_bitset< Block > a, b; + Tests::operator_equal( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ), b( std::string( "0" ) ); + Tests::operator_equal( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ), b( std::string( "1" ) ); + Tests::operator_equal( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + Tests::operator_equal( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + a[ long_string.size() / 2 ].flip(); + Tests::operator_equal( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + b[ long_string.size() / 2 ].flip(); + Tests::operator_equal( a, b ); + } + //===================================================================== + // Test operator!= + { + boost::dynamic_bitset< Block > a, b; + Tests::operator_not_equal( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ), b( std::string( "0" ) ); + Tests::operator_not_equal( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ), b( std::string( "1" ) ); + Tests::operator_not_equal( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + Tests::operator_not_equal( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + a[ long_string.size() / 2 ].flip(); + Tests::operator_not_equal( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + b[ long_string.size() / 2 ].flip(); + Tests::operator_not_equal( a, b ); + } + //===================================================================== + // Test operator< + { + boost::dynamic_bitset< Block > a, b; + Tests::operator_less_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ), b( std::string( "0" ) ); + Tests::operator_less_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ), b( std::string( "1" ) ); + Tests::operator_less_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "10" ) ), b( std::string( "11" ) ); + Tests::operator_less_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "101" ) ), b( std::string( "11" ) ); + Tests::operator_less_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "10" ) ), b( std::string( "111" ) ); + Tests::operator_less_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + Tests::operator_less_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + a[ long_string.size() / 2 ].flip(); + Tests::operator_less_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + b[ long_string.size() / 2 ].flip(); + Tests::operator_less_than( a, b ); + } + // check for consistency with ulong behaviour when the sizes are equal + { + boost::dynamic_bitset< Block > a( 3, 4ul ), b( 3, 5ul ); + BOOST_TEST( a < b ); + } + { + boost::dynamic_bitset< Block > a( 3, 4ul ), b( 3, 4ul ); + BOOST_TEST( ! ( a < b ) ); + } + { + boost::dynamic_bitset< Block > a( 3, 5ul ), b( 3, 4ul ); + BOOST_TEST( ! ( a < b ) ); + } + // when the sizes are not equal lexicographic compare does not necessarily correspond to ulong behavior + { + boost::dynamic_bitset< Block > a( 4, 4ul ), b( 3, 5ul ); + BOOST_TEST( a < b ); + } + { + boost::dynamic_bitset< Block > a( 3, 4ul ), b( 4, 5ul ); + BOOST_TEST( ! ( a < b ) ); + } + { + boost::dynamic_bitset< Block > a( 4, 4ul ), b( 3, 4ul ); + BOOST_TEST( a < b ); + } + { + boost::dynamic_bitset< Block > a( 3, 4ul ), b( 4, 4ul ); + BOOST_TEST( ! ( a < b ) ); + } + { + boost::dynamic_bitset< Block > a( 4, 5ul ), b( 3, 4ul ); + BOOST_TEST( a < b ); + } + { + boost::dynamic_bitset< Block > a( 3, 5ul ), b( 4, 4ul ); + BOOST_TEST( ! ( a < b ) ); + } + //===================================================================== + // Test operator<= + { + boost::dynamic_bitset< Block > a, b; + Tests::operator_less_than_eq( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ), b( std::string( "0" ) ); + Tests::operator_less_than_eq( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ), b( std::string( "1" ) ); + Tests::operator_less_than_eq( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + Tests::operator_less_than_eq( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + a[ long_string.size() / 2 ].flip(); + Tests::operator_less_than_eq( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + b[ long_string.size() / 2 ].flip(); + Tests::operator_less_than_eq( a, b ); + } + // check for consistency with ulong behaviour + { + boost::dynamic_bitset< Block > a( 3, 4ul ), b( 3, 5ul ); + BOOST_TEST( a <= b ); + } + { + boost::dynamic_bitset< Block > a( 3, 4ul ), b( 3, 4ul ); + BOOST_TEST( a <= b ); + } + { + boost::dynamic_bitset< Block > a( 3, 5ul ), b( 3, 4ul ); + BOOST_TEST( ! ( a <= b ) ); + } + //===================================================================== + // Test operator> + { + boost::dynamic_bitset< Block > a, b; + Tests::operator_greater_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ), b( std::string( "0" ) ); + Tests::operator_greater_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ), b( std::string( "1" ) ); + Tests::operator_greater_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + Tests::operator_greater_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + a[ long_string.size() / 2 ].flip(); + Tests::operator_greater_than( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + b[ long_string.size() / 2 ].flip(); + Tests::operator_greater_than( a, b ); + } + // check for consistency with ulong behaviour + { + boost::dynamic_bitset< Block > a( 3, 4ul ), b( 3, 5ul ); + BOOST_TEST( ! ( a > b ) ); + } + { + boost::dynamic_bitset< Block > a( 3, 4ul ), b( 3, 4ul ); + BOOST_TEST( ! ( a > b ) ); + } + { + boost::dynamic_bitset< Block > a( 3, 5ul ), b( 3, 4ul ); + BOOST_TEST( a > b ); + } + //===================================================================== + // Test operator<= + { + boost::dynamic_bitset< Block > a, b; + Tests::operator_greater_than_eq( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "0" ) ), b( std::string( "0" ) ); + Tests::operator_greater_than_eq( a, b ); + } + { + boost::dynamic_bitset< Block > a( std::string( "1" ) ), b( std::string( "1" ) ); + Tests::operator_greater_than_eq( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + Tests::operator_greater_than_eq( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + a[ long_string.size() / 2 ].flip(); + Tests::operator_greater_than_eq( a, b ); + } + { + boost::dynamic_bitset< Block > a( long_string ), b( long_string ); + b[ long_string.size() / 2 ].flip(); + Tests::operator_greater_than_eq( a, b ); + } + // check for consistency with ulong behaviour + { + boost::dynamic_bitset< Block > a( 3, 4ul ), b( 3, 5ul ); + BOOST_TEST( ! ( a >= b ) ); + } + { + boost::dynamic_bitset< Block > a( 3, 4ul ), b( 3, 4ul ); + BOOST_TEST( a >= b ); + } + { + boost::dynamic_bitset< Block > a( 3, 5ul ), b( 3, 4ul ); + BOOST_TEST( a >= b ); + } + //===================================================================== + // Test b.test(pos) + { // case pos >= b.size() + boost::dynamic_bitset< Block > b; + Tests::test_bit( b, 0 ); + } + { // case pos < b.size() + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::test_bit( b, 0 ); + } + { // case pos == b.size() / 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::test_bit( b, long_string.size() / 2 ); + } + //===================================================================== + // Test b.test_set(pos) + { // case pos >= b.size() + boost::dynamic_bitset< Block > b; + Tests::test_set_bit( b, 0, true ); + Tests::test_set_bit( b, 0, false ); + } + { // case pos < b.size() + boost::dynamic_bitset< Block > b( std::string( "0" ) ); + Tests::test_set_bit( b, 0, true ); + Tests::test_set_bit( b, 0, false ); + } + { // case pos == b.size() / 2 + boost::dynamic_bitset< Block > b( long_string ); + Tests::test_set_bit( b, long_string.size() / 2, true ); + Tests::test_set_bit( b, long_string.size() / 2, false ); + } + //===================================================================== + // Test b << pos + { // case pos == 0 + std::size_t pos = 0; + boost::dynamic_bitset< Block > b( std::string( "1010" ) ); + Tests::operator_shift_left( b, pos ); + } + { // case pos == size()/2 + std::size_t pos = long_string.size() / 2; + boost::dynamic_bitset< Block > b( long_string ); + Tests::operator_shift_left( b, pos ); + } + { // case pos >= n + std::size_t pos = long_string.size(); + boost::dynamic_bitset< Block > b( long_string ); + Tests::operator_shift_left( b, pos ); + } + //===================================================================== + // Test b >> pos + { // case pos == 0 + std::size_t pos = 0; + boost::dynamic_bitset< Block > b( std::string( "1010" ) ); + Tests::operator_shift_right( b, pos ); + } + { // case pos == size()/2 + std::size_t pos = long_string.size() / 2; + boost::dynamic_bitset< Block > b( long_string ); + Tests::operator_shift_right( b, pos ); + } + { // case pos >= n + std::size_t pos = long_string.size(); + boost::dynamic_bitset< Block > b( long_string ); + Tests::operator_shift_right( b, pos ); + } + //===================================================================== + // Test a & b + { + boost::dynamic_bitset< Block > lhs, rhs; + Tests::operator_and( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( std::string( "1" ) ), rhs( std::string( "0" ) ); + Tests::operator_and( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 0 ), rhs( long_string ); + Tests::operator_and( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 1 ), rhs( long_string ); + Tests::operator_and( lhs, rhs ); + } + //===================================================================== + // Test a | b + { + boost::dynamic_bitset< Block > lhs, rhs; + Tests::operator_or( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( std::string( "1" ) ), rhs( std::string( "0" ) ); + Tests::operator_or( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 0 ), rhs( long_string ); + Tests::operator_or( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 1 ), rhs( long_string ); + Tests::operator_or( lhs, rhs ); + } + //===================================================================== + // Test a^b + { + boost::dynamic_bitset< Block > lhs, rhs; + Tests::operator_xor( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( std::string( "1" ) ), rhs( std::string( "0" ) ); + Tests::operator_xor( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 0 ), rhs( long_string ); + Tests::operator_xor( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 1 ), rhs( long_string ); + Tests::operator_xor( lhs, rhs ); + } + //===================================================================== + // Test a-b + { + boost::dynamic_bitset< Block > lhs, rhs; + Tests::operator_sub( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( std::string( "1" ) ), rhs( std::string( "0" ) ); + Tests::operator_sub( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 0 ), rhs( long_string ); + Tests::operator_sub( lhs, rhs ); + } + { + boost::dynamic_bitset< Block > lhs( long_string.size(), 1 ), rhs( long_string ); + Tests::operator_sub( lhs, rhs ); + } } - int main() { - run_test_cases(); - run_test_cases(); - run_test_cases(); - run_test_cases(); -# ifdef BOOST_HAS_LONG_LONG - run_test_cases< ::boost::ulong_long_type>(); -# endif + run_test_cases< unsigned char >(); + run_test_cases< unsigned short >(); + run_test_cases< unsigned int >(); + run_test_cases< unsigned long >(); +#ifdef BOOST_HAS_LONG_LONG + run_test_cases< ::boost::ulong_long_type >(); +#endif - return boost::report_errors(); + return boost::report_errors(); } diff --git a/test/dyn_bitset_unit_tests4.cpp b/test/dyn_bitset_unit_tests4.cpp index e03d27d..c1feb16 100644 --- a/test/dyn_bitset_unit_tests4.cpp +++ b/test/dyn_bitset_unit_tests4.cpp @@ -12,314 +12,300 @@ #include "boost/config.hpp" #include "boost/config/workaround.hpp" #include "boost/dynamic_bitset/dynamic_bitset.hpp" - #include #include #include #include #include -#if !defined (BOOST_NO_STRINGSTREAM) -# include +#if ! defined( BOOST_NO_STRINGSTREAM ) +# include #endif - #if defined BOOST_NO_STD_WSTRING || defined BOOST_NO_STD_LOCALE -# define BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS +# define BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS #endif -#if !defined BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS -std::wstring widen_string( const std::string & str, - const std::locale & loc = std::locale() ) +#if ! defined BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS +std::wstring +widen_string( const std::string & str, const std::locale & loc = std::locale() ) { - std::wstring result; - const std::string::size_type len = str.length(); - if(len != 0) { + std::wstring result; + const std::string::size_type len = str.length(); + if ( len != 0 ) { + typedef std::ctype< wchar_t > ct_type; + typedef std::wstring::traits_type tr_type; + const ct_type & ct = BOOST_USE_FACET( ct_type, loc ); - typedef std::ctype ct_type; - typedef std::wstring::traits_type tr_type; - const ct_type & ct = BOOST_USE_FACET(ct_type, loc); - - result.resize(len); - for (std::size_t i = 0; i < len; ++i) - tr_type::assign(result[i], ct.widen(str[i])); - - } - return result; + result.resize( len ); + for ( std::size_t i = 0; i < len; ++i ) + tr_type::assign( result[ i ], ct.widen( str[ i ] ) ); + } + return result; } #endif -template -void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) +template< typename Block > +void +run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE( Block ) ) { + typedef boost::dynamic_bitset< Block > bitset_type; + typedef bitset_test< bitset_type > Tests; - typedef boost::dynamic_bitset bitset_type; - typedef bitset_test Tests; + //===================================================================== + // Test stream operator<< + { + // The test "variables" are: the stream type and its state, the + // exception mask, the width, the fill char and the padding side (left/right) - //===================================================================== - // Test stream operator<< - { + std::ios::iostate masks[] = { + std::ios::goodbit, + std::ios::eofbit, + std::ios::failbit, + std::ios::eofbit | std::ios::failbit + }; - // The test "variables" are: the stream type and its state, the - // exception mask, the width, the fill char and the padding side (left/right) + static std::string strings[] = { + std::string( "" ), + std::string( "0" ), + std::string( "1" ), + std::string( "11100" ), + get_long_string() + }; - std::ios::iostate masks[] = { - std::ios::goodbit, - std::ios::eofbit, - std::ios::failbit, - std::ios::eofbit | std::ios::failbit - }; + char fill_chars[] = { '*', 'x', ' ' }; - static std::string strings[] = { - std::string(""), - std::string("0"), - std::string("1"), - std::string("11100"), - get_long_string() - }; + std::size_t num_masks = sizeof( masks ) / sizeof( masks[ 0 ] ); + std::size_t num_strings = sizeof( strings ) / sizeof( strings[ 0 ] ); + std::size_t num_chars = sizeof( fill_chars ) / sizeof( fill_chars[ 0 ] ); - char fill_chars[] = { '*', 'x', ' ' }; + std::fstream not_good_stream( "dynamic_bitset_tests - this file shouldn't exist", std::ios::in ); - std::size_t num_masks = sizeof(masks) / sizeof(masks[0]); - std::size_t num_strings = sizeof(strings) / sizeof(strings[0]); - std::size_t num_chars = sizeof(fill_chars) / sizeof(fill_chars[0]); + for ( std::size_t mi = 0; mi < num_masks; ++mi ) { + for ( std::size_t si = 0; si < num_strings; ++si ) { + std::streamsize slen = (std::streamsize)( strings[ si ].length() ); - std::fstream not_good_stream("dynamic_bitset_tests - this file shouldn't exist", - std::ios::in); + assert( ( std::numeric_limits< std::streamsize >::max )() >= (std::streamsize)( 1 + slen * 2 ) ); + for ( std::size_t ci = 0; ci < num_chars; ++ci ) { + // note how "negative widths" are tested too + const std::streamsize widths[] = { -1 - slen / 2, 0, slen / 2, 1 + slen * 2 }; + std::size_t num_widths = sizeof( widths ) / sizeof( widths[ 0 ] ); - for (std::size_t mi = 0; mi < num_masks; ++mi) { - for (std::size_t si = 0; si < num_strings; ++si) { + for ( std::size_t wi = 0; wi < num_widths; ++wi ) { + std::streamsize w = widths[ wi ]; + { + // test 0 - stream !good() + if ( not_good_stream.good() ) + throw std::logic_error( "Error in operator << tests" + " - please, double check" ); + bitset_type b( strings[ si ] ); + not_good_stream.width( w ); + not_good_stream.fill( fill_chars[ ci ] ); + try { + not_good_stream.exceptions( masks[ mi ] ); + } catch ( ... ) { + } - std::streamsize slen = (std::streamsize)(strings[si].length()); - - assert( (std::numeric_limits::max)() - >=(std::streamsize)(1+slen*2) ); - - for (std::size_t ci = 0; ci < num_chars; ++ci) { - - // note how "negative widths" are tested too - const std::streamsize widths[] = { -1 - slen/2, 0, slen/2, 1 + slen*2 }; - std::size_t num_widths = sizeof(widths) / sizeof(widths[0]); - - for (std::size_t wi = 0; wi < num_widths; ++wi) { - std::streamsize w = widths[wi]; - { - // test 0 - stream !good() - if(not_good_stream.good()) - throw std::logic_error("Error in operator << tests" - " - please, double check"); - bitset_type b(strings[si]); - not_good_stream.width(w); - not_good_stream.fill(fill_chars[ci]); - try { not_good_stream.exceptions(masks[mi]); } catch(...) {} - - Tests::stream_inserter(b, not_good_stream, ""); - } - { - // test 1a - file stream - scoped_temp_file stf; - bitset_type b(strings[si]); - std::ofstream file(stf.path().string().c_str(), std::ios::trunc); - file.width(w); - file.fill(fill_chars[ci]); - file.exceptions(masks[mi]); - Tests::stream_inserter(b, file, stf.path().string().c_str()); - } - { - //NOTE: there are NO string stream tests - } -#if !defined (BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS) - { - // test 1b - wide file stream - scoped_temp_file stf; - bitset_type b(strings[si]); - std::wofstream file(stf.path().string().c_str()); - file.width(w); - file.fill(fill_chars[ci]); - file.exceptions(masks[mi]); - Tests::stream_inserter(b, file, stf.path().string().c_str()); - } + Tests::stream_inserter( b, not_good_stream, "" ); + } + { + // test 1a - file stream + scoped_temp_file stf; + bitset_type b( strings[ si ] ); + std::ofstream file( stf.path().string().c_str(), std::ios::trunc ); + file.width( w ); + file.fill( fill_chars[ ci ] ); + file.exceptions( masks[ mi ] ); + Tests::stream_inserter( b, file, stf.path().string().c_str() ); + } + { + // NOTE: there are NO string stream tests + } +#if ! defined( BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS ) + { + // test 1b - wide file stream + scoped_temp_file stf; + bitset_type b( strings[ si ] ); + std::wofstream file( stf.path().string().c_str() ); + file.width( w ); + file.fill( fill_chars[ ci ] ); + file.exceptions( masks[ mi ] ); + Tests::stream_inserter( b, file, stf.path().string().c_str() ); + } #endif - } - } - } - } // for (; mi..) + } + } + } + } // for (; mi..) + } - } + //===================================================================== + // Test stream operator>> + { + // The test "variables" are: the stream type, the exception mask, + // the actual contents (and/or state) of the stream, and width. + // + // With few exceptions, each test case consists of writing a different + // assortment of digits and "whitespaces" to a text stream and then checking + // that what was written gets read back unchanged. That's NOT guaranteed by + // the standard, unless the assortment always ends with a '\n' and satisfies + // other conditions (see C99, 7.19.2/2), however it works in practice and is + // a good "real life" test. Some characters, such as '\v' and '\f', are not + // used exactly because they are the ones which will most likely give problems + // on some systems (for instance '\f' could actually be written as a sequence + // of new-lines, and we could never be able to read it back) + // + // Note how the bitset object is not initially empty. That helps checking + // that it isn't erroneously clear()ed by operator>>. - //===================================================================== - // Test stream operator>> - { + std::ios::iostate masks[] = { + std::ios::goodbit, + std::ios::eofbit, + std::ios::failbit, + std::ios::eofbit | std::ios::failbit + }; - // The test "variables" are: the stream type, the exception mask, - // the actual contents (and/or state) of the stream, and width. - // - // With few exceptions, each test case consists of writing a different - // assortment of digits and "whitespaces" to a text stream and then checking - // that what was written gets read back unchanged. That's NOT guaranteed by - // the standard, unless the assortment always ends with a '\n' and satisfies - // other conditions (see C99, 7.19.2/2), however it works in practice and is - // a good "real life" test. Some characters, such as '\v' and '\f', are not - // used exactly because they are the ones which will most likely give problems - // on some systems (for instance '\f' could actually be written as a sequence - // of new-lines, and we could never be able to read it back) - // - // Note how the bitset object is not initially empty. That helps checking - // that it isn't erroneously clear()ed by operator>>. + const std::string spaces = "\t\n "; //"\t\n\v\f "; - - std::ios::iostate masks[] = { - std::ios::goodbit, - std::ios::eofbit, - std::ios::failbit, - std::ios::eofbit | std::ios::failbit - }; - - const std::string spaces = "\t\n "; //"\t\n\v\f "; - - const std::string long_string = get_long_string(); - const static std::string strings[] = { + const std::string long_string = get_long_string(); + const static std::string strings[] = { // empty string - std::string(""), + std::string( "" ), // no bitset spaces, // no bitset - std::string("x"), - std::string("\t xyz"), + std::string( "x" ), + std::string( "\t xyz" ), // bitset of size 1 - std::string("0"), - std::string("1"), + std::string( "0" ), + std::string( "1" ), - std::string(" 0 "), - std::string(" 1 "), + std::string( " 0 " ), + std::string( " 1 " ), spaces + "1", "1" + spaces, spaces + "1" + spaces, - std::string(" x1x "), - std::string(" 1x "), + std::string( " x1x " ), + std::string( " 1x " ), // long bitset long_string, " " + long_string + " xyz", spaces + long_string, spaces + long_string + spaces - }; + }; + //----------------------------------------------------- - //----------------------------------------------------- + std::stringstream not_good_stream; + not_good_stream << "test"; + std::string sink; + not_good_stream >> sink; // now the stream should be in eof state - std::stringstream not_good_stream; - not_good_stream << "test"; - std::string sink; - not_good_stream >> sink; // now the stream should be in eof state + const std::size_t num_masks = sizeof( masks ) / sizeof( masks[ 0 ] ); + const std::size_t num_strings = sizeof( strings ) / sizeof( strings[ 0 ] ); - const std::size_t num_masks = sizeof(masks) / sizeof(masks[0]); - const std::size_t num_strings = sizeof(strings) / sizeof(strings[0]); + for ( std::size_t mi = 0; mi < num_masks; ++mi ) { + for ( std::size_t si = 0; si < num_strings; ++si ) { + const std::streamsize slen = (std::streamsize)( strings[ si ].length() ); + assert( ( std::numeric_limits< std::streamsize >::max )() >= (std::streamsize)( 1 + slen * 2 ) ); - for (std::size_t mi = 0; mi < num_masks; ++mi) { - for (std::size_t si = 0; si < num_strings; ++si) { + std::streamsize widths[] = { -1, 0, slen / 2, slen, 1 + slen * 2 }; + std::size_t num_widths = sizeof( widths ) / sizeof( widths[ 0 ] ); - const std::streamsize slen = (std::streamsize)(strings[si].length()); - assert((std::numeric_limits::max)() >= (std::streamsize)(1+slen*2)); + for ( std::size_t wi = 0; wi < num_widths; ++wi ) { + const std::streamsize w = widths[ wi ]; - std::streamsize widths[] = { -1, 0, slen/2, slen, 1 + slen*2 }; - std::size_t num_widths = sizeof(widths) / sizeof(widths[0]); + // test 0 - !good() stream + { + if ( not_good_stream.good() ) + throw std::logic_error( "Error in operator >> tests" + " - please, double check" ); + bitset_type b( 1, 15ul ); // note: b is not empty + not_good_stream.width( w ); + try { + not_good_stream.exceptions( masks[ mi ] ); + } catch ( ... ) { + } + std::string irrelevant; + Tests::stream_extractor( b, not_good_stream, irrelevant ); + } + // test 1a - (narrow) file stream + { + scoped_temp_file stf; + bitset_type b( 1, 255ul ); + { + std::ofstream f( stf.path().string().c_str() ); + f << strings[ si ]; + } - for(std::size_t wi = 0; wi < num_widths; ++wi) { - const std::streamsize w = widths[wi]; - - // test 0 - !good() stream - { - if(not_good_stream.good()) - throw std::logic_error("Error in operator >> tests" - " - please, double check"); - bitset_type b(1, 15ul); // note: b is not empty - not_good_stream.width(w); - try { not_good_stream.exceptions(masks[mi]); } catch(...) {} - std::string irrelevant; - Tests::stream_extractor(b, not_good_stream, irrelevant); - } - // test 1a - (narrow) file stream - { - scoped_temp_file stf; - bitset_type b(1, 255ul); - { - std::ofstream f(stf.path().string().c_str()); - f << strings[si]; - } - - std::ifstream f(stf.path().string().c_str()); - f.width(w); - f.exceptions(masks[mi]); - Tests::stream_extractor(b, f, strings[si]); - } -#if !defined(BOOST_NO_STRINGSTREAM) - // test 2a - stringstream - { - bitset_type b(1, 255ul); - std::istringstream stream(strings[si]); - stream.width(w); - stream.exceptions(masks[mi]); - Tests::stream_extractor(b, stream, strings[si]); - } + std::ifstream f( stf.path().string().c_str() ); + f.width( w ); + f.exceptions( masks[ mi ] ); + Tests::stream_extractor( b, f, strings[ si ] ); + } +#if ! defined( BOOST_NO_STRINGSTREAM ) + // test 2a - stringstream + { + bitset_type b( 1, 255ul ); + std::istringstream stream( strings[ si ] ); + stream.width( w ); + stream.exceptions( masks[ mi ] ); + Tests::stream_extractor( b, stream, strings[ si ] ); + } #endif -#if !defined(BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS) - // test 1b - wchar_t file stream - { - scoped_temp_file stf; - std::wstring wstr = widen_string(strings[si]); - bitset_type b(1, 255ul); - { - std::basic_ofstream of(stf.path().string().c_str()); - of << wstr; - } +#if ! defined( BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS ) + // test 1b - wchar_t file stream + { + scoped_temp_file stf; + std::wstring wstr = widen_string( strings[ si ] ); + bitset_type b( 1, 255ul ); + { + std::basic_ofstream< wchar_t > of( stf.path().string().c_str() ); + of << wstr; + } - std::basic_ifstream f(stf.path().string().c_str()); - f.width(w); - f.exceptions(masks[mi]); - Tests::stream_extractor(b, f, wstr); - } - // test 2b - wstringstream - { - bitset_type b(1, 255ul); - std::wstring wstr = widen_string(strings[si]); + std::basic_ifstream< wchar_t > f( stf.path().string().c_str() ); + f.width( w ); + f.exceptions( masks[ mi ] ); + Tests::stream_extractor( b, f, wstr ); + } + // test 2b - wstringstream + { + bitset_type b( 1, 255ul ); + std::wstring wstr = widen_string( strings[ si ] ); - std::wistringstream wstream(wstr); - wstream.width(w); - wstream.exceptions(masks[mi]); - Tests::stream_extractor(b, wstream, wstr); - } + std::wistringstream wstream( wstr ); + wstream.width( w ); + wstream.exceptions( masks[ mi ] ); + Tests::stream_extractor( b, wstream, wstr ); + } #endif // BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS + } + } - } + } // for ( mi = 0; ...) } - - } // for ( mi = 0; ...) - - - } - //===================================================================== - // << Any other tests go here >> - // ..... - + //===================================================================== + // << Any other tests go here >> + // ..... } - int main() { - run_test_cases(); - run_test_cases(); - run_test_cases(); - run_test_cases(); -# ifdef BOOST_HAS_LONG_LONG - run_test_cases< ::boost::ulong_long_type>(); -# endif + run_test_cases< unsigned char >(); + run_test_cases< unsigned short >(); + run_test_cases< unsigned int >(); + run_test_cases< unsigned long >(); +#ifdef BOOST_HAS_LONG_LONG + run_test_cases< ::boost::ulong_long_type >(); +#endif - return boost::report_errors(); + return boost::report_errors(); } diff --git a/test/dyn_bitset_unit_tests5.cpp b/test/dyn_bitset_unit_tests5.cpp index e83bd0f..8d05e8b 100644 --- a/test/dyn_bitset_unit_tests5.cpp +++ b/test/dyn_bitset_unit_tests5.cpp @@ -20,84 +20,94 @@ #include "boost/dynamic_bitset/serialization.hpp" #include "boost/serialization/vector.hpp" -#if !defined (BOOST_NO_STRINGSTREAM) -# include +#if ! defined( BOOST_NO_STRINGSTREAM ) +# include #endif - #if defined BOOST_NO_STD_WSTRING || defined BOOST_NO_STD_LOCALE -# define BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS +# define BOOST_DYNAMIC_BITSET_NO_WCHAR_T_TESTS #endif namespace { - template - struct SerializableType { - boost::dynamic_bitset x; +template< typename Block > +struct SerializableType +{ + boost::dynamic_bitset< Block > x; - private: - friend class boost::serialization::access; - template void serialize(Archive &ar, unsigned int) { - ar & BOOST_SERIALIZATION_NVP(x); - } - }; +private: + friend class boost::serialization::access; + template< class Archive > + void + serialize( Archive & ar, unsigned int ) + { + ar & BOOST_SERIALIZATION_NVP( x ); + } +}; - template - void test_serialization( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) - { - SerializableType a; +template< typename Block, typename IArchive, typename OArchive > +void +test_serialization( BOOST_EXPLICIT_TEMPLATE_TYPE( Block ) ) +{ + SerializableType< Block > a; - for (int i=0; i<128; ++i) - a.x.resize(11*i, i%2); + for ( int i = 0; i < 128; ++i ) + a.x.resize( 11 * i, i % 2 ); -#if !defined (BOOST_NO_STRINGSTREAM) - std::stringstream ss; +#if ! defined( BOOST_NO_STRINGSTREAM ) + std::stringstream ss; - // test serialization - { - OArchive oa(ss); - oa << BOOST_SERIALIZATION_NVP(a); - } + // test serialization + { + OArchive oa( ss ); + oa << BOOST_SERIALIZATION_NVP( a ); + } - // test de-serialization - { - IArchive ia(ss); - SerializableType b; - ia >> BOOST_SERIALIZATION_NVP(b); + // test de-serialization + { + IArchive ia( ss ); + SerializableType< Block > b; + ia >> BOOST_SERIALIZATION_NVP( b ); - BOOST_TEST(a.x == b.x); - } + BOOST_TEST( a.x == b.x ); + } #else -# error "TODO implement file-based test path?" +# error "TODO implement file-based test path?" #endif - } - - template - void test_binary_archive( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) { - test_serialization(); - } - - template - void test_xml_archive( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) { - test_serialization(); - } } -template -void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) +template< typename Block > +void +test_binary_archive( BOOST_EXPLICIT_TEMPLATE_TYPE( Block ) ) { - test_binary_archive(); - test_xml_archive(); + test_serialization< Block, boost::archive::binary_iarchive, boost::archive::binary_oarchive >(); } -int main() +template< typename Block > +void +test_xml_archive( BOOST_EXPLICIT_TEMPLATE_TYPE( Block ) ) { - run_test_cases(); - run_test_cases(); - run_test_cases(); - run_test_cases(); -# ifdef BOOST_HAS_LONG_LONG - run_test_cases< ::boost::ulong_long_type>(); -# endif + test_serialization< Block, boost::archive::xml_iarchive, boost::archive::xml_oarchive >(); +} +} + +template< typename Block > +void +run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE( Block ) ) +{ + test_binary_archive< Block >(); + test_xml_archive< Block >(); +} + +int +main() +{ + run_test_cases< unsigned char >(); + run_test_cases< unsigned short >(); + run_test_cases< unsigned int >(); + run_test_cases< unsigned long >(); +#ifdef BOOST_HAS_LONG_LONG + run_test_cases< ::boost::ulong_long_type >(); +#endif return boost::report_errors(); } diff --git a/test/test_ambiguous_set.cpp b/test/test_ambiguous_set.cpp index 9933649..e8ca4a1 100644 --- a/test/test_ambiguous_set.cpp +++ b/test/test_ambiguous_set.cpp @@ -14,17 +14,18 @@ #include "boost/core/lightweight_test.hpp" #include "boost/dynamic_bitset.hpp" -int main(int, char*[]) +int +main( int, char *[] ) { - boost::dynamic_bitset<> x(5); // all 0's by default - x.set(1, 2); - x.set(3, 1, true); - x.set(2, 1, false); - BOOST_TEST(!x.test(0)); - BOOST_TEST( x.test(1)); - BOOST_TEST(!x.test(2)); - BOOST_TEST( x.test(3)); - BOOST_TEST(!x.test(4)); + boost::dynamic_bitset<> x( 5 ); // all 0's by default + x.set( 1, 2 ); + x.set( 3, 1, true ); + x.set( 2, 1, false ); + BOOST_TEST( ! x.test( 0 ) ); + BOOST_TEST( x.test( 1 ) ); + BOOST_TEST( ! x.test( 2 ) ); + BOOST_TEST( x.test( 3 ) ); + BOOST_TEST( ! x.test( 4 ) ); return boost::report_errors(); } diff --git a/test/test_boost_hash.cpp b/test/test_boost_hash.cpp index 29e42cd..3dcee50 100644 --- a/test/test_boost_hash.cpp +++ b/test/test_boost_hash.cpp @@ -16,32 +16,33 @@ #include "boost/dynamic_bitset.hpp" #include -int main(int, char*[]) +int +main( int, char *[] ) { - typedef boost::dynamic_bitset bitset_type; - const std::string long_string = + typedef boost::dynamic_bitset< unsigned long > bitset_type; + const std::string long_string = "01001110101110110101011010000000000011110101101111111111"; const std::string long_string_prime_begin = "11001110101110110101011010000000000011110101101111111111"; const std::string long_string_prime_end = "01001110101110110101011010000000000011110101101111111110"; - bitset_type zeroes(long_string.size(), 0); - bitset_type stuff (long_string); - bitset_type stupb (long_string_prime_begin); - bitset_type stupe (long_string_prime_end); - bitset_type ones (long_string.size(), 1); + bitset_type zeroes( long_string.size(), 0 ); + bitset_type stuff( long_string ); + bitset_type stupb( long_string_prime_begin ); + bitset_type stupe( long_string_prime_end ); + bitset_type ones( long_string.size(), 1 ); - boost::hash bitset_hasher; - std::set results; - results.insert(bitset_hasher(zeroes)); - results.insert(bitset_hasher(stuff)); - results.insert(bitset_hasher(stupb)); - results.insert(bitset_hasher(stupe)); - results.insert(bitset_hasher(ones)); + boost::hash< bitset_type > bitset_hasher; + std::set< std::size_t > results; + results.insert( bitset_hasher( zeroes ) ); + results.insert( bitset_hasher( stuff ) ); + results.insert( bitset_hasher( stupb ) ); + results.insert( bitset_hasher( stupe ) ); + results.insert( bitset_hasher( ones ) ); // if any hash is identical to another there will be less than 5 - BOOST_TEST_EQ(results.size(), 5); + BOOST_TEST_EQ( results.size(), 5 ); return boost::report_errors(); } diff --git a/test/test_lowest_bit.cpp b/test/test_lowest_bit.cpp index df96e68..cf67415 100644 --- a/test/test_lowest_bit.cpp +++ b/test/test_lowest_bit.cpp @@ -15,14 +15,15 @@ #include "boost/cstdint.hpp" #include "boost/dynamic_bitset/detail/lowest_bit.hpp" -int main(int, char*[]) +int +main( int, char *[] ) { - for (boost::int32_t i = 1; i < 32; ++i) { - BOOST_TEST_EQ(i, boost::detail::lowest_bit(1u << i)); + for ( boost::int32_t i = 1; i < 32; ++i ) { + BOOST_TEST_EQ( i, boost::detail::lowest_bit( 1u << i ) ); } - BOOST_TEST_EQ(2, boost::detail::lowest_bit(123456788)); - BOOST_TEST_EQ(30, boost::detail::lowest_bit(static_cast(1507208177123328))); + BOOST_TEST_EQ( 2, boost::detail::lowest_bit( 123456788 ) ); + BOOST_TEST_EQ( 30, boost::detail::lowest_bit( static_cast< boost::int64_t >( 1507208177123328 ) ) ); return boost::report_errors(); } diff --git a/test/test_std_hash.cpp b/test/test_std_hash.cpp index 2aaa698..4f66577 100644 --- a/test/test_std_hash.cpp +++ b/test/test_std_hash.cpp @@ -14,25 +14,25 @@ #include "boost/config.hpp" #include "boost/detail/lightweight_test.hpp" #include "boost/dynamic_bitset.hpp" - #include -int main(int, char*[]) +int +main( int, char *[] ) { - typedef boost::dynamic_bitset bitset_type; - const std::string long_string = + typedef boost::dynamic_bitset< unsigned long > bitset_type; + const std::string long_string = "01001110101110110101011010000000000011110101101111111111"; - bitset_type zeroes(long_string.size(), 0); - bitset_type stuff (long_string); - bitset_type ones (long_string.size(), 1); + bitset_type zeroes( long_string.size(), 0 ); + bitset_type stuff( long_string ); + bitset_type ones( long_string.size(), 1 ); - std::unordered_set bitsets; - bitsets.insert(zeroes); - bitsets.insert(stuff); - bitsets.insert(ones); + std::unordered_set< bitset_type > bitsets; + bitsets.insert( zeroes ); + bitsets.insert( stuff ); + bitsets.insert( ones ); - BOOST_TEST_EQ(bitsets.size(), 3); + BOOST_TEST_EQ( bitsets.size(), 3 ); return boost::report_errors(); }